From 33665a39fc58c86e7eae8e40f3e0eb247b163ffd Mon Sep 17 00:00:00 2001 From: Donaldson Date: Thu, 15 Oct 2020 18:30:26 -0500 Subject: [PATCH] Fixing up unit test issues --- contrib/python/moth/moth.py | 26 ++++++++++ contrib/python/test/category_test/category.py | 11 ----- contrib/python/test/test_moth.py | 47 +++---------------- contrib/python/tox.ini | 1 + 4 files changed, 33 insertions(+), 52 deletions(-) delete mode 100644 contrib/python/test/category_test/category.py diff --git a/contrib/python/moth/moth.py b/contrib/python/moth/moth.py index 15bf28a..9861198 100644 --- a/contrib/python/moth/moth.py +++ b/contrib/python/moth/moth.py @@ -16,6 +16,7 @@ import string import sys import tempfile import types +import warnings LOGGER = logging.getLogger(__name__) SEED = os.getenv("SEED", "0") @@ -166,6 +167,31 @@ class Puzzle: # pylint: disable=too-many-instance-attributes self.randseed = "%s %d" % (category_seed, self.points) self.rand = random.Random(self.randseed) + @property + def author(self): + """Retrieve the first author + + This function is retained for backwards-compatibility with legacy puzzles which use the .author field + + :returns: The first author in the .authors field, if one exists + """ + warnings.warn("This author field has been deprecated. Please use authors instead", DeprecationWarning) + if len(self.authors) > 0: + return self.authors[0] + + return None + + @author.setter + def author(self, new_author): + """Set the author + + This function is retained for backwards-compatibility with legacy puzzles which use the .author field + + :param new_author: The new author + """ + + self.authors = [new_author] + def set_markup(self, markup): """Set the markup function to convert body to HTML. diff --git a/contrib/python/test/category_test/category.py b/contrib/python/test/category_test/category.py deleted file mode 100644 index d285083..0000000 --- a/contrib/python/test/category_test/category.py +++ /dev/null @@ -1,11 +0,0 @@ -POINT_VALUES = [1,2,3,4,5] - -def pointvals(): - return POINT_VALUES - -def make(points, puzzle): - if points not in POINT_VALUES: - return None - - puzzle.answers.append(points) - return puzzle diff --git a/contrib/python/test/test_moth.py b/contrib/python/test/test_moth.py index 1185c20..74f7af8 100644 --- a/contrib/python/test/test_moth.py +++ b/contrib/python/test/test_moth.py @@ -4,13 +4,14 @@ import tempfile import unittest import moth +from moth.moth import sha256hash class TestMoth(unittest.TestCase): - def test_djb2hash(self): + def test_sha256hash(self): input_data = "test" - expected = 2090756197 - self.assertEqual(moth.djb2hash(input_data), expected) + expected = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" + self.assertEqual(sha256hash(input_data), expected) def test_log(self): puzzle = moth.Puzzle(12345, 1) @@ -83,36 +84,6 @@ class TestMoth(unittest.TestCase): self.assertEqual(puzzle.files[os.path.basename(tf.name)].stream.read(), data) self.assertEqual(puzzle.files[os.path.basename(tf.name)].visible, True) - def test_invalid_header(self): - puzzle = moth.Puzzle(12345, 1) - stream = """author: foo - answer: 10 - baz: stuff - - - Some body - """ - - with io.StringIO(stream) as buff: - with self.assertRaisesRegex(ValueError, "Unrecognized header field: baz"): - puzzle.read_stream(buff) - - def test_known_headers_moth(self): - puzzle = moth.Puzzle(12345, 1) - stream = """author: foo - answer: the answer is answer - summary: read the puzzle - pattern: a matching pattern - hint: This is a helpful hint - name: No idea what this is for - - Some body - """ - - with io.StringIO(stream) as buff: - puzzle.read_stream(buff) - pkg = puzzle.package() - def test_hexdump(self): puzzle = moth.Puzzle(12345, 1) test_data = [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] @@ -128,19 +99,13 @@ class TestMoth(unittest.TestCase): test_data = [1 for x in range(4*16)] puzzle.hexdump(test_data) - def test_category(self): - category = moth.Category("./test/category_test", 1) - self.assertIsNotNone(category.catmod) - - puzzles = list(category) - def test_authors_legacy(self): puzzle = moth.Puzzle(12345, 1) puzzle.author = "foo" - self.assertEqual(puzzle.get_authors(), ["foo"]) + self.assertEqual(puzzle.authors, ["foo"]) def test_authors(self): puzzle = moth.Puzzle(12345, 1) - self.assertEqual(puzzle.get_authors(), []) + self.assertEqual(puzzle.authors, []) diff --git a/contrib/python/tox.ini b/contrib/python/tox.ini index ef22998..2bd5e73 100644 --- a/contrib/python/tox.ini +++ b/contrib/python/tox.ini @@ -16,3 +16,4 @@ commands = vulture moth --min-confidence 70 bandit -r moth dodgy + nosetests --exe