def test_named(self): """Do named caches work?""" t = TestCaching.TestClass(x=4) y = t.get_y() # Setting the x prop should _not_ invalidate the cache for y t.changed = False t.set_x(17) self.assertEqual(y, t.get_y()) self.assertFalse(t.changed) # Setting the y prop should _not_ invalidate the cache for x x = t.xprop t.changed = False t.set_y(1) self.assertEqual(x, t.xprop) self.assertFalse(t.changed) # ... but it should invalidate the cache for y self.assertEqual(t.get_y(), 1) self.assertTrue(t.changed) # ... and so should calling named reset t.changed = False t.y = 9 reset(t, "y_cache") self.assertEqual(x, t.xprop) self.assertFalse(t.changed) self.assertEqual(t.get_y(), 9) self.assertTrue(t.changed)
def test_reset(self): """Does manual reset work properly""" t = TestCaching.TestClass(x=4) self.assertEqual(t.xprop, 4) t.changed = False t.x = 17 self.assertEqual(t.xprop, 4) self.assertFalse(t.changed) reset(t) self.assertEqual(t.xprop, 17) self.assertTrue(t.changed)
def test_set_cache(self): """Can we manually set the cache?""" t = TestCaching.TestClass(x=4, y=1) # Are set values used? set_cache(t, "xprop", 99) self.assertEqual(t.xprop, 99) self.assertFalse(t.changed) # Does reset bring back real value? reset(t) self.assertEqual(t.xprop, 4) self.assertTrue(t.changed) # Same for named prop t.changed = False set_cache(t, t.get_y.__name__, 7, "y_cache") self.assertEqual(t.get_y(), 7) self.assertFalse(t.changed) reset(t, "y_cache") self.assertEqual(t.get_y(), 1) self.assertTrue(t.changed)