home · contact · privacy
Return NotFoundExceptions from InputsParser if no value found to key.
[plomtask] / tests / misc.py
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
6
7
8 class TestsSansServer(TestCase):
9     """Tests that do not require DB setup or a server."""
10
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'))
16         parser.strict = True
17         with self.assertRaises(NotFoundException):
18             parser.get_str('foo')
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'))
30
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)
35             self.assertEqual({},
36                              parser.get_first_strings_starting(''))
37             parser = InputsParser({}, strictness)
38             self.assertEqual({},
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)
50             self.assertEqual({},
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'))
60
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'))
78
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'))
98
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'))
112         if strictness:
113             with self.assertRaises(NotFoundException):
114                 InputsParser({}, strictness).get_float('foo')
115             with self.assertRaises(NotFoundException):
116                 InputsParser({'foo': []}, strictness).get_float('foo')
117         else:
118             with self.assertRaises(BadFormatException):
119                 InputsParser({}, strictness).get_float('foo')
120             with self.assertRaises(BadFormatException):
121                 InputsParser({'foo': []}, strictness).get_float('foo')
122
123     def test_InputsParser_get_all_str(self) -> None:
124         """Test InputsParser.get_all_str on strict and non-strict."""
125         for strictness in (False, True):
126             parser = InputsParser({}, strictness)
127             self.assertEqual([], parser.get_all_str('foo'))
128             parser = InputsParser({'foo': []}, strictness)
129             self.assertEqual([], parser.get_all_str('foo'))
130             parser = InputsParser({'foo': ['bar']}, strictness)
131             self.assertEqual(['bar'], parser.get_all_str('foo'))
132             parser = InputsParser({'foo': ['bar', 'baz']}, strictness)
133             self.assertEqual(['bar', 'baz'], parser.get_all_str('foo'))
134
135     def test_InputsParser_strict_get_all_int(self) -> None:
136         """Test InputsParser.get_all_int on strict and non-strict."""
137         for strictness in (False, True):
138             parser = InputsParser({}, strictness)
139             self.assertEqual([], parser.get_all_int('foo'))
140             parser = InputsParser({'foo': []}, strictness)
141             self.assertEqual([], parser.get_all_int('foo'))
142             parser = InputsParser({'foo': ['']}, strictness)
143             self.assertEqual([], parser.get_all_int('foo'))
144             parser = InputsParser({'foo': ['0']}, strictness)
145             self.assertEqual([0], parser.get_all_int('foo'))
146             parser = InputsParser({'foo': ['0', '17']}, strictness)
147             self.assertEqual([0, 17], parser.get_all_int('foo'))
148             parser = InputsParser({'foo': ['0.1', '17']}, strictness)
149             with self.assertRaises(BadFormatException):
150                 parser.get_all_int('foo')
151             parser = InputsParser({'foo': ['None', '17']}, strictness)
152             with self.assertRaises(BadFormatException):
153                 parser.get_all_int('foo')
154
155
156 class TestsWithServer(TestCaseWithServer):
157     """Tests against our HTTP server/handler (and database)."""
158
159     def test_do_GET(self) -> None:
160         """Test GET / redirect, and unknown targets failing."""
161         self.conn.request('GET', '/')
162         self.check_redirect('/day')
163         self.check_get('/foo', 404)
164
165     def test_do_POST(self) -> None:
166         """Test POST to / and other unknown targets failing."""
167         self.check_post({}, '/', 404)
168         self.check_post({}, '/foo', 404)