1 """Miscellaneous tests."""
2 from unittest import TestCase
3 from tests.utils import TestCaseWithServer
4 from plomtask.http import InputsParser
5 from plomtask.exceptions import BadFormatException, NotFoundException
8 class TestsSansServer(TestCase):
9 """Tests that do not require DB setup or a server."""
11 def test_InputsParser_get_str(self) -> None:
12 """Test InputsParser.get_str on strict and non-strictk."""
13 parser = InputsParser({}, False)
14 self.assertEqual('', parser.get_str('foo'))
15 self.assertEqual('bar', parser.get_str('foo', 'bar'))
17 with self.assertRaises(NotFoundException):
19 with self.assertRaises(NotFoundException):
20 parser.get_str('foo', 'bar')
21 parser = InputsParser({'foo': []}, False)
22 self.assertEqual('bar', parser.get_str('foo', 'bar'))
23 with self.assertRaises(NotFoundException):
24 InputsParser({'foo': []}, True).get_str('foo', 'bar')
25 for strictness in (False, True):
26 parser = InputsParser({'foo': ['baz']}, strictness)
27 self.assertEqual('baz', parser.get_str('foo', 'bar'))
28 parser = InputsParser({'foo': ['baz', 'quux']}, strictness)
29 self.assertEqual('baz', parser.get_str('foo', 'bar'))
31 def test_InputsParser_get_first_strings_starting(self) -> None:
32 """Test InputsParser.get_first_strings_starting [non-]strict."""
33 for strictness in (False, True):
34 parser = InputsParser({}, strictness)
36 parser.get_first_strings_starting(''))
37 parser = InputsParser({}, strictness)
39 parser.get_first_strings_starting('foo'))
40 parser = InputsParser({'foo': ['bar']}, strictness)
41 self.assertEqual({'foo': 'bar'},
42 parser.get_first_strings_starting(''))
43 parser = InputsParser({'x': ['y']}, strictness)
44 self.assertEqual({'x': 'y'},
45 parser.get_first_strings_starting('x'))
46 parser = InputsParser({'xx': ['y']}, strictness)
47 self.assertEqual({'xx': 'y'},
48 parser.get_first_strings_starting('x'))
49 parser = InputsParser({'xx': ['y']}, strictness)
51 parser.get_first_strings_starting('xxx'))
52 d = {'xxx': ['x'], 'xxy': ['y'], 'xyy': ['z']}
53 parser = InputsParser(d, strictness)
54 self.assertEqual({'xxx': 'x', 'xxy': 'y'},
55 parser.get_first_strings_starting('xx'))
56 d = {'xxx': ['x', 'y', 'z'], 'xxy': ['y', 'z']}
57 parser = InputsParser(d, strictness)
58 self.assertEqual({'xxx': 'x', 'xxy': 'y'},
59 parser.get_first_strings_starting('xx'))
61 def test_InputsParser_get_int(self) -> None:
62 """Test InputsParser.get_int on strict and non-strict."""
63 for strictness in (False, True):
64 with self.assertRaises(BadFormatException):
65 InputsParser({}, strictness).get_int('foo')
66 with self.assertRaises(BadFormatException):
67 InputsParser({'foo': []}, strictness).get_int('foo')
68 with self.assertRaises(BadFormatException):
69 InputsParser({'foo': ['']}, strictness).get_int('foo')
70 with self.assertRaises(BadFormatException):
71 InputsParser({'foo': ['bar']}, strictness).get_int('foo')
72 with self.assertRaises(BadFormatException):
73 InputsParser({'foo': ['0.1']}).get_int('foo')
74 parser = InputsParser({'foo': ['0']}, strictness)
75 self.assertEqual(0, parser.get_int('foo'))
76 parser = InputsParser({'foo': ['17', '23']}, strictness)
77 self.assertEqual(17, parser.get_int('foo'))
79 def test_InputsParser_get_int_or_none(self) -> None:
80 """Test InputsParser.get_int_or_none on strict and non-strict."""
81 for strictness in (False, True):
82 parser = InputsParser({}, strictness)
83 self.assertEqual(None, parser.get_int_or_none('foo'))
84 parser = InputsParser({'foo': []}, strictness)
85 self.assertEqual(None, parser.get_int_or_none('foo'))
86 parser = InputsParser({'foo': ['']}, strictness)
87 self.assertEqual(None, parser.get_int_or_none('foo'))
88 parser = InputsParser({'foo': ['0']}, strictness)
89 self.assertEqual(0, parser.get_int_or_none('foo'))
90 with self.assertRaises(BadFormatException):
91 InputsParser({'foo': ['None']},
92 strictness).get_int_or_none('foo')
93 with self.assertRaises(BadFormatException):
94 InputsParser({'foo': ['0.1']},
95 strictness).get_int_or_none('foo')
96 parser = InputsParser({'foo': ['23']}, strictness)
97 self.assertEqual(23, parser.get_int_or_none('foo'))
99 def test_InputsParser_get_float(self) -> None:
100 """Test InputsParser.get_float on strict and non-strict."""
101 for strictness in (False, True):
102 with self.assertRaises(BadFormatException):
103 InputsParser({'foo': ['']}, strictness).get_float('foo')
104 with self.assertRaises(BadFormatException):
105 InputsParser({'foo': ['bar']}, strictness).get_float('foo')
106 parser = InputsParser({'foo': ['0']}, strictness)
107 self.assertEqual(0, parser.get_float('foo'))
108 parser = InputsParser({'foo': ['0.1']}, strictness)
109 self.assertEqual(0.1, parser.get_float('foo'))
110 parser = InputsParser({'foo': ['1.23', '456']}, strictness)
111 self.assertEqual(1.23, parser.get_float('foo'))
113 with self.assertRaises(NotFoundException):
114 InputsParser({}, strictness).get_float('foo')
115 with self.assertRaises(NotFoundException):
116 InputsParser({'foo': []}, strictness).get_float('foo')
118 with self.assertRaises(BadFormatException):
119 InputsParser({}, strictness).get_float('foo')
120 with self.assertRaises(BadFormatException):
121 InputsParser({'foo': []}, strictness).get_float('foo')
123 def test_InputsParser_get_float_or_none(self) -> None:
124 """Test InputsParser.get_float_or_none on strict and non-strict."""
125 for strictness in (False, True):
126 with self.assertRaises(BadFormatException):
127 InputsParser({'foo': ['bar']}, strictness).\
128 get_float_or_none('foo')
129 parser = InputsParser({'foo': ['']}, strictness)
130 self.assertEqual(None, parser.get_float_or_none('foo'))
131 parser = InputsParser({'foo': ['0']}, strictness)
132 self.assertEqual(0, parser.get_float_or_none('foo'))
133 parser = InputsParser({'foo': ['0.1']}, strictness)
134 self.assertEqual(0.1, parser.get_float_or_none('foo'))
135 parser = InputsParser({'foo': ['1.23', '456']}, strictness)
136 self.assertEqual(1.23, parser.get_float_or_none('foo'))
138 with self.assertRaises(NotFoundException):
139 InputsParser({}, strictness).get_float_or_none('foo')
140 with self.assertRaises(NotFoundException):
141 InputsParser({'foo': []}, strictness).get_float_or_none('foo')
143 parser = InputsParser({}, strictness)
144 self.assertEqual(None, parser.get_float_or_none('foo'))
145 parser = InputsParser({'foo': []}, strictness)
146 self.assertEqual(None, parser.get_float_or_none('foo'))
148 def test_InputsParser_get_all_str(self) -> None:
149 """Test InputsParser.get_all_str on strict and non-strict."""
150 for strictness in (False, True):
151 parser = InputsParser({}, strictness)
152 self.assertEqual([], parser.get_all_str('foo'))
153 parser = InputsParser({'foo': []}, strictness)
154 self.assertEqual([], parser.get_all_str('foo'))
155 parser = InputsParser({'foo': ['bar']}, strictness)
156 self.assertEqual(['bar'], parser.get_all_str('foo'))
157 parser = InputsParser({'foo': ['bar', 'baz']}, strictness)
158 self.assertEqual(['bar', 'baz'], parser.get_all_str('foo'))
160 def test_InputsParser_strict_get_all_int(self) -> None:
161 """Test InputsParser.get_all_int on strict and non-strict."""
162 for strictness in (False, True):
163 parser = InputsParser({}, strictness)
164 self.assertEqual([], parser.get_all_int('foo'))
165 parser = InputsParser({'foo': []}, strictness)
166 self.assertEqual([], parser.get_all_int('foo'))
167 parser = InputsParser({'foo': ['']}, strictness)
168 self.assertEqual([], parser.get_all_int('foo'))
169 parser = InputsParser({'foo': ['0']}, strictness)
170 self.assertEqual([0], parser.get_all_int('foo'))
171 parser = InputsParser({'foo': ['0', '17']}, strictness)
172 self.assertEqual([0, 17], parser.get_all_int('foo'))
173 parser = InputsParser({'foo': ['0.1', '17']}, strictness)
174 with self.assertRaises(BadFormatException):
175 parser.get_all_int('foo')
176 parser = InputsParser({'foo': ['None', '17']}, strictness)
177 with self.assertRaises(BadFormatException):
178 parser.get_all_int('foo')
181 class TestsWithServer(TestCaseWithServer):
182 """Tests against our HTTP server/handler (and database)."""
184 def test_do_GET(self) -> None:
185 """Test GET / redirect, and unknown targets failing."""
186 self.conn.request('GET', '/')
187 self.check_redirect('/day')
188 self.check_get('/foo', 404)
190 def test_do_POST(self) -> None:
191 """Test POST to / and other unknown targets failing."""
192 self.check_post({}, '/', 404)
193 self.check_post({}, '/foo', 404)