Changeset - eb7f73e64465
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-11 13:20:35
brettcsmith@brettcsmith.org
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.
2 files changed with 23 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -20,6 +20,7 @@ throughout Conservancy tools.
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
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
tests/test_data_posting_meta.py
Show inline comments
...
 
@@ -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):
0 comments (0 inline, 0 general)