Download tests/test_franklin_service.py from OpenVar/backend: direct link, hf CLI and curl.
- Browser
- Download file 12 kB
-
https://huggingface.co/spaces/OpenVar/backend/resolve/main/tests/test_franklin_service.py
- Command line
-
hf download hf://spaces/OpenVar/backend/tests/test_franklin_service.py
-
curl -L -o test_franklin_service.py https://huggingface.co/spaces/OpenVar/backend/resolve/main/tests/test_franklin_service.py
12 kB
| import asyncio | |
| import sqlite3 | |
| from datetime import datetime, timedelta, timezone | |
| from app.services import franklin_service | |
| class TestFranklinSvCache: | |
| def _cacheable_sv_payload(): | |
| return { | |
| "variant": {"key": "chr5-125423848-128107072-DUP-hg38"}, | |
| "tabs": { | |
| "variant_assessment": { | |
| "summary": { | |
| "ok": True, | |
| "status": 200, | |
| "data": { | |
| "variant_type": "SV", | |
| "genes": ["MARCH3"], | |
| "cytobands_description": "5q23.2-q23.3", | |
| }, | |
| }, | |
| "region_viewer": { | |
| "region_data": { | |
| "ok": True, | |
| "status": 200, | |
| "data": { | |
| "region_data": { | |
| "123": { | |
| "genes": [{"gene": "MARCH3"}], | |
| } | |
| } | |
| }, | |
| } | |
| }, | |
| "population_frequencies": { | |
| "ok": True, | |
| "status": 200, | |
| "data": {"dgv": [], "dgv_gold": [], "gnomad": []}, | |
| }, | |
| "clinical_evidence": {"ok": True, "status": 200, "data": {"clinvar": {"entries": []}}}, | |
| "variant_details": {"ok": True, "status": 200, "data": {"genes": ["MARCH3"]}}, | |
| }, | |
| "alternate_reference": {"ok": False, "status": 401, "data": {"raw": "no token"}}, | |
| }, | |
| "generated_at": datetime.now(timezone.utc).isoformat(), | |
| } | |
| def test_sv_region_defaults_follow_franklin_windowing(self): | |
| default_view = franklin_service._build_sv_default_view(125423848, 128107072, None) | |
| assert default_view == {"start": 124516250, "end": 129014670} | |
| assert franklin_service._build_sv_region_buckets(default_view["start"], default_view["end"]) == [ | |
| 123, | |
| 124, | |
| 125, | |
| 126, | |
| 127, | |
| 128, | |
| 129, | |
| 130, | |
| ] | |
| def test_sv_occurrence_merge_deduplicates_across_buckets(self): | |
| merged = franklin_service._collect_sv_public_occurrences( | |
| { | |
| "123": { | |
| "dgv_occurrences": [{"id": "dgv-1"}, {"id": "dgv-2"}], | |
| "dgv_gold_occurrences": [{"id": "gold-1"}], | |
| "gnomad_occurrences": [{"id": "gn-1"}], | |
| }, | |
| "124": { | |
| "dgv_occurrences": [{"id": "dgv-2"}, {"id": "dgv-3"}], | |
| "dgv_gold_occurrences": [{"id": "gold-1"}, {"id": "gold-2"}], | |
| "gnomad_occurrences": [{"id": "gn-2"}], | |
| }, | |
| } | |
| ) | |
| assert [item["id"] for item in merged["dgv"]] == ["dgv-1", "dgv-2", "dgv-3"] | |
| assert [item["id"] for item in merged["dgv_gold"]] == ["gold-1", "gold-2"] | |
| assert [item["id"] for item in merged["gnomad"]] == ["gn-1", "gn-2"] | |
| def test_sv_alt_reference_fallback_uses_ensembl_map(self, monkeypatch): | |
| async def fake_map_range(chrom, start, end, source_assembly, target_assembly): | |
| assert chrom == "5" | |
| assert start == 125423848 | |
| assert end == 128107072 | |
| assert source_assembly == "GRCh38" | |
| assert target_assembly == "GRCh37" | |
| return { | |
| "chrom": "5", | |
| "start_pos": 124759541, | |
| "end_pos": 127442764, | |
| } | |
| monkeypatch.setattr(franklin_service, "_map_assembly_range", fake_map_range) | |
| context = franklin_service.FranklinStructuralVariantContext( | |
| chrom="5", | |
| start=125423848, | |
| end=128107072, | |
| sv_type="DUP", | |
| reference_version="hg38", | |
| ) | |
| result = asyncio.run( | |
| franklin_service._build_sv_alt_reference_fallback( | |
| context, | |
| {"cytobands_description": "5q23.2-q23.3"}, | |
| ) | |
| ) | |
| assert result == { | |
| "ok": True, | |
| "status": 200, | |
| "data": { | |
| "success": True, | |
| "reference_version": "HG19", | |
| "sv_variant": { | |
| "chrom": "chr5", | |
| "start_pos": 124759541, | |
| "end_pos": 127442764, | |
| "sv_type": "DUP", | |
| "reference_genome_type": "hg19", | |
| "reference_version": "HG19", | |
| "length": 2683224, | |
| }, | |
| "cytobands": "5q23.2-q23.3", | |
| "link": "https://franklin.genoox.com/clinical-db/variant/sv/chr5-124759541-127442764-DUP", | |
| "source": "ensembl-map-fallback", | |
| }, | |
| } | |
| def test_sv_cache_miss_fetches_and_persists(self, monkeypatch, tmp_path): | |
| cache_path = tmp_path / "franklin-cache.sqlite3" | |
| monkeypatch.setenv("FRANKLIN_CACHE_DB_PATH", str(cache_path)) | |
| fetched_payload = self._cacheable_sv_payload() | |
| async def fake_fetch(context, query): | |
| assert context.chrom == "5" | |
| assert context.start == 125423848 | |
| assert context.end == 128107072 | |
| assert context.sv_type == "DUP" | |
| assert context.reference_label == "hg38" | |
| assert query == "DUP:Chr5:125423848-128107072" | |
| return fetched_payload | |
| monkeypatch.setattr(franklin_service, "_fetch_structural_variant_assessment", fake_fetch) | |
| result = asyncio.run( | |
| franklin_service.get_franklin_variant_assessment( | |
| "DUP:Chr5:125423848-128107072", | |
| reference_version="hg38", | |
| ) | |
| ) | |
| assert result["cache"]["source"] == "franklin-live" | |
| assert result["cache"]["stale"] is False | |
| with sqlite3.connect(cache_path) as connection: | |
| row = connection.execute( | |
| "SELECT cache_key, reference_version FROM franklin_sv_cache" | |
| ).fetchone() | |
| assert row == ("chr5-125423848-128107072-DUP-hg38", "hg38") | |
| def test_sv_cache_miss_does_not_persist_empty_failure_payload(self, monkeypatch, tmp_path): | |
| cache_path = tmp_path / "franklin-cache.sqlite3" | |
| monkeypatch.setenv("FRANKLIN_CACHE_DB_PATH", str(cache_path)) | |
| failed_payload = { | |
| "variant": {"key": "chr5-125423848-128107072-DUP-hg38"}, | |
| "tabs": { | |
| "variant_assessment": { | |
| "summary": { | |
| "ok": False, | |
| "status": 502, | |
| "data": {"variant_type": "SV", "genes": [], "upstream_error": "[Errno 8]"}, | |
| }, | |
| "region_viewer": { | |
| "region_data": { | |
| "ok": True, | |
| "status": 200, | |
| "data": {"region_data": {}}, | |
| } | |
| }, | |
| "population_frequencies": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| "clinical_evidence": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| "variant_details": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| }, | |
| "alternate_reference": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| }, | |
| "generated_at": datetime.now(timezone.utc).isoformat(), | |
| } | |
| async def fake_fetch(context, query): | |
| return failed_payload | |
| monkeypatch.setattr(franklin_service, "_fetch_structural_variant_assessment", fake_fetch) | |
| result = asyncio.run( | |
| franklin_service.get_franklin_variant_assessment( | |
| "DUP:Chr5:125423848-128107072", | |
| reference_version="hg38", | |
| ) | |
| ) | |
| assert result["cache"]["source"] == "franklin-live" | |
| with sqlite3.connect(cache_path) as connection: | |
| row = connection.execute("SELECT COUNT(*) FROM franklin_sv_cache").fetchone() | |
| assert row == (0,) | |
| def test_sv_cache_hit_returns_cached_payload_and_triggers_background_refresh(self, monkeypatch, tmp_path): | |
| cache_path = tmp_path / "franklin-cache.sqlite3" | |
| monkeypatch.setenv("FRANKLIN_CACHE_DB_PATH", str(cache_path)) | |
| stale_payload = self._cacheable_sv_payload() | |
| franklin_service._save_cached_sv_assessment( | |
| "chr5-125423848-128107072-DUP-hg38", | |
| "DUP:Chr5:125423848-128107072", | |
| "hg38", | |
| stale_payload, | |
| ) | |
| stale_time = (datetime.now(timezone.utc) - timedelta(days=8)).isoformat() | |
| with sqlite3.connect(cache_path) as connection: | |
| connection.execute( | |
| "UPDATE franklin_sv_cache SET fetched_at = ?, updated_at = ? WHERE cache_key = ?", | |
| (stale_time, stale_time, "chr5-125423848-128107072-DUP-hg38"), | |
| ) | |
| connection.commit() | |
| refresh_calls = [] | |
| def fake_refresh(cache_key, query, reference_version): | |
| refresh_calls.append((cache_key, query, reference_version)) | |
| monkeypatch.setattr(franklin_service, "_start_sv_cache_refresh", fake_refresh) | |
| result = asyncio.run( | |
| franklin_service.get_franklin_variant_assessment( | |
| "chr5-125423848-128107072-DUP-hg38", | |
| reference_version="hg19", | |
| ) | |
| ) | |
| assert result["cache"]["source"] == "local-cache" | |
| assert result["cache"]["stale"] is True | |
| assert refresh_calls == [ | |
| ( | |
| "chr5-125423848-128107072-DUP-hg38", | |
| "chr5-125423848-128107072-DUP-hg38", | |
| "hg38", | |
| ) | |
| ] | |
| def test_bad_cached_payload_is_discarded_before_re_fetch(self, monkeypatch, tmp_path): | |
| cache_path = tmp_path / "franklin-cache.sqlite3" | |
| monkeypatch.setenv("FRANKLIN_CACHE_DB_PATH", str(cache_path)) | |
| failed_payload = { | |
| "variant": {"key": "chr5-125423848-128107072-DUP-hg38"}, | |
| "tabs": { | |
| "variant_assessment": { | |
| "summary": {"ok": False, "status": 502, "data": {"variant_type": "SV", "genes": []}}, | |
| "region_viewer": {"region_data": {"ok": True, "status": 200, "data": {"region_data": {}}}}, | |
| "population_frequencies": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| "clinical_evidence": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| "variant_details": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| }, | |
| "alternate_reference": {"ok": False, "status": 502, "error": "[Errno 8]"}, | |
| }, | |
| "generated_at": datetime.now(timezone.utc).isoformat(), | |
| } | |
| franklin_service._save_cached_sv_assessment( | |
| "chr5-125423848-128107072-DUP-hg38", | |
| "DUP:Chr5:125423848-128107072", | |
| "hg38", | |
| failed_payload, | |
| ) | |
| fetched_payload = self._cacheable_sv_payload() | |
| fetch_calls = [] | |
| async def fake_fetch(context, query): | |
| fetch_calls.append((context.variant_key, query)) | |
| return fetched_payload | |
| monkeypatch.setattr(franklin_service, "_fetch_structural_variant_assessment", fake_fetch) | |
| result = asyncio.run( | |
| franklin_service.get_franklin_variant_assessment( | |
| "DUP:Chr5:125423848-128107072", | |
| reference_version="hg38", | |
| ) | |
| ) | |
| assert result["cache"]["source"] == "franklin-live" | |
| assert fetch_calls == [("chr5-125423848-128107072-DUP-hg38", "DUP:Chr5:125423848-128107072")] | |