home · contact · privacy
f825ba460400be22e650608bf0ecafe51e94e990
[plomtask] / tests / conditions.py
1 """Test Conditions module."""
2 from unittest import TestCase
3 from tests.utils import TestCaseWithDB, TestCaseWithServer
4 from plomtask.conditions import Condition
5 from plomtask.processes import Process
6 from plomtask.todos import Todo
7 from plomtask.exceptions import HandledException
8
9
10 class TestsSansDB(TestCase):
11     """Tests requiring no DB setup."""
12
13     def test_Condition_id_setting(self) -> None:
14         """Test .id_ being set and its legal range being enforced."""
15         with self.assertRaises(HandledException):
16             Condition(0)
17         condition = Condition(5)
18         self.assertEqual(condition.id_, 5)
19
20
21 class TestsWithDB(TestCaseWithDB):
22     """Tests requiring DB, but not server setup."""
23     checked_class = Condition
24     default_ids = (1, 2, 3)
25
26     def versioned_condition(self) -> Condition:
27         """Create Condition with some VersionedAttribute values."""
28         c = Condition(None)
29         c.title.set('title1')
30         c.title.set('title2')
31         c.description.set('desc1')
32         c.description.set('desc2')
33         return c
34
35     def test_Condition_saving_and_caching(self) -> None:
36         """Test .save/.save_core."""
37         kwargs = {'id_': 1, 'is_active': False}
38         self.check_saving_and_caching(**kwargs)
39         # check .id_ set if None, and versioned attributes too
40         c = self.versioned_condition()
41         c.save(self.db_conn)
42         self.assertEqual(c.id_, 2)
43         self.assertEqual(sorted(c.title.history.values()),
44                          ['title1', 'title2'])
45         self.assertEqual(sorted(c.description.history.values()),
46                          ['desc1', 'desc2'])
47
48     def test_Condition_from_table_row(self) -> None:
49         """Test .from_table_row() properly reads in class from DB"""
50         self.check_from_table_row(1)
51         c = self.versioned_condition()
52         c.save(self.db_conn)
53         assert isinstance(c.id_, int)
54         for row in self.db_conn.row_where(Condition.table_name, 'id', c.id_):
55             retrieved = Condition.from_table_row(self.db_conn, row)
56             # pylint: disable=no-member
57             self.assertEqual(sorted(retrieved.title.history.values()),
58                              ['title1', 'title2'])
59             # pylint: disable=no-member
60             self.assertEqual(sorted(retrieved.description.history.values()),
61                              ['desc1', 'desc2'])
62
63     def test_Condition_by_id(self) -> None:
64         """Test .by_id(), including creation."""
65         self.check_by_id()
66
67     def test_Condition_all(self) -> None:
68         """Test .all()."""
69         self.check_all()
70
71     def test_Condition_singularity(self) -> None:
72         """Test pointers made for single object keep pointing to it."""
73         self.check_singularity('is_active', True)
74
75     def test_Condition_remove(self) -> None:
76         """Test .remove() effects on DB and cache."""
77         self.check_remove()
78         c = Condition(None)
79         proc = Process(None)
80         todo = Todo(None, proc, False, '2024-01-01')
81         for depender in (proc, todo):
82             assert hasattr(depender, 'save')
83             assert hasattr(depender, 'set_conditions')
84             c.save(self.db_conn)
85             depender.save(self.db_conn)
86             depender.set_conditions(self.db_conn, [c.id_], 'conditions')
87             depender.save(self.db_conn)
88             with self.assertRaises(HandledException):
89                 c.remove(self.db_conn)
90             depender.set_conditions(self.db_conn, [], 'conditions')
91             depender.save(self.db_conn)
92             c.remove(self.db_conn)
93
94
95 class TestsWithServer(TestCaseWithServer):
96     """Module tests against our HTTP server/handler (and database)."""
97
98     def test_do_POST_condition(self) -> None:
99         """Test POST /condition and its effect on the database."""
100         form_data = {'title': 'foo', 'description': 'foo'}
101         self.check_post(form_data, '/condition', 302, '/condition?id=1')
102         self.assertEqual(1, len(Condition.all(self.db_conn)))
103         form_data['delete'] = ''
104         self.check_post(form_data, '/condition?id=', 404)
105         self.check_post(form_data, '/condition?id=2', 404)
106         self.check_post(form_data, '/condition?id=1', 302, '/conditions')
107         self.assertEqual(0, len(Condition.all(self.db_conn)))
108
109     def test_do_GET(self) -> None:
110         """Test /condition and /conditions response codes."""
111         form_data = {'title': 'foo', 'description': 'foo'}
112         self.check_post(form_data, '/condition', 302, '/condition?id=1')
113         self.check_get_defaults('/condition')
114         self.check_get('/conditions', 200)