From 4905bc77350089c25073c11cfcbb40ab97850860 Mon Sep 17 00:00:00 2001 From: Oliver Layer Date: Wed, 25 Oct 2023 15:40:52 +0200 Subject: [PATCH] reduce code dup in tests --- tests/server/cache_test.py | 88 +++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 48 deletions(-) diff --git a/tests/server/cache_test.py b/tests/server/cache_test.py index f81544b..348ac88 100644 --- a/tests/server/cache_test.py +++ b/tests/server/cache_test.py @@ -42,83 +42,75 @@ def test_simple(self): assert "other_hash" not in cache + @staticmethod + def assert_hash_in_cache(cache: Cache, hash_value: str): + assert hash_value in cache + assert (cache.cache_folder / hash_value).exists() + + @staticmethod + def assert_hash_not_in_cache(cache: Cache, hash_value: str): + assert hash_value not in cache + assert not (cache.cache_folder / hash_value).exists() + def test_eviction_size_limit(self): with TemporaryDirectory() as tmp_dir: - root_dir = Path(tmp_dir) - cache = Cache(root_dir, max_size_bytes=10) - cache_dir = root_dir / "cache" + cache = Cache(Path(tmp_dir), max_size_bytes=10) cache.put("hash1", bytearray([0x1, 0x2, 0x3, 0x9])) cache.put("hash2", bytearray([0x1, 0x2, 0x3, 0xA])) cache.put("hash3", bytearray([0xFF, 0xFF])) assert len(cache) == 3 - assert (cache_dir / "hash1").exists() - assert (cache_dir / "hash2").exists() - assert (cache_dir / "hash3").exists() + self.assert_hash_in_cache(cache, "hash1") + self.assert_hash_in_cache(cache, "hash2") + self.assert_hash_in_cache(cache, "hash3") cache.put("hash4", bytearray([0x1])) assert len(cache) == 3 - assert "hash2" in cache - assert "hash3" in cache - assert "hash4" in cache - assert not (cache_dir / "hash1").exists() - assert (cache_dir / "hash2").exists() - assert (cache_dir / "hash3").exists() - assert (cache_dir / "hash4").exists() + self.assert_hash_not_in_cache(cache, "hash1") + self.assert_hash_in_cache(cache, "hash2") + self.assert_hash_in_cache(cache, "hash3") + self.assert_hash_in_cache(cache, "hash4") cache.put("hash5", bytearray([0x1])) assert len(cache) == 4 - assert "hash2" in cache - assert "hash3" in cache - assert "hash4" in cache - assert "hash5" in cache - assert (cache_dir / "hash2").exists() - assert (cache_dir / "hash3").exists() - assert (cache_dir / "hash4").exists() - assert (cache_dir / "hash5").exists() + self.assert_hash_in_cache(cache, "hash2") + self.assert_hash_in_cache(cache, "hash3") + self.assert_hash_in_cache(cache, "hash4") + self.assert_hash_in_cache(cache, "hash5") cache.put("hash6", bytearray([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9])) assert len(cache) == 2 - assert not (cache_dir / "hash2").exists() - assert not (cache_dir / "hash3").exists() - assert not (cache_dir / "hash4").exists() - assert "hash5" in cache - assert "hash6" in cache + self.assert_hash_not_in_cache(cache, "hash2") + self.assert_hash_not_in_cache(cache, "hash3") + self.assert_hash_not_in_cache(cache, "hash4") + self.assert_hash_in_cache(cache, "hash5") + self.assert_hash_in_cache(cache, "hash6") def test_eviction_order_lru(self): with TemporaryDirectory() as tmp_dir: - root_dir = Path(tmp_dir) - cache = Cache(root_dir, max_size_bytes=10) - cache_dir = root_dir / "cache" + cache = Cache(Path(tmp_dir), max_size_bytes=10) cache.put("hash1", bytearray([0x1, 0x2, 0x3, 0x9])) cache.put("hash2", bytearray([0x1, 0x2, 0x3, 0xA])) cache.put("hash3", bytearray([0xFF, 0xFF])) assert len(cache) == 3 - assert (cache_dir / "hash1").exists() - assert (cache_dir / "hash2").exists() - assert (cache_dir / "hash3").exists() + self.assert_hash_in_cache(cache, "hash1") + self.assert_hash_in_cache(cache, "hash2") + self.assert_hash_in_cache(cache, "hash3") cache.get("hash1") # make "hash1" the latest used element cache.put("hash4", bytearray([0xFF, 0xFF, 0x0, 0x0])) assert len(cache) == 3 - assert "hash2" not in cache - assert "hash1" in cache - assert "hash3" in cache - assert "hash4" in cache - # TODO: method for asserts combining IO exists and cache exists to reduce boilerplate - assert not (cache_dir / "hash2").exists() - assert (cache_dir / "hash1").exists() - assert (cache_dir / "hash3").exists() - assert (cache_dir / "hash4").exists() + self.assert_hash_not_in_cache(cache, "hash2") + self.assert_hash_in_cache(cache, "hash1") + self.assert_hash_in_cache(cache, "hash3") + self.assert_hash_in_cache(cache, "hash4") assert "hash3" in cache # make "hash3" the latest used element cache.put("hash5", bytearray([0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0])) assert len(cache) == 2 - assert "hash3" in cache - assert "hash5" in cache - assert not (cache_dir / "hash1").exists() - assert not (cache_dir / "hash2").exists() - assert (cache_dir / "hash3").exists() - assert not (cache_dir / "hash4").exists() - assert (cache_dir / "hash5").exists() + self.assert_hash_in_cache(cache, "hash3") + self.assert_hash_in_cache(cache, "hash5") + self.assert_hash_not_in_cache(cache, "hash1") + self.assert_hash_not_in_cache(cache, "hash2") + self.assert_hash_not_in_cache(cache, "hash4")