-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3666b66
commit 188ce5f
Showing
2 changed files
with
38 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,57 @@ | ||
from contextlib import contextmanager | ||
|
||
from large_image.cache_util.base import BaseCache | ||
import pytest | ||
|
||
from django_large_image import tilesource | ||
from django_large_image.cache import DjangoCache | ||
|
||
|
||
@pytest.fixture | ||
def cache_miss_counter(): | ||
@contextmanager | ||
def cache_tracker(): | ||
cache, _ = DjangoCache.getCache() | ||
cache.clear() | ||
|
||
class Counter: | ||
def __init__(self): | ||
self.count = 0 | ||
self.reset() | ||
self.cache = cache | ||
|
||
def reset(self): | ||
self.count = 0 | ||
self.keys = set() | ||
|
||
counter = Counter() | ||
|
||
def missing(*args, **kwargs): | ||
def missing(self, key, *args, **kwargs): | ||
counter.count += 1 | ||
BaseCache.__missing__(*args, **kwargs) | ||
counter.keys.add(key) | ||
BaseCache.__missing__(self, key, *args, **kwargs) | ||
|
||
original = DjangoCache.__missing__ | ||
DjangoCache.__missing__ = missing | ||
yield counter | ||
DjangoCache.__missing__ = original | ||
|
||
|
||
def test_tile(geotiff_path, cache_miss_counter): | ||
def test_cache_tile(geotiff_path): | ||
source = tilesource.get_tilesource_from_path(geotiff_path) | ||
with cache_tracker() as tracker: | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert tracker.count == 1 | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert tracker.count == 1 | ||
|
||
|
||
def test_cache_access(geotiff_path): | ||
source = tilesource.get_tilesource_from_path(geotiff_path) | ||
cache_miss_counter.reset() | ||
# Check size of cache | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert cache_miss_counter.count == 1 | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert cache_miss_counter.count == 1 | ||
with cache_tracker() as tracker: | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert tracker.count == 1 | ||
assert all([k in tracker.cache for k in tracker.keys]) | ||
for k in tracker.keys: | ||
del tracker.cache[k] | ||
assert all([k not in tracker.cache for k in tracker.keys]) | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert tracker.count == 2 | ||
assert len(tracker.keys) == 1 |