Files @ 5784068904e8
Branch filter:

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

bkuhn
payroll-type — US:403b:Employee:Roth — needed separate since taxable

Since Roth contributions are taxable, there are some reports that
need to include these amounts in total salary (i.e., when running a
report that seeks to show total taxable income for an employee). As
such, we need a `payroll-type` specifically for Roth 403(b)
contributions.
  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
410
411
412
413
414
"""test_reports_query.py - Unit tests for query report"""
# Copyright © 2021  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 argparse
import collections
import copy
import csv
import datetime
import io
import itertools
import re

import odf.table
import odf.text
import pytest

from . import testutil

from beancount.core import data as bc_data
from beancount.query import query_compile as bc_query_compile
from beancount.query import query_execute as bc_query_execute
from beancount.query import query_parser as bc_query_parser
from conservancy_beancount.books import FiscalYear
from conservancy_beancount.reports import query as qmod
from conservancy_beancount import rtutil

from decimal import Decimal

UTC = datetime.timezone.utc

class MockRewriteRuleset:
    def __init__(self, multiplier=2):
        self.multiplier = multiplier

    def rewrite(self, posts):
        for post in posts:
            number, currency = post.units
            number *= self.multiplier
            yield post._replace(units=testutil.Amount(number, currency))


class RowContext(bc_query_execute.RowContext):
    def __init__(self, entry, posting=None):
        super().__init__()
        self.entry = entry
        self.posting = posting


@pytest.fixture(scope='module')
def qparser():
    return bc_query_parser.Parser()

@pytest.fixture(scope='module')
def rt():
    return rtutil.RT(testutil.RTClient())

@pytest.fixture(scope='module')
def ticket_query():
    return qmod.RTTicket.with_client(testutil.RTClient(), 'testfixture')

def const_operands(*args):
    return [bc_query_compile.EvalConstant(v) for v in args]

def pipe_main(arglist, config, stdout_type=io.StringIO):
    stdout = stdout_type()
    stderr = io.StringIO()
    returncode = qmod.main(arglist, stdout, stderr, config)
    return returncode, stdout, stderr

def test_rt_ticket_unconfigured():
    with pytest.raises(RuntimeError):
        qmod.RTTicket(const_operands('id', 'rt-id'))

@pytest.mark.parametrize('field_name', ['foo', 'bar'])
def test_rt_ticket_bad_field(ticket_query, field_name):
    with pytest.raises(ValueError):
        ticket_query(const_operands(field_name, 'rt-id'))

@pytest.mark.parametrize('meta_name', ['foo', 'bar'])
def test_rt_ticket_bad_metadata(ticket_query, meta_name):
    with pytest.raises(ValueError):
        ticket_query(const_operands('id', meta_name))

@pytest.mark.parametrize('field_name,meta_name,expected', [
    ('id', 'rt-id', {1}),
    ('Queue', 'approval', {'general'}),
    ('Requestors', 'invoice', {'mx1@example.org', 'requestor2@example.org'}),
    ('Due', 'tax-reporting', {datetime.datetime(2017, 1, 14, 12, 1, 0, tzinfo=UTC)}),
    ('cf.{payment-to}', 'statement', {'Hon. Mx. 1'}),
])
def test_rt_ticket_from_txn(ticket_query, field_name, meta_name, expected):
    func = ticket_query(const_operands(field_name, meta_name))
    txn = testutil.Transaction(**{meta_name: 'rt:1'}, postings=[
        ('Assets:Cash', 80),
    ])
    context = RowContext(txn, txn.postings[0])
    assert func(context) == expected

@pytest.mark.parametrize('field_name,meta_name,expected', [
    ('id', 'rt-id', {2}),
    ('Queue', 'approval', {'general'}),
    ('Requestors', 'invoice', {'mx2@example.org', 'requestor2@example.org'}),
    ('Due', 'tax-reporting', {datetime.datetime(2017, 1, 14, 12, 2, 0, tzinfo=UTC)}),
    ('CF_payment-to', 'statement', {'Hon. Mx. 2'}),
])
def test_rt_ticket_from_post(ticket_query, field_name, meta_name, expected):
    func = ticket_query(const_operands(field_name, meta_name))
    txn = testutil.Transaction(**{meta_name: 'rt:1'}, postings=[
        ('Assets:Cash', 110, {meta_name: 'rt:2/8'}),
    ])
    context = RowContext(txn, txn.postings[0])
    assert func(context) == expected

