+from plomtask.http import ParamsParser, PostvarsParser
+from plomtask.exceptions import BadFormatException
+
+
+class TestsSansServer(TestCase):
+ """Tests that do not require DB setup or a server."""
+
+ def test_params_parser(self) -> None:
+ """Test behavior of ParamsParser."""
+ self.assertEqual('',
+ ParamsParser({}).get_str('foo'))
+ self.assertEqual('bar',
+ ParamsParser({}).get_str('foo', 'bar'))
+ self.assertEqual('bar',
+ ParamsParser({'foo': []}).get_str('foo', 'bar'))
+ self.assertEqual('baz',
+ ParamsParser({'foo': ['baz']}).get_str('foo', 'bar'))
+ self.assertEqual(None,
+ ParamsParser({}).get_int_or_none('foo'))
+ self.assertEqual(None,
+ ParamsParser({'foo': []}).get_int_or_none('foo'))
+ self.assertEqual(None,
+ ParamsParser({'foo': ['']}).get_int_or_none('foo'))
+ self.assertEqual(0,
+ ParamsParser({'foo': ['0']}).get_int_or_none('foo'))
+ with self.assertRaises(BadFormatException):
+ ParamsParser({'foo': ['None']}).get_int_or_none('foo')
+ with self.assertRaises(BadFormatException):
+ ParamsParser({'foo': ['0.1']}).get_int_or_none('foo')
+ self.assertEqual(23,
+ ParamsParser({'foo': ['23']}).get_int_or_none('foo'))
+
+ def test_postvars_parser(self) -> None:
+ """Test behavior of PostvarsParser."""
+ self.assertEqual([],
+ PostvarsParser({}).get_all_str('foo'))
+ self.assertEqual([],
+ PostvarsParser({'foo': []}).get_all_str('foo'))
+ self.assertEqual(['bar'],
+ PostvarsParser({'foo': ['bar']}).get_all_str('foo'))
+ self.assertEqual(['bar', 'baz'],
+ PostvarsParser({'foo': ['bar', 'baz']}).
+ get_all_str('foo'))
+ self.assertEqual([],
+ PostvarsParser({}).get_all_int('foo'))
+ self.assertEqual([],
+ PostvarsParser({'foo': []}).get_all_int('foo'))
+ self.assertEqual([],
+ PostvarsParser({'foo': ['']}).get_all_int('foo'))
+ self.assertEqual([0],
+ PostvarsParser({'foo': ['0']}).get_all_int('foo'))
+ self.assertEqual([0, 17],
+ PostvarsParser({'foo': ['0', '17']}).
+ get_all_int('foo'))
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['0.1', '17']}).get_all_int('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['None', '17']}).get_all_int('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({}).get_str('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': []}).get_str('foo')
+ self.assertEqual('bar',
+ PostvarsParser({'foo': ['bar']}).get_str('foo'))
+ self.assertEqual('',
+ PostvarsParser({'foo': ['', 'baz']}).get_str('foo'))
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({}).get_int('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': []}).get_int('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['']}).get_int('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['bar']}).get_int('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['0.1']}).get_int('foo')
+ self.assertEqual(0,
+ PostvarsParser({'foo': ['0']}).get_int('foo'))
+ self.assertEqual(17,
+ PostvarsParser({'foo': ['17', '23']}).get_int('foo'))
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({}).get_float('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': []}).get_float('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['']}).get_float('foo')
+ with self.assertRaises(BadFormatException):
+ PostvarsParser({'foo': ['bar']}).get_float('foo')
+ self.assertEqual(0,
+ PostvarsParser({'foo': ['0']}).get_float('foo'))
+ self.assertEqual(0.1,
+ PostvarsParser({'foo': ['0.1']}).get_float('foo'))
+ self.assertEqual(1.23,
+ PostvarsParser({'foo': ['1.23', '456']}).
+ get_float('foo'))