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
...
 
@@ -22,2 +22,5 @@ class FileCache(cache.CacheBase):
 

	
 
    def is_cache(self):
 
        return True
 

	
 

	
...
 
@@ -32,2 +35,5 @@ class OXRAPIRequest:
 

	
 
    def is_cache(self):
 
        return False
 

	
 
    def _get_response_encoding(self, response, default=None):
...
 
@@ -71,2 +77,3 @@ class LoaderChain:
 
        self.loaders = []
 
        self.can_cache = False
 

	
...
 
@@ -74,2 +81,3 @@ class LoaderChain:
 
        self.loaders.append(loader)
 
        self.can_cache = self.can_cache or loader.is_cache()
 

	
...
 
@@ -95 +103,4 @@ class LoaderChain:
 
        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
...
 
@@ -13,4 +13,5 @@ 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
 

	
...
 
@@ -22,9 +23,9 @@ class FakeLoader:
 

	
 
    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
 

	
...
 
@@ -72 +73,19 @@ def test_no_success(lchain, any_date, error_loader, count):
 
        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)