1 """Test Conditions module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer, TestCaseSansDB
3 from plomtask.conditions import Condition
4 from plomtask.processes import Process
5 from plomtask.todos import Todo
6 from plomtask.exceptions import HandledException
9 class TestsSansDB(TestCaseSansDB):
10 """Tests requiring no DB setup."""
11 checked_class = Condition
13 def test_Condition_id_setting(self) -> None:
14 """Test .id_ being set and its legal range being enforced."""
15 self.check_id_setting()
17 def test_Condition_versioned_defaults(self) -> None:
18 """Test defaults of VersionedAttributes."""
19 self.check_versioned_defaults({
24 class TestsWithDB(TestCaseWithDB):
25 """Tests requiring DB, but not server setup."""
26 checked_class = Condition
28 def versioned_condition(self) -> Condition:
29 """Create Condition with some VersionedAttribute values."""
33 c.description.set('desc1')
34 c.description.set('desc2')
37 def test_Condition_saving_and_caching(self) -> None:
38 """Test .save/.save_core."""
39 kwargs = {'id_': 1, 'is_active': False}
40 self.check_saving_and_caching(**kwargs)
41 # check .id_ set if None, and versioned attributes too
42 c = self.versioned_condition()
44 self.assertEqual(c.id_, 2)
45 self.assertEqual(sorted(c.title.history.values()),
47 self.assertEqual(sorted(c.description.history.values()),
50 def test_Condition_from_table_row(self) -> None:
51 """Test .from_table_row() properly reads in class from DB"""
52 self.check_from_table_row()
53 c = self.versioned_condition()
55 assert isinstance(c.id_, int)
56 for row in self.db_conn.row_where(Condition.table_name, 'id', c.id_):
57 retrieved = Condition.from_table_row(self.db_conn, row)
58 # pylint: disable=no-member
59 self.assertEqual(sorted(retrieved.title.history.values()),
61 # pylint: disable=no-member
62 self.assertEqual(sorted(retrieved.description.history.values()),
65 def test_Condition_by_id(self) -> None:
66 """Test .by_id(), including creation."""
69 def test_Condition_all(self) -> None:
73 def test_Condition_singularity(self) -> None:
74 """Test pointers made for single object keep pointing to it."""
75 self.check_singularity('is_active', True)
77 def test_Condition_versioned_attributes_singularity(self) -> None:
78 """Test behavior of VersionedAttributes on saving (with .title)."""
79 self.check_versioned_singularity()
81 def test_Condition_remove(self) -> None:
82 """Test .remove() effects on DB and cache."""
86 proc.save(self.db_conn)
87 todo = Todo(None, proc, False, '2024-01-01')
88 for depender in (proc, todo):
89 assert hasattr(depender, 'save')
90 assert hasattr(depender, 'set_conditions')
92 depender.save(self.db_conn)
93 depender.set_conditions(self.db_conn, [c.id_], 'conditions')
94 depender.save(self.db_conn)
95 with self.assertRaises(HandledException):
96 c.remove(self.db_conn)
97 depender.set_conditions(self.db_conn, [], 'conditions')
98 depender.save(self.db_conn)
99 c.remove(self.db_conn)
102 class TestsWithServer(TestCaseWithServer):
103 """Module tests against our HTTP server/handler (and database)."""
105 def test_do_POST_condition(self) -> None:
106 """Test POST /condition and its effect on the database."""
107 form_data = {'title': 'foo', 'description': 'foo'}
108 self.check_post(form_data, '/condition', 302, '/condition?id=1')
109 self.assertEqual(1, len(Condition.all(self.db_conn)))
110 form_data['delete'] = ''
111 self.check_post(form_data, '/condition?id=', 404)
112 self.check_post(form_data, '/condition?id=2', 404)
113 self.check_post(form_data, '/condition?id=1', 302, '/conditions')
114 self.assertEqual(0, len(Condition.all(self.db_conn)))
116 def test_do_GET(self) -> None:
117 """Test /condition and /conditions response codes."""
118 form_data = {'title': 'foo', 'description': 'foo'}
119 self.check_post(form_data, '/condition', 302, '/condition?id=1')
120 self.check_get_defaults('/condition')
121 self.check_get('/conditions', 200)