+ def check_storage(self, content: list[Condition]) -> None:
+ """Test cache and DB equal content."""
+ expected_cache = {}
+ for item in content:
+ expected_cache[item.id_] = item
+ self.assertEqual(Condition.get_cache(), expected_cache)
+ db_found: list[Condition] = []
+ for item in content:
+ assert isinstance(item.id_, int)
+ for row in self.db_conn.row_where(Condition.table_name, 'id',
+ item.id_):
+ db_found += [Condition.from_table_row(self.db_conn, row)]
+ self.assertEqual(sorted(content), sorted(db_found))
+
+ def test_Condition_saving_and_caching(self) -> None:
+ """Test .save/.save_core."""
+ c = Condition(None, False)
+ c.title.set('title1')
+ c.title.set('title2')
+ c.description.set('desc1')
+ c.description.set('desc2')
+ # check object init itself doesn't store anything yet
+ self.check_storage([])
+ # check saving stores in cache and DB
+ c.save(self.db_conn)
+ self.check_storage([c])
+ # check attributes set properly (and not unset by saving)
+ self.assertEqual(c.id_, 1)
+ self.assertEqual(c.is_active, False)
+ self.assertEqual(sorted(c.title.history.values()),
+ ['title1', 'title2'])
+ self.assertEqual(sorted(c.description.history.values()),
+ ['desc1', 'desc2'])
+
+ def test_Condition_from_table_row(self) -> None:
+ """Test .from_table_row() properly reads in class from DB"""
+ c = Condition(1, True)
+ c.title.set('title1')
+ c.title.set('title2')
+ c.description.set('desc1')
+ c.description.set('desc2')
+ c.save(self.db_conn)
+ assert isinstance(c.id_, int)
+ for row in self.db_conn.row_where(Condition.table_name, 'id', c.id_):
+ retrieved = Condition.from_table_row(self.db_conn, row)
+ assert isinstance(retrieved, Condition)
+ self.assertEqual(c, retrieved)
+ self.assertEqual({c.id_: c}, Condition.get_cache())
+ # pylint: disable=no-member
+ self.assertEqual(sorted(retrieved.title.history.values()),
+ ['title1', 'title2'])
+ # pylint: disable=no-member
+ self.assertEqual(sorted(retrieved.description.history.values()),
+ ['desc1', 'desc2'])
+