Changeset - 2b23eba549ad
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-10-26 18:52:41
brettcsmith@brettcsmith.org
data: PostingMeta.detached() can be chain-called.
2 files changed with 21 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -558,25 +558,25 @@ class PostingMeta(Metadata):
 
    @property
 
    def date(self) -> datetime.date:
 
        return self.txn.date
 

	
 
    def detached(self) -> 'PostingMeta':
 
        """Create a copy of this PostingMeta detached from the original post
 

	
 
        Changes you make to the detached copy will not propagate to the
 
        underlying data structures. This is mostly useful for reporting code
 
        that may want to "split" and manipulate the metadata multiple times.
 
        """
 
        retval = type(self)(self.txn, self.index, self.post)
 
        retval.meta = retval.meta.new_child()
 
        retval.meta = self.meta.new_child()
 
        return retval
 

	
 

	
 
class Posting(BasePosting):
 
    """Enhanced Posting objects
 

	
 
    This class is a subclass of Beancount's native Posting class where
 
    specific fields are replaced with enhanced versions:
 

	
 
    * The `account` field is an Account object
 
    * The `units` field is our Amount object (which simply declares that the
 
      number is always a Decimal—see that docstring for details)
tests/test_data_posting_meta.py
Show inline comments
...
 
@@ -135,24 +135,44 @@ def test_mutable_copy():
 
    meta = data.PostingMeta(txn, 1).detached()
 
    meta['layerkey'] = 'two'
 
    assert dict(meta) == {
 
        'filename': 'f',
 
        'lineno': 130,
 
        'txnkey': 'one',
 
        'layerkey': 'two',
 
    }
 
    assert 'layerkey' not in txn.meta
 
    assert all(post.meta is None for post in txn.postings)
 
    assert meta.date == txn.date
 

	
 
def test_double_detached():
 
    txn = testutil.Transaction(filename='f', lineno=140, postings=[
 
        ('Income:Donations', -19),
 
    ])
 
    meta1 = data.PostingMeta(txn, 0).detached()
 
    meta1['metakey'] = 'meta'
 
    meta1['layerkey'] = 'one'
 
    meta2 = meta1.detached()
 
    meta2['layerkey'] = 'two'
 
    expected = {
 
        'filename': 'f',
 
        'lineno': 140,
 
        'metakey': 'meta',
 
        'layerkey': 'two',
 
    }
 
    assert dict(meta2) == expected
 
    expected['layerkey'] = 'one'
 
    assert dict(meta1) == expected
 
    assert not any(post.meta for post in txn.postings)
 

	
 
# 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):
 
    assert data.PostingMeta(simple_txn, 1).get('note') == 'donation love'
 

	
 
def test_get_with_txn_value(simple_txn):
 
    assert data.PostingMeta(simple_txn, 0).get('note') == 'txn note'
 

	
 
def test_get_with_no_value(simple_txn):
 
    assert data.PostingMeta(simple_txn, 0).get('extra') is None
 

	
 
def test_get_with_specified_default(simple_txn):
0 comments (0 inline, 0 general)