-
Notifications
You must be signed in to change notification settings - Fork 0
Mock OpenAIRE API in test to avoid test failure due to stale fixtures #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,26 @@ def raise_for_status(self): | |
| return DummyResponse() | ||
|
|
||
|
|
||
| def make_dummy_get_success(fixture_path): | ||
| """Factory that creates a mock GET function returning fixture data.""" | ||
| with open(fixture_path, "r") as f: | ||
| fixture_data = load(f) | ||
|
|
||
| def dummy_get_success(url, headers=None): | ||
| class DummyResponse: | ||
| status_code = 200 | ||
|
|
||
| def json(self): | ||
| return fixture_data | ||
|
|
||
| def raise_for_status(self): | ||
| pass | ||
|
|
||
| return DummyResponse() | ||
|
|
||
| return dummy_get_success | ||
|
|
||
|
|
||
| class TestMetadataFetcher403: | ||
| def test_api_403_response(self, session, monkeypatch): | ||
| monkeypatch.setattr(session, "get", dummy_get_403) | ||
|
|
@@ -49,12 +69,14 @@ def test_api_403_response(self, session, monkeypatch): | |
| expected = "OpenAire refresh token is invalid or expired. Please update token and try again." | ||
| assert str(e.value) == expected | ||
|
|
||
| def test_openaire_v2(self, session): | ||
| def test_openaire_v2(self, session, monkeypatch): | ||
|
||
| fixture_path = os.path.join("tests", "fixtures", "openaire_v2.json") | ||
| monkeypatch.setattr(session, "get", make_dummy_get_success(fixture_path)) | ||
|
|
||
| fetcher = MetadataFetcher(session=session) | ||
| actual = fetcher.get_metadata_from_openaire("10.5281/zenodo.4650794") | ||
| with open( | ||
| os.path.join("tests", "fixtures", "openaire_v2.json"), "r" | ||
| ) as f: | ||
|
|
||
| with open(fixture_path, "r") as f: | ||
| expected = load(f) | ||
|
Comment on lines
+73
to
80
|
||
| assert actual["results"] == expected["results"] | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter signature
dummy_get_success(url, headers=None)is inconsistent withdummy_get_403(url, headers)which requires theheadersparameter. While this works becauseheaders=Nonemakes it optional, for consistency with the existing mock function pattern (dummy_get_403at line 29), consider making the signature match:dummy_get_success(url, headers).This ensures both mock functions have the same signature, making them more interchangeable and reducing potential confusion.