From 4ca188611fcb83bcb78438d0c8c05c78b2f3387b 2020-06-12 19:54:38 From: Brett Smith Date: 2020-06-12 19:54:38 Subject: [PATCH] rtutil: Add RT.unparse() classmethod. --- diff --git a/conservancy_beancount/rtutil.py b/conservancy_beancount/rtutil.py index abf8f237d882267002ada903511be24b0955a26e..df8e8fa0659b237a871dc5a59da3923d8e358520 100644 --- a/conservancy_beancount/rtutil.py +++ b/conservancy_beancount/rtutil.py @@ -427,6 +427,14 @@ class RT: )) for post in txn.postings], ) + @classmethod + def unparse(cls, ticket_id: RTId, attachment_id: Optional[RTId]=None) -> str: + """Return a metadata link string for the given ticket+attachment id""" + if attachment_id is None: + return f'rt:{ticket_id}' + else: + return f'rt:{ticket_id}/{attachment_id}' + def url(self, ticket_id: RTId, attachment_id: Optional[RTId]=None) -> Optional[str]: if attachment_id is None: return self.ticket_url(ticket_id) diff --git a/tests/test_rtutil.py b/tests/test_rtutil.py index 3be8b22bf7f094cb1955062bea8ad2d753b9d2ae..46591b85ca54c06947ffe3191303c2ee04675b79 100644 --- a/tests/test_rtutil.py +++ b/tests/test_rtutil.py @@ -188,6 +188,15 @@ def test_exists_caches(new_client): def test_parse(rt, link, expected): assert rt.parse(link) == expected +@pytest.mark.parametrize('ticket_id,attachment_id,expected', [ + ('12', None, 'rt:12'), + (34, None, 'rt:34'), + ('56', '78', 'rt:56/78'), + (90, 880, 'rt:90/880'), +]) +def test_unparse(rt, ticket_id, attachment_id, expected): + assert rt.unparse(ticket_id, attachment_id) == expected + def test_uncommon_server_url_parsing(): url = 'https://example.org/REST/1.0/' client = testutil.RTClient(url + 'REST/1.0/')