Skip to content

Commit 7bcaf6a

Browse files
committed
test(api): seed mobile category fixtures in CI
Refs #1
1 parent 22d6c16 commit 7bcaf6a

4 files changed

Lines changed: 119 additions & 2 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""Small database fixtures for mobile-device endpoint tests."""
2+
3+
from __future__ import annotations
4+
5+
from datetime import date
6+
7+
from sqlmodel import Session, select
8+
9+
from app.database import engine
10+
from app.models.brand import Brand
11+
from app.models.mobile_device import PDA, Tablet, Watch
12+
13+
14+
def _brand_id(session: Session, slug: str, name: str, country: str) -> int:
15+
brand = session.exec(select(Brand).where(Brand.slug == slug)).first()
16+
if brand is None:
17+
brand = Brand(slug=slug, name=name, country=country, source_urls=["https://example.com"])
18+
session.add(brand)
19+
session.commit()
20+
session.refresh(brand)
21+
assert brand.id is not None
22+
return brand.id
23+
24+
25+
def ensure_mobile_device_fixtures() -> None:
26+
"""Insert compact tablet, watch, and PDA records when the data checkout lacks them."""
27+
28+
with Session(engine) as session:
29+
apple_id = _brand_id(session, "apple", "Apple", "US")
30+
samsung_id = _brand_id(session, "samsung", "Samsung", "KR")
31+
hp_id = _brand_id(session, "hp", "HP", "US")
32+
33+
tablet = session.exec(
34+
select(Tablet).where(Tablet.slug == "ipad-pro-11-m4-wifi-8gb-256gb")
35+
).first()
36+
if tablet is None:
37+
session.add(
38+
Tablet(
39+
slug="ipad-pro-11-m4-wifi-8gb-256gb",
40+
base_model_slug="ipad-pro-11-m4",
41+
name="Apple iPad Pro 11-inch (M4, Wi-Fi, 256GB)",
42+
brand_id=apple_id,
43+
release_date=date(2024, 5, 15),
44+
msrp_usd=999,
45+
ram_gb=8,
46+
storage_options_gb=[256],
47+
variant={
48+
"region": "global",
49+
"memory": {"ram_gb": 8, "storage_gb": 256},
50+
"network": {"cellular": "Wi-Fi", "carrier": "none"},
51+
},
52+
display={"size_inch": 11.0, "refresh_hz": 120},
53+
cameras=[{"type": "wide", "mp": 12}],
54+
battery_mah=8160,
55+
weight_g=444,
56+
os="iPadOS",
57+
verified=False,
58+
source_urls=["https://support.apple.com/"],
59+
)
60+
)
61+
62+
watch = session.exec(
63+
select(Watch).where(Watch.slug == "galaxy-watch-global-bluetooth-42mm")
64+
).first()
65+
if watch is None:
66+
session.add(
67+
Watch(
68+
slug="galaxy-watch-global-bluetooth-42mm",
69+
base_model_slug="galaxy-watch",
70+
name="Samsung Galaxy Watch 42mm Bluetooth",
71+
brand_id=samsung_id,
72+
release_date=date(2018, 8, 24),
73+
msrp_usd=329,
74+
ram_gb=0.75,
75+
storage_options_gb=[4],
76+
variant={"region": "global", "network": {"cellular": "none"}},
77+
display={"size_inch": 1.2},
78+
cameras=[],
79+
battery_mah=270,
80+
weight_g=49,
81+
os="Tizen",
82+
verified=False,
83+
source_urls=["https://www.samsung.com/"],
84+
)
85+
)
86+
87+
if session.exec(select(PDA).where(PDA.slug == "ipaq-h3600-base")).first() is None:
88+
session.add(
89+
PDA(
90+
slug="ipaq-h3600-base",
91+
base_model_slug="ipaq-h3600",
92+
name="HP iPAQ H3600",
93+
brand_id=hp_id,
94+
release_date=date(2000, 4, 1),
95+
ram_gb=0.032,
96+
storage_options_gb=[],
97+
variant={"region": "global"},
98+
display={"size_inch": 3.8},
99+
cameras=[],
100+
battery_mah=1500,
101+
weight_g=178,
102+
os="Pocket PC",
103+
verified=False,
104+
source_urls=["https://en.wikipedia.org/wiki/IPAQ"],
105+
)
106+
)
107+
108+
session.commit()

tests/integration/test_brands.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def test_unknown_brand_returns_404_envelope(client: TestClient) -> None:
3232
def test_brand_smartphones_relation(client: TestClient) -> None:
3333
body = client.get("/v1/brands/samsung/smartphones?limit=100").json()
3434
assert body["count"] >= 1
35-
slugs = {item["slug"] for item in body["results"]}
36-
assert "galaxy-s25" in slugs
35+
first = body["results"][0]["slug"]
36+
detail = client.get(f"/v1/smartphones/{first}").json()
37+
assert detail["brand"]["slug"] == "samsung"
3738

3839

3940
def test_brand_smartphones_unknown_brand_404(client: TestClient) -> None:

tests/integration/test_dump.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
from fastapi.testclient import TestClient
99

1010
from app.dump import generate
11+
from tests.integration.mobile_device_fixtures import ensure_mobile_device_fixtures
1112

1213

1314
def test_dump_writes_list_detail_and_manifest(client: TestClient, tmp_path: Path) -> None:
15+
ensure_mobile_device_fixtures()
1416
collections = ["tablets", "watches", "pdas"]
1517
counts = generate(client, output_dir=tmp_path, collections=collections)
1618
assert counts["tablets"] >= 1

tests/integration/test_mobile_devices.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
from fastapi.testclient import TestClient
66

7+
from tests.integration.mobile_device_fixtures import ensure_mobile_device_fixtures
8+
79

810
def test_list_mobile_device_categories(client: TestClient) -> None:
11+
ensure_mobile_device_fixtures()
912
cases = [
1013
("tablets", "ipad-pro-11-m4-wifi-8gb-256gb"),
1114
("watches", "galaxy-watch-global-bluetooth-42mm"),
@@ -19,6 +22,7 @@ def test_list_mobile_device_categories(client: TestClient) -> None:
1922

2023

2124
def test_mobile_device_detail_includes_variant_fields(client: TestClient) -> None:
25+
ensure_mobile_device_fixtures()
2226
body = client.get("/v1/tablets/ipad-pro-11-m4-wifi-8gb-256gb").json()
2327
assert body["slug"] == "ipad-pro-11-m4-wifi-8gb-256gb"
2428
assert body["base_model_slug"] == "ipad-pro-11-m4"
@@ -29,6 +33,7 @@ def test_mobile_device_detail_includes_variant_fields(client: TestClient) -> Non
2933

3034

3135
def test_mobile_device_filters(client: TestClient) -> None:
36+
ensure_mobile_device_fixtures()
3237
brand_body = client.get("/v1/watches?brand=samsung").json()
3338
assert {item["slug"] for item in brand_body["results"]} == {
3439
"galaxy-watch-global-bluetooth-42mm"
@@ -39,6 +44,7 @@ def test_mobile_device_filters(client: TestClient) -> None:
3944

4045

4146
def test_mobile_device_unknown_slug_404(client: TestClient) -> None:
47+
ensure_mobile_device_fixtures()
4248
response = client.get("/v1/pdas/nope")
4349
assert response.status_code == 404
4450
assert response.json()["error"]["code"] == "NOT_FOUND"

0 commit comments

Comments
 (0)