From 351811bb30b11ae9487b1993e1532884a85fec08 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Wed, 17 May 2017 12:27:20 -0400 Subject: [PATCH] LoaderChain: Add should_cache() method. --- oxrlib/loaders.py | 11 +++++++++++ tests/test_LoaderChain.py | 29 ++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/oxrlib/loaders.py b/oxrlib/loaders.py index 2a600ed..3203d31 100644 --- a/oxrlib/loaders.py +++ b/oxrlib/loaders.py @@ -20,6 +20,9 @@ class FileCache(cache.CacheBase): def open(self, path): return self.CacheFile(path) + def is_cache(self): + return True + class OXRAPIRequest: DEFAULT_API_ROOT = 'https://openexchangerates.org/api/' @@ -30,6 +33,9 @@ class OXRAPIRequest: self.app_id = app_id self.open_url = open_func + def is_cache(self): + return False + def _get_response_encoding(self, response, default=None): try: content_type = response.getheader('Content-Type', 'application/json') @@ -69,9 +75,11 @@ class OXRAPIRequest: class LoaderChain: def __init__(self): self.loaders = [] + self.can_cache = False def add_loader(self, loader): self.loaders.append(loader) + self.can_cache = self.can_cache or loader.is_cache() def _wrap_load_method(orig_func): @functools.wraps(orig_func) @@ -93,3 +101,6 @@ class LoaderChain: @_wrap_load_method def historical(self, date, base): pass + + def should_cache(self): + return self.can_cache and self.used_loader and not self.used_loader.is_cache() diff --git a/tests/test_LoaderChain.py b/tests/test_LoaderChain.py index 1327934..5f3fa21 100644 --- a/tests/test_LoaderChain.py +++ b/tests/test_LoaderChain.py @@ -11,8 +11,9 @@ SUCCESS_S = '"success"\n' ERROR = oxrlib.errors.LoaderNoDataError("test") class FakeLoader: - def __init__(self, result): + def __init__(self, result, *, is_cache=False): self.result = result + self._is_cache = is_cache def _respond(self, *args, **kwargs): return io.StringIO(self.result) @@ -20,13 +21,13 @@ class FakeLoader: def __getattr__(self, name): return self._respond + def is_cache(self): + return self._is_cache + class FakeErrorLoader(FakeLoader): - def __init__(self, error): - self.error = error - def _respond(self, *args, **kwargs): - raise self.error + raise self.result @pytest.fixture @@ -70,3 +71,21 @@ def test_no_success(lchain, any_date, error_loader, count): assert error is ERROR else: 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"