Changeset - 8e5cb0642fe1
[Not reviewed]
0 1 0
Brett Smith - 7 years ago 2017-05-17 19:29:21
brettcsmith@brettcsmith.org
CacheWriter: Update tests for previous open file change.
1 file changed with 3 insertions and 6 deletions:
0 comments (0 inline, 0 general)
tests/test_CacheWriter.py
Show inline comments
 
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 TestCacheFile(oxrlib.cache.WriteCacheFile):
 
    def __init__(self, path, *args, **kwargs):
 
        self.path = path
 
        assert args[0].startswith('w')
 

	
 
    def open(self):
 
        last_file = StringIO()
 
        last_file.name = self.path.as_posix()
 
        type(self).last_file = last_file
 
        return self.last_file
 
        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=TestCacheFile)
 
    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 TestCacheFile.last_file.last_value == json.dumps(rate.serialize())
0 comments (0 inline, 0 general)