Changeset - ef03893bfed3
[Not reviewed]
0 2 0
Brett Smith - 3 years ago 2021-03-12 15:56:43
brettcsmith@brettcsmith.org
query: Convert query functions that return List to Set.

Beancount's built-in renderers expect this and are better equipped for it.
2 files changed with 27 insertions and 34 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/query.py
Show inline comments
...
 
@@ -159,3 +159,3 @@ class MetaDocs(bc_query_env.AnyMeta):
 
    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
 
        super(bc_query_env.AnyMeta, self).__init__(operands, list)
 
        super(bc_query_env.AnyMeta, self).__init__(operands, set)
 
        # The second argument is our return type.
...
 
@@ -163,8 +163,6 @@ class MetaDocs(bc_query_env.AnyMeta):
 

	
 
    def __call__(self, context: PostingContext) -> List[str]:
 
    def __call__(self, context: PostingContext) -> Set[str]:
 
        raw_value = super().__call__(context)
 
        if isinstance(raw_value, str):
 
            return raw_value.split()
 
        else:
 
            return []
 
        seq = raw_value.split() if isinstance(raw_value, str) else ''
 
        return set(seq)
 

	
...
 
@@ -249,3 +247,3 @@ class RTTicket(bc_query_compile.EvalFunction):
 
            operands.append(bc_query_compile.EvalConstant(sys.maxsize))
 
        super().__init__(operands, list)
 
        super().__init__(operands, set)
 

	
...
 
@@ -263,3 +261,3 @@ class RTTicket(bc_query_compile.EvalFunction):
 

	
 
    def __call__(self, context: PostingContext) -> list:
 
    def __call__(self, context: PostingContext) -> Set[object]:
 
        rt_key: str
...
 
@@ -285,3 +283,3 @@ class RTTicket(bc_query_compile.EvalFunction):
 
                    break
 
        retval: List[object] = []
 
        retval: Set[object] = set()
 
        for ticket_id in ticket_ids:
...
 
@@ -296,5 +294,5 @@ class RTTicket(bc_query_compile.EvalFunction):
 
            elif isinstance(field_value, list):
 
                retval.extend(field_value)
 
                retval.update(field_value)
 
            else:
 
                retval.append(field_value)
 
                retval.add(field_value)
 
        return retval
tests/test_reports_query.py
Show inline comments
...
 
@@ -88,6 +88,6 @@ def test_rt_ticket_bad_metadata(ticket_query, 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)),
 
    ('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)}),
 
])
...
 
@@ -99,4 +99,2 @@ def test_rt_ticket_from_txn(ticket_query, field_name, meta_name, expected):
 
    context = RowContext(txn, txn.postings[0])
 
    if not isinstance(expected, list):
 
        expected = [expected]
 
    assert func(context) == expected
...
 
@@ -104,6 +102,6 @@ def test_rt_ticket_from_txn(ticket_query, field_name, meta_name, 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)),
 
    ('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)}),
 
])
...
 
@@ -115,4 +113,2 @@ def test_rt_ticket_from_post(ticket_query, field_name, meta_name, expected):
 
    context = RowContext(txn, txn.postings[0])
 
    if not isinstance(expected, list):
 
        expected = [expected]
 
    assert func(context) == expected
...
 
@@ -120,5 +116,5 @@ def test_rt_ticket_from_post(ticket_query, field_name, meta_name, expected):
 
@pytest.mark.parametrize('field_name,meta_name,expected,on_txn', [
 
    ('id', 'approval', [1, 2], True),
 
    ('Queue', 'check', ['general', 'general'], False),
 
    ('Requestors', 'invoice', [
 
    ('id', 'approval', {1, 2}, True),
 
    ('Queue', 'check', {'general'}, False),
 
    ('Requestors', 'invoice', {
 
        'mx1@example.org',
...
 
@@ -126,4 +122,3 @@ def test_rt_ticket_from_post(ticket_query, field_name, meta_name, expected):
 
        'requestor2@example.org',
 
        'requestor2@example.org',
 
    ], False),
 
    }, False),
 
])
...
 
@@ -138,3 +133,3 @@ def test_rt_ticket_multi_results(ticket_query, field_name, meta_name, expected,
 
    context = RowContext(txn, post)
 
    assert sorted(func(context)) == expected
 
    assert func(context) == expected
 

	
...
 
@@ -153,3 +148,3 @@ def test_rt_ticket_no_results(ticket_query, meta_value, on_txn):
 
    context = RowContext(txn, post)
 
    assert func(context) == []
 
    assert func(context) == set()
 

	
...
 
@@ -164,5 +159,5 @@ def test_rt_ticket_caches_tickets():
 
    context = RowContext(txn, txn.postings[0])
 
    assert func(context) == [3]
 
    assert func(context) == {3}
 
    del rt_client.TICKET_DATA['3']
 
    assert func(context) == [3]
 
    assert func(context) == {3}
 

	
...
 
@@ -178,5 +173,5 @@ def test_rt_ticket_caches_tickets_not_found():
 
    context = RowContext(txn, txn.postings[0])
 
    assert func(context) == []
 
    assert func(context) == set()
 
    rt_client.TICKET_DATA['3'] = rt3
 
    assert func(context) == []
 
    assert func(context) == set()
 

	
0 comments (0 inline, 0 general)