oxrlib/tests/test_CacheWriter.py

36 lines
958 B
Python
Raw Normal View History

2017-05-12 17:27:45 +00:00
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