data: Add Account.load_from_books convenience classmethod.

This commit is contained in:
Brett Smith 2020-07-16 10:33:19 -04:00
parent fff9e37bf8
commit df0c3546fd
2 changed files with 25 additions and 0 deletions

View file

@ -192,6 +192,11 @@ class Account(str):
elif isinstance(entry, bc_data.Close):
cls.load_closing(entry) # type:ignore[arg-type]
@classmethod
def load_from_books(cls, entries: Iterable[Directive], options_map: OptionsMap) -> None:
cls.load_options_map(options_map)
cls.load_openings_and_closings(entries)
@classmethod
def is_account(cls, s: str) -> bool:
return cls.ACCOUNT_RE.fullmatch(s) is not None

View file

@ -313,3 +313,23 @@ def test_is_account_respects_configured_roots(clean_account_meta, account_s, exp
config['name_income'] = 'Revenue'
data.Account.load_options_map(config)
assert data.Account.is_account(account_s) == expected
def test_load_from_books(clean_account_meta):
entries = [
Open({'lineno': 310}, Date(2001, 1, 1), 'Assets:Bank:Checking', ['USD'], None),
Open({'lineno': 315}, Date(2001, 2, 1), 'Revenue:Donations', None, Booking.STRICT),
testutil.Transaction(date=Date(2001, 2, 10), postings=[
('Revenue:Donations', -10),
('Assets:Bank:Checking', 10),
]),
Close({'lineno': 320}, Date(2001, 3, 1), 'Assets:Bank:Checking'),
]
config = bc_options.OPTIONS_DEFAULTS.copy()
config['name_expenses'] = 'Costs'
config['name_income'] = 'Revenue'
data.Account.load_from_books(entries, config)
for post in entries[2].postings:
assert data.Account.is_account(post.account)
check_meta = data.Account(entries[0].account).meta
assert check_meta.open_date == entries[0].date
assert check_meta.close_date == entries[-1].date