def test_accessing_preserves(self): cache = Cache(self.__dir, 25, min_time=0) val = b"0123456789" cache.set("key1", val) cache.set("key2", val) self.assertEqual(cache.size, 20) cache.get_value("key1") cache.set("key3", val) self.assertEqual(cache.size, 20) self.assertTrue(cache.has("key1")) self.assertFalse(cache.has("key2")) self.assertTrue(cache.has("key3"))
def test_store_literal(self): cache = Cache(self.__dir, 10) val = b"0123456789" cache.set("key", val) self.assertEqual(cache.size, 10) self.assertTrue(cache.has("key")) self.assertEqual(cache.get_value("key"), val)
def test_store_to_fp(self): cache = Cache(self.__dir, 10) val = b"0123456789" with cache.set_fileobj("key") as fp: fp.write(val) self.assertEqual(cache.size, 0) self.assertEqual(cache.size, 10) self.assertEqual(cache.get_value("key"), val)
def test_access_data(self): cache = Cache(self.__dir, 25, min_time=0) val = b"0123456789" cache.set("key", val) self.assertEqual(cache.get_value("key"), val) with cache.get_fileobj("key") as f: self.assertEqual(f.read(), val) with open(cache.get("key"), "rb") as f: self.assertEqual(f.read(), val)
def test_existing_files_order(self): cache = Cache(self.__dir, 30) val = b"0123456789" cache.set("key1", val) cache.set("key2", val) cache.set("key3", val) self.assertEqual(cache.size, 30) # file mtime is accurate to the second time.sleep(1) cache.get_value("key1") cache = Cache(self.__dir, 30, min_time=0) self.assertEqual(cache.size, 30) self.assertTrue(cache.has("key1")) self.assertTrue(cache.has("key2")) self.assertTrue(cache.has("key3")) cache.set("key4", val) self.assertEqual(cache.size, 30) self.assertTrue(cache.has("key1")) self.assertFalse(cache.has("key2")) self.assertTrue(cache.has("key3")) self.assertTrue(cache.has("key4"))
def test_store_generated(self): cache = Cache(self.__dir, 10) val = [b"0", b"12", b"345", b"6789"] def gen(): yield from val t = [] for x in cache.set_generated("key", gen): t.append(x) self.assertEqual(cache.size, 0) self.assertFalse(cache.has("key")) self.assertEqual(t, val) self.assertEqual(cache.size, 10) self.assertEqual(cache.get_value("key"), b"".join(val))
def test_missing(self): cache = Cache(self.__dir, 10) self.assertFalse(cache.has("missing")) with self.assertRaises(CacheMiss): cache.get_value("missing")