diff --git a/oxrlib/loaders.py b/oxrlib/loaders.py new file mode 100644 index 0000000000000000000000000000000000000000..95020c97e96216299555d48e08b95868d9f9fe8a --- /dev/null +++ b/oxrlib/loaders.py @@ -0,0 +1,9 @@ +class FileCache: + def __init__(self, dir_path, filename_pattern): + self.dir_path = dir_path + self.pattern = filename_pattern + + def historical(self, date, base): + path = self.dir_path / self.pattern.format(date=date.isoformat(), base=base) + return path.open() + diff --git a/tests/__init__.py b/tests/__init__.py index e3fbdfdbd7115d6d1b753e7f340380f48571db9e..98b8c22b7d673d59f255948f1022f87135263725 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,5 +2,5 @@ import pathlib TEST_DIR = pathlib.Path(__file__).parent -def relpath(path): - return TEST_DIR / pathlib.Path(path) +def relpath(*parts): + return TEST_DIR / pathlib.Path(*parts) diff --git a/tests/filecache/1999-02-01_USD_cache.json b/tests/filecache/1999-02-01_USD_cache.json new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/filecache/1999-03-01_EUR_cache.json b/tests/filecache/1999-03-01_EUR_cache.json new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/test_loaders.py b/tests/test_loaders.py new file mode 100644 index 0000000000000000000000000000000000000000..841b56a4a48c4199f4fac1121ef4fbae53b086c8 --- /dev/null +++ b/tests/test_loaders.py @@ -0,0 +1,36 @@ +import datetime +import pathlib + +import pytest + +from . import relpath +import oxrlib.loaders + +CACHE_PATH = relpath('filecache') +CACHE_PATTERN = '{date}_{base}_cache.json' + +@pytest.fixture +def dummycache(): + return oxrlib.loaders.FileCache(CACHE_PATH, CACHE_PATTERN) + +@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) + 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'), +]) +def test_cache_not_found(dummycache, date, base): + try: + cache_file = dummycache.historical(date, base) + except FileNotFoundError: + pass + else: + cache_file.close() + assert False, "cache file found when unexpected"