diff --git a/tests/test_CacheWriter.py b/tests/test_CacheWriter.py index b9330757e412328ce149382f35c15dd38a21e4d3..1ceadce2cfe17eaadd3af6a004282dab688312a7 100644 --- a/tests/test_CacheWriter.py +++ b/tests/test_CacheWriter.py @@ -1,10 +1,10 @@ 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 @@ -17,9 +17,19 @@ class TestCacheFile(oxrlib.cache.WriteCacheFile): 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 @@ -33,3 +43,8 @@ def test_cache_success(dummycache, any_date): 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())