def test_task_body(self): kwargs = dict(attr1=1, attr2="test", attr3=1.1) foo1 = Foo.objects.create(**kwargs) self.assertIn(foo1, simple_foo.get(**kwargs)) foo2 = Foo.objects.create(**kwargs) self.assertNotIn(foo2, simple_foo.get(**kwargs)) run_invalidate_task(INVALIDATE, "simple_foo", outdated=kwargs) self.assertIn(foo2, simple_foo.get(**kwargs)) foo3 = Foo.objects.create(**kwargs) self.assertNotIn(foo3, simple_foo.get(**kwargs)) run_invalidate_task(INVALIDATE_ALL, "simple_foo") self.assertIn(foo3, simple_foo.get(**kwargs)) cache.clear()
def test_invalidation_field_was_empty(self): foo1, foo2, foo3 = self._setup_testing_data() kwargs2 = dict(attr1=foo2.attr1, attr2=foo2.attr2, attr3=foo2.attr3) kwargs3 = dict(attr1=foo3.attr1, attr2=foo3.attr2, attr3=foo3.attr3) simple_foo.save(**kwargs3) self.assertIn(foo3, simple_foo.get(**kwargs3)) self.assertNotIn(foo3, simple_foo.get(**kwargs2)) for key, value in kwargs2.items(): setattr(foo3, key, value) foo3.save() invalidate(simple_foo, kwargs3, kwargs2) self.assertNotIn(foo3, simple_foo.get(**kwargs3)) self.assertIn(foo3, simple_foo.get(**kwargs2)) cache.clear()
def test_invalidation_change_by_admin_action_fields(self): foo1, foo2, foo3 = self._setup_testing_data() kwargs1 = dict(attr1=foo1.attr1, attr2=foo1.attr2, attr3=foo1.attr3) kwargs2 = dict(attr1=foo2.attr1, attr2=foo2.attr2, attr3=foo2.attr3) simple_foo.save(**kwargs1) simple_foo.save(**kwargs2) self.assertIn(foo1, simple_foo.get(**kwargs1)) self.assertNotIn(foo1, simple_foo.get(**kwargs2)) for key, value in kwargs2.items(): setattr(foo1, key, value) foo1.save() invalidate_action(None, None, CreatedCache.objects.all()) self.assertNotIn(foo1, simple_foo.get(**kwargs1)) self.assertIn(foo1, simple_foo.get(**kwargs2)) cache.clear()
def test_same_cache_object_cleaning(self): kwargs = dict(attr1=1, attr2="test", attr3=1.1) kwargs2 = dict(attr1=2, attr2="test2", attr3=2.1) foo1 = Foo.objects.create(**kwargs) foo2 = Foo.objects.create(**kwargs2) self.assertIn(foo1, simple_foo.get(**kwargs)) c_object1 = CreatedCache.objects.first() self.assertIn(foo2, simple_foo.get(**kwargs2)) c_object2 = CreatedCache.objects.last() self.assertEqual(CreatedCache.objects.count(), 2) foo3 = Foo.objects.create(**kwargs) self.assertIn(foo3, simple_foo.save(**kwargs)) self.assertFalse(CreatedCache.objects.filter(id=c_object1.id).exists()) self.assertTrue(CreatedCache.objects.filter(id=c_object2.id).exists()) cache.clear()
def test_simple_saving_and_getting(self): foo1, foo2, foo3 = self._setup_testing_data() kwargs = dict(attr1=foo1.attr1, attr2=foo1.attr2, attr3=foo1.attr3) res = list(simple_foo.save(**kwargs)) self.assertEqual(list(simple_foo.get(**kwargs)), res) self.assertEqual( list(cache.get(simple_foo.get_key(**kwargs)).get("value")), res)
def test_invalidation_on_object_creation(self): foo1 = Foo.objects.create(attr1=1, attr2="test", attr3=1.1) simple_foo.save(attr1=1, attr2="test", attr3=1.1) self.assertIn(foo1, simple_foo.get(attr1=1, attr2="test", attr3=1.1)) foo2 = Foo.objects.create(attr1=1, attr2="test", attr3=1.1) self.assertNotIn(foo2, simple_foo.get(attr1=1, attr2="test", attr3=1.1)) invalidate( simple_foo, { "attr1": 1, "attr2": "test", "attr3": 1.1 }, ) self.assertIn(foo2, simple_foo.get(attr1=1, attr2="test", attr3=1.1)) cache.clear()
def test_invalidation_partial_changed_attrs(self): foo1 = Foo.objects.create(attr1=1, attr2="test", attr3=1.1) foo2 = Foo.objects.create(attr1=2, attr2="test") simple_foo.save(attr1=1, attr2="test", attr3=1.1) simple_foo.save(attr1=2, attr2="test", attr3=None) foo1.attr1 = 2 foo1.attr3 = None foo1.save() invalidate(simple_foo, { "attr1": 1, "attr2": "test", "attr3": 1.1 }, { "attr1": 2, "attr2": "test", "attr3": None }) self.assertNotIn(foo1, simple_foo.get(attr1=1, attr2="test", attr3=1.1)) self.assertIn(foo2, simple_foo.get(attr1=2, attr2="test", attr3=None)) cache.clear()