Changeset - d1ba2dbc6a33
[Not reviewed]
0 1 4
Brett Smith - 7 years ago 2017-05-09 14:32:23
brettcsmith@brettcsmith.org
loaders: Initial commit.
5 files changed with 47 insertions and 2 deletions:
0 comments (0 inline, 0 general)
oxrlib/loaders.py
Show inline comments
 
new file 100644
 
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()
 

	
tests/__init__.py
Show inline comments
...
 
@@ -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)
tests/filecache/1999-02-01_USD_cache.json
Show inline comments
 
new file 100644
tests/filecache/1999-03-01_EUR_cache.json
Show inline comments
 
new file 100644
tests/test_loaders.py
Show inline comments
 
new file 100644
 
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"
0 comments (0 inline, 0 general)