Changeset - 2d482bb7b045
[Not reviewed]
0 2 1
Brett Smith - 7 years ago 2017-05-12 17:27:45
brettcsmith@brettcsmith.org
cache: Add CacheWriter class.
3 files changed with 52 insertions and 1 deletions:
0 comments (0 inline, 0 general)
oxrlib/cache.py
Show inline comments
...
 
@@ -33,6 +33,10 @@ class CacheBase:
 

	
 
    def __init__(self, dir_path, **kwargs):
 
        self.dir_path = dir_path
 
        try:
 
            self.CacheFile = kwargs.pop('file_class')
 
        except KeyError:
 
            pass
 
        self.fn_patterns = {}
 
        self.setup(**kwargs)
 

	
...
 
@@ -62,3 +66,14 @@ class CacheBase:
 
    @_wrap_api_method
 
    def historical(self, date, base):
 
        return {'date': date.isoformat(), 'base': base}
 

	
 

	
 
class WriteCacheFile(CacheFileBase):
 
    ERRORS_MAP = []
 

	
 

	
 
class CacheWriter(CacheBase):
 
    CacheFile = WriteCacheFile
 

	
 
    def open(self, path):
 
        return self.CacheFile(path, 'w')
oxrlib/loaders.py
Show inline comments
...
 
@@ -14,10 +14,11 @@ class ReadCacheFile(cache.CacheFileBase):
 

	
 

	
 
class FileCache(cache.CacheBase):
 
    CacheFile = ReadCacheFile
 
    ConfigurationError = errors.CacheLoaderConfigurationError
 

	
 
    def open(self, path):
 
        return ReadCacheFile(path)
 
        return self.CacheFile(path)
 

	
 

	
 
class OXRAPIRequest:
tests/test_CacheWriter.py
Show inline comments
 
new file 100644
 
import datetime
 
import io
 
import pathlib
 

	
 
import pytest
 

	
 
from . import any_date, relpath
 
import oxrlib.cache
 
import oxrlib.errors
 

	
 
CACHE_PATH = relpath('writecache')
 
HISTORICAL_PATTERN = '{date}_{base}_cache.json'
 

	
 
class TestCacheFile(oxrlib.cache.WriteCacheFile):
 
    def __init__(self, path, *args, **kwargs):
 
        self.path = path
 
        assert args[0].startswith('w')
 

	
 
    def open(self):
 
        open_file = io.StringIO()
 
        open_file.name = self.path.as_posix()
 
        return open_file
 

	
 

	
 
@pytest.fixture
 
def dummycache():
 
    cache = oxrlib.cache.CacheWriter(CACHE_PATH, file_class=TestCacheFile)
 
    cache.setup(historical=HISTORICAL_PATTERN)
 
    return cache
 

	
 
def test_cache_success(dummycache, any_date):
 
    base = 'USD'
 
    expect_name = CACHE_PATH / HISTORICAL_PATTERN.format(date=any_date.isoformat(), base=base)
 
    with dummycache.historical(any_date, base) as cache_file:
 
        assert pathlib.Path(cache_file.name) == expect_name
0 comments (0 inline, 0 general)