Exemple #1
0
 def test_eq(self) -> None:
     """test LRUCacheStrategy.__eq__"""
     strategy1 = LRUCacheStrategy(1)
     strategy2 = LRUCacheStrategy(2)
     self.assertEqual(strategy1, strategy1)
     self.assertNotEqual(strategy1, strategy2)
     self.assertNotEqual(1, strategy2)
Exemple #2
0
 def test_peek(self) -> None:
     """test LRUCacheStrategy.peek"""
     strategy1 = LRUCacheStrategy(3)
     strategy2 = LRUCacheStrategy(3)
     strategy1["test"] = "Test"
     strategy2["test"] = "Test"
     self.assertEqual(strategy1.peek("test"), "Test")
     self.assertEqual(strategy1.peek("test"), "Test")  # no side effects
     self.assertEqual(strategy1, strategy2)
Exemple #3
0
 def test_views(self) -> None:
     """test LRUCacheStrategy keys, values and items"""
     strategy = LRUCacheStrategy(3)
     strategy["a"] = 1
     strategy["b"] = 2
     strategy["c"] = 3
     self.assertEqual(list(strategy), ["a", "b", "c"])
     self.assertEqual(list(strategy.keys()), ["a", "b", "c"])
     self.assertEqual(list(strategy.values()), [1, 2, 3])
     self.assertEqual(list(strategy.items()), [("a", 1), ("b", 2),
                                               ("c", 3)])
Exemple #4
0
 def test_eq(self) -> None:
     """test MemoryCacheSource.__eq__"""
     strategy = UnboundedCacheStrategy()
     sources = [
         MemoryCacheSource(
             FileSource.from_path("tests/embedding/syntax.pyhp", compiler),
             "test", strategy),
         MemoryCacheSource(
             FileSource.from_path("tests/embedding/syntax.pyhp", compiler2),
             "test", strategy),
         MemoryCacheSource(
             FileSource.from_path("tests/embedding/syntax.pyhp", compiler),
             "test2", strategy),
         MemoryCacheSource(
             FileSource.from_path("tests/embedding/syntax.pyhp", compiler),
             "test", LRUCacheStrategy(9)),
         MemoryCacheSource(
             FileSource.from_path("tests/embedding/syntax.pyhp", compiler),
             "test", strategy, 9)
     ]
     try:
         for source in sources:
             self.assertEqual([source], [s for s in sources if s == source])
     finally:
         for source in sources:
             source.close()
Exemple #5
0
 def test_pop(self) -> None:
     """test LRUCacheStrategy.pop and popitem"""
     strategy = LRUCacheStrategy(3)
     strategy["a"] = 1
     strategy["b"] = 2
     strategy["c"] = 3
     self.assertEqual(strategy.pop("a"), 1)
     self.assertNotIn("a", strategy)
     with self.assertRaises(KeyError):
         strategy.pop("a")
     self.assertEqual(strategy.pop("a", 2), 2)
     self.assertEqual(strategy.popitem(), ("c", 3))
     self.assertEqual(strategy.popitem(), ("b", 2))
     with self.assertRaises(KeyError):
         strategy.popitem()
Exemple #6
0
 def test_eq(self) -> None:
     """test MemoryCache.__eq__"""
     strategy1 = UnboundedCacheStrategy()
     strategy2 = LRUCacheStrategy(9)
     with MemoryCache(Directory("tests/embedding", compiler), strategy1) as cache1, \
             MemoryCache(Directory("tests/embedding", compiler2), strategy1) as cache2, \
             MemoryCache(Directory("tests/embedding", compiler), strategy1, 9) as cache3, \
             MemoryCache(Directory("tests/embedding", compiler), strategy2) as cache4:
         self.assertEqual(cache1, cache1)
         self.assertNotEqual(cache1, cache2)
         self.assertNotEqual(cache2, cache3)
         self.assertNotEqual(cache3, cache4)
Exemple #7
0
 def test_clear(self) -> None:
     """test LRUCacheStrategy.clear"""
     strategy = LRUCacheStrategy(3)
     strategy["a"] = 1
     strategy["b"] = 2
     strategy["c"] = 3
     strategy.clear()
     self.assertEqual(len(strategy), 0)
     strategy.clear()  # check for exceptions
Exemple #8
0
 def test_lru(self) -> None:
     """test LRUCacheStrategy get, set and del"""
     strategy = LRUCacheStrategy(3)
     strategy["a"] = 1
     strategy["b"] = 2
     strategy["c"] = 3
     self.assertEqual(len(strategy), 3)
     self.assertIn("a", strategy)
     self.assertIn("b", strategy)
     self.assertIn("c", strategy)
     self.assertEqual(strategy["b"], 2)
     strategy["d"] = 4
     self.assertEqual(len(strategy), 3)
     self.assertIn("a", strategy)
     self.assertIn("b", strategy)
     self.assertNotIn("c", strategy)
     self.assertIn("d", strategy)
     del strategy["a"]
     self.assertNotIn("a", strategy)