-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_models.py
More file actions
117 lines (94 loc) · 4.12 KB
/
test_api_models.py
File metadata and controls
117 lines (94 loc) · 4.12 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
"""Tests for Pydantic models in code/api/models.py."""
from datetime import datetime, timezone
import pytest
from models import HealthScore, PackageDetail, PackageSummary, SentimentDetail
VALID_SCORE_DATA = dict(
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=datetime(2026, 3, 26, 12, 0, 0, tzinfo=timezone.utc),
)
VALID_SENTIMENT_DATA = dict(
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,
)
class TestPackageSummary:
def test_valid_creation(self):
s = PackageSummary(package_name="pandas", overall_health_score=72.5, health_tier="A")
assert s.package_name == "pandas"
assert s.overall_health_score == 72.5
assert s.health_tier == "A"
def test_score_is_float(self):
s = PackageSummary(package_name="flask", overall_health_score=55, health_tier="B")
assert isinstance(s.overall_health_score, float)
def test_missing_package_name_raises(self):
with pytest.raises(Exception):
PackageSummary(overall_health_score=70.0, health_tier="A")
class TestHealthScore:
def test_valid_creation(self):
hs = HealthScore(**VALID_SCORE_DATA)
assert hs.package_name == "requests"
assert hs.health_tier == "A"
assert hs.overall_health_score == pytest.approx(82.25)
def test_scored_at_preserves_timezone(self):
hs = HealthScore(**VALID_SCORE_DATA)
assert hs.scored_at.tzinfo is not None
def test_all_score_fields_present(self):
hs = HealthScore(**VALID_SCORE_DATA)
for field in ("github_score", "pypi_score", "community_score", "sentiment_score"):
assert getattr(hs, field) is not None
def test_missing_required_field_raises(self):
data = {k: v for k, v in VALID_SCORE_DATA.items() if k != "github_score"}
with pytest.raises(Exception):
HealthScore(**data)
class TestSentimentDetail:
def test_valid_creation_all_fields(self):
sd = SentimentDetail(**VALID_SENTIMENT_DATA)
assert sd.package_name == "requests"
assert sd.overall_sentiment == pytest.approx(0.38)
def test_all_fields_optional_except_name(self):
sd = SentimentDetail(package_name="torch")
assert sd.so_question_sentiment_avg is None
assert sd.so_answer_sentiment_avg is None
assert sd.readme_sentiment_compound is None
assert sd.pypi_desc_sentiment_compound is None
assert sd.overall_sentiment is None
def test_partial_fields(self):
sd = SentimentDetail(package_name="numpy", overall_sentiment=0.15)
assert sd.overall_sentiment == pytest.approx(0.15)
assert sd.readme_sentiment_compound is None
def test_sentiment_range_is_not_enforced(self):
sd = SentimentDetail(package_name="django", overall_sentiment=1.5)
assert sd.overall_sentiment == pytest.approx(1.5)
class TestPackageDetail:
def test_valid_nested_creation(self):
pd_model = PackageDetail(
scores=HealthScore(**VALID_SCORE_DATA),
sentiment=SentimentDetail(**VALID_SENTIMENT_DATA),
)
assert pd_model.scores.package_name == "requests"
assert pd_model.sentiment.overall_sentiment == pytest.approx(0.38)
def test_missing_scores_raises(self):
with pytest.raises(Exception):
PackageDetail(sentiment=SentimentDetail(package_name="x"))
def test_missing_sentiment_raises(self):
with pytest.raises(Exception):
PackageDetail(scores=HealthScore(**VALID_SCORE_DATA))
def test_json_round_trip(self):
original = PackageDetail(
scores=HealthScore(**VALID_SCORE_DATA),
sentiment=SentimentDetail(**VALID_SENTIMENT_DATA),
)
as_dict = original.model_dump()
restored = PackageDetail(**as_dict)
assert restored.scores.overall_health_score == pytest.approx(
original.scores.overall_health_score
)