X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/cards/%7B%7Bcard_id%7D%7D/form?a=blobdiff_plain;ds=sidebyside;f=tests%2Futils.py;h=55c948a409dbf1b2c43f775c0d39106ba3ed510d;hb=25b71c6f0b10db05907128daf50c6e543e514c35;hp=9d3d11d9f841290fed50b03c8d33acea7f5248ac;hpb=15ba54fa4132e46804e20748fd80243ad187b98c;p=plomtask diff --git a/tests/utils.py b/tests/utils.py index 9d3d11d..55c948a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -87,8 +87,8 @@ class TestCaseWithDB(TestCase): attr = getattr(retrieved, attr_name) self.assertEqual(sorted(attr.history.values()), vals) - def check_storage(self, content: list[Any]) -> None: - """Test cache and DB equal content.""" + def check_identity_with_cache_and_db(self, content: list[Any]) -> None: + """Test both cache and DB equal content.""" expected_cache = {} for item in content: expected_cache[item.id_] = item @@ -108,30 +108,47 @@ class TestCaseWithDB(TestCase): """Test instance.save in its core without relations.""" obj = self.checked_class(**kwargs) # pylint: disable=not-callable # check object init itself doesn't store anything yet - self.check_storage([]) + self.check_identity_with_cache_and_db([]) # check saving sets core attributes properly obj.save(self.db_conn) for key, value in kwargs.items(): self.assertEqual(getattr(obj, key), value) # check saving stored properly in cache and DB - self.check_storage([obj]) + self.check_identity_with_cache_and_db([obj]) - def check_by_id(self) -> None: - """Test .by_id(), including creation.""" + @_within_checked_class + def test_by_id(self) -> None: + """Test .by_id().""" + id1, id2, _ = self.default_ids # check failure if not yet saved - id1, id2 = self.default_ids[0], self.default_ids[1] - obj = self.checked_class(id1) # pylint: disable=not-callable + obj1 = self.checked_class(id1, **self.default_init_kwargs) with self.assertRaises(NotFoundException): self.checked_class.by_id(self.db_conn, id1) + # check identity of cached and retrieved + obj1.cache() + self.assertEqual(obj1, self.checked_class.by_id(self.db_conn, id1)) # check identity of saved and retrieved - obj.save(self.db_conn) - self.assertEqual(obj, self.checked_class.by_id(self.db_conn, id1)) - # check create=True acts like normal instantiation (sans saving) - by_id_created = self.checked_class.by_id(self.db_conn, id2, - create=True) - # pylint: disable=not-callable - self.assertEqual(self.checked_class(id2), by_id_created) - self.check_storage([obj]) + obj2 = self.checked_class(id2, **self.default_init_kwargs) + obj2.save(self.db_conn) + self.assertEqual(obj2, self.checked_class.by_id(self.db_conn, id2)) + # obj1.save(self.db_conn) + # self.check_identity_with_cache_and_db([obj1, obj2]) + + @_within_checked_class + def test_by_id_or_create(self) -> None: + """Test .by_id_or_create.""" + # check .by_id_or_create acts like normal instantiation (sans saving) + id_ = self.default_ids[0] + if not self.checked_class.can_create_by_id: + with self.assertRaises(HandledException): + self.checked_class.by_id_or_create(self.db_conn, id_) + # check .by_id_or_create fails if wrong class + else: + by_id_created = self.checked_class.by_id_or_create(self.db_conn, + id_) + with self.assertRaises(NotFoundException): + self.checked_class.by_id(self.db_conn, id_) + self.assertEqual(self.checked_class(id_), by_id_created) @_within_checked_class def test_from_table_row(self) -> None: @@ -230,7 +247,7 @@ class TestCaseWithDB(TestCase): obj.remove(self.db_conn) obj.save(self.db_conn) obj.remove(self.db_conn) - self.check_storage([]) + self.check_identity_with_cache_and_db([]) class TestCaseWithServer(TestCaseWithDB):