import2ledger/tests/test_hooks.py
Brett Smith 5c73c40bcc import2ledger: First version.
This is a pretty feature-complete version 1.
I don't know why I waited this long to commit anything.
2017-10-22 13:40:20 -04:00

66 lines
1.8 KiB
Python

import argparse
import datetime
import itertools
import pytest
from import2ledger import hooks
from import2ledger.hooks import add_entity, default_date
def test_load_all():
all_hooks = list(hooks.load_all())
assert add_entity.AddEntityHook in all_hooks
@pytest.mark.parametrize('payee,expected', [
('Alex Smith', 'Smith-Alex'),
('Dakota D. Doe', 'Doe-Dakota-D'),
('Björk', 'Bjork'),
('Fran Doe-Smith', 'Doe-Smith-Fran'),
('Alex(Nickname) Smith', 'Smith-Alex'),
('稲荷', '稲荷'),
])
def test_add_entity(payee, expected):
data = {'payee': payee}
hook = add_entity.AddEntityHook(argparse.Namespace())
hook.run(data)
assert data['entity'] == expected
class DefaultDateConfig:
ONE_DAY = datetime.timedelta(days=1)
def __init__(self, start_date=None):
if start_date is None:
start_date = datetime.date(2016, 3, 5)
self.date = start_date - self.ONE_DAY
def get_default_date(self, section_name=None):
self.date += self.ONE_DAY
return self.date
class TestDefaultDate:
def test_simple_case(self):
expect_date = datetime.date(2016, 2, 4)
config = DefaultDateConfig(expect_date)
data = {}
hook = default_date.DefaultDateHook(config)
hook.run(data)
assert data['date'] == expect_date
def test_no_caching(self):
config = DefaultDateConfig()
hook = default_date.DefaultDateHook(config)
d1 = {}
d2 = {}
hook.run(d1)
hook.run(d2)
assert d1['date'] != d2['date']
def test_no_override(self):
expect_date = datetime.date(2016, 2, 6)
config = DefaultDateConfig(expect_date + datetime.timedelta(days=300))
hook = default_date.DefaultDateHook(config)
data = {'date': expect_date}
hook.run(data)
assert data['date'] is expect_date