Changeset - 4a28596db267
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-06-15 22:08:18
brettcsmith@brettcsmith.org
data: bank-statement and tax-statement are link metadata.

Not including them earlier was an oversight.
3 files changed with 6 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -11,103 +11,105 @@ throughout Conservancy tools.
 
# 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 collections
 
import datetime
 
import decimal
 
import functools
 
import re
 

	
 
from beancount.core import account as bc_account
 
from beancount.core import amount as bc_amount
 
from beancount.core import convert as bc_convert
 
from beancount.core import position as bc_position
 

	
 
from typing import (
 
    cast,
 
    overload,
 
    Callable,
 
    Hashable,
 
    Iterable,
 
    Iterator,
 
    MutableMapping,
 
    Optional,
 
    Sequence,
 
    TypeVar,
 
    Union,
 
)
 

	
 
from .beancount_types import (
 
    Directive,
 
    MetaKey,
 
    MetaValue,
 
    Posting as BasePosting,
 
    Transaction,
 
)
 

	
 
DecimalCompat = Union[decimal.Decimal, int]
 

	
 
LINK_METADATA = frozenset([
 
    'approval',
 
    'bank-statement',
 
    'check',
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
 
    'rt-id',
 
    'statement',
 
    'tax-statement',
 
])
 

	
 
class Account(str):
 
    """Account name string
 

	
 
    This is a string that names an account, like Assets:Bank:Checking
 
    or Income:Donations. This class provides additional methods for common
 
    account name parsing and queries.
 
    """
 
    __slots__ = ()
 

	
 
    SEP = bc_account.sep
 

	
 
    def is_cash_equivalent(self) -> bool:
 
        return (
 
            self.is_under('Assets:') is not None
 
            and self.is_under('Assets:Prepaid', 'Assets:Receivable') is None
 
        )
 

	
 
    def is_checking(self) -> bool:
 
        return self.is_cash_equivalent() and ':Check' in self
 

	
 
    def is_credit_card(self) -> bool:
 
        return self.is_under('Liabilities:CreditCard') is not None
 

	
 
    def is_opening_equity(self) -> bool:
 
        return self.is_under('Equity:Funds', 'Equity:OpeningBalance') is not None
 

	
 
    def is_under(self, *acct_seq: str) -> Optional[str]:
 
        """Return a match if this account is "under" a part of the hierarchy
 

	
 
        Pass in any number of account name strings as arguments. If this
 
        account is under one of those strings in the account hierarchy, the
 
        first matching string will be returned. Otherwise, None is returned.
 

	
 
        You can use the return value of this method as a boolean if you don't
 
        care which account string is matched.
 

	
 
        An account is considered to be under itself:
 

	
 
          Account('Expenses:Tax').is_under('Expenses:Tax') # returns 'Expenses:Tax'
 

	
 
        To do a "strictly under" search, end your search strings with colons:
 

	
 
          Account('Expenses:Tax').is_under('Expenses:Tax:') # returns None
 
          Account('Expenses:Tax').is_under('Expenses:') # returns 'Expenses:'
 

	
 
        This method does check that all the account boundaries match:
tests/test_meta_repo_links.py
Show inline comments
 
"""Test link checker for repository documents"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# 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 itertools
 

	
 
from pathlib import Path
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import errors as errormod
 
from conservancy_beancount.plugin import meta_repo_links
 

	
 
METADATA_KEYS = [
 
    'approval',
 
    'bank-statement',
 
    'check',
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
 
    'statement',
 
    'tax-statement',
 
]
 

	
 
GOOD_LINKS = [Path(s) for s in [
 
    'Projects/project-data.yml',
 
    'Projects/project-list.yml',
 
]]
 

	
 
BAD_LINKS = [Path(s) for s in [
 
    'NonexistentDirectory/NonexistentFile1.txt',
 
    'NonexistentDirectory/NonexistentFile2.txt',
 
]]
 

	
 
NOT_FOUND_MSG = '{} not found in repository: {}'.format
 

	
 
def build_meta(keys=None, *sources):
 
    if keys is None:
 
        keys = iter(METADATA_KEYS)
 
    sources = (itertools.cycle(src) for src in sources)
 
    return {key: ' '.join(str(x) for x in rest)
 
            for key, *rest in zip(keys, *sources)}
 

	
 
@pytest.fixture(scope='module')
 
def hook():
 
    config = testutil.TestConfig(repo_path='repository')
 
    return meta_repo_links.MetaRepoLinks(config)
 

	
 
def test_error_with_no_repository():
 
    config = testutil.TestConfig(repo_path=None)
 
    with pytest.raises(errormod.ConfigurationError):
 
        meta_repo_links.MetaRepoLinks(config)
 

	
 
def test_good_txn_links(hook):
 
    meta = build_meta(None, GOOD_LINKS)
 
    txn = testutil.Transaction(**meta, postings=[
 
        ('Income:Donations', -5),
 
        ('Assets:Cash', 5),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
def test_good_post_links(hook):
 
    meta = build_meta(None, GOOD_LINKS)
 
    txn = testutil.Transaction(postings=[
 
        ('Income:Donations', -5, meta),
 
        ('Assets:Cash', 5),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
def test_bad_txn_links(hook):
tests/test_meta_rt_links.py
Show inline comments
 
"""Test link checker for RT links"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# 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 itertools
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import errors as errormod
 
from conservancy_beancount.plugin import meta_rt_links
 

	
 
METADATA_KEYS = [
 
    'approval',
 
    'bank-statement',
 
    'check',
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
 
    'rt-id',
 
    'statement',
 
    'tax-statement',
 
]
 

	
 
GOOD_LINKS = [
 
    'rt:1',
 
    'rt:1/5',
 
    'rt://ticket/2',
 
    'rt://ticket/3/attachments/15',
 
]
 

	
 
MALFORMED_LINKS = [
 
    'rt:one',
 
    'rt:two/three',
 
    'rt://4',
 
    'rt://ticket/5/attach/6',
 
]
 

	
 
NOT_FOUND_LINKS = [
 
    'rt:1/10',
 
    'rt:10',
 
    'rt://ticket/9',
 
    'rt://ticket/3/attachments/99',
 
]
 

	
 
MALFORMED_MSG = '{} link is malformed: {}'.format
 
NOT_FOUND_MSG = '{} not found in RT: {}'.format
 

	
 
def build_meta(keys=None, *sources):
 
    if keys is None:
 
        keys = iter(METADATA_KEYS)
 
    sources = (itertools.cycle(src) for src in sources)
 
    return {key: ' '.join(str(x) for x in rest)
 
            for key, *rest in zip(keys, *sources)}
 

	
 
@pytest.fixture(scope='module')
 
def hook():
 
    config = testutil.TestConfig(rt_client=testutil.RTClient())
 
    return meta_rt_links.MetaRTLinks(config)
 

	
 
def test_error_with_no_rt():
 
    config = testutil.TestConfig()
 
    with pytest.raises(errormod.ConfigurationError):
 
        meta_rt_links.MetaRTLinks(config)
 

	
 
def test_good_txn_links(hook):
 
    meta = build_meta(None, GOOD_LINKS)
 
    txn = testutil.Transaction(**meta, postings=[
 
        ('Income:Donations', -5),
 
        ('Assets:Cash', 5),
0 comments (0 inline, 0 general)