@pytest.mark.parametrize('field_name,meta_name,expected,on_txn', [
    ('id', 'approval', {1, 2}, True),
    ('Queue', 'check', {'general'}, False),
    ('Requestors', 'invoice', {
        'mx1@example.org',
        'mx2@example.org',
        'requestor2@example.org',
    }, False),
    ('cf_payment-to', 'statement', {'Hon. Mx. 1', 'Hon. Mx. 2'}, True),
])
def test_rt_ticket_multi_results(ticket_query, field_name, meta_name, expected, on_txn):
    func = ticket_query(const_operands(field_name, meta_name))
    txn = testutil.Transaction(**{'rt-id': 'rt:1'}, postings=[
        ('Assets:Cash', 110, {'rt-id': 'rt:2'}),
    ])
    post = txn.postings[0]
    meta = txn.meta if on_txn else post.meta
    meta[meta_name] = 'rt:1/2 Docs/12.pdf rt:2/8'
    context = RowContext(txn, post)
    assert func(context) == expected

@pytest.mark.parametrize('meta_value,on_txn', testutil.combine_values(
    ['', 'Docs/34.pdf', 'Docs/100.pdf Docs/120.pdf'],
    [True, False],
))
def test_rt_ticket_no_results(ticket_query, meta_value, on_txn):
    func = ticket_query(const_operands('Queue', 'check'))
    txn = testutil.Transaction(**{'rt-id': 'rt:1'}, postings=[
        ('Assets:Cash', 110, {'rt-id': 'rt:2'}),
    ])
    post = txn.postings[0]
    meta = txn.meta if on_txn else post.meta
    meta['check'] = meta_value
    context = RowContext(txn, post)
    assert func(context) == set()

def test_rt_ticket_caches_tickets():
    rt_client = testutil.RTClient()
    rt_client.TICKET_DATA = testutil.RTClient.TICKET_DATA.copy()
    ticket_query = qmod.RTTicket.with_client(rt_client, 'cachetestA')
    func = ticket_query(const_operands('id', 'rt-id'))
    txn = testutil.Transaction(postings=[
        ('Assets:Cash', 160, {'rt-id': 'rt:3'}),
    ])
    context = RowContext(txn, txn.postings[0])
    assert func(context) == {3}
    del rt_client.TICKET_DATA['3']
    assert func(context) == {3}

def test_rt_ticket_caches_tickets_not_found():
    rt_client = testutil.RTClient()
    rt_client.TICKET_DATA = testutil.RTClient.TICKET_DATA.copy()
    rt3 = rt_client.TICKET_DATA.pop('3')
    ticket_query = qmod.RTTicket.with_client(rt_client, 'cachetestB')
    func = ticket_query(const_operands('id', 'rt-id'))
    txn = testutil.Transaction(postings=[
        ('Assets:Cash', 160, {'rt-id': 'rt:3'}),
    ])
    context = RowContext(txn, txn.postings[0])
    assert func(context) == set()
    rt_client.TICKET_DATA['3'] = rt3
    assert func(context) == set()

def test_books_loader_empty():
    result = qmod.BooksLoader(None)()
    assert not result.entries
    assert len(result.errors) == 1

def test_books_loader_plain():
    books_path = testutil.test_path(f'books/books/2018.beancount')
    loader = testutil.TestBooksLoader(books_path)
    result = qmod.BooksLoader(loader)()
    assert not result.errors
    assert result.entries
    min_date = datetime.date(2018, 3, 1)
    assert all(ent.date >= min_date for ent in result.entries)

def test_books_loader_rewrites():
    rewrites = [MockRewriteRuleset()]
    books_path = testutil.test_path(f'books/books/2018.beancount')
    loader = testutil.TestBooksLoader(books_path)
    result = qmod.BooksLoader(loader, None, None, rewrites)()
    assert not result.errors
    assert result.entries
    numbers = frozenset(
        abs(post.units.number)
        for entry in result.entries
        for post in getattr(entry, 'postings', ())
    )
    assert numbers
    assert all(abs(number) >= 40 for number in numbers)

@pytest.mark.parametrize('arglist,fy', testutil.combine_values(
    [['--report-type', 'text'], ['--format=text'], ['-f', 'txt']],
    range(2018, 2021),
))
def test_text_query(arglist, fy):
    books_path = testutil.test_path(f'books/books/{fy}.beancount')
    config = testutil.TestConfig(books_path=books_path)
    arglist += ['select', 'date,', 'narration,', 'account,', 'position']
    returncode, stdout, stderr = pipe_main(arglist, config)
    assert returncode == 0
    stdout.seek(0)
    lines = iter(stdout)
    next(lines); next(lines)  # Skip header
    for count, line in enumerate(lines, 1):
        assert re.match(rf'^{fy}-\d\d-\d\d\s+{fy} ', line)
    assert count >= 2

