Skip to content

Commit

Permalink
Increase test coverage of cache_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Aug 20, 2024
1 parent cd7a019 commit 7de979a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/test_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,47 @@ def testCacheExpiryDatasetsFromDisabled(self) -> None:
self.assertExpiration(cache_manager, 5, threshold + 1)
self.assertIn(f"{mode}={threshold}", str(cache_manager))

def testExpirationModeOverride(self) -> None:
threshold = 2 # Keep 2 datasets.
mode = "datasets"
config_str = self._expiration_config(mode, threshold)

mode = "size"
threshold = 55
with unittest.mock.patch.dict(
os.environ,
{"DAF_BUTLER_CACHE_EXPIRATION_MODE": f"{mode}={threshold}"},
):
cache_manager = self._make_cache_manager(config_str)
self.assertExpiration(cache_manager, 10, 6)
self.assertIn(f"{mode}={threshold}", str(cache_manager))

# Check we get a warning with unrecognized form.
with unittest.mock.patch.dict(
os.environ,
{"DAF_BUTLER_CACHE_EXPIRATION_MODE": "something"},
):
with self.assertLogs(level="WARNING") as cm:
self._make_cache_manager(config_str)
self.assertIn("Unrecognized form (something)", cm.output[0])

with unittest.mock.patch.dict(
os.environ,
{"DAF_BUTLER_CACHE_EXPIRATION_MODE": "something=5"},
):
with self.assertRaises(ValueError) as cm:
self._make_cache_manager(config_str)
self.assertIn("Unrecognized value", str(cm.exception))

def testMissingThreshold(self) -> None:
threshold = ""
mode = "datasets"
config_str = self._expiration_config(mode, threshold)

with self.assertRaises(ValueError) as cm:
self._make_cache_manager(config_str)
self.assertIn("Cache expiration threshold", str(cm.exception))

def testCacheExpiryDatasetsComposite(self) -> None:
threshold = 2 # Keep 2 datasets.
mode = "datasets"
Expand Down

0 comments on commit 7de979a

Please sign in to comment.