Files @ f09f029fc4cb
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/testutil.py - annotation

Brett Smith
config: Add Config.rt_client method.
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
0d370c445b9e
786291902231
16c47c64b23d
16c47c64b23d
786291902231
786291902231
16c47c64b23d
8d3816a8fd87
786291902231
e7720b8fb866
e7720b8fb866
e7720b8fb866
e7720b8fb866
e7720b8fb866
e7720b8fb866
f1c115de49a8
f1c115de49a8
f1c115de49a8
f1c115de49a8
e7720b8fb866
786291902231
786291902231
786291902231
8d3816a8fd87
8d3816a8fd87
8d3816a8fd87
8d3816a8fd87
8d3816a8fd87
8d3816a8fd87
8d3816a8fd87
8d3816a8fd87
786291902231
786291902231
786291902231
547ae657808f
547ae657808f
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
786291902231
0d370c445b9e
0d370c445b9e
0d370c445b9e
0d370c445b9e
8d3816a8fd87
0d370c445b9e
0d370c445b9e
0d370c445b9e
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
f09f029fc4cb
"""Mock Beancount objects for testing"""
# 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 datetime

import beancount.core.amount as bc_amount
import beancount.core.data as bc_data

from decimal import Decimal
from pathlib import Path

EXTREME_FUTURE_DATE = datetime.date(datetime.MAXYEAR, 12, 30)
FUTURE_DATE = datetime.date.today() + datetime.timedelta(days=365 * 99)
FY_START_DATE = datetime.date(2020, 3, 1)
FY_MID_DATE = datetime.date(2020, 9, 1)
PAST_DATE = datetime.date(2000, 1, 1)
TESTS_DIR = Path(__file__).parent

def check_post_meta(txn, *expected_meta, default=None):
    assert len(txn.postings) == len(expected_meta)
    for post, expected in zip(txn.postings, expected_meta):
        if not expected:
            assert not post.meta
        else:
            actual = None if post.meta is None else {
                key: post.meta.get(key, default) for key in expected
            }
            assert actual == expected

def parse_date(s, fmt='%Y-%m-%d'):
    return datetime.datetime.strptime(s, fmt).date()

def test_path(s):
    if s is None:
        return s
    s = Path(s)
    if not s.is_absolute():
        s = TESTS_DIR / s
    return s

def Posting(account, number,
            currency='USD', cost=None, price=None, flag=None,
            **meta):
    if not meta:
        meta = None
    return bc_data.Posting(
        account,
        bc_amount.Amount(Decimal(number), currency),
        cost,
        price,
        flag,
        meta,
    )

class Transaction:
    def __init__(self,
                 date=FY_MID_DATE, flag='*', payee=None,
                 narration='', tags=None, links=None, postings=None,
                 **meta):
        if isinstance(date, str):
            date = parse_date(date)
        self.date = date
        self.flag = flag
        self.payee = payee
        self.narration = narration
        self.tags = set(tags or '')
        self.links = set(links or '')
        self.postings = []
        self.meta = {
            'filename': '<test>',
            'lineno': 0,
        }
        self.meta.update(meta)
        for posting in postings:
            self.add_posting(*posting)

    def add_posting(self, arg, *args, **kwargs):
        """Add a posting to this transaction. Use any of these forms:

           txn.add_posting(account, number, …, kwarg=value, …)
           txn.add_posting(account, number, …, posting_kwargs_dict)
           txn.add_posting(posting_object)
        """
        if kwargs:
            posting = Posting(arg, *args, **kwargs)
        elif args:
            if isinstance(args[-1], dict):
                kwargs = args[-1]
                args = args[:-1]
            posting = Posting(arg, *args, **kwargs)
        else:
            posting = arg
        self.postings.append(posting)


class TestConfig:
    def __init__(self, repo_path=None):
        self.repo_path = test_path(repo_path)

    def repository_path(self):
        return self.repo_path


class RTClient:
    def __init__(self,
                 url,
                 default_login=None,
                 default_password=None,
                 proxy=None,
                 default_queue='General',
                 skip_login=False,
                 verify_cert=True,
                 http_auth=None,
    ):
        self.url = url
        if http_auth is None:
            self.user = default_login
            self.password = default_password
            self.auth_method = 'login'
            self.login_result = skip_login or None
        else:
            self.user = http_auth.username
            self.password = http_auth.password
            self.auth_method = type(http_auth).__name__
            self.login_result = True
        self.last_login = None

    def login(self, login=None, password=None):
        if login is None and password is None:
            login = self.user
            password = self.password
        self.login_result = bool(login and password and not password.startswith('bad'))
        self.last_login = (login, password, self.login_result)
        return self.login_result