@pytest.mark.parametrize('arglist,fy', testutil.combine_values(
    [['--format=csv'], ['-f', 'csv'], ['-t', 'csv']],
    range(2018, 2021),
))
def test_csv_query(arglist, fy):
    books_path = testutil.test_path(f'books/books/{fy}.beancount')
    config = testutil.TestConfig(books_path=books_path)
    arglist += ['select', 'date,', 'narration,', 'account,', 'position']
    returncode, stdout, stderr = pipe_main(arglist, config)
    assert returncode == 0
    stdout.seek(0)
    for count, row in enumerate(csv.DictReader(stdout), 1):
        assert re.fullmatch(rf'{fy}-\d\d-\d\d', row['date'])
        assert row['narration'].startswith(f'{fy} ')
    assert count >= 2

@pytest.mark.parametrize('end_index', range(3))
def test_rewrite_query(end_index):
    books_path = testutil.test_path(f'books/books/2018.beancount')
    config = testutil.TestConfig(books_path=books_path)
    accounts = ['Assets', 'Income']
    expected = frozenset(accounts[:end_index])
    rewrite_paths = [
        testutil.test_path(f'userconfig/Rewrite{s}.yml')
        for s in expected
    ]
    arglist = [f'--rewrite-rules={path}' for path in rewrite_paths]
    arglist.append('--format=txt')
    arglist.append('select any_meta("root") as root')
    returncode, stdout, stderr = pipe_main(arglist, config)
    assert returncode == 0
    stdout.seek(0)
    actual = frozenset(line.rstrip('\n') for line in stdout)
    assert expected.issubset(actual)
    assert frozenset(accounts).difference(expected).isdisjoint(actual)

def test_ods_amount_formatting(qparser):
    statement = qparser.parse('SELECT UNITS(position)')
    row_types = [('amount', bc_data.Amount)]
    row_source = [(testutil.Amount(12),), (testutil.Amount(1480, 'JPY'),)]
    ods = qmod.QueryODS()
    ods.write_query(statement, row_types, row_source)
    actual = testutil.ODSCell.from_sheet(ods.document.spreadsheet.firstChild)
    assert next(actual)[0].text == 'Amount'
    assert next(actual)[0].text == '$12.00'
    assert next(actual)[0].text == '¥1,480'
    assert next(actual, None) is None

def test_ods_datetime_formatting(qparser):
    statement = qparser.parse('SELECT date')
    row_types = [('date', datetime.date)]
    row_source = [(testutil.PAST_DATE,), (testutil.FUTURE_DATE,)]
    ods = qmod.QueryODS()
    ods.write_query(statement, row_types, row_source)
    actual = testutil.ODSCell.from_sheet(ods.document.spreadsheet.firstChild)
    assert next(actual)[0].text == 'Date'
    assert next(actual)[0].text == testutil.PAST_DATE.isoformat()
    assert next(actual)[0].text == testutil.FUTURE_DATE.isoformat()
    assert next(actual, None) is None

@pytest.mark.parametrize('meta_key,meta_func', [
    ('check', 'ANY_META'),
    ('purchase-order', 'META'),
    ('rt-id', 'META_DOCS'),
])
def test_ods_link_formatting(qparser, rt, meta_key, meta_func):
    meta_func_returns_list = meta_func == 'META_DOCS'
    statement = qparser.parse(f'SELECT {meta_func}({meta_key!r}) AS docs')
    row_types = [('docs', list if meta_func_returns_list else str)]
    row_source = [
        (s.split() if meta_func_returns_list else s,)
        for s in ['rt:1/5', 'rt:3 Checks/9.pdf']
    ]
    ods = qmod.QueryODS(rt)
    ods.write_query(statement, row_types, row_source)
    rows = iter(ods.document.spreadsheet.firstChild.getElementsByType(odf.table.TableRow))
    assert next(rows).text == 'Docs'
    actual = iter(
        [link.text for link in row.getElementsByType(odf.text.A)]
        for row in rows
    )
    assert next(actual) == ['photo.jpg']
    assert next(actual) == ['rt:3', '9.pdf']
    assert next(actual, None) is None

