Changeset - 351811bb30b1
[Not reviewed]
0 2 0
Brett Smith - 7 years ago 2017-05-17 16:27:20
brettcsmith@brettcsmith.org
LoaderChain: Add should_cache() method.
2 files changed with 35 insertions and 5 deletions:
0 comments (0 inline, 0 general)
oxrlib/loaders.py
Show inline comments
...
 
@@ -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()
tests/test_LoaderChain.py
Show inline comments
...
 
@@ -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
 

	
 
class FakeErrorLoader(FakeLoader):
 
    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"
0 comments (0 inline, 0 general)