2017-05-09 14:32:23 +00:00
|
|
|
import datetime
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2017-05-12 14:15:12 +00:00
|
|
|
from . import any_date, relpath
|
2017-05-12 12:12:21 +00:00
|
|
|
import oxrlib.errors
|
2017-05-09 14:32:23 +00:00
|
|
|
import oxrlib.loaders
|
|
|
|
|
|
|
|
CACHE_PATH = relpath('filecache')
|
2017-05-12 14:15:12 +00:00
|
|
|
HISTORICAL_PATTERN = '{date}_{base}_cache.json'
|
2017-05-09 14:32:23 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def dummycache():
|
2017-05-12 14:15:12 +00:00
|
|
|
cache = oxrlib.loaders.FileCache(CACHE_PATH)
|
|
|
|
cache.setup(historical=HISTORICAL_PATTERN)
|
|
|
|
return cache
|
2017-05-09 14:32:23 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('date,base', [
|
|
|
|
(datetime.date(1999, 2, 1), 'USD'),
|
|
|
|
(datetime.date(1999, 3, 1), 'EUR'),
|
|
|
|
])
|
|
|
|
def test_cache_success(dummycache, date, base):
|
2017-05-12 14:15:12 +00:00
|
|
|
expect_name = CACHE_PATH / HISTORICAL_PATTERN.format(date=date.isoformat(), base=base)
|
2017-05-09 14:32:23 +00:00
|
|
|
with dummycache.historical(date, base) as cache_file:
|
|
|
|
assert pathlib.Path(cache_file.name) == expect_name
|
|
|
|
|
2017-05-12 14:15:12 +00:00
|
|
|
@pytest.mark.parametrize('date,base,exc_type', [
|
|
|
|
(datetime.date(1999, 2, 1), 'EUR', oxrlib.errors.LoaderNoDataError),
|
|
|
|
(datetime.date(1999, 3, 1), 'USD', oxrlib.errors.LoaderNoDataError),
|
|
|
|
(datetime.date(1200, 12, 31), 'USD', oxrlib.errors.LoaderSourceError),
|
2017-05-09 14:32:23 +00:00
|
|
|
])
|
2017-05-12 14:15:12 +00:00
|
|
|
def test_cache_read_error(dummycache, date, base, exc_type):
|
|
|
|
try:
|
|
|
|
with dummycache.historical(date, base):
|
|
|
|
assert False, "{e.__name__} not raised".format(e=exc_type)
|
|
|
|
except exc_type:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_cache_unconfigured(any_date):
|
|
|
|
cache = oxrlib.loaders.FileCache(CACHE_PATH)
|
2017-05-09 14:32:23 +00:00
|
|
|
try:
|
2017-05-12 14:15:12 +00:00
|
|
|
cache.historical(any_date, 'USD')
|
|
|
|
except oxrlib.errors.CacheLoaderConfigurationError:
|
2017-05-09 14:32:23 +00:00
|
|
|
pass
|
|
|
|
else:
|
2017-05-12 14:15:12 +00:00
|
|
|
assert False, "CacheLoaderConfigurationError not raised"
|