Skip to content

Commit

Permalink
Rectify xpath issue, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Jan 19, 2021
1 parent a1adb0e commit 2d9b245
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions indigo/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
19 changes: 17 additions & 2 deletions indigo_api/tests/test_annotations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,29 @@ 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)
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'], '"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'))

0 comments on commit 2d9b245

Please sign in to comment.