2017-10-22 17:38:53 +00:00
|
|
|
import datetime
|
2017-11-09 20:06:02 +00:00
|
|
|
import decimal
|
2017-10-22 17:38:53 +00:00
|
|
|
import importlib
|
|
|
|
import itertools
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import yaml
|
|
|
|
from import2ledger import importers
|
|
|
|
|
|
|
|
from . import DATA_DIR
|
|
|
|
|
|
|
|
class TestImporters:
|
2017-11-09 20:06:02 +00:00
|
|
|
def _date(parts_list):
|
|
|
|
return datetime.date(*parts_list)
|
|
|
|
|
|
|
|
DATA_TYPES = {
|
|
|
|
'date': _date,
|
|
|
|
'fee': decimal.Decimal,
|
|
|
|
'tax': decimal.Decimal,
|
|
|
|
}
|
|
|
|
|
2017-10-22 17:38:53 +00:00
|
|
|
with pathlib.Path(DATA_DIR, 'imports.yml').open() as yaml_file:
|
|
|
|
test_data = yaml.load(yaml_file)
|
|
|
|
for test in test_data:
|
|
|
|
test['source'] = DATA_DIR / test['source']
|
|
|
|
|
|
|
|
module_name, class_name = test['importer'].rsplit('.', 1)
|
|
|
|
module = importlib.import_module('.' + module_name, 'import2ledger.importers')
|
|
|
|
test['importer'] = getattr(module, class_name)
|
|
|
|
|
|
|
|
for expect_result in test['expect']:
|
2017-11-09 20:06:02 +00:00
|
|
|
for key, type_func in DATA_TYPES.items():
|
|
|
|
try:
|
|
|
|
expect_result[key] = type_func(expect_result[key])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-10-22 17:38:53 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('source_path,importer', [
|
|
|
|
(t['source'], t['importer']) for t in test_data
|
|
|
|
])
|
|
|
|
def test_can_import(self, source_path, importer):
|
|
|
|
with source_path.open() as source_file:
|
|
|
|
assert importer.can_import(source_file)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('source_path,import_class,expect_results', [
|
|
|
|
(t['source'], t['importer'], t['expect']) for t in test_data
|
|
|
|
])
|
|
|
|
def test_import(self, source_path, import_class, expect_results):
|
|
|
|
with source_path.open() as source_file:
|
|
|
|
importer = import_class(source_file)
|
|
|
|
for actual, expected in itertools.zip_longest(importer, expect_results):
|
|
|
|
assert actual == expected
|
|
|
|
|
|
|
|
def test_loader(self):
|
|
|
|
all_importers = list(importers.load_all())
|
|
|
|
for test in self.test_data:
|
|
|
|
assert test['importer'] in all_importers
|