2017-10-22 17:38:53 +00:00
|
|
|
import io
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from . import DATA_DIR, normalize_whitespace
|
|
|
|
|
|
|
|
from import2ledger import __main__ as i2lmain
|
|
|
|
|
|
|
|
ARGLIST = [
|
|
|
|
'-C', (DATA_DIR / 'test_main.ini').as_posix(),
|
|
|
|
]
|
|
|
|
|
|
|
|
def run_main(arglist):
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
exitcode = i2lmain.main(arglist, stdout, stderr)
|
|
|
|
stdout.seek(0)
|
|
|
|
stderr.seek(0)
|
|
|
|
return exitcode, stdout, stderr
|
|
|
|
|
|
|
|
def iter_entries(in_file):
|
|
|
|
lines = []
|
|
|
|
for line in in_file:
|
|
|
|
if line == '\n':
|
|
|
|
if lines:
|
|
|
|
yield ''.join(lines)
|
|
|
|
lines = []
|
|
|
|
else:
|
|
|
|
lines.append(line)
|
|
|
|
if lines:
|
|
|
|
yield ''.join(lines)
|
|
|
|
|
2017-12-19 14:06:24 +00:00
|
|
|
def format_entry(entry_s, format_vars):
|
|
|
|
return normalize_whitespace(entry_s).format_map(format_vars)
|
2017-10-22 17:38:53 +00:00
|
|
|
|
2017-12-19 14:06:24 +00:00
|
|
|
def format_entries(source, format_vars=None):
|
|
|
|
if format_vars is None:
|
|
|
|
format_vars = {}
|
|
|
|
return (format_entry(e, format_vars) for e in iter_entries(source))
|
|
|
|
|
|
|
|
def expected_entries(path, format_vars=None):
|
2017-10-22 17:38:53 +00:00
|
|
|
path = pathlib.Path(path)
|
|
|
|
if not path.is_absolute():
|
|
|
|
path = DATA_DIR / path
|
|
|
|
with path.open() as in_file:
|
2017-12-19 14:06:24 +00:00
|
|
|
return list(format_entries(in_file, format_vars))
|
|
|
|
|
|
|
|
def path_vars(path):
|
|
|
|
return {
|
|
|
|
'source_abspath': str(path),
|
|
|
|
'source_name': path.name,
|
|
|
|
'source_path': str(path),
|
|
|
|
}
|
2017-10-22 17:38:53 +00:00
|
|
|
|
|
|
|
def test_fees_import():
|
2017-12-19 14:06:24 +00:00
|
|
|
source_path = pathlib.Path(DATA_DIR, 'PatreonEarnings.csv')
|
2017-10-22 17:38:53 +00:00
|
|
|
arglist = ARGLIST + [
|
|
|
|
'-c', 'One',
|
2017-12-19 14:06:24 +00:00
|
|
|
source_path.as_posix(),
|
2017-10-22 17:38:53 +00:00
|
|
|
]
|
|
|
|
exitcode, stdout, _ = run_main(arglist)
|
|
|
|
assert exitcode == 0
|
2017-12-19 14:06:24 +00:00
|
|
|
actual = list(format_entries(stdout))
|
|
|
|
expected = expected_entries('test_main_fees_import.ledger', path_vars(source_path))
|
|
|
|
assert actual == expected
|
2017-10-22 20:10:17 +00:00
|
|
|
|
|
|
|
def test_date_range_import():
|
2017-12-19 14:06:24 +00:00
|
|
|
source_path = pathlib.Path(DATA_DIR, 'PatreonEarnings.csv')
|
2017-10-22 20:10:17 +00:00
|
|
|
arglist = ARGLIST + [
|
|
|
|
'-c', 'One',
|
|
|
|
'--date-range', '2017/10/01-',
|
2017-12-19 14:06:24 +00:00
|
|
|
source_path.as_posix(),
|
2017-10-22 20:10:17 +00:00
|
|
|
]
|
|
|
|
exitcode, stdout, _ = run_main(arglist)
|
|
|
|
assert exitcode == 0
|
2017-12-19 14:06:24 +00:00
|
|
|
actual = list(format_entries(stdout))
|
|
|
|
valid = expected_entries('test_main_fees_import.ledger', path_vars(source_path))
|
|
|
|
expected = [entry for entry in valid if entry.startswith('2017/10/')]
|
2017-10-22 20:10:17 +00:00
|
|
|
assert actual == expected
|