From: Christian Heller Date: Wed, 12 Jun 2024 06:20:33 +0000 (+0200) Subject: Improve InputsParser tests. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/cards/%7B%7Bcard_id%7D%7D/edit?a=commitdiff_plain;h=3c065cb3174a323df64e875537d2ad6db6596d62;p=plomtask Improve InputsParser tests. --- diff --git a/tests/misc.py b/tests/misc.py index d49870f..b0fb872 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -8,94 +8,143 @@ from plomtask.exceptions import BadFormatException class TestsSansServer(TestCase): """Tests that do not require DB setup or a server.""" - def test_InputsParser_non_strict(self) -> None: - """Test behavior of non-strict (= params) InputsParser.""" - params = InputsParser({}, False) - self.assertEqual('', params.get_str('foo')) - params = InputsParser({}, False) - self.assertEqual('bar', params.get_str('foo', 'bar')) - params = InputsParser({'foo': []}, False) - self.assertEqual('bar', params.get_str('foo', 'bar')) - params = InputsParser({'foo': ['baz']}, False) - self.assertEqual('baz', params.get_str('foo', 'bar')) - params = InputsParser({}, False) - self.assertEqual(None, params.get_int_or_none('foo')) - params = InputsParser({'foo': []}, False) - self.assertEqual(None, params.get_int_or_none('foo')) - params = InputsParser({'foo': ['']}, False) - self.assertEqual(None, params.get_int_or_none('foo')) - params = InputsParser({'foo': ['0']}, False) - self.assertEqual(0, params.get_int_or_none('foo')) + def test_InputsParser_get_str(self) -> None: + """Test InputsParser.get_str on strict and non-strictk.""" + parser = InputsParser({}, False) + self.assertEqual('', parser.get_str('foo')) + self.assertEqual('bar', parser.get_str('foo', 'bar')) + parser.strict = True with self.assertRaises(BadFormatException): - InputsParser({'foo': ['None']}, False).get_int_or_none('foo') + parser.get_str('foo') with self.assertRaises(BadFormatException): - InputsParser({'foo': ['0.1']}, False).get_int_or_none('foo') - params = InputsParser({'foo': ['23']}, False) - self.assertEqual(23, params.get_int_or_none('foo')) - - def test_InputsParser_strict(self) -> None: - """Test behavior of strict (= postvars) InputsParser.""" - self.assertEqual([], - InputsParser({}).get_all_str('foo')) - self.assertEqual([], - InputsParser({'foo': []}).get_all_str('foo')) - self.assertEqual(['bar'], - InputsParser({'foo': ['bar']}).get_all_str('foo')) - self.assertEqual(['bar', 'baz'], - InputsParser({'foo': ['bar', 'baz']}). - get_all_str('foo')) - self.assertEqual([], - InputsParser({}).get_all_int('foo')) - self.assertEqual([], - InputsParser({'foo': []}).get_all_int('foo')) - self.assertEqual([], - InputsParser({'foo': ['']}).get_all_int('foo')) - self.assertEqual([0], - InputsParser({'foo': ['0']}).get_all_int('foo')) - self.assertEqual([0, 17], - InputsParser({'foo': ['0', '17']}). - get_all_int('foo')) - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['0.1', '17']}).get_all_int('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['None', '17']}).get_all_int('foo') - with self.assertRaises(BadFormatException): - InputsParser({}).get_str('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': []}).get_str('foo') - self.assertEqual('bar', - InputsParser({'foo': ['bar']}).get_str('foo')) - self.assertEqual('', - InputsParser({'foo': ['', 'baz']}).get_str('foo')) - with self.assertRaises(BadFormatException): - InputsParser({}).get_int('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': []}).get_int('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['']}).get_int('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['bar']}).get_int('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['0.1']}).get_int('foo') - self.assertEqual(0, - InputsParser({'foo': ['0']}).get_int('foo')) - self.assertEqual(17, - InputsParser({'foo': ['17', '23']}).get_int('foo')) + parser.get_str('foo', 'bar') + parser = InputsParser({'foo': []}, False) + self.assertEqual('bar', parser.get_str('foo', 'bar')) with self.assertRaises(BadFormatException): - InputsParser({}).get_float('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': []}).get_float('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['']}).get_float('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': ['bar']}).get_float('foo') - self.assertEqual(0, - InputsParser({'foo': ['0']}).get_float('foo')) - self.assertEqual(0.1, - InputsParser({'foo': ['0.1']}).get_float('foo')) - self.assertEqual(1.23, - InputsParser({'foo': ['1.23', '456']}). - get_float('foo')) + InputsParser({'foo': []}, True).get_str('foo', 'bar') + for strictness in (False, True): + parser = InputsParser({'foo': ['baz']}, strictness) + self.assertEqual('baz', parser.get_str('foo', 'bar')) + parser = InputsParser({'foo': ['baz', 'quux']}, strictness) + self.assertEqual('baz', parser.get_str('foo', 'bar')) + + def test_InputsParser_get_first_strings_starting(self) -> None: + """Test InputsParser.get_first_strings_starting [non-]strict.""" + for strictness in (False, True): + parser = InputsParser({}, strictness) + self.assertEqual({}, + parser.get_first_strings_starting('')) + parser = InputsParser({}, strictness) + self.assertEqual({}, + parser.get_first_strings_starting('foo')) + parser = InputsParser({'foo': ['bar']}, strictness) + self.assertEqual({'foo': 'bar'}, + parser.get_first_strings_starting('')) + parser = InputsParser({'x': ['y']}, strictness) + self.assertEqual({'x': 'y'}, + parser.get_first_strings_starting('x')) + parser = InputsParser({'xx': ['y']}, strictness) + self.assertEqual({'xx': 'y'}, + parser.get_first_strings_starting('x')) + parser = InputsParser({'xx': ['y']}, strictness) + self.assertEqual({}, + parser.get_first_strings_starting('xxx')) + d = {'xxx': ['x'], 'xxy': ['y'], 'xyy': ['z']} + parser = InputsParser(d, strictness) + self.assertEqual({'xxx': 'x', 'xxy': 'y'}, + parser.get_first_strings_starting('xx')) + d = {'xxx': ['x', 'y', 'z'], 'xxy': ['y', 'z']} + parser = InputsParser(d, strictness) + self.assertEqual({'xxx': 'x', 'xxy': 'y'}, + parser.get_first_strings_starting('xx')) + + def test_InputsParser_get_int(self) -> None: + """Test InputsParser.get_int on strict and non-strict.""" + for strictness in (False, True): + with self.assertRaises(BadFormatException): + InputsParser({}, strictness).get_int('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': []}, strictness).get_int('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['']}, strictness).get_int('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['bar']}, strictness).get_int('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['0.1']}).get_int('foo') + parser = InputsParser({'foo': ['0']}, strictness) + self.assertEqual(0, parser.get_int('foo')) + parser = InputsParser({'foo': ['17', '23']}, strictness) + self.assertEqual(17, parser.get_int('foo')) + + def test_InputsParser_get_int_or_none(self) -> None: + """Test InputsParser.get_int_or_none on strict and non-strict.""" + for strictness in (False, True): + parser = InputsParser({}, strictness) + self.assertEqual(None, parser.get_int_or_none('foo')) + parser = InputsParser({'foo': []}, strictness) + self.assertEqual(None, parser.get_int_or_none('foo')) + parser = InputsParser({'foo': ['']}, strictness) + self.assertEqual(None, parser.get_int_or_none('foo')) + parser = InputsParser({'foo': ['0']}, strictness) + self.assertEqual(0, parser.get_int_or_none('foo')) + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['None']}, + strictness).get_int_or_none('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['0.1']}, + strictness).get_int_or_none('foo') + parser = InputsParser({'foo': ['23']}, strictness) + self.assertEqual(23, parser.get_int_or_none('foo')) + + def test_InputsParser_get_float(self) -> None: + """Test InputsParser.get_float on strict and non-strict.""" + for strictness in (False, True): + with self.assertRaises(BadFormatException): + InputsParser({}, strictness).get_float('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': []}, strictness).get_float('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['']}, strictness).get_float('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': ['bar']}, strictness).get_float('foo') + parser = InputsParser({'foo': ['0']}, strictness) + self.assertEqual(0, parser.get_float('foo')) + parser = InputsParser({'foo': ['0.1']}, strictness) + self.assertEqual(0.1, parser.get_float('foo')) + parser = InputsParser({'foo': ['1.23', '456']}, strictness) + self.assertEqual(1.23, parser.get_float('foo')) + + def test_InputsParser_get_all_str(self) -> None: + """Test InputsParser.get_all_str on strict and non-strict.""" + for strictness in (False, True): + parser = InputsParser({}, strictness) + self.assertEqual([], parser.get_all_str('foo')) + parser = InputsParser({'foo': []}, strictness) + self.assertEqual([], parser.get_all_str('foo')) + parser = InputsParser({'foo': ['bar']}, strictness) + self.assertEqual(['bar'], parser.get_all_str('foo')) + parser = InputsParser({'foo': ['bar', 'baz']}, strictness) + self.assertEqual(['bar', 'baz'], parser.get_all_str('foo')) + + def test_InputsParser_strict_get_all_int(self) -> None: + """Test InputsParser.get_all_int on strict and non-strict.""" + for strictness in (False, True): + parser = InputsParser({}, strictness) + self.assertEqual([], parser.get_all_int('foo')) + parser = InputsParser({'foo': []}, strictness) + self.assertEqual([], parser.get_all_int('foo')) + parser = InputsParser({'foo': ['']}, strictness) + self.assertEqual([], parser.get_all_int('foo')) + parser = InputsParser({'foo': ['0']}, strictness) + self.assertEqual([0], parser.get_all_int('foo')) + parser = InputsParser({'foo': ['0', '17']}, strictness) + self.assertEqual([0, 17], parser.get_all_int('foo')) + parser = InputsParser({'foo': ['0.1', '17']}, strictness) + with self.assertRaises(BadFormatException): + parser.get_all_int('foo') + parser = InputsParser({'foo': ['None', '17']}, strictness) + with self.assertRaises(BadFormatException): + parser.get_all_int('foo') class TestsWithServer(TestCaseWithServer):