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
...
 
@@ -154,22 +154,20 @@ class PostingContext:
 
    store: Store
 

	
 

	
 
class MetaDocs(bc_query_env.AnyMeta):
 
    """Return a list of document links from metadata."""
 
    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.
 
        # It should match the annotated return type of __call__.
 

	
 
    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)
 

	
 

	
 
class RTField(NamedTuple):
 
    key: str
 
    parse: Optional[Callable[[str], object]]
 
    unset_value: Optional[str] = None
...
 
@@ -244,13 +242,13 @@ class RTTicket(bc_query_compile.EvalFunction):
 
        if isinstance(rt_op, bc_query_compile.EvalConstant):
 
            self._rt_key(rt_op.value)
 
        if isinstance(meta_op, bc_query_compile.EvalConstant):
 
            self._meta_key(meta_op.value)
 
        if not rest:
 
            operands.append(bc_query_compile.EvalConstant(sys.maxsize))
 
        super().__init__(operands, list)
 
        super().__init__(operands, set)
 

	
 
    def _rt_key(self, key: str) -> RTField:
 
        try:
 
            return self.FIELDS[key]
 
        except KeyError:
 
            raise ValueError(f"unknown RT ticket field {key!r}") from None
...
 
@@ -258,13 +256,13 @@ class RTTicket(bc_query_compile.EvalFunction):
 
    def _meta_key(self, key: str) -> str:
 
        if key in data.LINK_METADATA:
 
            return key
 
        else:
 
            raise ValueError(f"metadata key {key!r} does not contain documentation links")
 

	
 
    def __call__(self, context: PostingContext) -> list:
 
    def __call__(self, context: PostingContext) -> Set[object]:
 
        rt_key: str
 
        meta_key: str
 
        limit: int
 
        rt_key, meta_key, limit = self.eval_args(context)
 
        rt_field = self._rt_key(rt_key)
 
        meta_key = self._meta_key(meta_key)
...
 
@@ -280,26 +278,26 @@ class RTTicket(bc_query_compile.EvalFunction):
 
        for link_s in meta_value.split():
 
            rt_id = rtutil.RT.parse(link_s)
 
            if rt_id is not None:
 
                ticket_ids.add(rt_id[0])
 
                if len(ticket_ids) >= limit:
 
                    break
 
        retval: List[object] = []
 
        retval: Set[object] = set()
 
        for ticket_id in ticket_ids:
 
            try:
 
                rt_ticket = self._rt_cache[ticket_id]
 
            except KeyError:
 
                rt_ticket = self.RT_CLIENT.get_ticket(ticket_id)
 
                self._rt_cache[ticket_id] = rt_ticket
 
            field_value = rt_field.load(rt_ticket)
 
            if field_value is None:
 
                pass
 
            elif isinstance(field_value, list):
 
                retval.extend(field_value)
 
                retval.update(field_value)
 
            else:
 
                retval.append(field_value)
 
                retval.add(field_value)
 
        return retval
 

	
 

	
 
class StrMeta(bc_query_env.AnyMeta):
 
    """Looks up metadata like AnyMeta, then always returns a string."""
 
    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
tests/test_reports_query.py
Show inline comments
...
 
@@ -83,63 +83,58 @@ def test_rt_ticket_bad_field(ticket_query, field_name):
 
@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)),
 
    ('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)}),
 
])
 
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])
 
    if not isinstance(expected, list):
 
        expected = [expected]
 
    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)),
 
    ('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)}),
 
])
 
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])
 
    if not isinstance(expected, list):
 
        expected = [expected]
 
    assert func(context) == 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',
 
        'mx2@example.org',
 
        'requestor2@example.org',
 
        'requestor2@example.org',
 
    ], False),
 
    }, False),
 
])
 
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 sorted(func(context)) == expected
 
    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):
...
 
@@ -148,40 +143,40 @@ def test_rt_ticket_no_results(ticket_query, meta_value, on_txn):
 
        ('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) == []
 
    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]
 
    assert func(context) == {3}
 
    del rt_client.TICKET_DATA['3']
 
    assert func(context) == [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) == []
 
    assert func(context) == set()
 
    rt_client.TICKET_DATA['3'] = rt3
 
    assert func(context) == []
 
    assert func(context) == set()
 

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

	
0 comments (0 inline, 0 general)