def test_ods_meta_formatting(qparser):
    statement = qparser.parse('SELECT ANY_META("entity") AS entity')
    row_types = [('entity', object)]
    row_source = [(testutil.Amount(14),), (None,), ('foo bar',)]
    ods = qmod.QueryODS()
    ods.write_query(statement, row_types, row_source)
    actual = testutil.ODSCell.from_sheet(ods.document.spreadsheet.firstChild)
    assert next(actual)[0].text == 'Entity'
    assert next(actual)[0].text == '$14.00'
    assert next(actual)[0].text == ''
    assert next(actual)[0].text == 'foo bar'
    assert next(actual, None) is None

def test_ods_multicolumn_write(qparser, rt):
    statement = qparser.parse(
        'SELECT MIN(date) AS date, SET(META_DOCS("rt-id")) AS tix, STR_META("entity") AS entity',
    )
    row_types = [('date', datetime.date), ('tix', set), ('entity', str)]
    row_source = [
        (testutil.PAST_DATE, {'rt:1'}, 'AA'),
        (testutil.FY_START_DATE, {'rt:2'}, 'BB'),
        (testutil.FUTURE_DATE, {'rt:3', 'rt:4'}, 'CC'),
    ]
    ods = qmod.QueryODS(rt)
    ods.write_query(statement, list(row_types), list(row_source))
    actual = iter(
        cell.text
        for row in testutil.ODSCell.from_sheet(ods.document.spreadsheet.firstChild)
        for cell in row
    )
    for expected, _ in row_types:
        assert next(actual) == expected.title()
    assert next(actual) == testutil.PAST_DATE.isoformat()
    assert next(actual) == 'rt:1'
    assert next(actual) == 'AA'
    assert next(actual) == testutil.FY_START_DATE.isoformat()
    assert next(actual) == 'rt:2'
    assert next(actual) == 'BB'
    assert next(actual) == testutil.FUTURE_DATE.isoformat()
    assert frozenset(next(actual).split('\0')) == row_source[-1][1]
    assert next(actual) == 'CC'
    assert next(actual, None) is None

def test_ods_is_empty(qparser):
    statement = qparser.parse('SELECT * WHERE date < 1900-01-01')
    ods = qmod.QueryODS()
    assert ods.is_empty()
    ods.write_query(statement, [], [])
    assert not ods.is_empty()

@pytest.mark.parametrize('fy,account,amt_prefix', [
    (2018, 'Assets', '($'),
    (2019, 'Income', '$'),
])
def test_ods_output(fy, account, amt_prefix):
    books_path = testutil.test_path(f'books/books/{fy}.beancount')
    config = testutil.TestConfig(books_path=books_path)
    arglist = [
        '-O', '-',
        '-f', 'ods',
        f'SELECT date, narration, UNITS(position) WHERE account ~ "^{account}:"',
    ]
    returncode, stdout, stderr = pipe_main(arglist, config, io.BytesIO)
    assert returncode == 0
    with stdout:
        stdout.seek(0)
        ods_doc = odf.opendocument.load(stdout)
    rows = iter(ods_doc.spreadsheet.firstChild.getElementsByType(odf.table.TableRow))
    next(rows)  # Skip header row
    amt_pattern = rf'^{re.escape(amt_prefix)}\d'
    for count, row in enumerate(rows, 1):
        date, narration, amount = row.childNodes
        assert re.fullmatch(rf'{fy}-\d{{2}}-\d{{2}}', date.text)
        assert narration.text.startswith(f'{fy} ')
        assert re.match(amt_pattern, amount.text)
    assert count

def test_ods_aggregate_output():
    books_path = testutil.test_path(f'books/books/2020.beancount')
    config = testutil.TestConfig(books_path=books_path)
    arglist = [
        '-O', '-',
        '-f', 'ods',
        'SELECT account, SET(narration), SUM(UNITS(position))',
        'WHERE date >= 2020-04-01 AND date <= 2020-04-02',
        'GROUP BY account ORDER BY account ASC',
    ]
    returncode, stdout, stderr = pipe_main(arglist, config, io.BytesIO)
    assert returncode == 0
    with stdout:
        stdout.seek(0)
        ods_doc = odf.opendocument.load(stdout)
    rows = iter(ods_doc.spreadsheet.firstChild.getElementsByType(odf.table.TableRow))
    next(rows)  # Skip header row
    actual = {}
    for row in rows:
        acct, descs, balance = row.childNodes
        actual[acct.text] = (frozenset(descs.text.split('\0')), balance.text)
    in_desc = {'2020 donation'}
    ex_desc = {'2020 bank maintenance fee'}
    assert actual['Income:Donations'] == (in_desc, '$20.20')
    assert actual['Expenses:BankingFees'] == (ex_desc, '$1.00')
    assert actual['Assets:Checking'] == (in_desc | ex_desc, '($21.20)')