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.
This commit is contained in:
Brett Smith 2020-04-11 09:20:35 -04:00
parent a156617b4d
commit eb7f73e644
2 changed files with 23 additions and 0 deletions

View file

@ -20,6 +20,7 @@ throughout Conservancy tools.
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import collections import collections
import datetime
import decimal import decimal
import functools import functools
@ -231,6 +232,14 @@ class PostingMeta(Metadata):
else: else:
super().__delitem__(key) 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): class Posting(BasePosting):
"""Enhanced Posting objects """Enhanced Posting objects

View file

@ -112,6 +112,20 @@ def test_keyerror_when_no_entity_or_payee(simple_txn):
with pytest.raises(KeyError): with pytest.raises(KeyError):
meta['entity'] 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 # The .get() tests are arguably testing the stdlib, but they're short and
# they confirm that we're using the stdlib as we intend. # they confirm that we're using the stdlib as we intend.
def test_get_with_meta_value(simple_txn): def test_get_with_meta_value(simple_txn):