cache: Add CacheWriter.save_rate method.
This commit is contained in:
parent
2d482bb7b0
commit
6a416b162d
3 changed files with 34 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
||||||
import functools
|
import functools
|
||||||
|
import json
|
||||||
|
|
||||||
from . import errors
|
from . import errors
|
||||||
|
|
||||||
|
@ -77,3 +78,10 @@ class CacheWriter(CacheBase):
|
||||||
|
|
||||||
def open(self, path):
|
def open(self, path):
|
||||||
return self.CacheFile(path, 'w')
|
return self.CacheFile(path, 'w')
|
||||||
|
|
||||||
|
def write_json(self, cache_file, thing):
|
||||||
|
json.dump(thing, cache_file, indent=2)
|
||||||
|
|
||||||
|
def save_rate(self, rate):
|
||||||
|
with self.historical(rate.timestamp.date(), rate.base) as cache_file:
|
||||||
|
self.write_json(cache_file, rate.serialize())
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import io
|
||||||
import pathlib
|
import pathlib
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -6,6 +7,11 @@ import pytest
|
||||||
|
|
||||||
TEST_DIR = pathlib.Path(__file__).parent
|
TEST_DIR = pathlib.Path(__file__).parent
|
||||||
|
|
||||||
|
class StringIO(io.StringIO):
|
||||||
|
def close(self):
|
||||||
|
self.last_value = self.getvalue()
|
||||||
|
super().close()
|
||||||
|
|
||||||
def relpath(*parts):
|
def relpath(*parts):
|
||||||
return TEST_DIR / pathlib.Path(*parts)
|
return TEST_DIR / pathlib.Path(*parts)
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import datetime
|
import datetime
|
||||||
import io
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from . import any_date, relpath
|
from . import any_date, relpath, StringIO
|
||||||
import oxrlib.cache
|
import oxrlib.cache
|
||||||
import oxrlib.errors
|
import oxrlib.errors
|
||||||
|
|
||||||
|
@ -17,9 +17,19 @@ class TestCacheFile(oxrlib.cache.WriteCacheFile):
|
||||||
assert args[0].startswith('w')
|
assert args[0].startswith('w')
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
open_file = io.StringIO()
|
last_file = StringIO()
|
||||||
open_file.name = self.path.as_posix()
|
last_file.name = self.path.as_posix()
|
||||||
return open_file
|
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
|
@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)
|
expect_name = CACHE_PATH / HISTORICAL_PATTERN.format(date=any_date.isoformat(), base=base)
|
||||||
with dummycache.historical(any_date, base) as cache_file:
|
with dummycache.historical(any_date, base) as cache_file:
|
||||||
assert pathlib.Path(cache_file.name) == expect_name
|
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())
|
||||||
|
|
Loading…
Reference in a new issue