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
10 class TestsSansDB(TestCase):
11 """Tests requiring no DB setup."""
13 def test_Condition_id_setting(self) -> None:
14 """Test .id_ being set and its legal range being enforced."""
15 with self.assertRaises(HandledException):
17 condition = Condition(5)
18 self.assertEqual(condition.id_, 5)
21 class TestsWithDB(TestCaseWithDB):
22 """Tests requiring DB, but not server setup."""
23 checked_class = Condition
24 default_ids = (1, 2, 3)
26 def versioned_condition(self) -> Condition:
27 """Create Condition with some VersionedAttribute values."""
31 c.description.set('desc1')
32 c.description.set('desc2')
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()
42 self.assertEqual(c.id_, 2)
43 self.assertEqual(sorted(c.title.history.values()),
45 self.assertEqual(sorted(c.description.history.values()),
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()
51 c = self.versioned_condition()
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()),
59 # pylint: disable=no-member
60 self.assertEqual(sorted(retrieved.description.history.values()),
63 def test_Condition_by_id(self) -> None:
64 """Test .by_id(), including creation."""
67 def test_Condition_all(self) -> None:
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)
75 def test_Condition_remove(self) -> None:
76 """Test .remove() effects on DB and cache."""
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')
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)
95 class TestsWithServer(TestCaseWithServer):
96 """Module tests against our HTTP server/handler (and database)."""
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)))
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)