Skip to content

Commit

Permalink
Merge pull request #3099 from hypothesis/fix-elastic-documenturis-par…
Browse files Browse the repository at this point in the history
…sing

Only add self-claim DocumentURI when claimant is set
  • Loading branch information
nickstenning committed Mar 16, 2016
2 parents 7eb40a3 + 9c3553c commit 1c5f7c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
18 changes: 9 additions & 9 deletions h/api/models/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,22 @@ def _transform_links(self):
transformed = []
links = self.get('link', [])

# add self-claim uri when claimant is not missing
if self.claimant:
transformed.append({'claimant': self.claimant,
'uri': self.claimant,
'type': 'self-claim',
'created': self.created,
'updated': self.updated})

# When document link is just a string, transform it to a link object with
# an href, so it gets further processed as either a self-claim or another
# claim.
if isinstance(links, basestring):
links = [{"href": links}]

for link in links:
# disregard self-claim urls as they're are being added separately
# later on.
# disregard self-claim urls as they have already been added
if link.keys() == ['href'] and link['href'] == self.claimant:
continue

Expand Down Expand Up @@ -351,13 +358,6 @@ def _transform_links(self):
'created': self.created,
'updated': self.updated})

# add self claim
transformed.append({'claimant': self.claimant,
'uri': self.claimant,
'type': 'self-claim',
'created': self.created,
'updated': self.updated})

return transformed


Expand Down
13 changes: 10 additions & 3 deletions h/api/models/test/elastic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,18 @@ def test_uris_only_one_self_claim(self):

assert doc.document_uris == expected

def test_uris_discard_self_claim_when_claimant_is_missing(self):
doc = Document({'link': [{'href': 'http://example.com'}]})
expected = [DocumentURI({'claimant': None,
'uri': 'http://example.com',
'type': None,
'content_type': None,
'created': None, 'updated': None})]
assert doc.document_uris == expected

def test_uris_disregard_doi_links(self):
doc = Document({'link': [{'href': 'doi:foobar'}]})
# it always includes a self-claim, not removing doi links would result
# in a length of 2
assert len(doc.document_uris) == 1
assert len(doc.document_uris) == 0

def test_uris_str_link(self):
doc = Document({'link': 'http://example.com'},
Expand Down

0 comments on commit 1c5f7c8

Please sign in to comment.