def test_cache_datasets(dataset_cache):
datasets = [PublishedDatasetFactory(title={"en": f"title {i}"}) for i in range(10)]
out = StringIO()
err = StringIO()
call_command("cache_datasets", stdout=out, stderr=err)
assert len(err.getvalue()) == 0
output = out.getvalue().strip().split("\n")
assert output == [
"Using LocMemCache cache backend",
"Existing cached datasets: 0/10",
"Caching 10 uncached datasets",
"Prefetch complete",
"Cached 10/10 datasets",
"Cache ok",
]
# Check that cache contains correct data
assert dataset_cache.get(datasets[3].id)["title"] == {"en": "title 3"}
assert dataset_cache.get(datasets[4].id)["title"] == {"en": "title 4"}
# Some datasets are modified, their cached values should be invalid
datasets[3].title = {"en": "new title 3"}
datasets[3].save()
datasets[4].save()
# Cache only uncached datasets
out = StringIO()
err = StringIO()
call_command("cache_datasets", stdout=out, stderr=err)
assert len(err.getvalue()) == 0
output = out.getvalue().strip().split("\n")
assert output == [
"Using LocMemCache cache backend",
"Existing cached datasets: 8/10",
"Caching 2 uncached datasets",
"Prefetch complete",
"Cached 2/2 datasets",
"Cache ok",
]
# Check that cache contains updated data
assert dataset_cache.get(datasets[3].id)["title"] == {"en": "new title 3"}
assert dataset_cache.get(datasets[4].id)["title"] == {"en": "title 4"}
# Test clearing dataset cache
assert len(dataset_cache._cache) == 10
out = StringIO()
err = StringIO()
call_command("clear_dataset_cache", stdout=out, stderr=err)
assert len(err.getvalue()) == 0
output = out.getvalue().strip().split("\n")
assert output == ["Cleared serialized_datasets cache"]
# Verify that cached data is gone
assert len(dataset_cache._cache) == 0
assert dataset_cache.get(datasets[3].id) is None
assert dataset_cache.get(datasets[4].id) is None