product_attribute / tests /test_api.py
kshitiz14's picture
Fix Dockerfile for HF Spaces (port 7860), stop tracking model artifact
896a559
Raw History Blame Contribute Delete
1.87 kB
"""
Basic tests for the extraction pipeline and API.
Run with: pytest tests/ -v (from repo root, with src/ on PYTHONPATH)
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from fastapi.testclient import TestClient
from api import app
from rules_extractor import extract_attributes_rules
client = TestClient(app)
def test_health():
r = client.get("/health")
assert r.status_code == 200
assert r.json()["status"] == "ok"
def test_extract_basic():
r = client.post("/extract", json={
"text": "Lace mermaid wedding dress with long sleeves and scalloped hem"
})
assert r.status_code == 200
body = r.json()
attrs = body["attributes"]
assert "mermaid" in attrs["silhouette"]
assert "lace" in attrs["fabric"]
assert "long sleeves" in attrs["sleeve"]
assert "wedding dress" in attrs["category"]
def test_extract_empty_text_rejected():
r = client.post("/extract", json={"text": ""})
assert r.status_code == 422
def test_extract_invalid_mode():
r = client.post("/extract", json={"text": "a dress", "mode": "bogus"})
assert r.status_code == 400
def test_extract_multi_color():
r = client.post("/extract", json={
"text": "Floor length chiffon bridesmaid dress with pleated bodice and V neckline available in sage and dusty blue"
})
attrs = r.json()["attributes"]
assert set(["sage", "dusty blue"]).issubset(set(attrs["color"]))
assert attrs["length"] == ["floor length"]
def test_rules_extractor_directly():
result = extract_attributes_rules(
"Velvet winter formal dress with square neckline and puff sleeves"
)
assert result["fabric"] == ["velvet"]
assert result["neckline"] == ["square neckline"]
assert result["sleeve"] == ["puff sleeves"]
assert result["category"] == ["formal dress"]