File size: 3,658 Bytes
af567cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, AsyncMock
from app.main import app

client = TestClient(app)


class TestHealthEndpoint:
    def test_root(self):
        response = client.get("/")
        assert response.status_code == 200
        assert response.json()["message"] == "OpenVar API"

    def test_health(self):
        response = client.get("/health")
        assert response.status_code == 200
        assert response.json()["status"] == "healthy"


class TestGenesAPI:
    def test_list_genes(self):
        response = client.get("/api/genes")
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)
        assert len(data) > 0
        assert data[0]["symbol"] == "BRCA1"

    def test_list_genes_with_query(self):
        response = client.get("/api/genes?query=BRCA")
        assert response.status_code == 200
        data = response.json()
        assert all("BRCA" in gene["symbol"] for gene in data)

    def test_list_genes_limit_validation(self):
        response = client.get("/api/genes?limit=1000")
        assert response.status_code == 422

    def test_get_gene(self):
        response = client.get("/api/genes/BRCA1")
        assert response.status_code == 200
        data = response.json()
        assert data["symbol"] == "BRCA1"

    def test_get_gene_not_found(self):
        response = client.get("/api/genes/UNKNOWN")
        assert response.status_code == 404


class TestAuthAPI:
    def test_login_success(self):
        response = client.post(
            "/api/auth/login",
            json={"email": "demo@openvar.io", "password": "demo"}
        )
        assert response.status_code == 200
        data = response.json()
        assert "access_token" in data
        assert data["user"]["email"] == "demo@openvar.io"

    def test_login_invalid_credentials(self):
        response = client.post(
            "/api/auth/login",
            json={"email": "wrong@example.com", "password": "wrong"}
        )
        assert response.status_code == 401

    def test_get_current_user(self):
        response = client.get("/api/auth/me")
        assert response.status_code == 200
        data = response.json()
        assert "email" in data


class TestVariantAPI:
    @patch("app.services.variant_service.annotate_variant")
    def test_search_variant_coordinates(self, mock_annotate):
        mock_annotate.return_value = {
            "variant": {
                "id": "9-135800978-A-G",
                "chromosome": "9",
                "position": 135800978,
                "ref": "A",
                "alt": "G",
                "gene": "PTPRD"
            },
            "acmg": {"classification": "Uncertain significance"}
        }
        
        response = client.post(
            "/api/variant/search",
            json={"query": "chr9-135800978-A-G", "genome_build": "hg38"}
        )
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)

    def test_search_structural_variant(self):
        response = client.post(
            "/api/variant/search",
            json={"query": "DUP:Chr5:125423848-128107072", "genome_build": "hg38"}
        )
        assert response.status_code == 200
        data = response.json()
        assert len(data) == 1
        assert data[0]["variant"]["id"] == "chr5-125423848-128107072-DUP-hg38"
        assert data[0]["consequences"]["sv_type"] == "DUP"

    def test_get_variant_not_found(self):
        response = client.get("/api/variant/invalid-variant-id")
        assert response.status_code == 404