From 2d9b2451afd86ba10f2660f07c396a3e3e63a3a2 Mon Sep 17 00:00:00 2001 From: Greg Kempe Date: Tue, 19 Jan 2021 14:12:49 +0200 Subject: [PATCH] Rectify xpath issue, add test --- indigo/documents.py | 6 +++--- indigo_api/tests/test_annotations_api.py | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/indigo/documents.py b/indigo/documents.py index a58f09715..a622a7ddf 100644 --- a/indigo/documents.py +++ b/indigo/documents.py @@ -31,7 +31,7 @@ def resolve(self, exact): escaped = prefix.replace("'", "\\'") # find the attachment with this id results = list(self.document.doc.main.xpath(f"./a:attachments/a:attachment[@eId='{escaped}']", namespaces={'a': self.document.doc.namespace})) - if results: + if len(results): component = results[0] else: component = self.document.doc.main @@ -44,14 +44,14 @@ def resolve(self, exact): while anchor_id: escaped = anchor_id.replace("'", "\\'") elems = component.xpath(f".//a:*[@eId='{escaped}']", namespaces={'a': self.document.doc.namespace}) - if elems is not None: + if len(elems): self.resolve_element(elems[0]) break elif anchor_id in ['preface', 'preamble']: # HACK HACK HACK # We sometimes use 'preamble' and 'preface' even though they aren't IDs elems = component.xpath(f".//a:{escaped}", namespaces={'a': self.document.doc.namespace}) - if elems is not None: + if len(elems): self.resolve_element(elems[0]) break diff --git a/indigo_api/tests/test_annotations_api.py b/indigo_api/tests/test_annotations_api.py index b98637ff1..6462e9ea2 100644 --- a/indigo_api/tests/test_annotations_api.py +++ b/indigo_api/tests/test_annotations_api.py @@ -112,7 +112,7 @@ def test_cant_change_logged_out(self): def test_create_annotation_task(self): response = self.client.post('/api/documents/10/annotations', { 'text': 'hello', - 'anchor_id': 'section.1', + 'anchor_id': 'sec_1', }) assert_equal(response.status_code, 201) @@ -120,6 +120,21 @@ def test_create_annotation_task(self): response = self.client.post('/api/documents/10/annotations/%s/task' % note_id) assert_equal(response.status_code, 201) - assert_equal(response.data['title'], '"section.1": hello') + assert_equal(response.data['title'], '"Section 1.": hello') + assert_equal(response.data['state'], 'open') + assert_is_none(response.data.get('anchor_id')) + + def test_create_annotation_task_anchor_missing(self): + response = self.client.post('/api/documents/10/annotations', { + 'text': 'hello', + # sec_99 doesn't exist + 'anchor_id': 'sec_99', + }) + assert_equal(response.status_code, 201) + note_id = response.data['id'] + + response = self.client.post('/api/documents/10/annotations/%s/task' % note_id) + assert_equal(response.status_code, 201) + assert_equal(response.data['title'], '"sec_99": hello') assert_equal(response.data['state'], 'open') assert_is_none(response.data.get('anchor_id'))