102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
import argparse
|
|
import datetime
|
|
import itertools
|
|
|
|
import pytest
|
|
|
|
from import2ledger import hooks
|
|
from import2ledger.hooks import add_entity, default_date, filter_by_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'),
|
|
('稲荷', '稲荷'),
|
|
('稲(Jan)荷', '稲荷'),
|
|
('Pøweł', 'Powel'),
|
|
('Elyse Jan Smith', 'Smith-Elyse-Jan'),
|
|
('Jan van Smith', 'van-Smith-Jan'),
|
|
('Francis da Silva', 'da-Silva-Francis'),
|
|
('A van der B', 'van-der-B-A'),
|
|
('A de B de la C', 'de-la-C-A-de-B'),
|
|
])
|
|
def test_add_entity(payee, expected):
|
|
data = {'payee': payee}
|
|
hook = add_entity.AddEntityHook(argparse.Namespace())
|
|
hook.run(data)
|
|
assert data['entity'] == expected
|
|
|
|
|
|
class DateRangeConfig:
|
|
def __init__(self, start_date=None, end_date=None):
|
|
self.start_date = start_date
|
|
self.end_date = end_date
|
|
|
|
def date_in_want_range(self, date):
|
|
return (
|
|
((self.start_date is None) or (date >= self.start_date))
|
|
and ((self.end_date is None) or (date <= self.end_date))
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize('entry_date,start_date,end_date,allowed', [
|
|
(datetime.date(2016, 5, 10), datetime.date(2016, 1, 1), datetime.date(2016, 12, 31), True),
|
|
(datetime.date(2016, 1, 1), datetime.date(2016, 1, 1), datetime.date(2016, 12, 31), True),
|
|
(datetime.date(2016, 12, 31), datetime.date(2016, 1, 1), datetime.date(2016, 12, 31), True),
|
|
(datetime.date(2016, 1, 1), datetime.date(2016, 1, 1), None, True),
|
|
(datetime.date(2016, 12, 31), None, datetime.date(2016, 12, 31), True),
|
|
(datetime.date(1999, 1, 2), None, None, True),
|
|
(datetime.date(2016, 1, 25), datetime.date(2016, 2, 1), datetime.date(2016, 12, 31), False),
|
|
(datetime.date(2016, 12, 26), datetime.date(2016, 1, 1), datetime.date(2016, 11, 30), False),
|
|
(datetime.date(2016, 1, 31), datetime.date(2016, 2, 1), None, False),
|
|
(datetime.date(2016, 12, 1), None, datetime.date(2016, 11, 30), False),
|
|
])
|
|
def test_filter_by_date(entry_date, start_date, end_date, allowed):
|
|
entry_data = {'date': entry_date}
|
|
hook = filter_by_date.FilterByDateHook(DateRangeConfig(start_date, end_date))
|
|
assert hook.run(entry_data) is (None if allowed else False)
|
|
|
|
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
|