LoaderChain: Add should_cache() method.

This commit is contained in:
Brett Smith 2017-05-17 12:27:20 -04:00
parent 08073f752b
commit 351811bb30
2 changed files with 35 additions and 5 deletions

View file

@ -20,6 +20,9 @@ class FileCache(cache.CacheBase):
def open(self, path): def open(self, path):
return self.CacheFile(path) return self.CacheFile(path)
def is_cache(self):
return True
class OXRAPIRequest: class OXRAPIRequest:
DEFAULT_API_ROOT = 'https://openexchangerates.org/api/' DEFAULT_API_ROOT = 'https://openexchangerates.org/api/'
@ -30,6 +33,9 @@ class OXRAPIRequest:
self.app_id = app_id self.app_id = app_id
self.open_url = open_func self.open_url = open_func
def is_cache(self):
return False
def _get_response_encoding(self, response, default=None): def _get_response_encoding(self, response, default=None):
try: try:
content_type = response.getheader('Content-Type', 'application/json') content_type = response.getheader('Content-Type', 'application/json')
@ -69,9 +75,11 @@ class OXRAPIRequest:
class LoaderChain: class LoaderChain:
def __init__(self): def __init__(self):
self.loaders = [] self.loaders = []
self.can_cache = False
def add_loader(self, loader): def add_loader(self, loader):
self.loaders.append(loader) self.loaders.append(loader)
self.can_cache = self.can_cache or loader.is_cache()
def _wrap_load_method(orig_func): def _wrap_load_method(orig_func):
@functools.wraps(orig_func) @functools.wraps(orig_func)
@ -93,3 +101,6 @@ class LoaderChain:
@_wrap_load_method @_wrap_load_method
def historical(self, date, base): def historical(self, date, base):
pass pass
def should_cache(self):
return self.can_cache and self.used_loader and not self.used_loader.is_cache()

View file

@ -11,8 +11,9 @@ SUCCESS_S = '"success"\n'
ERROR = oxrlib.errors.LoaderNoDataError("test") ERROR = oxrlib.errors.LoaderNoDataError("test")
class FakeLoader: class FakeLoader:
def __init__(self, result): def __init__(self, result, *, is_cache=False):
self.result = result self.result = result
self._is_cache = is_cache
def _respond(self, *args, **kwargs): def _respond(self, *args, **kwargs):
return io.StringIO(self.result) return io.StringIO(self.result)
@ -20,13 +21,13 @@ class FakeLoader:
def __getattr__(self, name): def __getattr__(self, name):
return self._respond return self._respond
def is_cache(self):
return self._is_cache
class FakeErrorLoader(FakeLoader): class FakeErrorLoader(FakeLoader):
def __init__(self, error):
self.error = error
def _respond(self, *args, **kwargs): def _respond(self, *args, **kwargs):
raise self.error raise self.result
@pytest.fixture @pytest.fixture
@ -70,3 +71,21 @@ def test_no_success(lchain, any_date, error_loader, count):
assert error is ERROR assert error is ERROR
else: else:
assert False, "{} not raised".format(type(ERROR).__name__) assert False, "{} not raised".format(type(ERROR).__name__)
def test_should_cache(lchain, any_date, good_loader):
cache_loader = FakeErrorLoader(ERROR, is_cache=True)
lchain.add_loader(cache_loader)
lchain.add_loader(good_loader)
lchain.historical(any_date, 'USD')
assert lchain.should_cache()
def test_should_cache_unable(lchain, any_date, good_loader):
lchain.add_loader(good_loader)
lchain.historical(any_date, 'USD')
assert not lchain.should_cache(), "suggested using unavailable cache"
def test_should_cache_unneeded(lchain, any_date):
loader = FakeLoader(SUCCESS_S, is_cache=True)
lchain.add_loader(loader)
lchain.historical(any_date, 'USD')
assert not lchain.should_cache(), "suggested rewriting cache"