From eb7f73e644657b91c0aaf471bea22f2da267d2e6 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sat, 11 Apr 2020 09:20:35 -0400 Subject: [PATCH] data.PostingMeta: Add date property. This is something reporting tools will want a lot. This will make it easier for them to look at just postings without worrying about the parent transaction. --- conservancy_beancount/data.py | 9 +++++++++ tests/test_data_posting_meta.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/conservancy_beancount/data.py b/conservancy_beancount/data.py index 2e0e217..ce79448 100644 --- a/conservancy_beancount/data.py +++ b/conservancy_beancount/data.py @@ -20,6 +20,7 @@ throughout Conservancy tools. # along with this program. If not, see . import collections +import datetime import decimal import functools @@ -231,6 +232,14 @@ class PostingMeta(Metadata): else: super().__delitem__(key) + # This is arguably cheating a litttle bit, but I'd argue the date of + # the parent transaction still qualifies as posting metadata, and + # it's something we want to access so often it's good to have it + # within easy reach. + @property + def date(self) -> datetime.date: + return self.txn.date + class Posting(BasePosting): """Enhanced Posting objects diff --git a/tests/test_data_posting_meta.py b/tests/test_data_posting_meta.py index 0fb5002..9ab88fd 100644 --- a/tests/test_data_posting_meta.py +++ b/tests/test_data_posting_meta.py @@ -112,6 +112,20 @@ def test_keyerror_when_no_entity_or_payee(simple_txn): with pytest.raises(KeyError): meta['entity'] +@pytest.mark.parametrize('date', [ + testutil.FUTURE_DATE, + testutil.FY_START_DATE, + testutil.FY_MID_DATE, + testutil.PAST_DATE, +]) +def test_date(date): + txn = testutil.Transaction(date=date, postings=[ + ('Income:Donations', -15), + ('Assets:Cash', 15), + ]) + for index, post in enumerate(txn.postings): + assert data.PostingMeta(txn, index, post).date == date + # The .get() tests are arguably testing the stdlib, but they're short and # they confirm that we're using the stdlib as we intend. def test_get_with_meta_value(simple_txn):