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
...
 
@@ -5,24 +5,25 @@
 
# it under the terms of the GNU Affero General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# 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
 

	
 
from typing import (
 
    cast,
 
    Iterable,
 
    Optional,
 
    Pattern,
...
 
@@ -30,24 +31,31 @@ from typing import (
 
)
 
from .beancount_types import (
 
    Directive,
 
    Entries,
 
    MetaKey,
 
    MetaValue,
 
    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:
 
            pass
 

	
 
def filter_meta_match(postings: Postings, key: MetaKey, regexp: Regexp) -> Postings:
 
    for post in postings:
 
        try:
 
            if re.search(regexp, post.meta[key]):
tests/test_filters.py
Show inline comments
...
 
@@ -13,24 +13,26 @@
 
#
 
# 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 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
 
def cc_txn_pair():
 
    dates = testutil.date_seq()
 
    txn_meta = {
 
        'payee': 'Smith-Dakota',
 
        'rt-id': 'rt:550',
 
    }
...
 
@@ -150,12 +152,34 @@ def test_remove_opening_balance_txn(opening_txn):
 
                ('Expenses:Other', 75),
 
        ]]
 
    if opening_txn is not None:
 
        entries.insert(1, opening_txn)
 
    actual = filters.remove_opening_balance_txn(entries)
 
    assert actual is 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)