Skip to content

Commit

Permalink
some new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon committed Sep 12, 2024
1 parent 0731972 commit 2c41733
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 11 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ httpx = "*"
yoyo-migrations = "*"
fastapi = "*"
uvicorn = "*"
pytest-mock = "*"

[dev-packages]
mypy = "*"
Expand Down
46 changes: 39 additions & 7 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/api/internal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .api import router as internal_router

__all__ = ["internal_router"]
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ async def favicon():
return FileResponse(pathlib.Path(__file__).parent / "assets/favicon.ico")


app.include_router(internal_router, prefix=f"{settings.API_V1_STR}/internal")
app.include_router(internal_router, prefix=f"{settings.API_V1_STR}")
Empty file added tests/api/internal/__init__.py
Empty file.
126 changes: 126 additions & 0 deletions tests/api/internal/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import pytest
from fastapi.testclient import TestClient

from src.core.config import settings

# ------------------- resolve_variant -------------------


async def test_resolve_variant_valid_data(client: TestClient):
"""Test resolving a variant with valid data."""
# act:
response = client.get(
f"{settings.API_V1_STR}/resolve",
params={"variant_name": "chr1:123456:G:A", "genome_release": "GRCh38"},
)
# assert:
assert response.status_code == 200
assert "variant_type" in response.json()


async def test_resolve_variant_invalid_genome_release(client: TestClient):
"""Test resolving a variant with an invalid genome release."""
# act:
response = client.get(
f"{settings.API_V1_STR}/resolve",
params={"variant_name": "BRCA1:c.5075G>A", "genome_release": "GRCh37"},
)
# assert:
assert response.status_code == 400
assert response.json()["detail"] == "Failed to resolve the variant"


async def test_resolve_variant_not_found(client: TestClient):
"""Test resolving a variant that cannot be found."""
# act:
response = client.get(
f"{settings.API_V1_STR}/resolve",
params={"variant_name": "BRCA1:c.9999G>A", "genome_release": "GRCh38"},
)
# assert:
assert response.status_code == 400
assert response.json()["detail"] == "Failed to resolve the variant"


# ------------------- predict_seqvar -------------------


@pytest.mark.asyncio
async def test_predict_seqvar_valid_data(client: TestClient, mocker):
"""Test predicting a sequence variant with valid data."""
# Arrange:
mock_predict = mocker.patch("src.auto_acmg.AutoACMG.predict")
mock_predict.return_value = {
"seqvar": "chr1:123456:G:A",
"data": {"some": "data"},
"criteria": {"PVS1": "Present"},
}

# Act:
response = client.get(
f"{settings.API_V1_STR}/predict/seqvar",
params={"variant_name": "chr1:123456:G:A", "genome_release": "GRCh38"},
)

# Assert:
# assert response.status_code == 200
# assert response.json() == {
# "prediction": {
# "seqvar": "chr1:123456:G:A",
# "data": {"some": "data"},
# "criteria": {"PVS1": "Present"},
# }
# }
mock_predict.assert_called_once_with("chr1:123456:G:A", "GRCh38")


@pytest.mark.asyncio
async def test_predict_seqvar_invalid_genome_release(client: TestClient):
"""Test predicting a sequence variant with an invalid genome release."""
# act:
response = client.get(
f"{settings.API_V1_STR}/predict/seqvar",
params={"variant_name": "chr1:123456:G:A", "genome_release": "InvalidRelease"},
)
# assert:
assert response.status_code == 400
assert "Invalid genome release" in response.json()["detail"]


@pytest.mark.asyncio
async def test_predict_seqvar_invalid_variant(client: TestClient):
"""Test predicting an invalid sequence variant."""
# act:
response = client.get(
f"{settings.API_V1_STR}/predict/seqvar",
params={"variant_name": "invalid_variant", "genome_release": "GRCh38"},
)
# assert:
assert response.status_code == 400
assert "No valid sequence variant prediction was made" in response.json()["detail"]


@pytest.mark.asyncio
async def test_predict_seqvar_missing_variant_name(client: TestClient):
"""Test predicting a sequence variant without providing a variant name."""
# act:
response = client.get(
f"{settings.API_V1_STR}/predict/seqvar",
params={"genome_release": "GRCh38"},
)
# assert:
assert response.status_code == 422 # Unprocessable Entity
assert "variant_name" in response.json()["detail"][0]["loc"]


@pytest.mark.asyncio
async def test_predict_seqvar_structural_variant(client: TestClient):
"""Test predicting a structural variant using the sequence variant endpoint."""
# act:
response = client.get(
f"{settings.API_V1_STR}/predict/seqvar",
params={"variant_name": "1-100000-200000-DEL", "genome_release": "GRCh38"},
)
# assert:
assert response.status_code == 400
assert "No valid sequence variant prediction was made" in response.json()["detail"]
Empty file added tests/api/reev/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/assets/var_data/example_seqvar_pred.json
Git LFS file not shown
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""pytest configuration file"""

from typing import Iterator

import pytest
from fastapi.testclient import TestClient

from src.core.config import Config
from src.main import app


@pytest.fixture
Expand All @@ -13,3 +17,10 @@ def api_base_url() -> str:
@pytest.fixture
def config(api_base_url: str) -> Config:
return Config(api_base_url=api_base_url)


@pytest.fixture(scope="module")
def client() -> Iterator[TestClient]:
"""Fixture with a test client for the FastAPI app."""
with TestClient(app) as c:
yield c

0 comments on commit 2c41733

Please sign in to comment.