Files @ 6deaacb11bdd
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_reports_related_postings.py

bkuhn
Add US:TN:Unemployment as a valid `payroll-type` metadata for taxes
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""test_reports_related_postings - Unit tests for RelatedPostings"""
# Copyright © 2020  Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.

import collections
import datetime
import itertools

from decimal import Decimal

import pytest

from . import testutil

from conservancy_beancount import data
from conservancy_beancount.reports import core

def accruals_and_payments(acct, src_acct, dst_acct, start_date, *amounts):
    dates = testutil.date_seq(start_date)
    for amt, currency in amounts:
        post_meta = {'metanumber': amt, 'metacurrency': currency}
        yield testutil.Transaction(date=next(dates), postings=[
            (acct, amt, currency, post_meta),
            (dst_acct if amt < 0 else src_acct, -amt, currency, post_meta),
        ])

@pytest.fixture
def credit_card_cycle():
    return list(accruals_and_payments(
        'Liabilities:CreditCard',
        'Assets:Checking',
        'Expenses:Other',
        datetime.date(2020, 4, 1),
        (-110, 'USD'),
        (110, 'USD'),
        (-120, 'USD'),
        (120, 'USD'),
    ))

@pytest.fixture
def two_accruals_three_payments():
    return list(accruals_and_payments(
        'Assets:Receivable:Accounts',
        'Income:Donations',
        'Assets:Checking',
        datetime.date(2020, 4, 10),
        (440, 'USD'),
        (-230, 'USD'),
        (550, 'EUR'),
        (-210, 'USD'),
        (-550, 'EUR'),
    ))

@pytest.fixture
def link_swap_posts():
    retval = []
    meta = {
        'rt-id': 'rt:12 rt:16',
        '_post_type': data.Posting,
        '_meta_type': data.Metadata,
    }
    for n in range(1, 3):
        n = Decimal(n)
        retval.append(testutil.Posting(
            'Assets:Receivable:Accounts', n * 10, metanum=n, **meta,
        ))
    meta['rt-id'] = 'rt:16 rt:12'
    for n in range(1, 3):
        n = Decimal(n)
        retval.append(testutil.Posting(
            'Liabilities:Payable:Accounts', n * -10, metanum=n, **meta,
        ))
    return retval

def test_initialize_with_list(credit_card_cycle):
    related = core.RelatedPostings(credit_card_cycle[0].postings)
    assert len(related) == 2

def test_initialize_with_iterable(two_accruals_three_payments):
    related = core.RelatedPostings(
        post for txn in two_accruals_three_payments
        for post in txn.postings
        if post.account == 'Assets:Receivable:Accounts'
    )
    assert len(related) == 5

def test_balance_empty():
    balance = core.RelatedPostings().balance()
    assert not balance
    assert balance.is_zero()

@pytest.mark.parametrize('index,expected', enumerate([
    -110,
    0,
    -120,
    0,
]))
def test_balance_credit_card(credit_card_cycle, index, expected):
    related = core.RelatedPostings(
        txn.postings[0] for txn in credit_card_cycle[:index + 1]
    )
    assert related.balance() == {'USD': testutil.Amount(expected, 'USD')}

def check_iter_with_balance(entries):
    expect_posts = [txn.postings[0] for txn in entries]
    expect_balances = []
    balance_tally = collections.defaultdict(Decimal)
    for post in expect_posts:
        number, currency = post.units
        balance_tally[currency] += number
        expect_balances.append({code: testutil.Amount(number, code)
                                for code, number in balance_tally.items()})
    related = core.RelatedPostings(expect_posts)
    for (post, balance), exp_post, exp_balance in zip(
            related.iter_with_balance(),
            expect_posts,
            expect_balances,
    ):
        assert post is exp_post
        assert balance == exp_balance
    assert post is expect_posts[-1]
    assert related.balance() == expect_balances[-1]

def test_iter_with_balance_empty():
    assert not list(core.RelatedPostings().iter_with_balance())

def test_iter_with_balance_credit_card(credit_card_cycle):
    check_iter_with_balance(credit_card_cycle)

def test_iter_with_balance_two_acccruals(two_accruals_three_payments):
    check_iter_with_balance(two_accruals_three_payments)

def test_balance_at_cost_mixed():
    txn = testutil.Transaction(postings=[
        ('Expenses:Other', '22'),
        ('Expenses:Other', '30', 'EUR', ('1.1',)),
        ('Expenses:Other', '40', 'EUR'),
        ('Expenses:Other', '50', 'USD', ('1.1', 'EUR')),
    ])
    related = core.RelatedPostings(data.Posting.from_txn(txn))
    balance = related.balance_at_cost()
    amounts = set(balance.values())
    assert amounts == {testutil.Amount(55, 'USD'), testutil.Amount(95, 'EUR')}

def test_balance_at_single_currency_cost():
    txn = testutil.Transaction(postings=[
        ('Expenses:Other', '22'),
        ('Expenses:Other', '30', 'EUR', ('1.1',)),
        ('Expenses:Other', '40', 'GBP', ('1.1',)),
    ])
    related = core.RelatedPostings(data.Posting.from_txn(txn))
    balance = related.balance_at_cost()
    amounts = set(balance.values())
    assert amounts == {testutil.Amount(99)}

def test_balance_at_cost_zeroed_out():
    txn = testutil.Transaction(postings=[
        ('Income:Other', '-22'),
        ('Assets:Receivable:Accounts', '20', 'EUR', ('1.1',)),
    ])
    related = core.RelatedPostings(data.Posting.from_txn(txn))
    balance = related.balance_at_cost()
    assert balance.is_zero()

def test_balance_at_cost_singleton():
    txn = testutil.Transaction(postings=[
        ('Assets:Receivable:Accounts', '20', 'EUR', ('1.1',)),
    ])
    related = core.RelatedPostings(data.Posting.from_txn(txn))
    balance = related.balance_at_cost()
    amounts = set(balance.values())
    assert amounts == {testutil.Amount(22)}

def test_balance_at_cost_singleton_without_cost():
    txn = testutil.Transaction(postings=[
        ('Assets:Receivable:Accounts', '20'),
    ])
    related = core.RelatedPostings(data.Posting.from_txn(txn))
    balance = related.balance_at_cost()
    amounts = set(balance.values())
    assert amounts == {testutil.Amount(20)}

def test_balance_at_cost_empty():
    related = core.RelatedPostings()
    balance = related.balance_at_cost()
    assert balance.is_zero()

@pytest.mark.parametrize('date,expected', [
    (testutil.FY_MID_DATE - datetime.timedelta(days=1), 0),
    (testutil.FY_MID_DATE, 0),
    (testutil.FY_MID_DATE + datetime.timedelta(days=1), 25),
    (testutil.FY_MID_DATE + datetime.timedelta(days=2), 70),
    (testutil.FY_MID_DATE + datetime.timedelta(days=3), 135),
    (testutil.FY_MID_DATE + datetime.timedelta(days=4), 135),
])
def test_balance_at_cost_by_date(date, expected):
    dates = testutil.date_seq()
    jpy_cost = ('0.01', 'USD')
    entries = [
        testutil.Transaction(date=next(dates), postings=[
            ('Assets:Cash', 1000, 'JPY', jpy_cost),
            ('Assets:Cash', 15),
        ]),
        testutil.Transaction(date=next(dates), postings=[
            ('Assets:Cash', 2000, 'JPY', jpy_cost),
            ('Assets:Cash', 25),
        ]),
        testutil.Transaction(date=next(dates), postings=[
            ('Assets:Cash', 3000, 'JPY', jpy_cost),
            ('Assets:Cash', 35),
        ]),
    ]
    related = core.RelatedPostings(data.Posting.from_entries(entries))
    actual = related.balance_at_cost_by_date(date)
    if not expected:
        assert actual.is_zero()
    else:
        assert actual == {'USD': testutil.Amount(expected)}

def test_meta_values_empty():
    related = core.RelatedPostings()
    assert related.meta_values('key') == set()

def test_meta_values_no_match():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, metakey='metavalue'),
    ])
    assert related.meta_values('key') == {None}

def test_meta_values_no_match_default_given():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, metakey='metavalue'),
    ])
    assert related.meta_values('key', '') == {''}

def test_meta_values_one_match():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, key='metavalue'),
    ])
    assert related.meta_values('key') == {'metavalue'}

def test_meta_values_some_match():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, key='1'),
        testutil.Posting('Income:Donations', -2, metakey='2'),
    ])
    assert related.meta_values('key') == {'1', None}

def test_meta_values_some_match_default_given():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, key='1'),
        testutil.Posting('Income:Donations', -2, metakey='2'),
    ])
    assert related.meta_values('key', '') == {'1', ''}

def test_meta_values_all_match():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, key='1'),
        testutil.Posting('Income:Donations', -2, key='2'),
    ])
    assert related.meta_values('key') == {'1', '2'}

def test_meta_values_all_match_one_value():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, key='1'),
        testutil.Posting('Income:Donations', -2, key='1'),
    ])
    assert related.meta_values('key') == {'1'}

def test_meta_values_all_match_default_given():
    related = core.RelatedPostings([
        testutil.Posting('Income:Donations', -1, key='1'),
        testutil.Posting('Income:Donations', -2, key='2'),
    ])
    assert related.meta_values('key', '') == {'1', '2'}

def test_meta_values_many_types():
    expected = {
        datetime.date(2020, 4, 1),
        Decimal(42),
        testutil.Amount(5),
        'rt:42',
    }
    related = core.RelatedPostings(
        testutil.Posting('Income:Donations', -index, key=value)
        for index, value in enumerate(expected)
    )
    assert related.meta_values('key') == expected

@pytest.mark.parametrize('count', range(3))
def test_all_meta_links_zero(count):
    related = core.RelatedPostings(testutil.Posting(
        'Income:Donations', -n, testkey=str(n), _meta_type=data.Metadata,
    ) for n in range(count))
    assert next(related.all_meta_links('approval'), None) is None

def test_all_meta_links_singletons():
    related = core.RelatedPostings(testutil.Posting(
        'Income:Donations', -10, statement=value, _meta_type=data.Metadata,
    ) for value in itertools.chain(
        testutil.NON_LINK_METADATA_STRINGS,
        testutil.LINK_METADATA_STRINGS,
        testutil.NON_STRING_METADATA_VALUES,
    ))
    assert set(related.all_meta_links('statement')) == testutil.LINK_METADATA_STRINGS

def test_all_meta_links_multiples():
    related = core.RelatedPostings(testutil.Posting(
        'Income:Donations', -10, approval=' '.join(value), _meta_type=data.Metadata,
    ) for value in itertools.permutations(testutil.LINK_METADATA_STRINGS, 2))
    assert set(related.all_meta_links('approval')) == testutil.LINK_METADATA_STRINGS

def test_all_meta_links_preserves_order():
    related = core.RelatedPostings(testutil.Posting(
        'Income:Donations', -10, approval=c, _meta_type=data.Metadata,
    ) for c in '121323')
    assert list(related.all_meta_links('approval')) == list('123')

def test_first_meta_links():
    related = core.RelatedPostings(testutil.Posting(
        'Assets:Cash', 10, contract=value, _meta_type=data.Metadata,
    ) for value in ['1 2', '', '1 3', testutil.PAST_DATE, '2 3', None])
    del related[-1].meta['contract']
    assert list(related.first_meta_links('contract')) == list('12')

def test_first_meta_links_fallback():
    related = core.RelatedPostings(testutil.Posting(
        'Assets:Cash', 10, contract=value, _meta_type=data.Metadata,
    ) for value in ['1 2', testutil.PAST_DATE, '1 3', None, '2 3'])
    del related[-2].meta['contract']
    assert list(related.first_meta_links('contract', None)) == ['1', None, '2']

def test_group_by_meta_zero():
    assert not list(core.RelatedPostings.group_by_meta([], 'metacurrency'))

def test_group_by_meta_one(credit_card_cycle):
    posting = next(post for post in data.Posting.from_entries(credit_card_cycle)
                   if post.account.is_credit_card())
    actual = core.RelatedPostings.group_by_meta([posting], 'metacurrency')
    assert set(key for key, _ in actual) == {'USD'}

def test_group_by_meta_many(two_accruals_three_payments):
    postings = [post for post in data.Posting.from_entries(two_accruals_three_payments)
                if post.account == 'Assets:Receivable:Accounts']
    actual = dict(core.RelatedPostings.group_by_meta(postings, 'metacurrency'))
    assert set(actual) == {'USD', 'EUR'}
    for key, group in actual.items():
        assert 2 <= len(group) <= 3
        assert group.balance().is_zero()

def test_group_by_meta_many_single_posts(two_accruals_three_payments):
    postings = [post for post in data.Posting.from_entries(two_accruals_three_payments)
                if post.account == 'Assets:Receivable:Accounts']
    actual = dict(core.RelatedPostings.group_by_meta(postings, 'metanumber'))
    assert set(actual) == {post.units.number for post in postings}
    assert len(actual) == len(postings)

def test_group_by_first_meta_link_zero():
    assert not list(core.RelatedPostings.group_by_first_meta_link([], 'foo'))

def test_group_by_first_meta_link_no_key(link_swap_posts):
    actual = dict(core.RelatedPostings.group_by_first_meta_link(
        iter(link_swap_posts), 'Nonexistent',
    ))
    assert len(actual) == 1
    assert list(actual[None]) == link_swap_posts

def test_group_by_first_meta_link_bad_type(link_swap_posts):
    assert all(post.meta.get('metanum') for post in link_swap_posts), \
        "did not find metadata required by test"
    actual = dict(core.RelatedPostings.group_by_first_meta_link(
        iter(link_swap_posts), 'metanum',
    ))
    assert len(actual) == 1
    assert list(actual[None]) == link_swap_posts

def test_group_by_first_meta_link(link_swap_posts):
    actual_all = dict(core.RelatedPostings.group_by_first_meta_link(
        iter(link_swap_posts), 'rt-id',
    ))
    assert len(actual_all) == 2
    for key, expect_account in [
            ('rt:12', 'Assets:Receivable:Accounts'),
            ('rt:16', 'Liabilities:Payable:Accounts'),
    ]:
        actual = actual_all.get(key, '')
        assert len(actual) == 2
        assert all(post.account == expect_account for post in actual)

def test_group_by_account():
    entries = [
        testutil.Transaction(postings=[
            ('Income:Donations', -10),
            ('Assets:Cash', 10),
        ]),
        testutil.Transaction(postings=[
            ('Income:Donations', -20),
            ('Assets:Cash', 20),
        ]),
    ]
    postings = data.Posting.from_entries(entries)
    actual = dict(core.RelatedPostings.group_by_account(postings))
    assert len(actual) == 2
    for key, related in actual.items():
        assert len(related) == 2
        assert all(post.account == key for post in related)