Skip to content

Commit fcb89a2

Browse files
committed
test(demography-service): 🚨 add API tests
1 parent 4d7ae82 commit fcb89a2

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

api-tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# FIXME https
44
PEOPLE_URL = "http://localhost:51313/api/v1"
5+
DEMOGRAPHY_URL = "http://localhost:51312/api/v1"
56

67
def pytest_configure(config):
78
pytest.PEOPLE_URL = PEOPLE_URL
9+
pytest.DEMOGRAPHY_URL = DEMOGRAPHY_URL

api-tests/test_demography_api.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import pytest
2+
import requests
3+
from models import ErrorResponse, HairColor, EyeColor, Country, PersonInput
4+
5+
DEMOGRAPHY_URL = pytest.DEMOGRAPHY_URL
6+
PEOPLE_URL = pytest.PEOPLE_URL
7+
8+
def assert_error(response, status_code: int):
9+
assert response.status_code == status_code
10+
err = ErrorResponse.model_validate(response.json())
11+
assert err.code == status_code
12+
13+
@pytest.fixture(scope="module")
14+
def demography_test_data():
15+
requests.delete(f"{PEOPLE_URL}/people/nationality/NORTH_KOREA", verify=False)
16+
17+
people = []
18+
for data in [
19+
{"name": "Demography_Blue_Eyes", "eyeColor": "BLUE", "hairColor": "BROWN"},
20+
{"name": "Demography_Red_Eyes", "eyeColor": "RED", "hairColor": "GREEN"},
21+
{"name": "Demography_No_Hair", "eyeColor": "YELLOW", "hairColor": None},
22+
]:
23+
payload = {
24+
"name": data["name"],
25+
"coordinates": {"x": 0, "y": 0},
26+
"eyeColor": data["eyeColor"],
27+
"hairColor": data["hairColor"],
28+
"nationality": "NORTH_KOREA",
29+
"location": {"x": 0, "y": 0, "z": 0}
30+
}
31+
resp = requests.post(f"{PEOPLE_URL}/people", json=payload, verify=False)
32+
assert resp.status_code == 201
33+
people.append(resp.json())
34+
35+
yield people
36+
37+
requests.delete(f"{PEOPLE_URL}/people/nationality/NORTH_KOREA", verify=False)
38+
39+
def test_hair_color_percentage_valid(demography_test_data):
40+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/hair-color/BROWN/percentage", verify=False)
41+
assert resp.status_code == 200
42+
percentage = resp.json()
43+
assert isinstance(percentage, float)
44+
assert 0.0 <= percentage <= 100.0
45+
assert 33.0 <= percentage <= 34.0
46+
47+
def test_eye_color_count_valid(demography_test_data):
48+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/eye-color/BLUE", verify=False)
49+
assert resp.status_code == 200
50+
assert resp.json() == 1
51+
52+
def test_hair_color_percentage_zero():
53+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/hair-color/ORANGE/percentage", verify=False)
54+
assert resp.status_code == 200
55+
assert resp.json() == 0.0
56+
57+
def test_hair_color_percentage_invalid_enum():
58+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/hair-color/PURPLE/percentage", verify=False)
59+
assert_error(resp, 400)
60+
61+
def test_eye_color_count_zero():
62+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/eye-color/ORANGE", verify=False)
63+
assert resp.status_code == 200
64+
assert resp.json() == 0.0
65+
66+
def test_eye_color_count_invalid_enum():
67+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/eye-color/PURPLE", verify=False)
68+
assert_error(resp, 400)
69+
70+
def test_demography_consistency():
71+
resp = requests.get(f"{PEOPLE_URL}/people?pageSize=1000", verify=False)
72+
people = [p for p in resp.json()["people"] if p.get("nationality") == "NORTH_KOREA"]
73+
74+
blue_count = sum(1 for p in people if p["eyeColor"] == "BLUE")
75+
brown_count = sum(1 for p in people if p.get("hairColor") == "BROWN")
76+
total = len(people)
77+
78+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/eye-color/BLUE", verify=False)
79+
assert resp.status_code == 200
80+
assert resp.json() == blue_count
81+
82+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/hair-color/BROWN/percentage", verify=False)
83+
assert resp.status_code == 200
84+
expected_pct = (brown_count / total) * 100
85+
actual_pct = resp.json()
86+
assert abs(actual_pct - expected_pct) < 0.01
87+
88+
def test_demography_404_when_collection_empty():
89+
for nat in ["CHINA", "INDIA", "ITALY", "NORTH_KOREA"]:
90+
requests.delete(f"{PEOPLE_URL}/people/nationality/{nat}", verify=False)
91+
92+
resp = requests.get(f"{PEOPLE_URL}/people?pageSize=1000", verify=False)
93+
assert resp.status_code == 200
94+
assert len(resp.json()["people"]) == 0
95+
96+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/eye-color/BLUE", verify=False)
97+
assert_error(resp, 404)
98+
99+
resp = requests.get(f"{DEMOGRAPHY_URL}/demography/hair-color/BROWN/percentage", verify=False)
100+
assert_error(resp, 404)

0 commit comments

Comments
 (0)