-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_article_match.py
275 lines (223 loc) · 10.3 KB
/
test_article_match.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from doajtest.helpers import DoajTestCase
from portality import models
import uuid, time
from random import randint
from portality.bll.doaj import DOAJ
from portality.bll.exceptions import ArticleMergeConflict
from portality.lib import dates
class TestArticleMatch(DoajTestCase):
def setUp(self):
super(TestArticleMatch, self).setUp()
def tearDown(self):
super(TestArticleMatch, self).tearDown()
def test_01_same_fulltext(self):
"""Check duplication detection on articles with the same fulltext URL"""
# A list of various URLs to check matching on
ftus = [
"http://examplejournal.telfor.rs/Published/Vol1No1/Vol1No1_A5.pdf",
"http://www.sbe.deu.edu.tr/dergi/cilt15.say%C4%B12/06%20AKALIN.pdf",
"http://www.ujcem.med.sumdu.edu.ua/images/sampledata/2013/4/408_412_IV-020.pdf",
"http://www.psychologie-aktuell.com/fileadmin/download/ptam/1-2014_20140324/01_Geiser.pdf"
]
for ftu in ftus:
# make ourselves an example article
a = models.Article()
b = a.bibjson()
b.title = "Example article with a fulltext url"
b.add_url(ftu, urltype="fulltext")
a.save(blocking=True)
# create a replacement article
z = models.Article()
y = z.bibjson()
y.title = "Replacement article for fulltext url"
y.add_url(ftu, urltype="fulltext")
# determine if there's a duplicate
articleService = DOAJ.articleService()
d = articleService.get_duplicate(z)
assert d is not None
assert d.bibjson().title == "Example article with a fulltext url"
def test_02_different_fulltext(self):
"""Check that an article with different fulltext URLs is not considered a duplicate"""
# make ourselves an example article
a = models.Article()
b = a.bibjson()
b.title = "Example 2 article with a fulltext url"
b.add_url("http://www.sbe.deu.edu.tr/dergi/cilt15.say%C4%B12/06%20AKALIN.pdf", urltype="fulltext")
a.save(blocking=True)
# create another article
z = models.Article()
y = z.bibjson()
y.title = "Replacement article for fulltext url"
y.add_url("http://this.is/a/different/url", urltype="fulltext")
# determine if there's a duplicate
articleService = DOAJ.articleService()
d = articleService.get_duplicate(z)
assert d is None
def test_03_retrieve_multiple_conflict(self):
ftu = "http://www.sbe.deu.edu.tr/dergi/cilt15.say%C4%B12/06%20AKALIN.pdf"
# make ourselves a couple of example articles
a = models.Article()
b = a.bibjson()
b.title = "Example A article with a fulltext url"
b.add_url(ftu, urltype="fulltext")
a.save(blocking=True)
# Wait a second to ensure the timestamps are different
time.sleep(1.01)
a2 = models.Article()
b2 = a2.bibjson()
b2.title = "Example B article with a fulltext url"
b2.add_url(ftu, urltype="fulltext")
a2.save(blocking=True)
# create an article which should not be caught by the duplicate detection
not_duplicate = models.Article()
not_duplicate_bibjson = not_duplicate.bibjson()
not_duplicate_bibjson.title = "Example C article with a fulltext url"
not_duplicate_bibjson.add_url("http://this.is/a/different/url", urltype="fulltext")
not_duplicate.save(blocking=True)
# create a replacement article
z = models.Article()
y = z.bibjson()
y.title = "Replacement article for fulltext url"
y.add_url(ftu, urltype="fulltext")
# determine that there are multiple duplicates
articleService = DOAJ.articleService()
with self.assertRaises(ArticleMergeConflict):
d = articleService.get_duplicate(z)
# get the xwalk to determine all duplicates
# sort both results and expectations here to avoid false alarm
# we don't care about the order of duplicates
expected = [a, a2]
expected.sort(key=lambda x: dates.parse(x.last_updated), reverse=True)
# determine if there's a duplicate
l = articleService.get_duplicates(z)
assert isinstance(l, list), l
assert l is not None
l.sort(key=lambda x: dates.parse(x.last_updated), reverse=True)
assert expected == l
def test_04_with_doi_instead(self):
"""Detect a duplicate using the DOI field."""
# make ourselves a couple of example articles
a = models.Article()
b = a.bibjson()
b.title = "Example A article with a DOI"
b.add_identifier('doi', "10.doi/123")
a.save(blocking=True)
# create an article which should not be caught by the duplicate detection
not_duplicate = models.Article()
not_duplicate_bibjson = not_duplicate.bibjson()
not_duplicate_bibjson.title = "Example C article with a DOI"
not_duplicate_bibjson.add_identifier('doi', "10.doi/DIFFERENT")
not_duplicate.save(blocking=True)
# create a replacement article
z = models.Article()
y = z.bibjson()
y.title = "Replacement article for DOI"
y.add_identifier('doi', "10.doi/123")
# determine if there's a duplicate
articleService = DOAJ.articleService()
dups = articleService.get_duplicates(z)
assert len(dups) == 1
# Check when we ask for one duplicate we get the most recent duplicate.
d = articleService.get_duplicate(z)
assert d is not None
assert d.bibjson().title == "Example A article with a DOI", d.bibjson().title
def test_05_full_doi(self):
""" Test that we still detect duplicate DOIs when we have the full URI, not just the 10. """
# make ourselves a couple of example articles
a = models.Article()
b = a.bibjson()
b.title = "Example A article with a DOI"
b.add_identifier('doi', "https://doi.org/10.doi/123")
a.save(blocking=True)
# create an article which should not be caught by the duplicate detection
not_duplicate = models.Article()
not_duplicate_bibjson = not_duplicate.bibjson()
not_duplicate_bibjson.title = "Example C article with a DOI"
not_duplicate_bibjson.add_identifier('doi', "https://doi.org/10.doi/DIFFERENT")
not_duplicate.save(blocking=True)
# create a replacement article
z = models.Article()
y = z.bibjson()
y.title = "Replacement article for DOI"
y.add_identifier('doi', "http://doi.org/10.doi/123")
# determine if there's a duplicate
articleService = DOAJ.articleService()
dups = articleService.get_duplicates(z)
assert len(dups) == 1
# Check when we ask for one duplicate we get the most recent duplicate.
d = articleService.get_duplicate(z)
assert d is not None
assert d.bibjson().title == "Example A article with a DOI", d.bibjson().title
def test_06_merge_replaces_metadata(self):
"""Ensure that merging replaces metadata of a new article, but keeps its old id."""
ftu = "http://www.sbe.deu.edu.tr/dergi/cilt15.say%C4%B12/06%20AKALIN.pdf"
id1 = uuid.uuid4().hex
id2 = uuid.uuid4().hex
assert id1 != id2
a = models.Article()
a.set_id(id1)
b = a.bibjson()
b.title = "Example A article with a fulltext url"
b.abstract = "a bunch of text"
b.add_url(ftu, urltype="fulltext")
a2 = models.Article()
a2.set_id(id2)
b2 = a2.bibjson()
b2.title = "Example B article with a fulltext url"
b2.add_url(ftu, urltype="fulltext")
# perform a merge, which updates article records of a2 based on a - including the id.
assert a2.id == id2
a2.merge(a)
assert a2.id == id1
# Check that we have the newer metadata
assert a2.bibjson().title == "Example B article with a fulltext url"
assert a2.bibjson().abstract is None
# Create a 3rd article without an explicit id
a3 = models.Article()
b3 = a3.bibjson()
b3.title = "Example C article with a fulltext url"
b3.abstract = "a newer bunch of text"
b3.add_url(ftu, urltype="fulltext")
a3.merge(a2)
assert a3.id == a2.id == a.id
assert a3.bibjson().title == "Example C article with a fulltext url"
assert a3.bibjson().abstract == "a newer bunch of text"
def test_07_both_duplication_criteria(self):
"""Check that an article is only reported once if it is duplicated by both DOI and fulltext URL"""
# make ourselves an example article
ftu = "http://www.sbe.deu.edu.tr/dergi/cilt15.say%C4%B12/06%20AKALIN.pdf"
doi = "10.doi/123"
a = models.Article()
b = a.bibjson()
b.title = "Example article with a fulltext url and a DOI"
b.add_url(ftu, urltype="fulltext")
b.add_identifier('doi', doi)
a.save(blocking=True)
# create another article
z = models.Article()
y = z.bibjson()
y.title = "Replacement article for fulltext url and a DOI"
y.add_url(ftu, urltype="fulltext")
y.add_identifier('doi', doi)
# determine if there's a duplicate
articleService = DOAJ.articleService()
d = articleService.get_duplicates(z)
assert len(d) == 1
print(len(d))
assert d[0].bibjson().title == "Example article with a fulltext url and a DOI"
def test_08_many_issns(self):
"""Test that a query with a LOT of ISSNs is still successful."""
a = models.Article()
b = a.bibjson()
b.journal_issns = ["0000-0000"]
b.title = "Example A article with a fulltext url"
b.add_identifier(b.DOI, "10.1234/duplicate")
a.save(blocking=True)
def random_issn():
bits = []
for i in range(8):
bits.append(str(randint(1, 9)))
return "".join(bits[:4]) + "-" + "".join(bits[4:])
issns = [random_issn() for _ in range(2000)] + ["0000-0000"]
dupes = models.Article.duplicates(doi="10.1234/duplicate")
assert len(dupes) == 1