2020-04-11 20:16:35 +00:00
|
|
|
"""Test Posting methods"""
|
2020-03-17 21:08:47 +00:00
|
|
|
# Copyright © 2020 Brett Smith
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount import data
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def simple_txn(index=None, key=None):
|
|
|
|
return testutil.Transaction(note='txn note', postings=[
|
|
|
|
('Assets:Cash', 5),
|
|
|
|
('Income:Donations', -5, {'note': 'donation love', 'extra': 'Extra'}),
|
|
|
|
])
|
|
|
|
|
2020-04-11 20:16:35 +00:00
|
|
|
def test_from_beancount():
|
|
|
|
txn = testutil.Transaction(payee='Smith-Dakota', postings=[
|
|
|
|
('Income:Donations', -50),
|
|
|
|
('Assets:Cash', 50, {'receipt': 'cash-donation.pdf'}),
|
|
|
|
])
|
|
|
|
post = data.Posting.from_beancount(txn, 1)
|
|
|
|
# We don't just want to assert isinstance(post.attr, data.SomeClass);
|
|
|
|
# we also want to double-check that attributes were instantiated correctly.
|
|
|
|
assert post.account.is_under('Assets:Cash')
|
|
|
|
assert post.meta['receipt'] == 'cash-donation.pdf'
|
|
|
|
assert post.meta['entity'] == 'Smith-Dakota'
|
|
|
|
assert post.meta.date == testutil.FY_MID_DATE
|
2020-03-17 21:08:47 +00:00
|
|
|
|
2020-03-17 21:47:00 +00:00
|
|
|
def test_setting_metadata_propagates_to_source(simple_txn):
|
2020-04-11 20:16:35 +00:00
|
|
|
src_post = simple_txn.postings[1]
|
|
|
|
post = data.Posting.from_beancount(simple_txn, 1)
|
|
|
|
post.meta['edited'] = 'yes'
|
|
|
|
assert src_post.meta['edited'] == 'yes'
|
|
|
|
assert not isinstance(src_post.meta, data.PostingMeta)
|
2020-03-17 21:47:00 +00:00
|
|
|
|
|
|
|
def test_deleting_metadata_propagates_to_source(simple_txn):
|
2020-04-11 20:16:35 +00:00
|
|
|
post = data.Posting.from_beancount(simple_txn, 1)
|
|
|
|
del post.meta['extra']
|
2020-03-17 21:47:00 +00:00
|
|
|
assert 'extra' not in simple_txn.postings[1].meta
|
2020-04-11 20:16:35 +00:00
|
|
|
|
|
|
|
def test_from_txn(simple_txn):
|
|
|
|
for source, post in zip(simple_txn.postings, data.Posting.from_txn(simple_txn)):
|
|
|
|
assert all(source[x] == post[x] for x in range(len(source) - 1))
|
|
|
|
assert isinstance(post.account, data.Account)
|
|
|
|
assert post.meta['note'] # Only works with PostingMeta
|