Fixing up unit test issues

This commit is contained in:
Donaldson 2020-10-15 18:30:26 -05:00
parent 473c640586
commit 33665a39fc
4 changed files with 33 additions and 52 deletions

View File

@ -16,6 +16,7 @@ import string
import sys import sys
import tempfile import tempfile
import types import types
import warnings
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
SEED = os.getenv("SEED", "0") 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.randseed = "%s %d" % (category_seed, self.points)
self.rand = random.Random(self.randseed) 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): def set_markup(self, markup):
"""Set the markup function to convert body to HTML. """Set the markup function to convert body to HTML.

View File

@ -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

View File

@ -4,13 +4,14 @@ import tempfile
import unittest import unittest
import moth import moth
from moth.moth import sha256hash
class TestMoth(unittest.TestCase): class TestMoth(unittest.TestCase):
def test_djb2hash(self): def test_sha256hash(self):
input_data = "test" input_data = "test"
expected = 2090756197 expected = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
self.assertEqual(moth.djb2hash(input_data), expected) self.assertEqual(sha256hash(input_data), expected)
def test_log(self): def test_log(self):
puzzle = moth.Puzzle(12345, 1) 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)].stream.read(), data)
self.assertEqual(puzzle.files[os.path.basename(tf.name)].visible, True) 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): def test_hexdump(self):
puzzle = moth.Puzzle(12345, 1) puzzle = moth.Puzzle(12345, 1)
test_data = [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] 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)] test_data = [1 for x in range(4*16)]
puzzle.hexdump(test_data) 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): def test_authors_legacy(self):
puzzle = moth.Puzzle(12345, 1) puzzle = moth.Puzzle(12345, 1)
puzzle.author = "foo" puzzle.author = "foo"
self.assertEqual(puzzle.get_authors(), ["foo"]) self.assertEqual(puzzle.authors, ["foo"])
def test_authors(self): def test_authors(self):
puzzle = moth.Puzzle(12345, 1) puzzle = moth.Puzzle(12345, 1)
self.assertEqual(puzzle.get_authors(), []) self.assertEqual(puzzle.authors, [])

View File

@ -16,3 +16,4 @@ commands =
vulture moth --min-confidence 70 vulture moth --min-confidence 70
bandit -r moth bandit -r moth
dodgy dodgy
nosetests --exe