Changeset - 8250f0a8ef33
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-06-10 19:59:56
brettcsmith@brettcsmith.org
filters: Add audit_date() function.
2 files changed with 32 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/filters.py
Show inline comments
...
 
@@ -11,12 +11,13 @@
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import datetime
 
import re
 

	
 
from beancount.core import data as bc_data
 

	
 
from . import data
 
from . import rtutil
...
 
@@ -36,12 +37,19 @@ from .beancount_types import (
 
    Transaction,
 
)
 

	
 
Postings = Iterable[data.Posting]
 
Regexp = Union[str, Pattern]
 

	
 
def audit_date(entries: Entries) -> Optional[datetime.date]:
 
    for entry in entries:
 
        if (isinstance(entry, bc_data.Custom)
 
            and entry.type == 'conservancy_beancount_audit'):  # type:ignore[attr-defined]
 
            return entry.date
 
    return None
 

	
 
def filter_meta_equal(postings: Postings, key: MetaKey, value: MetaValue) -> Postings:
 
    for post in postings:
 
        try:
 
            if post.meta[key] == value:
 
                yield post
 
        except KeyError:
tests/test_filters.py
Show inline comments
...
 
@@ -19,12 +19,14 @@ import itertools
 
import pytest
 

	
 
from . import testutil
 

	
 
from datetime import date
 

	
 
from beancount.core import data as bc_data
 

	
 
from conservancy_beancount import data
 
from conservancy_beancount import filters
 

	
 
MISSING_POSTING = testutil.Posting('<Missing Posting>', 0)
 

	
 
@pytest.fixture
...
 
@@ -156,6 +158,28 @@ def test_remove_opening_balance_txn(opening_txn):
 
    assert opening_txn not in entries
 
    assert not any(
 
        post.account.startswith('Equity:')
 
        for entry in entries
 
        for post in getattr(entry, 'postings', ())
 
    )
 

	
 
@pytest.mark.parametrize('entry', [
 
    bc_data.Custom({}, testutil.FY_START_DATE, 'conservancy_beancount_audit', []),
 
    None,
 
])
 
def test_audit_date(entry):
 
    dates = testutil.date_seq()
 
    entries = [
 
        bc_data.Open({}, next(dates), 'Income:Donations', ['USD'], None),
 
        bc_data.Open({}, next(dates), 'Assets:Cash', ['USD'], None),
 
        testutil.Transaction(postings=[
 
            ('Income:Donations', -10),
 
            ('Assets:Cash', 10),
 
        ]),
 
    ]
 
    if entry is not None:
 
        entries.append(entry)
 
    actual = filters.audit_date(entries)
 
    if entry is None:
 
        assert actual is None
 
    else:
 
        assert actual == entry.date
0 comments (0 inline, 0 general)