File diff 4cbd89099230 → eba27c16ae78
tests/test_FileCache.py
Show inline comments
...
 
@@ -3,35 +3,45 @@ import pathlib
 

	
 
import pytest
 

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

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

	
 
@pytest.fixture
 
def dummycache():
 
    return oxrlib.loaders.FileCache(CACHE_PATH, CACHE_PATTERN)
 
    cache = oxrlib.loaders.FileCache(CACHE_PATH)
 
    cache.setup(historical=HISTORICAL_PATTERN)
 
    return cache
 

	
 
@pytest.mark.parametrize('date,base', [
 
    (datetime.date(1999, 2, 1), 'USD'),
 
    (datetime.date(1999, 3, 1), 'EUR'),
 
])
 
def test_cache_success(dummycache, date, base):
 
    expect_name = CACHE_PATH / CACHE_PATTERN.format(date=date.isoformat(), base=base)
 
    expect_name = CACHE_PATH / HISTORICAL_PATTERN.format(date=date.isoformat(), base=base)
 
    with dummycache.historical(date, base) as cache_file:
 
        assert pathlib.Path(cache_file.name) == expect_name
 

	
 
@pytest.mark.parametrize('date,base', [
 
    (datetime.date(1999, 2, 1), 'EUR'),
 
    (datetime.date(1999, 3, 1), 'USD'),
 
@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),
 
])
 
def test_cache_not_found(dummycache, date, base):
 
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)
 
    try:
 
        cache_file = dummycache.historical(date, base)
 
    except oxrlib.errors.LoaderNoDataError:
 
        cache.historical(any_date, 'USD')
 
    except oxrlib.errors.CacheLoaderConfigurationError:
 
        pass
 
    else:
 
        cache_file.close()
 
        assert False, "cache file found when unexpected"
 
        assert False, "CacheLoaderConfigurationError not raised"