1 """Miscellaneous tests."""
2 from unittest import TestCase
3 from tests.utils import TestCaseWithServer
4 from plomtask.http import ParamsParser, PostvarsParser
5 from plomtask.exceptions import BadFormatException
8 class TestsSansServer(TestCase):
9 """Tests that do not require DB setup or a server."""
11 def test_params_parser(self) -> None:
12 """Test behavior of ParamsParser."""
14 ParamsParser({}).get_str('foo'))
15 self.assertEqual('bar',
16 ParamsParser({}).get_str('foo', 'bar'))
17 self.assertEqual('bar',
18 ParamsParser({'foo': []}).get_str('foo', 'bar'))
19 self.assertEqual('baz',
20 ParamsParser({'foo': ['baz']}).get_str('foo', 'bar'))
21 self.assertEqual(None,
22 ParamsParser({}).get_int_or_none('foo'))
23 self.assertEqual(None,
24 ParamsParser({'foo': []}).get_int_or_none('foo'))
25 self.assertEqual(None,
26 ParamsParser({'foo': ['']}).get_int_or_none('foo'))
28 ParamsParser({'foo': ['0']}).get_int_or_none('foo'))
29 with self.assertRaises(BadFormatException):
30 ParamsParser({'foo': ['None']}).get_int_or_none('foo')
31 with self.assertRaises(BadFormatException):
32 ParamsParser({'foo': ['0.1']}).get_int_or_none('foo')
34 ParamsParser({'foo': ['23']}).get_int_or_none('foo'))
36 def test_postvars_parser(self) -> None:
37 """Test behavior of PostvarsParser."""
39 PostvarsParser({}).get_all_str('foo'))
41 PostvarsParser({'foo': []}).get_all_str('foo'))
42 self.assertEqual(['bar'],
43 PostvarsParser({'foo': ['bar']}).get_all_str('foo'))
44 self.assertEqual(['bar', 'baz'],
45 PostvarsParser({'foo': ['bar', 'baz']}).
48 PostvarsParser({}).get_all_int('foo'))
50 PostvarsParser({'foo': []}).get_all_int('foo'))
52 PostvarsParser({'foo': ['']}).get_all_int('foo'))
54 PostvarsParser({'foo': ['0']}).get_all_int('foo'))
55 self.assertEqual([0, 17],
56 PostvarsParser({'foo': ['0', '17']}).
58 with self.assertRaises(BadFormatException):
59 PostvarsParser({'foo': ['0.1', '17']}).get_all_int('foo')
60 with self.assertRaises(BadFormatException):
61 PostvarsParser({'foo': ['None', '17']}).get_all_int('foo')
62 with self.assertRaises(BadFormatException):
63 PostvarsParser({}).get_str('foo')
64 with self.assertRaises(BadFormatException):
65 PostvarsParser({'foo': []}).get_str('foo')
66 self.assertEqual('bar',
67 PostvarsParser({'foo': ['bar']}).get_str('foo'))
69 PostvarsParser({'foo': ['', 'baz']}).get_str('foo'))
70 with self.assertRaises(BadFormatException):
71 PostvarsParser({}).get_int('foo')
72 with self.assertRaises(BadFormatException):
73 PostvarsParser({'foo': []}).get_int('foo')
74 with self.assertRaises(BadFormatException):
75 PostvarsParser({'foo': ['']}).get_int('foo')
76 with self.assertRaises(BadFormatException):
77 PostvarsParser({'foo': ['bar']}).get_int('foo')
78 with self.assertRaises(BadFormatException):
79 PostvarsParser({'foo': ['0.1']}).get_int('foo')
81 PostvarsParser({'foo': ['0']}).get_int('foo'))
83 PostvarsParser({'foo': ['17', '23']}).get_int('foo'))
84 with self.assertRaises(BadFormatException):
85 PostvarsParser({}).get_float('foo')
86 with self.assertRaises(BadFormatException):
87 PostvarsParser({'foo': []}).get_float('foo')
88 with self.assertRaises(BadFormatException):
89 PostvarsParser({'foo': ['']}).get_float('foo')
90 with self.assertRaises(BadFormatException):
91 PostvarsParser({'foo': ['bar']}).get_float('foo')
93 PostvarsParser({'foo': ['0']}).get_float('foo'))
95 PostvarsParser({'foo': ['0.1']}).get_float('foo'))
96 self.assertEqual(1.23,
97 PostvarsParser({'foo': ['1.23', '456']}).
101 class TestsWithServer(TestCaseWithServer):
102 """Tests against our HTTP server/handler (and database)."""
104 def test_do_GET(self) -> None:
105 """Test / redirect, and unknown targets failing."""
106 self.conn.request('GET', '/')
107 self.check_redirect('/day')
108 self.check_get('/foo', 404)
110 def test_do_POST(self) -> None:
111 """Test POST to / and other unknown targets failing."""
112 self.check_post({}, '/', 404)
113 self.check_post({}, '/foo', 404)