-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_endpoints.py
More file actions
198 lines (158 loc) · 5.95 KB
/
test_api_endpoints.py
File metadata and controls
198 lines (158 loc) · 5.95 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""Tests for the FastAPI endpoints. Databricks is stubbed out with mock data."""
from __future__ import annotations
import sys
import types
from contextlib import asynccontextmanager
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import MagicMock
import pandas as pd
import pytest
from fastapi.testclient import TestClient
_fake_db = types.ModuleType("db")
_fake_db.get_spark = MagicMock()
sys.modules.setdefault("db", _fake_db)
MOCK_SCORES = pd.DataFrame(
[
{
"package_name": "requests",
"github_score": 85.0,
"pypi_score": 90.0,
"community_score": 80.0,
"sentiment_score": 70.0,
"overall_health_score": 82.25,
"health_tier": "A",
"scored_at": pd.Timestamp("2026-03-26 12:00:00"),
},
{
"package_name": "flask",
"github_score": 60.0,
"pypi_score": 65.0,
"community_score": 55.0,
"sentiment_score": 50.0,
"overall_health_score": 58.25,
"health_tier": "B",
"scored_at": pd.Timestamp("2026-03-26 12:00:00"),
},
]
)
MOCK_SENTIMENT = pd.DataFrame(
[
{
"package_name": "requests",
"so_question_sentiment_avg": 0.4,
"so_answer_sentiment_avg": 0.5,
"readme_sentiment_compound": 0.3,
"pypi_desc_sentiment_compound": 0.2,
"overall_sentiment": 0.38,
},
{
"package_name": "flask",
"so_question_sentiment_avg": 0.2,
"so_answer_sentiment_avg": 0.3,
"readme_sentiment_compound": 0.1,
"pypi_desc_sentiment_compound": 0.15,
"overall_sentiment": 0.19,
},
]
)
@pytest.fixture(scope="module")
def client():
import main as m
m._scores_df = MOCK_SCORES
m._sentiment_df = MOCK_SENTIMENT
@asynccontextmanager
async def _noop_lifespan(app):
yield
m.app.router.lifespan_context = _noop_lifespan
with TestClient(m.app) as c:
yield c
class TestHealth:
def test_returns_200(self, client):
r = client.get("/health")
assert r.status_code == 200
def test_status_is_ok(self, client):
r = client.get("/health")
assert r.json()["status"] == "ok"
def test_packages_cached_count(self, client):
r = client.get("/health")
assert r.json()["packages_cached"] == 2
def test_timestamp_present(self, client):
r = client.get("/health")
assert "timestamp" in r.json()
class TestListPackages:
def test_returns_all_packages(self, client):
r = client.get("/packages")
assert r.status_code == 200
assert len(r.json()) == 2
def test_sorted_descending_by_score(self, client):
r = client.get("/packages")
scores = [p["overall_health_score"] for p in r.json()]
assert scores == sorted(scores, reverse=True)
def test_each_item_has_required_fields(self, client):
r = client.get("/packages")
for item in r.json():
assert "package_name" in item
assert "overall_health_score" in item
assert "health_tier" in item
class TestGetPackage:
def test_known_package_returns_200(self, client):
r = client.get("/packages/requests")
assert r.status_code == 200
def test_response_has_scores_and_sentiment(self, client):
r = client.get("/packages/requests")
body = r.json()
assert "scores" in body
assert "sentiment" in body
def test_scores_sub_schema(self, client):
r = client.get("/packages/requests")
scores = r.json()["scores"]
for field in ("github_score", "pypi_score", "community_score",
"sentiment_score", "overall_health_score", "health_tier"):
assert field in scores
def test_correct_tier_returned(self, client):
r = client.get("/packages/requests")
assert r.json()["scores"]["health_tier"] == "A"
def test_unknown_package_returns_404(self, client):
r = client.get("/packages/nonexistent_xyz_pkg")
assert r.status_code == 404
def test_404_detail_message(self, client):
r = client.get("/packages/nonexistent_xyz_pkg")
assert "not found" in r.json()["detail"].lower()
class TestGetScores:
def test_returns_200(self, client):
r = client.get("/packages/flask/scores")
assert r.status_code == 200
def test_all_score_fields_are_floats(self, client):
r = client.get("/packages/flask/scores")
body = r.json()
for field in ("github_score", "pypi_score", "community_score",
"sentiment_score", "overall_health_score"):
assert isinstance(body[field], float)
def test_unknown_returns_404(self, client):
r = client.get("/packages/unknown_abc/scores")
assert r.status_code == 404
class TestGetSentiment:
def test_returns_200(self, client):
r = client.get("/packages/requests/sentiment")
assert r.status_code == 200
def test_overall_sentiment_present(self, client):
r = client.get("/packages/requests/sentiment")
assert "overall_sentiment" in r.json()
def test_package_name_in_response(self, client):
r = client.get("/packages/flask/sentiment")
assert r.json()["package_name"] == "flask"
class TestLeaderboard:
def test_returns_all_packages(self, client):
r = client.get("/scores/leaderboard")
assert r.status_code == 200
assert len(r.json()) == 2
def test_sorted_descending(self, client):
r = client.get("/scores/leaderboard")
scores = [p["overall_health_score"] for p in r.json()]
assert scores == sorted(scores, reverse=True)
def test_items_have_full_score_schema(self, client):
r = client.get("/scores/leaderboard")
for item in r.json():
assert "health_tier" in item
assert "scored_at" in item