home · contact · privacy
Allow Todo adoptions to be un-done in Todo view.
[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_non_strict(self) -> None:
12         """Test behavior of non-strict (= params) InputsParser."""
13         params = InputsParser({}, False)
14         self.assertEqual('', params.get_str('foo'))
15         params = InputsParser({}, False)
16         self.assertEqual('bar', params.get_str('foo', 'bar'))
17         params = InputsParser({'foo': []}, False)
18         self.assertEqual('bar', params.get_str('foo', 'bar'))
19         params = InputsParser({'foo': ['baz']}, False)
20         self.assertEqual('baz', params.get_str('foo', 'bar'))
21         params = InputsParser({}, False)
22         self.assertEqual(None, params.get_int_or_none('foo'))
23         params = InputsParser({'foo': []}, False)
24         self.assertEqual(None, params.get_int_or_none('foo'))
25         params = InputsParser({'foo': ['']}, False)
26         self.assertEqual(None, params.get_int_or_none('foo'))
27         params = InputsParser({'foo': ['0']}, False)
28         self.assertEqual(0, params.get_int_or_none('foo'))
29         with self.assertRaises(BadFormatException):
30             InputsParser({'foo': ['None']}, False).get_int_or_none('foo')
31         with self.assertRaises(BadFormatException):
32             InputsParser({'foo': ['0.1']}, False).get_int_or_none('foo')
33         params = InputsParser({'foo': ['23']}, False)
34         self.assertEqual(23, params.get_int_or_none('foo'))
35
36     def test_InputsParser_strict(self) -> None:
37         """Test behavior of strict (= postvars) InputsParser."""
38         self.assertEqual([],
39                          InputsParser({}).get_all_str('foo'))
40         self.assertEqual([],
41                          InputsParser({'foo': []}).get_all_str('foo'))
42         self.assertEqual(['bar'],
43                          InputsParser({'foo': ['bar']}).get_all_str('foo'))
44         self.assertEqual(['bar', 'baz'],
45                          InputsParser({'foo': ['bar', 'baz']}).
46                          get_all_str('foo'))
47         self.assertEqual([],
48                          InputsParser({}).get_all_int('foo'))
49         self.assertEqual([],
50                          InputsParser({'foo': []}).get_all_int('foo'))
51         self.assertEqual([],
52                          InputsParser({'foo': ['']}).get_all_int('foo'))
53         self.assertEqual([0],
54                          InputsParser({'foo': ['0']}).get_all_int('foo'))
55         self.assertEqual([0, 17],
56                          InputsParser({'foo': ['0', '17']}).
57                          get_all_int('foo'))
58         with self.assertRaises(BadFormatException):
59             InputsParser({'foo': ['0.1', '17']}).get_all_int('foo')
60         with self.assertRaises(BadFormatException):
61             InputsParser({'foo': ['None', '17']}).get_all_int('foo')
62         with self.assertRaises(BadFormatException):
63             InputsParser({}).get_str('foo')
64         with self.assertRaises(BadFormatException):
65             InputsParser({'foo': []}).get_str('foo')
66         self.assertEqual('bar',
67                          InputsParser({'foo': ['bar']}).get_str('foo'))
68         self.assertEqual('',
69                          InputsParser({'foo': ['', 'baz']}).get_str('foo'))
70         with self.assertRaises(BadFormatException):
71             InputsParser({}).get_int('foo')
72         with self.assertRaises(BadFormatException):
73             InputsParser({'foo': []}).get_int('foo')
74         with self.assertRaises(BadFormatException):
75             InputsParser({'foo': ['']}).get_int('foo')
76         with self.assertRaises(BadFormatException):
77             InputsParser({'foo': ['bar']}).get_int('foo')
78         with self.assertRaises(BadFormatException):
79             InputsParser({'foo': ['0.1']}).get_int('foo')
80         self.assertEqual(0,
81                          InputsParser({'foo': ['0']}).get_int('foo'))
82         self.assertEqual(17,
83                          InputsParser({'foo': ['17', '23']}).get_int('foo'))
84         with self.assertRaises(BadFormatException):
85             InputsParser({}).get_float('foo')
86         with self.assertRaises(BadFormatException):
87             InputsParser({'foo': []}).get_float('foo')
88         with self.assertRaises(BadFormatException):
89             InputsParser({'foo': ['']}).get_float('foo')
90         with self.assertRaises(BadFormatException):
91             InputsParser({'foo': ['bar']}).get_float('foo')
92         self.assertEqual(0,
93                          InputsParser({'foo': ['0']}).get_float('foo'))
94         self.assertEqual(0.1,
95                          InputsParser({'foo': ['0.1']}).get_float('foo'))
96         self.assertEqual(1.23,
97                          InputsParser({'foo': ['1.23', '456']}).
98                          get_float('foo'))
99
100
101 class TestsWithServer(TestCaseWithServer):
102     """Tests against our HTTP server/handler (and database)."""
103
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)
109
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)