Files
@ ae3e4617d31e
Branch filter:
Location: NPO-Accounting/oxrlib/tests/test_CacheWriter.py - annotation
ae3e4617d31e
1.4 KiB
text/x-python
historical: Always format rates with the same precision.
When we format a rate as a price, we don't know how much precision
is "enough" to do the conversion, because we don't know what's
being converted to. As a result, we may (=will almost certainly)
end up formatting the rate with different precision on the cost
date vs. the price date, and that causes Beancount/Ledger to fail
to make the connection between them.
Using a constant of 6 is enough to make the current test for
"enough" precision pass, so just do that for now. This might need
further refinement in the future.
When we format a rate as a price, we don't know how much precision
is "enough" to do the conversion, because we don't know what's
being converted to. As a result, we may (=will almost certainly)
end up formatting the rate with different precision on the cost
date vs. the price date, and that causes Beancount/Ledger to fail
to make the connection between them.
Using a constant of 6 is enough to make the current test for
"enough" precision pass, so just do that for now. This might need
further refinement in the future.
2d482bb7b045 6a416b162d8a 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 6a416b162d8a 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 84bc0c7e84a7 2d482bb7b045 2d482bb7b045 2d482bb7b045 8e5cb0642fe1 8e5cb0642fe1 8e5cb0642fe1 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 84bc0c7e84a7 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 2d482bb7b045 6a416b162d8a 6a416b162d8a 6a416b162d8a 6a416b162d8a 84bc0c7e84a7 | import datetime
import json
import pathlib
import pytest
from . import any_date, relpath, StringIO
import oxrlib.cache
import oxrlib.errors
CACHE_PATH = relpath('writecache')
HISTORICAL_PATTERN = '{date}_{base}_cache.json'
class FakeCacheFile(oxrlib.cache.WriteCacheFile):
def __init__(self, path, *args, **kwargs):
self.path = path
assert args[0].startswith('w')
self.open_file = StringIO()
self.open_file.name = self.path.as_posix()
type(self).last_file = self.open_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
def dummycache():
cache = oxrlib.cache.CacheWriter(CACHE_PATH, file_class=FakeCacheFile)
cache.setup(historical=HISTORICAL_PATTERN)
return cache
def test_cache_success(dummycache, any_date):
base = 'USD'
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 FakeCacheFile.last_file.last_value == json.dumps(rate.serialize())
|