Changeset - 163ecbc7d3d2
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-03-17 22:06:43
brettcsmith@brettcsmith.org
data: iter_postings uses Account.
2 files changed with 2 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -77,14 +77,15 @@ class PostingMeta(collections.abc.MutableMapping):
 
            self.post.meta[key] = value
 

	
 
    def __delitem__(self, key: MetaKey) -> None:
 
        if self.post.meta is None:
 
            raise KeyError(key)
 
        else:
 
            del self.post.meta[key]
 

	
 

	
 
def iter_postings(txn: Transaction) -> Iterator[Posting]:
 
    for index, source in enumerate(txn.postings):
 
        yield source._replace(
 
            account=Account(source.account),
 
            meta=PostingMeta(txn, index, source),
 
        )
tests/test_data_iter_postings.py
Show inline comments
...
 
@@ -21,24 +21,25 @@ 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'}),
 
    ])
 

	
 
def test_iter_postings(simple_txn):
 
    for source, post in zip(simple_txn.postings, data.iter_postings(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
 

	
 
def test_setting_metadata_propagates_to_source(simple_txn):
 
    for index, post in enumerate(data.iter_postings(simple_txn)):
 
        post.meta['edited'] = str(index)
 
    for index, post in enumerate(simple_txn.postings):
 
        assert post.meta['edited'] == str(index)
 
        assert not isinstance(post.meta, data.PostingMeta)
 

	
 
def test_deleting_metadata_propagates_to_source(simple_txn):
 
    posts = list(data.iter_postings(simple_txn))
 
    del posts[1].meta['extra']
0 comments (0 inline, 0 general)