/
test_desy.py
318 lines (270 loc) · 8.21 KB
/
test_desy.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: utf-8 -*-
#
# This file is part of hepcrawl.
# Copyright (C) 2017, 2019 CERN.
#
# hepcrawl is a free software; you can redistribute it and/or modify it
# under the terms of the Revised BSD License; see LICENSE file for
# more details.
"""Functional tests for Desy spider"""
from __future__ import absolute_import, division, print_function
import copy
import os
import shutil
from time import sleep
import pytest
from deepdiff import DeepDiff
from hepcrawl.testlib.celery_monitor import CeleryMonitor
from hepcrawl.testlib.fixtures import (
get_test_suite_path,
expected_json_results_from_file,
clean_dir,
)
from hepcrawl.testlib.tasks import app as celery_app
from hepcrawl.testlib.utils import get_crawler_instance
def override_dynamic_fields_on_records(records):
clean_records = []
for record in records:
clean_record = override_dynamic_fields_on_record(record)
clean_records.append(clean_record)
return clean_records
def override_dynamic_fields_on_record(record):
def _override(field_key, original_dict, backup_dict, new_value):
backup_dict[field_key] = original_dict[field_key]
original_dict[field_key] = new_value
clean_record = copy.deepcopy(record)
overriden_fields = {}
dummy_random_date = u'2017-04-03T10:26:40.365216'
overriden_fields['acquisition_source'] = {}
_override(
field_key='datetime',
original_dict=clean_record['acquisition_source'],
backup_dict=overriden_fields['acquisition_source'],
new_value=dummy_random_date,
)
_override(
field_key='submission_number',
original_dict=clean_record['acquisition_source'],
backup_dict=overriden_fields['acquisition_source'],
new_value=u'5652c7f6190f11e79e8000224dabeaad',
)
return clean_record
def get_ftp_settings():
netrc_location = get_test_suite_path(
'desy',
'fixtures',
'ftp_server',
'.netrc',
test_suite='functional',
)
return {
'CRAWLER_HOST_URL': 'http://scrapyd:6800',
'CRAWLER_PROJECT': 'hepcrawl',
'CRAWLER_ARGUMENTS': {
'ftp_host': 'ftp_server',
'ftp_netrc': netrc_location,
}
}
def get_local_settings():
package_location = get_test_suite_path(
'desy',
'fixtures',
'ftp_server',
'DESY',
test_suite='functional',
)
return {
'CRAWLER_HOST_URL': 'http://scrapyd:6800',
'CRAWLER_PROJECT': 'hepcrawl',
'CRAWLER_ARGUMENTS': {
'source_folder': package_location,
}
}
@pytest.fixture
def get_local_settings_for_broken():
package_location = get_test_suite_path(
'desy',
'fixtures',
'ftp_server',
'DESY',
'broken',
test_suite='functional',
)
os.mkdir(package_location)
tmp_file = os.path.join(package_location, 'broken_record.xml')
with open(tmp_file, 'w') as f:
f.write(
"<?xml version='1.0' encoding='UTF-8'?>"
"<collection>"
"<record>"
"<datafield tag='260' ind1=' ' ind2=' '>"
"<subfield code='c'>BROKEN DATE</subfield>"
"</datafield>"
"</record>"
"</collection>"
)
yield {
'CRAWLER_HOST_URL': 'http://scrapyd:6800',
'CRAWLER_PROJECT': 'hepcrawl',
'CRAWLER_ARGUMENTS': {
'source_folder': package_location,
}
}
shutil.rmtree(package_location)
@pytest.fixture(scope="function")
def cleanup():
# The test must wait until the docker environment is up (takes about 10
# seconds).
sleep(10)
yield
clean_dir(path=os.path.join(os.getcwd(), '.scrapy'))
clean_dir('/tmp/file_urls')
clean_dir('/tmp/DESY')
@pytest.mark.parametrize(
'expected_results, settings',
[
(
expected_json_results_from_file(
'desy',
'fixtures',
'desy_records_ftp_expected.json',
),
get_ftp_settings(),
),
(
expected_json_results_from_file(
'desy',
'fixtures',
'desy_records_local_expected.json',
),
get_local_settings(),
),
],
ids=[
'ftp package',
'local package',
]
)
def test_desy(
expected_results,
settings,
cleanup,
):
crawler = get_crawler_instance(
settings.get('CRAWLER_HOST_URL')
)
crawl_results = CeleryMonitor.do_crawl(
app=celery_app,
monitor_timeout=5,
monitor_iter_limit=100,
events_limit=1,
crawler_instance=crawler,
project=settings.get('CRAWLER_PROJECT'),
spider='desy',
settings={},
**settings.get('CRAWLER_ARGUMENTS')
)
crawl_result = crawl_results[0]
gotten_records = [
result['record'] for result in crawl_result['results_data']
]
gotten_records = override_dynamic_fields_on_records(gotten_records)
expected_results = override_dynamic_fields_on_records(expected_results)
gotten_records = sorted(
gotten_records,
key=lambda record: record['titles'][0]['title'],
)
expected_results = sorted(
expected_results,
key=lambda result: result['titles'][0]['title'],
)
assert DeepDiff(gotten_records, expected_results, ignore_order=True) == {}
assert not crawl_result['errors']
def test_desy_broken_xml(get_local_settings_for_broken, cleanup):
settings = get_local_settings_for_broken
crawler = get_crawler_instance(
settings.get('CRAWLER_HOST_URL')
)
crawl_results = CeleryMonitor.do_crawl(
app=celery_app,
monitor_timeout=5,
monitor_iter_limit=100,
events_limit=2,
crawler_instance=crawler,
project=settings.get('CRAWLER_PROJECT'),
spider='desy',
settings={},
**settings.get('CRAWLER_ARGUMENTS')
)
crawl_result = crawl_results[0]
result_records = crawl_result['results_data']
assert not crawl_result['errors']
assert len(result_records) == 1
res = result_records[0]
assert res['record']
assert len(res['errors']) == 1
assert 'DoJsonError' in res['errors'][0]['exception']
assert res['errors'][0]['traceback']
assert res['file_name'] == 'broken_record.xml'
assert res['source_data']
@pytest.mark.parametrize(
'expected_results, settings',
[
(
expected_json_results_from_file(
'desy',
'fixtures',
'desy_records_ftp_expected.json',
),
get_ftp_settings(),
),
],
ids=[
'ftp package crawl twice',
]
)
def test_desy_crawl_twice(expected_results, settings, cleanup):
crawler = get_crawler_instance(
settings.get('CRAWLER_HOST_URL')
)
crawl_results = CeleryMonitor.do_crawl(
app=celery_app,
monitor_timeout=5,
monitor_iter_limit=100,
events_limit=1,
crawler_instance=crawler,
project=settings.get('CRAWLER_PROJECT'),
spider='desy',
settings={},
**settings.get('CRAWLER_ARGUMENTS')
)
assert len(crawl_results) == 1
crawl_result = crawl_results[0]
gotten_records = [
result['record'] for result in crawl_result['results_data']
]
gotten_records = override_dynamic_fields_on_records(gotten_records)
expected_results = override_dynamic_fields_on_records(expected_results)
gotten_records = sorted(
gotten_records,
key=lambda record: record['titles'][0]['title'],
)
expected_results = sorted(
expected_results,
key=lambda result: result['titles'][0]['title'],
)
assert DeepDiff(gotten_records, expected_results, ignore_order=True) == {}
assert not crawl_result['errors']
# Second crawl
crawl_results = CeleryMonitor.do_crawl(
app=celery_app,
monitor_timeout=5,
monitor_iter_limit=100,
events_limit=1,
crawler_instance=crawler,
project=settings.get('CRAWLER_PROJECT'),
spider='desy',
settings={},
**settings.get('CRAWLER_ARGUMENTS')
)
assert len(crawl_results) == 0