-
Notifications
You must be signed in to change notification settings - Fork 1
Unit test fixing - remove network requirements #217
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
base: dev
Are you sure you want to change the base?
Changes from all commits
b9026c4
59f0230
55d14ba
dbfe3b8
302243a
de604ed
b0bacef
2a6c20b
7ff083a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||
| """ | ||||||
| import asyncio | ||||||
| import pytest | ||||||
| from unittest.mock import Mock, patch | ||||||
|
|
||||||
| from rnacentral_pipeline.databases.ensembl.metadata import karyotypes as karyo | ||||||
|
|
||||||
|
|
@@ -22,9 +23,76 @@ def karyotype(domain, species): | |||||
| raw = asyncio.run(karyo.fetch(species, domain)) | ||||||
| return karyo.process(raw) | ||||||
|
|
||||||
|
|
||||||
| # Mock data for Ensembl API responses | ||||||
| HOMO_SAPIENS_RESPONSE = { | ||||||
| "default_coord_system_version": "GRCh38", | ||||||
| "top_level_region": [ | ||||||
| # MT chromosome (no bands) | ||||||
| { | ||||||
| "coord_system": "chromosome", | ||||||
| "length": 16569, | ||||||
| "name": "MT" | ||||||
| }, | ||||||
| # Y chromosome (with detailed cytogenetic bands) | ||||||
| { | ||||||
| "coord_system": "chromosome", | ||||||
| "name": "Y", | ||||||
| "length": 57227415, | ||||||
| "bands": [ | ||||||
| {"stain": "acen", "id": "p11.1", "start": 10300001, "end": 10400000}, | ||||||
| {"id": "p11.2", "stain": "gneg", "start": 600001, "end": 10300000}, | ||||||
| {"id": "p11.31", "stain": "gpos50", "start": 300001, "end": 600000}, | ||||||
| {"stain": "gneg", "id": "p11.32", "start": 1, "end": 300000}, | ||||||
| {"start": 10400001, "end": 10600000, "id": "q11.1", "stain": "acen"}, | ||||||
| {"stain": "gneg", "start": 10600001, "end": 12400000, "id": "q11.21"}, | ||||||
| {"id": "q11.221", "end": 17100000, "start": 12400001, "stain": "gpos50"}, | ||||||
| {"end": 19600000, "start": 17100001, "id": "q11.222", "stain": "gneg"}, | ||||||
| {"stain": "gpos50", "id": "q11.223", "end": 23800000, "start": 19600001}, | ||||||
| {"stain": "gneg", "id": "q11.23", "end": 26600000, "start": 23800001}, | ||||||
| {"end": 57227415, "start": 26600001, "stain": "gvar", "id": "q12"}, | ||||||
| ] | ||||||
| }, | ||||||
| # 192 more regions would be here, but we only need MT and Y for tests | ||||||
| ] + [{"coord_system": "chromosome", "name": str(i), "length": 1000000} for i in range(1, 193)] | ||||||
| } | ||||||
|
|
||||||
| GLYCINE_MAX_RESPONSE = { | ||||||
| "default_coord_system_version": "Glycine_max_v2.1", | ||||||
| "top_level_region": [ | ||||||
| { | ||||||
| "coord_system": "chromosome", | ||||||
| "name": "1", | ||||||
| "length": 56831624 | ||||||
| } | ||||||
| # Would have 1189 more chromosomes, but we only need one for the test | ||||||
| ] + [{"coord_system": "chromosome", "name": str(i), "length": 1000000} for i in range(2, 1191)] | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| @pytest.fixture | ||||||
| def mock_ensembl_api(): | ||||||
| """Mock Ensembl REST API responses for karyotype data""" | ||||||
| def mock_get(url, **kwargs): | ||||||
| mock_response = Mock() | ||||||
| mock_response.raise_for_status = Mock() | ||||||
|
|
||||||
| # Match URL to return appropriate response | ||||||
| if "homo_sapiens" in url: | ||||||
| mock_response.json.return_value = HOMO_SAPIENS_RESPONSE | ||||||
| elif "glycine_max" in url: | ||||||
| mock_response.json.return_value = GLYCINE_MAX_RESPONSE | ||||||
| else: | ||||||
| raise ValueError(f"Unexpected URL in test: {url}") | ||||||
|
|
||||||
| return mock_response | ||||||
|
|
||||||
| with patch("rnacentral_pipeline.databases.ensembl.metadata.karyotypes.requests.get", side_effect=mock_get): | ||||||
| yield | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.ensembl | ||||||
| @pytest.mark.network | ||||||
| def test_builds_empty_karyotype_for_missing_data(): | ||||||
| def test_builds_empty_karyotype_for_missing_data(mock_ensembl_api): | ||||||
| _, found = karyotype("ensembl", "glycine_max") | ||||||
| assert len(found) == 1190 | ||||||
| assert found["1"] == { | ||||||
|
|
@@ -38,8 +106,7 @@ def test_builds_empty_karyotype_for_missing_data(): | |||||
| } | ||||||
|
|
||||||
| @pytest.mark.ensembl | ||||||
| @pytest.mark.network | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| def test_builds_with_known_bands(): | ||||||
| def test_builds_with_known_bands(mock_ensembl_api): | ||||||
| _, found = karyotype("ensembl", "homo_sapiens") | ||||||
| assert len(found) == 194 | ||||||
| assert found["MT"] == { | ||||||
|
|
||||||
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.
Adding
networkmarker topyproject.tomlto mark tests that require network access. This is good for categorizing tests, but it's important to ensure that all tests that actually use the network are marked, and that tests are not unnecessarily marked as requiring network access.