-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_projects_contract.py
More file actions
441 lines (353 loc) · 15.2 KB
/
Copy pathtest_projects_contract.py
File metadata and controls
441 lines (353 loc) · 15.2 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""
OpenAIRE Search Projects API – Contract Test Suite
====================================================
Exercises the ``/search/projects`` endpoint whose metadata schema differs
from the research-product endpoints (publications, datasets, software, other).
Projects expose fields such as grant ID (``code``), ``acronym``,
``startdate`` / ``enddate``, ``funder``, etc.
Documentation:
https://graph.openaire.eu/docs/apis/search-api/projects
Modes
-----
* ``--phase=record`` – call the API and persist the normalised response as a
baseline snapshot.
* ``--phase=compare`` – call the API, normalise, and assert equivalence with
the stored baseline.
Run with::
pytest test_projects_contract.py --phase=record -v
pytest test_projects_contract.py --phase=compare -v
"""
import os
import pytest
from helpers import (
DEFAULT_TOTAL_TOLERANCE_PCT,
query_api,
normalise_project_json_response,
normalise_project_xml_response,
save_snapshot,
load_snapshot,
compare_snapshots,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _run_test(
test_id: str,
params: dict,
base_url: str,
endpoint: str,
snapshot_dir: str,
phase: str,
*,
fmt: str = "xml",
total_tolerance_pct: float = DEFAULT_TOTAL_TOLERANCE_PCT,
loose: bool = True,
):
"""
Core test driver for the projects endpoint.
Uses project-specific normalisers and loose comparison by default.
"""
status, body, request_url, response_time_ms = query_api(base_url, endpoint, params, fmt=fmt)
assert status == 200, f"Expected HTTP 200, got {status}\n URL: {request_url}"
if fmt == "json":
current = normalise_project_json_response(body)
else:
current = normalise_project_xml_response(body)
print(f" [timing] {test_id}: {response_time_ms:.0f} ms")
if phase == "record":
save_snapshot(snapshot_dir, test_id, params=params, normalised=current, response_time_ms=response_time_ms)
else:
compare_dir = snapshot_dir.rstrip('/') + '_compare'
os.makedirs(compare_dir, exist_ok=True)
save_snapshot(compare_dir, test_id, params=params, normalised=current, response_time_ms=response_time_ms)
baseline_data = load_snapshot(snapshot_dir, test_id)
baseline = baseline_data["normalised"]
diffs = compare_snapshots(
baseline, current,
total_tolerance_pct=total_tolerance_pct,
loose=loose,
)
assert not diffs, (
f"Contract differences detected:\n"
f" URL: {request_url}\n"
+ "\n".join(f" • {d}" for d in diffs)
)
# ===================================================================
# 1. Keyword search
# ===================================================================
class TestKeywordSearch:
"""Search projects by keywords."""
def test_single_keyword(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"single_keyword",
{"keywords": "health", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_multiple_keywords(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"multiple_keywords",
{"keywords": "renewable energy transition", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 2. Grant ID lookup
# ===================================================================
class TestGrantIDLookup:
"""Retrieve projects by grant ID."""
def test_grant_id(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"grant_id",
{"grantID": "283595", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
loose=False,
)
# ===================================================================
# 3. Project name search
# ===================================================================
class TestNameSearch:
"""Search projects by name."""
def test_name(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"name",
{"name": "Open Science", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 4. Acronym search
# ===================================================================
class TestAcronymSearch:
"""Search project by acronym."""
def test_acronym(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"acronym",
{"acronym": "OpenAIRE", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
loose=False,
)
# ===================================================================
# 5. Funder filtering
# ===================================================================
class TestFunderFiltering:
"""Filter projects by funder."""
def test_funder_ec(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"funder_ec",
{"funder": "EC", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_funder_nsf(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"funder_nsf",
{"funder": "NSF", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_funder_wt(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"funder_wt",
{"funder": "WT", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_has_ec_funding_true(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"has_ec_funding_true",
{"hasECFunding": "true", "keywords": "innovation", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_has_wt_funding_true(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"has_wt_funding_true",
{"hasWTFunding": "true", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 6. Call ID filtering
# ===================================================================
class TestCallID:
"""Filter projects by call identifier."""
def test_call_id(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"call_id",
{"callID": "H2020-MSCA-IF-2014", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 7. Start / End year filtering
# ===================================================================
class TestYearFiltering:
"""Filter projects by start or end year."""
def test_start_year(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"start_year",
{"startYear": "2020", "funder": "EC", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_end_year(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"end_year",
{"endYear": "2023", "funder": "EC", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 8. Participant countries
# ===================================================================
class TestParticipantCountries:
"""Filter projects by participant countries."""
def test_participant_country_de(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"participant_country_de",
{"participantCountries": "DE", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_participant_country_fr(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"participant_country_fr",
{"participantCountries": "FR", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 9. Participant acronyms (institutions)
# ===================================================================
class TestParticipantAcronyms:
"""Filter projects by participant institution acronyms."""
def test_participant_acronym(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"participant_acronym",
{"participantAcronyms": "CERN", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 10. Sorting
# ===================================================================
class TestSorting:
"""Verify sorted result ordering is preserved for projects."""
def test_sort_by_start_date_desc(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"sort_by_start_date_desc",
{
"keywords": "climate",
"sortBy": "projectstartdate,descending",
"size": "10",
"page": "1",
},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_sort_by_start_date_asc(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"sort_by_start_date_asc",
{
"keywords": "climate",
"sortBy": "projectstartdate,ascending",
"size": "10",
"page": "1",
},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_sort_by_end_date_desc(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"sort_by_end_date_desc",
{
"keywords": "climate",
"sortBy": "projectenddate,descending",
"size": "10",
"page": "1",
},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 11. Pagination
# ===================================================================
class TestPagination:
"""Ensure pagination returns consistent slices."""
def test_page_1(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"page_1",
{"keywords": "digital", "funder": "EC", "size": "5", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_page_2(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"page_2",
{"keywords": "digital", "funder": "EC", "size": "5", "page": "2"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_large_page_size(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"large_page_size",
{"keywords": "digital", "funder": "EC", "size": "50", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 12. FP7 scientific area
# ===================================================================
class TestFP7ScientificArea:
"""Filter FP7 projects by scientific area."""
def test_fp7_scientific_area(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"fp7_scientific_area",
{"FP7scientificArea": "health", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 13. Funding stream
# ===================================================================
class TestFundingStream:
"""Filter projects by funding stream."""
def test_funding_stream(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"funding_stream",
{"fundingStream": "H2020", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
# ===================================================================
# 14. XML format
# ===================================================================
class TestJSONFormat:
"""Verify that JSON responses maintain the same contract."""
def test_json_keyword_search(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"json_keyword_search",
{"keywords": "artificial intelligence", "funder": "EC", "size": "10", "page": "1"},
base_url, endpoint_projects, snapshot_dir_projects, phase,
fmt="json",
)
# ===================================================================
# 15. Combined filters
# ===================================================================
class TestCombinedFilters:
"""Test queries that combine multiple filter parameters for projects."""
def test_funder_country_year(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"funder_country_year",
{
"funder": "EC",
"participantCountries": "DE",
"startYear": "2020",
"size": "10",
"page": "1",
},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_keywords_funder_sorted(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"keywords_funder_sorted",
{
"keywords": "sustainability",
"funder": "EC",
"sortBy": "projectstartdate,descending",
"size": "10",
"page": "1",
},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)
def test_funder_call_country(self, base_url, endpoint_projects, snapshot_dir_projects, phase):
_run_test(
"funder_call_country",
{
"funder": "EC",
"callID": "H2020-MSCA-IF-2014",
"participantCountries": "IT",
"size": "10",
"page": "1",
},
base_url, endpoint_projects, snapshot_dir_projects, phase,
)