home · contact · privacy
Split BaseModel.by_id into .by_id and by_id_or_create, refactor tests.
[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
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(BadFormatException):
18             parser.get_str('foo')
19         with self.assertRaises(BadFormatException):
20             parser.get_str('foo', 'bar')
21         parser = InputsParser({'foo': []}, False)
22         self.assertEqual('bar', parser.get_str('foo', 'bar'))
23         with self.assertRaises(BadFormatException):
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({}, strictness).get_float('foo')
104             with self.assertRaises(BadFormatException):
105                 InputsParser({'foo': []}, strictness).get_float('foo')
106             with self.assertRaises(BadFormatException):
107                 InputsParser({'foo': ['']}, strictness).get_float('foo')
108             with self.assertRaises(BadFormatException):
109                 InputsParser({'foo': ['bar']}, strictness).get_float('foo')
110             parser = InputsParser({'foo': ['0']}, strictness)
111             self.assertEqual(0, parser.get_float('foo'))
112             parser = InputsParser({'foo': ['0.1']}, strictness)
113             self.assertEqual(0.1, parser.get_float('foo'))
114             parser = InputsParser({'foo': ['1.23', '456']}, strictness)
115             self.assertEqual(1.23, parser.get_float('foo'))
116
117     def test_InputsParser_get_all_str(self) -> None:
118         """Test InputsParser.get_all_str on strict and non-strict."""
119         for strictness in (False, True):
120             parser = InputsParser({}, strictness)
121             self.assertEqual([], parser.get_all_str('foo'))
122             parser = InputsParser({'foo': []}, strictness)
123             self.assertEqual([], parser.get_all_str('foo'))
124             parser = InputsParser({'foo': ['bar']}, strictness)
125             self.assertEqual(['bar'], parser.get_all_str('foo'))
126             parser = InputsParser({'foo': ['bar', 'baz']}, strictness)
127             self.assertEqual(['bar', 'baz'], parser.get_all_str('foo'))
128
129     def test_InputsParser_strict_get_all_int(self) -> None:
130         """Test InputsParser.get_all_int on strict and non-strict."""
131         for strictness in (False, True):
132             parser = InputsParser({}, strictness)
133             self.assertEqual([], parser.get_all_int('foo'))
134             parser = InputsParser({'foo': []}, strictness)
135             self.assertEqual([], parser.get_all_int('foo'))
136             parser = InputsParser({'foo': ['']}, strictness)
137             self.assertEqual([], parser.get_all_int('foo'))
138             parser = InputsParser({'foo': ['0']}, strictness)
139             self.assertEqual([0], parser.get_all_int('foo'))
140             parser = InputsParser({'foo': ['0', '17']}, strictness)
141             self.assertEqual([0, 17], parser.get_all_int('foo'))
142             parser = InputsParser({'foo': ['0.1', '17']}, strictness)
143             with self.assertRaises(BadFormatException):
144                 parser.get_all_int('foo')
145             parser = InputsParser({'foo': ['None', '17']}, strictness)
146             with self.assertRaises(BadFormatException):
147                 parser.get_all_int('foo')
148
149
150 class TestsWithServer(TestCaseWithServer):
151     """Tests against our HTTP server/handler (and database)."""
152
153     def test_do_GET(self) -> None:
154         """Test GET / redirect, and unknown targets failing."""
155         self.conn.request('GET', '/')
156         self.check_redirect('/day')
157         self.check_get('/foo', 404)
158
159     def test_do_POST(self) -> None:
160         """Test POST to / and other unknown targets failing."""
161         self.check_post({}, '/', 404)
162         self.check_post({}, '/foo', 404)