Changeset - 6a416b162d8a
[Not reviewed]
0 3 0
Brett Smith - 7 years ago 2017-05-12 17:48:21
brettcsmith@brettcsmith.org
cache: Add CacheWriter.save_rate method.
3 files changed with 34 insertions and 5 deletions:
0 comments (0 inline, 0 general)
oxrlib/cache.py
Show inline comments
 
import functools
 
import json
 

	
 
from . import errors
 

	
 
class CacheFileBase:
 
    def __init__(self, path, *args, **kwargs):
 
        self.path = path
...
 
@@ -74,6 +75,13 @@ class WriteCacheFile(CacheFileBase):
 

	
 
class CacheWriter(CacheBase):
 
    CacheFile = WriteCacheFile
 

	
 
    def open(self, path):
 
        return self.CacheFile(path, 'w')
 

	
 
    def write_json(self, cache_file, thing):
 
        json.dump(thing, cache_file, indent=2)
 

	
 
    def save_rate(self, rate):
 
        with self.historical(rate.timestamp.date(), rate.base) as cache_file:
 
            self.write_json(cache_file, rate.serialize())
tests/__init__.py
Show inline comments
 
import datetime
 
import io
 
import pathlib
 
import random
 

	
 
import pytest
 

	
 
TEST_DIR = pathlib.Path(__file__).parent
 

	
 
class StringIO(io.StringIO):
 
    def close(self):
 
        self.last_value = self.getvalue()
 
        super().close()
 

	
 
def relpath(*parts):
 
    return TEST_DIR / pathlib.Path(*parts)
 

	
 
@pytest.fixture
 
def any_date():
 
    return datetime.date.today() - datetime.timedelta(days=730 - random.randint(0, 365))
tests/test_CacheWriter.py
Show inline comments
 
import datetime
 
import io
 
import json
 
import pathlib
 

	
 
import pytest
 

	
 
from . import any_date, relpath
 
from . import any_date, relpath, StringIO
 
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
 
        last_file = StringIO()
 
        last_file.name = self.path.as_posix()
 
        type(self).last_file = last_file
 
        return self.last_file
 

	
 

	
 
class FakeRate:
 
    def __init__(self, date, base):
 
        self.timestamp = datetime.datetime(date.year, date.month, date.day)
 
        self.base = base
 

	
 
    def serialize(self):
 
        return "test data for {} in {}".format(self.base, self.timestamp.date())
 

	
 

	
 
@pytest.fixture
 
def dummycache():
 
    cache = oxrlib.cache.CacheWriter(CACHE_PATH, file_class=TestCacheFile)
 
    cache.setup(historical=HISTORICAL_PATTERN)
...
 
@@ -30,6 +40,11 @@ def dummycache():
 

	
 
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
 

	
 
def test_save_rate(dummycache, any_date):
 
    rate = FakeRate(any_date, 'USD')
 
    dummycache.save_rate(rate)
 
    assert TestCacheFile.last_file.last_value == json.dumps(rate.serialize())
0 comments (0 inline, 0 general)