Stelle den Frontend-MVP und die v0.3-API auf develop bereit, damit Dev API und PWA zusammen deployen.
Deploy Development / deploy (push) Successful in 1m19s
Test Suite / pytest-backend (push) Successful in 31s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-checks (push) Successful in 28s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-04 13:04:59 +02:00
1 parent 827e6a2a11
commit b276758ee1
152 files changed
+25846 -34

No files matched your search

View File
Whitespace-only changes.
+150
View File
@@ -0,0 +1,150 @@
import json
from datetime import datetime, timezone
from decimal import Decimal
from pathlib import Path
from uuid import UUID, uuid4
import pytest
from app.domain.enums import Availability, NormalizationStatus, OfferCondition, ValueType
from app.domain.models import (
AttributeDefinition,
AttributeStatement,
Offer,
PriceObservation,
Product,
ProductVariant,
)
from app.domain.validation import parse_profile, parse_scoring_model
ROOT = Path(__file__).resolve().parents[1]
AS_OF = datetime(2026, 10, 3, 12, 0, tzinfo=timezone.utc)
CUTOFF = datetime(2026, 10, 3, 12, 0, tzinfo=timezone.utc)
@pytest.fixture
def scoring_model():
definition = json.loads((ROOT / "rules" / "generic-v1.json").read_text(encoding="utf-8"))
return parse_scoring_model(definition)
@pytest.fixture
def definitions() -> dict[str, AttributeDefinition]:
return {
"ram_gb": AttributeDefinition("ram_gb", ValueType.NUMBER, "GB", Decimal("0"), None, None),
"storage_gb": AttributeDefinition("storage_gb", ValueType.NUMBER, "GB", Decimal("0"), None, None),
"weight_kg": AttributeDefinition("weight_kg", ValueType.NUMBER, "kg", Decimal("0"), None, None),
"battery_score": AttributeDefinition("battery_score", ValueType.NUMBER, None, Decimal("0"), Decimal("100"), None),
"display_score": AttributeDefinition("display_score", ValueType.NUMBER, None, Decimal("0"), Decimal("100"), None),
"cpu_score": AttributeDefinition("cpu_score", ValueType.NUMBER, None, Decimal("0"), Decimal("100"), None),
"ports": AttributeDefinition("ports", ValueType.STRING_LIST, None, None, None, None),
"color": AttributeDefinition("color", ValueType.STRING, None, None, None, None),
"has_lte": AttributeDefinition("has_lte", ValueType.BOOLEAN, None, None, None, None),
}
@pytest.fixture
def profile(scoring_model, definitions):
raw = json.loads((ROOT / "profiles" / "mobile-development-notebook.json").read_text(encoding="utf-8"))
return parse_profile(
raw,
attribute_definitions=definitions,
scoring_versions={scoring_model.version},
supported_strategies=set(scoring_model.supported_preference_strategies),
)
def utc(year, month, day, hour=10, minute=0) -> datetime:
return datetime(year, month, day, hour, minute, tzinfo=timezone.utc)
def statement(attribute: str, value, *, confidence="1.0", status="normalized", observed=None, ingested=None, row_id=None):
return AttributeStatement(
id=row_id or uuid4(),
variant_id=UUID("22222222-2222-4222-8222-222222222222"),
attribute=attribute,
raw_value=value,
normalized_value=None if status != "normalized" else value,
unit="GB" if attribute.endswith("_gb") else ("kg" if attribute.endswith("_kg") else None),
normalization_status=NormalizationStatus(status),
normalizer_version="manual-v1",
confidence=Decimal(str(confidence)),
source_id="manual",
source_reference=f"test/{attribute}",
observed_at=observed or utc(2026, 10, 3, 9),
ingested_at=ingested or utc(2026, 10, 3, 9, 1),
)
def product_a() -> Product:
return Product(
id=UUID("11111111-1111-4111-8111-111111111111"),
category="notebook",
brand="Example",
name="Example Mobile 13",
model="EM13",
created_at=utc(2026, 10, 1, 8),
)
def variant_a() -> ProductVariant:
return ProductVariant(
id=UUID("22222222-2222-4222-8222-222222222222"),
product_id=UUID("11111111-1111-4111-8111-111111111111"),
name="32/1024",
manufacturer_sku="EM13-32-1T",
identifiers={},
created_at=utc(2026, 10, 1, 8),
)
def offer_a() -> Offer:
return Offer(
id=UUID("33333333-3333-4333-8333-333333333333"),
variant_id=UUID("22222222-2222-4222-8222-222222222222"),
source_id="manual",
external_id="demo-offer-a",
seller="DemoShop",
url=None,
market="DE",
currency="EUR",
condition=OfferCondition.NEW,
created_at=utc(2026, 10, 1, 8),
)
def observation(day, total, *, offer_id=None, obs_id=None, availability="in_stock", confidence="1.0", hour=10):
return PriceObservation(
id=obs_id or uuid4(),
offer_id=offer_id or UUID("33333333-3333-4333-8333-333333333333"),
source_id="manual",
external_observation_id=str(obs_id or uuid4()),
observed_at=utc(2026, day[0], day[1], hour) if isinstance(day, tuple) else day,
ingested_at=utc(2026, 10, 3, 11),
item_price=Decimal(str(total)),
shipping_price=Decimal("0.00"),
currency="EUR",
availability=Availability(availability),
raw_payload={"total": str(total)},
confidence=Decimal(str(confidence)),
)
def case_a_attributes():
return (
statement("ram_gb", 32),
statement("storage_gb", 1024),
statement("weight_kg", Decimal("1.2")),
statement("battery_score", 90),
statement("display_score", 90),
statement("cpu_score", 90),
)
def case_a_prices():
return (
observation((9, 1), "1599.00"),
observation((9, 15), "1599.00"),
observation((10, 1), "1599.00"),
observation((10, 3), "1298.00"),
)
View File
Whitespace-only changes.
@@ -0,0 +1,199 @@
import json
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import text
from app.main import app
from app.persistence.db import SessionLocal, engine
from scripts.bootstrap import run_bootstrap
from scripts.seed_notebook_pilot import seed_notebook_pilot
pytestmark = [pytest.mark.e2e, pytest.mark.integration]
ROOT = Path(__file__).resolve().parents[2]
PROFILE = json.loads((ROOT / "profiles" / "mobile-development-notebook.json").read_text(encoding="utf-8"))
DEFINITIONS = [
{"key": "ram_gb", "value_type": "number", "canonical_unit": "GB", "minimum": 0},
{"key": "storage_gb", "value_type": "number", "canonical_unit": "GB", "minimum": 0},
{"key": "weight_kg", "value_type": "number", "canonical_unit": "kg", "minimum": 0},
{"key": "battery_score", "value_type": "number", "minimum": 0, "maximum": 100},
{"key": "display_score", "value_type": "number", "minimum": 0, "maximum": 100},
{"key": "cpu_score", "value_type": "number", "minimum": 0, "maximum": 100},
]
CRITERIA = {
"items": [
{"id": "ram", "criterion": "ram_gb", "operator": ">=", "value": 24, "mandatory": True},
{"id": "price", "criterion": "price_eur", "operator": "<=", "value": 2000, "mandatory": True},
{"id": "weight", "criterion": "weight_kg", "operator": "<=", "value": 1.6, "mandatory": True},
]
}
@pytest.fixture(scope="module", autouse=True)
def require_postgres() -> None:
try:
with engine.connect() as connection:
connection.execute(text("SELECT 1"))
except Exception as exc:
pytest.skip(f"PostgreSQL not available: {exc}")
session = SessionLocal()
try:
run_bootstrap(session)
seed_notebook_pilot(session)
session.commit()
finally:
session.close()
@pytest.fixture
def client() -> TestClient:
return TestClient(app)
def _ok(response, allowed=(200, 201, 409)) -> None:
assert response.status_code in allowed, response.text
def _seed_profile(client: TestClient) -> None:
for definition in DEFINITIONS:
_ok(client.post("/api/v1/attribute-definitions", json=definition))
_ok(client.post("/api/v1/profiles", json=PROFILE))
def test_notebook_discovery_pilot(client: TestClient) -> None:
_seed_profile(client)
taxonomy = client.post("/api/v1/sources/geizhals/categories/nb/discover-taxonomy")
assert taxonomy.status_code == 201, taxonomy.text
category_id = taxonomy.json()["id"]
filters = client.get("/api/v1/sources/geizhals/categories/nb/filters")
assert filters.status_code == 200
assert {item["external_filter_key"] for item in filters.json()["filters"]} >= {"17023", "price"}
ram = client.post(
"/api/v1/criterion-mappings",
json={
"criterion_key": "ram_gb",
"source_id": "geizhals",
"source_category_id": category_id,
"external_filter_key": "17023",
"source_value": "ab 24GB",
"encoding": {"url_param": "xf"},
},
)
assert ram.status_code == 201, ram.text
assert ram.json()["status"] == "VERIFIED"
price = client.post(
"/api/v1/criterion-mappings",
json={
"criterion_key": "price_eur",
"source_id": "geizhals",
"source_category_id": category_id,
"external_filter_key": "price",
"source_value": None,
"encoding": {"url_param": "maxp"},
"probe_value": 2000,
"operator": "<=",
},
)
assert price.status_code == 201, price.text
assert price.json()["status"] == "VERIFIED"
project = client.post(
"/api/v1/buying-projects",
json={
"name": "Notebook-Pilot",
"profile_id": "mobile-development-notebook",
"profile_version": 1,
"shortlist_size": 5,
"schedule_interval_minutes": 180,
"criteria": CRITERIA,
},
)
assert project.status_code == 201, project.text
project_id = project.json()["id"]
assert project.json()["workspace_id"] is None
assert project.json()["criteria_version"] == 1
available = client.get(f"/api/v1/buying-projects/{project_id}/available-criteria")
assert available.status_code == 200, available.text
assert available.json()["product_category_id"] == "notebook"
scoring_keys = {item.get("scoring_attribute_key") for item in available.json()["criteria"]}
assert {"ram_gb", "price_eur"} <= scoring_keys
ram = next(item for item in available.json()["criteria"] if item.get("scoring_attribute_key") == "ram_gb")
assert ram["mapping_status"] == "VERIFIED"
search = client.post(
"/api/v1/discovery-searches",
json={
"buying_project_id": project_id,
"source_id": "geizhals",
"source_category_id": category_id,
"broad_result_threshold": 300,
},
)
assert search.status_code == 201, search.text
search_id = search.json()["id"]
assert "xf=17023_ab+24GB" in search.json()["generated_url"]
assert "maxp=2000" in search.json()["generated_url"]
assert search.json()["schedule_interval_minutes"] == 180
due = client.get("/api/v1/discovery-searches?due=true")
assert due.status_code == 200
assert any(item["id"] == search_id for item in due.json()["items"])
run = client.post(f"/api/v1/discovery-searches/{search_id}/run")
assert run.status_code == 201, run.text
assert run.json()["status"] == "SUCCESS"
assert run.json()["page_count"] == 2
assert run.json()["result_count"] == 3
assert {item["diff_status"] for item in run.json()["items"]} == {"NEW"}
shortlist = client.get(f"/api/v1/buying-projects/{project_id}/shortlist")
assert shortlist.status_code == 200
assert len(shortlist.json()["items"]) == 1
assert shortlist.json()["items"][0]["product_eligibility"] == "ELIGIBLE"
assert shortlist.json()["items"][0]["status"] == "SHORTLISTED"
candidates = client.get(f"/api/v1/buying-projects/{project_id}/candidates")
assert candidates.status_code == 200
eligibilities = {item["product_eligibility"] for item in candidates.json()["items"]}
assert eligibilities == {"ELIGIBLE", "INELIGIBLE", "UNKNOWN"}
assert all(item.get("deal_score") is None for item in candidates.json()["items"])
updated = client.post(
f"/api/v1/buying-projects/{project_id}/criteria",
json={"items": CRITERIA["items"][:2]},
)
assert updated.status_code == 201
assert updated.json()["criteria_version"] == 2
stale = client.get(f"/api/v1/discovery-searches/{search_id}")
assert stale.json()["status"] == "OUT_OF_SYNC"
blocked_run = client.post(f"/api/v1/discovery-searches/{search_id}/run")
assert blocked_run.status_code == 409
refreshed = client.post(f"/api/v1/discovery-searches/{search_id}/generate-query")
assert refreshed.status_code == 200
assert refreshed.json()["status"] == "ACTIVE"
assert "weight_kg" not in refreshed.json().get("internal_only", [])
assert "weight_kg" not in refreshed.json()["generated_url"]
client.patch(f"/api/v1/discovery-searches/{search_id}", json={"status": "DISABLED"})
broad = client.post(
"/api/v1/discovery-searches",
json={
"buying_project_id": project_id,
"source_id": "geizhals",
"source_category_id": category_id,
"broad_result_threshold": 2,
},
)
assert broad.status_code == 201, broad.text
broad_run = client.post(f"/api/v1/discovery-searches/{broad.json()['id']}/run")
assert broad_run.status_code == 201, broad_run.text
assert broad_run.json()["status"] == "TOO_BROAD"
assert broad_run.json()["result_count"] == 3
stored = client.get(f"/api/v1/discovery-searches/{broad.json()['id']}")
assert stored.json()["status"] == "TOO_BROAD"
+204
View File
@@ -0,0 +1,204 @@
import json
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import text
from app.main import app
from app.persistence.db import SessionLocal, engine
from scripts.bootstrap import run_bootstrap
pytestmark = [pytest.mark.e2e, pytest.mark.integration]
ROOT = Path(__file__).resolve().parents[2]
PROFILE = json.loads((ROOT / "profiles" / "mobile-development-notebook.json").read_text(encoding="utf-8"))
FIXTURE = json.loads((ROOT / "fixtures" / "manual-demo.json").read_text(encoding="utf-8"))
AS_OF = "2026-10-03T12:00:00Z"
DEFINITIONS = [
{"key": "ram_gb", "value_type": "number", "canonical_unit": "GB", "minimum": 0},
{"key": "storage_gb", "value_type": "number", "canonical_unit": "GB", "minimum": 0},
{"key": "weight_kg", "value_type": "number", "canonical_unit": "kg", "minimum": 0},
{"key": "battery_score", "value_type": "number", "minimum": 0, "maximum": 100},
{"key": "display_score", "value_type": "number", "minimum": 0, "maximum": 100},
{"key": "cpu_score", "value_type": "number", "minimum": 0, "maximum": 100},
]
@pytest.fixture(scope="module", autouse=True)
def require_postgres() -> None:
try:
with engine.connect() as connection:
connection.execute(text("SELECT 1"))
except Exception as exc:
pytest.skip(f"PostgreSQL not available: {exc}")
session = SessionLocal()
try:
run_bootstrap(session)
finally:
session.close()
@pytest.fixture
def client() -> TestClient:
return TestClient(app)
def _ok(response, allowed=(200, 201, 409)) -> None:
assert response.status_code in allowed, response.text
def _seed(client: TestClient) -> None:
for definition in DEFINITIONS:
_ok(client.post("/api/v1/attribute-definitions", json=definition))
_ok(client.post("/api/v1/profiles", json=PROFILE))
product = FIXTURE["products"][0]
_ok(
client.post(
"/api/v1/products",
json={
"id": product["id"],
"category": product["category"],
"brand": product["brand"],
"name": product["name"],
"model": product.get("model"),
},
)
)
for variant in product["variants"]:
_ok(
client.post(
f"/api/v1/products/{product['id']}/variants",
json={
"id": variant["id"],
"name": variant["name"],
"manufacturer_sku": variant.get("manufacturer_sku"),
"identifiers": variant.get("identifiers") or {},
},
)
)
for attribute in variant["attributes"]:
_ok(client.post(f"/api/v1/variants/{variant['id']}/attributes", json={**attribute, "source_id": "manual"}))
for offer in FIXTURE["offers"]:
_ok(
client.post(
f"/api/v1/variants/{offer['variant_id']}/offers",
json={
"id": offer["id"],
"source_id": "manual",
"external_id": offer["external_id"],
"seller": offer["seller"],
"url": offer.get("url"),
"market": offer["market"],
"currency": offer["currency"],
"condition": offer["condition"],
},
)
)
for price in FIXTURE["prices"]:
offer = next(item for item in FIXTURE["offers"] if item["external_id"] == price["external_offer_id"])
_ok(
client.post(
f"/api/v1/offers/{offer['id']}/prices",
json={
"id": price["id"],
"source_id": "manual",
"external_observation_id": price["external_observation_id"],
"observed_at": price["observed_at"],
"item_price": price["item_price"],
"shipping_price": price["shipping_price"],
"currency": price["currency"],
"availability": price["availability"],
"raw_payload": price["raw_payload"],
"confidence": price["confidence"],
},
),
allowed=(200, 201),
)
def test_http_cases_and_replay(client: TestClient) -> None:
_seed(client)
cases = {
"A": ("22222222-2222-4222-8222-222222222222", "33333333-3333-4333-8333-333333333333"),
"B": ("22222222-2222-4222-8222-22222222222b", "33333333-3333-4333-8333-33333333333b"),
"C": ("22222222-2222-4222-8222-22222222222c", "33333333-3333-4333-8333-33333333333c"),
"D": ("22222222-2222-4222-8222-222222222222", "33333333-3333-4333-8333-33333333333d"),
}
created = {}
for label, (variant_id, offer_id) in cases.items():
response = client.post(
f"/api/v1/evaluate/{variant_id}",
json={
"profile_id": "mobile-development-notebook",
"profile_version": 1,
"offer_id": offer_id,
"as_of": AS_OF,
},
)
assert response.status_code == 201, response.text
created[label] = response.json()
assert created["A"]["product_score"] == 90.21
assert created["A"]["price_score"] == 98.24
assert created["A"]["deal_score"] == 93.42
assert created["A"]["classification"] == "EXCEPTIONAL"
assert created["A"]["eligible"] is True
assert created["A"]["price_reference_scope"] == "offer"
assert created["B"]["product_eligibility"] == "INELIGIBLE"
assert created["B"]["offer_eligibility"] == "ELIGIBLE"
assert created["B"]["deal_score"] is None
assert created["B"]["product_score"] is not None
assert created["C"]["product_eligibility"] == "UNKNOWN"
assert created["C"]["classification"] == "INSUFFICIENT_DATA"
assert created["D"]["product_eligibility"] == "ELIGIBLE"
assert created["D"]["offer_eligibility"] == "INELIGIBLE"
assert created["D"]["product_score"] == 90.21
assert created["D"]["price_score"] is not None
assert created["D"]["deal_score"] is None
stored = client.get(f"/api/v1/evaluations/{created['A']['id']}")
assert stored.status_code == 200
assert stored.json()["deal_score"] == created["A"]["deal_score"]
assert stored.json()["input_snapshot"] == created["A"]["input_snapshot"]
extra = client.post(
"/api/v1/offers/33333333-3333-4333-8333-333333333333/prices",
json={
"source_id": "manual",
"external_observation_id": "a-later",
"observed_at": "2026-10-03T11:00:00Z",
"item_price": "999.00",
"shipping_price": "0.00",
"currency": "EUR",
"availability": "in_stock",
"raw_payload": {"total": "999.00"},
"confidence": 1.0,
},
)
assert extra.status_code == 201
replay = client.get(f"/api/v1/evaluations/{created['A']['id']}")
assert replay.json()["deal_score"] == created["A"]["deal_score"]
assert replay.json()["price_statistics"]["current_total"] == created["A"]["price_statistics"]["current_total"]
def test_error_contracts(client: TestClient) -> None:
missing = client.get("/api/v1/products/00000000-0000-4000-8000-000000000000")
assert missing.status_code == 404
assert "error" in missing.json()
assert missing.json()["error"]["code"] == "NOT_FOUND"
bad = client.post("/api/v1/products", json={"category": "notebook"})
assert bad.status_code == 422
assert "error" in bad.json()
client.post("/api/v1/attribute-definitions", json={"key": "ports", "value_type": "string_list"})
again = client.post("/api/v1/attribute-definitions", json={"key": "ports", "value_type": "string_list"})
assert again.status_code == 409
no_version = client.get("/api/v1/profiles/mobile-development-notebook")
assert no_version.status_code == 422
Whitespace-only changes.
@@ -0,0 +1,249 @@
from copy import deepcopy
from decimal import Decimal
import pytest
from sqlalchemy import text
from app.discovery.enums import DiffStatus, LifecycleStatus, MatchStatus, RunStatus
from app.errors import AppError
from app.persistence.db import SessionLocal, engine
from app.persistence.discovery_models import (
ProductObservationRecord,
ProjectCandidateRecord,
SourceProductReferenceRecord,
)
from app.services.discovery_service import (
CriterionMappingService,
DiscoverySearchService,
SearchExecutionService,
TaxonomyService,
)
from app.services.ingestion_service import IngestionService
from app.services.project_service import ProjectService
from app.sources.geizhals.adapter import GeizhalsAdapter
from scripts.bootstrap import run_bootstrap
from scripts.seed_notebook_pilot import seed_notebook_pilot
from tests.e2e.test_http_walkthrough import DEFINITIONS, PROFILE
pytestmark = pytest.mark.integration
ITEM_A = {
"source_external_id": "gh-nb-a",
"source_url": "https://geizhals.de/example-mobile-13-a.html",
"source_title": "Example Mobile 13 32GB",
"manufacturer": "Example",
"model": "Mobile 13",
"manufacturer_sku": "EM13-32-1T",
"gtin_ean": "4000000000001",
"best_price": "1298.00",
"currency": "EUR",
"availability": "in_stock",
"seller_count": 8,
"raw_attributes": {"RAM": "32GB"},
"normalized_attributes": {"ram_gb": 32, "storage_gb": 1024, "weight_kg": 1.2},
}
def _item(suffix: str, **overrides) -> dict:
item = deepcopy(ITEM_A)
item["source_external_id"] = f"gh-nb-{suffix}"
item["source_url"] = f"https://geizhals.de/example-mobile-13-{suffix}.html"
item["manufacturer_sku"] = f"EM13-{suffix}"
item["gtin_ean"] = None
item["source_title"] = f"Example Mobile 13 {suffix}"
item.update(overrides)
return item
class ScriptedAdapter:
source_key = "geizhals"
def __init__(self, pages: list[dict]) -> None:
self.pages = pages
self._inner = GeizhalsAdapter()
def discover_taxonomy(self, category_ref: str) -> dict:
return self._inner.discover_taxonomy(category_ref)
def build_query(self, criteria, mappings) -> dict:
return self._inner.build_query(criteria, mappings)
def fetch_results(self, query, pagination) -> dict:
page = int(pagination.get("page") or 1)
return self.pages[page - 1]
def parse_results(self, response) -> list:
return list(response.get("items") or [])
def normalize_result(self, result) -> dict:
return self._inner.normalize_result(result)
@pytest.fixture(scope="module", autouse=True)
def require_postgres() -> None:
try:
with engine.connect() as connection:
connection.execute(text("SELECT 1"))
except Exception as exc:
pytest.skip(f"PostgreSQL not available: {exc}")
def _session():
session = SessionLocal()
run_bootstrap(session)
seed_notebook_pilot(session)
ingestion = IngestionService(session)
for definition in DEFINITIONS:
try:
ingestion.create_definition(definition)
except AppError as exc:
if exc.status_code != 409:
raise
try:
ingestion.create_profile(PROFILE)
except AppError as exc:
if exc.status_code != 409:
raise
return session
def _prepare_search(session, adapter, threshold=300):
taxonomy = TaxonomyService(session, adapter).discover("geizhals", "nb")
mappings = CriterionMappingService(session)
mappings.create(
{
"criterion_key": "ram_gb",
"source_id": "geizhals",
"source_category_id": taxonomy["id"],
"external_filter_key": "17023",
"source_value": "ab 24GB",
"encoding": {"url_param": "xf"},
}
)
project = ProjectService(session).create(
{
"name": "Lifecycle",
"profile_id": "mobile-development-notebook",
"profile_version": 1,
"criteria": {"items": [{"id": "ram", "criterion": "ram_gb", "operator": ">=", "value": 24}]},
}
)
search = DiscoverySearchService(session, adapter).create(
{
"buying_project_id": project["id"],
"source_id": "geizhals",
"source_category_id": taxonomy["id"],
"broad_result_threshold": threshold,
}
)
return search["id"]
def _page(items, status=200, page_count=1):
return {"page": 1, "page_count": page_count, "result_count": len(items), "http_status": status, "items": items}
def test_partial_does_not_mark_missing_or_disappeared():
session = _session()
try:
adapter = ScriptedAdapter([_page([_item("partial")])])
search_id = _prepare_search(session, adapter)
first = SearchExecutionService(session, adapter).execute(search_id)
assert first["status"] == RunStatus.SUCCESS.value
ref_id = first["items"][0]["source_product_reference_id"]
adapter.pages = [_page([], status=429)]
second = SearchExecutionService(session, adapter).execute(search_id)
assert second["status"] == RunStatus.PARTIAL.value
assert all(item["diff_status"] != DiffStatus.MISSING.value for item in second["items"])
ref = session.get(SourceProductReferenceRecord, ref_id)
assert ref is not None
assert ref.lifecycle_status == LifecycleStatus.ACTIVE.value
assert ref.consecutive_missing_success_runs == 0
finally:
session.close()
def test_three_success_misses_disappear_and_reappear():
session = _session()
try:
item = _item("gone")
adapter = ScriptedAdapter([_page([item])])
search_id = _prepare_search(session, adapter)
first = SearchExecutionService(session, adapter).execute(search_id)
ref_id = first["items"][0]["source_product_reference_id"]
adapter.pages = [_page([])]
for _ in range(3):
SearchExecutionService(session, adapter).execute(search_id)
ref = session.get(SourceProductReferenceRecord, ref_id)
assert ref is not None
assert ref.lifecycle_status == LifecycleStatus.DISAPPEARED.value
changed = deepcopy(item)
changed["best_price"] = "999.00"
adapter.pages = [_page([changed])]
run = SearchExecutionService(session, adapter).execute(search_id)
session.refresh(ref)
assert ref.lifecycle_status == LifecycleStatus.ACTIVE.value
assert any(item["diff_status"] == DiffStatus.CHANGED.value for item in run["items"])
assert all(item["diff_status"] != DiffStatus.NEW.value for item in run["items"])
prices = [
row.best_price
for row in session.query(ProductObservationRecord).filter(
ProductObservationRecord.source_product_reference_id == ref_id
)
]
assert Decimal("1298.00") in prices
assert Decimal("999.00") in prices
finally:
session.close()
def test_uncertain_match_stays_separate():
session = _session()
try:
first = _item("matched", manufacturer="Example", model="Uncertain 13")
second = _item(
"uncertain",
source_external_id=None,
manufacturer_sku=None,
source_url="https://geizhals.de/other-uncertain.html",
source_title=first["source_title"],
manufacturer="Example",
model="Uncertain 13",
normalized_attributes={"ram_gb": 16, "storage_gb": 512, "weight_kg": 2.0},
)
adapter = ScriptedAdapter([_page([first, second])])
search_id = _prepare_search(session, adapter)
project_id = DiscoverySearchService(session, adapter).get(search_id)["buying_project_id"]
run = SearchExecutionService(session, adapter).execute(search_id)
assert run["status"] == RunStatus.SUCCESS.value
ref_ids = {item["source_product_reference_id"] for item in run["items"]}
assert len(ref_ids) == 2
refs = [session.get(SourceProductReferenceRecord, ref_id) for ref_id in ref_ids]
assert MatchStatus.MATCH_UNCERTAIN.value in {ref.match_status for ref in refs if ref is not None}
candidates = (
session.query(ProjectCandidateRecord)
.filter(ProjectCandidateRecord.buying_project_id == project_id)
.all()
)
assert len(candidates) == 2
assert MatchStatus.MATCH_UNCERTAIN.value in {row.match_status for row in candidates}
finally:
session.close()
def test_backoff_and_blocked():
session = _session()
try:
adapter = ScriptedAdapter([_page([ITEM_A], status=429)])
search_id = _prepare_search(session, adapter)
backoff = SearchExecutionService(session, adapter).execute(search_id)
assert backoff["status"] == RunStatus.PARTIAL.value
assert backoff["error_code"] == "BACKOFF"
adapter.pages = [_page([ITEM_A], status=403)]
blocked = SearchExecutionService(session, adapter).execute(search_id)
assert blocked["status"] == RunStatus.BLOCKED.value
stored = DiscoverySearchService(session, adapter).get(search_id)
assert stored["status"] == "BLOCKED"
assert stored["next_run_at"] is None
finally:
session.close()
@@ -0,0 +1,59 @@
from pathlib import Path
import pytest
from sqlalchemy import text
from app.persistence.db import SessionLocal, engine
from app.services.import_service import ImportService
from app.services.ingestion_service import IngestionService
from app.sources.manual import ManualSource
from scripts.bootstrap import run_bootstrap
pytestmark = pytest.mark.integration
ROOT = Path(__file__).resolve().parents[2]
@pytest.fixture(scope="module", autouse=True)
def require_postgres() -> None:
try:
with engine.connect() as connection:
connection.execute(text("SELECT 1"))
except Exception as exc:
pytest.skip(f"PostgreSQL not available: {exc}")
def test_manual_source_import_is_idempotent() -> None:
session = SessionLocal()
try:
run_bootstrap(session)
ingestion = IngestionService(session)
for item in (
{"key": "ram_gb", "value_type": "number", "canonical_unit": "GB", "minimum": 0},
{"key": "storage_gb", "value_type": "number", "canonical_unit": "GB", "minimum": 0},
{"key": "weight_kg", "value_type": "number", "canonical_unit": "kg", "minimum": 0},
{"key": "battery_score", "value_type": "number", "minimum": 0, "maximum": 100},
{"key": "display_score", "value_type": "number", "minimum": 0, "maximum": 100},
{"key": "cpu_score", "value_type": "number", "minimum": 0, "maximum": 100},
):
try:
ingestion.create_definition(item)
except Exception:
session.rollback()
source = ManualSource(ROOT / "fixtures" / "manual-demo.json")
importer = ImportService(ingestion)
importer.import_product(source, "example-mobile-13")
session.commit()
importer.import_product(source, "example-mobile-13")
session.commit()
from sqlalchemy import func, select
from app.persistence.models import OfferRecord, PriceObservationRecord, ProductRecord
products = session.scalar(select(func.count()).select_from(ProductRecord))
offers = session.scalar(select(func.count()).select_from(OfferRecord))
prices = session.scalar(select(func.count()).select_from(PriceObservationRecord))
assert products >= 1
assert offers >= 4
assert prices >= 16
finally:
session.close()
+40
View File
@@ -0,0 +1,40 @@
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import text
from alembic.config import Config
from alembic.runtime.migration import MigrationContext
from alembic.script import ScriptDirectory
from app.main import app
from app.persistence.db import engine
pytestmark = pytest.mark.integration
client = TestClient(app)
@pytest.fixture(scope="module", autouse=True)
def require_postgres() -> None:
try:
with engine.connect() as connection:
connection.execute(text("SELECT 1"))
except Exception as exc:
pytest.skip(f"PostgreSQL not available: {exc}")
def test_health_ready_when_migrated() -> None:
response = client.get("/health/ready")
assert response.status_code == 200
body = response.json()
assert body["status"] == "ready"
assert "environment" in body
assert "engine_build" in body
def test_alembic_is_at_head() -> None:
config = Config("alembic.ini")
script = ScriptDirectory.from_config(config)
with engine.connect() as connection:
context = MigrationContext.configure(connection)
assert set(context.get_current_heads()) == set(script.get_heads())
@@ -0,0 +1,53 @@
import pytest
from sqlalchemy import text
from alembic import command
from alembic.config import Config
from app.domain.validation import parse_scoring_model
from app.persistence.db import SessionLocal, engine
from app.persistence.models import ScoringModelRecord
from scripts.bootstrap import run_bootstrap
pytestmark = pytest.mark.integration
@pytest.fixture(scope="module", autouse=True)
def require_postgres() -> None:
try:
with engine.connect() as connection:
connection.execute(text("SELECT 1"))
except Exception as exc:
pytest.skip(f"PostgreSQL not available: {exc}")
def test_upgrade_is_idempotent() -> None:
config = Config("alembic.ini")
command.upgrade(config, "head")
command.upgrade(config, "head")
def test_bootstrap_is_idempotent_and_rejects_hash_change(tmp_path, monkeypatch) -> None:
session = SessionLocal()
try:
run_bootstrap(session)
run_bootstrap(session)
row = session.get(ScoringModelRecord, "generic-v1")
assert row is not None
model = parse_scoring_model(row.definition)
assert row.definition_hash == model.definition_hash
row.definition_hash = "0" * 64
session.commit()
with pytest.raises(SystemExit):
run_bootstrap(session)
row.definition_hash = model.definition_hash
session.commit()
finally:
session.close()
@pytest.mark.slow
def test_downgrade_and_upgrade_on_disposable_db() -> None:
config = Config("alembic.ini")
command.downgrade(config, "base")
command.upgrade(config, "head")
View File
Whitespace-only changes.
+59
View File
@@ -0,0 +1,59 @@
from decimal import Decimal
from app.domain.constraints import evaluate_constraint
from app.domain.enums import ConstraintOperator
from app.domain.models import Constraint
from tests.conftest import statement
def _eval(definitions, attribute, operator, value, stmt, confidence="0.8"):
return evaluate_constraint(
Constraint("c1", attribute, ConstraintOperator(operator), value, True),
stmt,
definitions[attribute],
Decimal(confidence),
)
def test_number_operators_pass_fail_unknown(definitions):
assert _eval(definitions, "ram_gb", ">=", 16, statement("ram_gb", 16)).status == "PASS"
assert _eval(definitions, "ram_gb", ">", 16, statement("ram_gb", 16)).status == "FAIL"
assert _eval(definitions, "ram_gb", "<", 16, statement("ram_gb", 8)).status == "PASS"
assert _eval(definitions, "ram_gb", "<=", 16, statement("ram_gb", 16)).status == "PASS"
assert _eval(definitions, "ram_gb", ">=", 16, None).status == "UNKNOWN"
def test_equality_and_in(definitions):
assert _eval(definitions, "color", "==", "black", statement("color", "black")).status == "PASS"
assert _eval(definitions, "color", "!=", "silver", statement("color", "black")).status == "PASS"
assert _eval(definitions, "has_lte", "==", True, statement("has_lte", True)).status == "PASS"
assert _eval(definitions, "ram_gb", "in", [16, 32], statement("ram_gb", 32)).status == "PASS"
assert _eval(definitions, "ram_gb", "in", [16], statement("ram_gb", 8)).status == "FAIL"
def test_contains_and_empty_list(definitions):
empty = statement("ports", [])
assert _eval(definitions, "ports", "exists", None, empty).status == "PASS"
assert _eval(definitions, "ports", "contains", "hdmi", empty).status == "FAIL"
present = statement("ports", ["hdmi", "usb-c"])
assert _eval(definitions, "ports", "contains", "hdmi", present).status == "PASS"
def test_exists_unknown_when_unusable(definitions):
assert _eval(definitions, "ram_gb", "exists", None, None).status == "UNKNOWN"
unknown = statement("ram_gb", None, status="unknown")
assert _eval(definitions, "ram_gb", "exists", None, unknown).status == "UNKNOWN"
assert _eval(definitions, "ram_gb", "!=", 8, None).status == "UNKNOWN"
def test_confidence_boundary(definitions):
exact = statement("ram_gb", 32, confidence="0.8")
below = statement("ram_gb", 32, confidence="0.799")
assert _eval(definitions, "ram_gb", ">=", 16, exact).status == "PASS"
assert _eval(definitions, "ram_gb", ">=", 16, below).status == "UNKNOWN"
def test_no_type_coercion(definitions):
text_number = statement("ram_gb", "32")
text_number = text_number.__class__(**{**text_number.__dict__, "normalized_value": "32"})
assert _eval(definitions, "ram_gb", ">=", 16, text_number).status == "UNKNOWN"
+74
View File
@@ -0,0 +1,74 @@
from app.discovery.diff import diff_run, next_lifecycle, observations_changed
from app.discovery.enums import DiffStatus, LifecycleStatus, RunStatus
PREV = {"best_price": "1298.00", "availability": "in_stock", "seller_count": 8, "normalized_attributes": {"ram_gb": 32}}
CURR = {"best_price": "1199.00", "availability": "in_stock", "seller_count": 8, "normalized_attributes": {"ram_gb": 32}}
def test_changed_only_for_relevant_fields():
assert observations_changed(PREV, CURR) is True
title_only = {**PREV, "normalized_attributes": {"ram_gb": 32, "title": "new title", "html": "<b>"}}
assert observations_changed(PREV, title_only) is False
assert observations_changed(PREV, {**PREV, "seller_count": 12}) is True
assert observations_changed(PREV, {**PREV, "availability": "out_of_stock"}) is True
def test_diff_statuses_and_partial_skips_missing():
previous_ids = {"a", "b"}
current_ids = {"b", "c"}
previous_obs = {"a": PREV, "b": PREV}
current_obs = {"b": CURR, "c": PREV}
success = diff_run(
previous_ids=previous_ids,
current_ids=current_ids,
previous_obs=previous_obs,
current_obs=current_obs,
run_status=RunStatus.SUCCESS.value,
)
assert success["c"] == DiffStatus.NEW
assert success["b"] == DiffStatus.CHANGED
assert success["a"] == DiffStatus.MISSING
partial = diff_run(
previous_ids=previous_ids,
current_ids=current_ids,
previous_obs=previous_obs,
current_obs=current_obs,
run_status=RunStatus.PARTIAL.value,
)
assert "a" not in partial
assert DiffStatus.MISSING not in partial.values()
def test_unchanged_and_disappeared_lifecycle():
same = diff_run(
previous_ids={"a"},
current_ids={"a"},
previous_obs={"a": PREV},
current_obs={"a": PREV},
run_status=RunStatus.SUCCESS.value,
)
assert same["a"] == DiffStatus.UNCHANGED
status, streak = next_lifecycle(
current=LifecycleStatus.ACTIVE.value,
missing_in_success_run=True,
consecutive_missing=2,
reappeared=False,
)
assert status == LifecycleStatus.DISAPPEARED.value
assert streak == 3
status, streak = next_lifecycle(
current=LifecycleStatus.DISAPPEARED.value,
missing_in_success_run=False,
consecutive_missing=3,
reappeared=True,
)
assert status == LifecycleStatus.ACTIVE.value
assert streak == 0
archived, _ = next_lifecycle(
current=LifecycleStatus.ARCHIVED.value,
missing_in_success_run=True,
consecutive_missing=5,
reappeared=True,
)
assert archived == LifecycleStatus.ARCHIVED.value
@@ -0,0 +1,83 @@
from app.discovery.enums import MatchStatus
from app.discovery.identity import IdentitySignals, decide_identity, spec_fingerprint
def _sig(**overrides) -> IdentitySignals:
base = IdentitySignals(
source_external_id="gh-1",
gtin_ean="4000000000001",
manufacturer_sku="EM13-32",
source_url="https://geizhals.de/a.html",
title="Example Mobile 13",
manufacturer="Example",
model="Mobile 13",
fingerprint=spec_fingerprint({"manufacturer": "Example", "model": "Mobile 13", "ram": 32}),
)
return IdentitySignals(**{**base.__dict__, **overrides})
def test_sku_and_ean_and_url_match():
known = [("a", _sig())]
assert decide_identity(_sig(), known).reason == "source_external_id"
assert decide_identity(_sig(source_external_id="other"), known).reason == "gtin_ean"
assert decide_identity(_sig(source_external_id="other", gtin_ean="x"), known).reason == "manufacturer_sku"
assert decide_identity(
_sig(source_external_id="other", gtin_ean="x", manufacturer_sku="y"),
known,
).reason == "source_url"
def test_title_plus_fingerprint_matches_stable_specs():
known = [("a", _sig(source_external_id=None, gtin_ean=None, manufacturer_sku=None, source_url=None))]
incoming = _sig(
source_external_id=None,
gtin_ean=None,
manufacturer_sku=None,
source_url=None,
title="Example Mobile 13",
)
decision = decide_identity(incoming, known)
assert decision.match_status == MatchStatus.MATCHED
assert decision.reason == "title+fingerprint"
def test_title_only_is_uncertain_and_never_merges():
known = [("a", _sig(source_external_id=None, gtin_ean=None, manufacturer_sku=None, source_url=None))]
incoming = _sig(
source_external_id=None,
gtin_ean=None,
manufacturer_sku=None,
source_url=None,
fingerprint="different",
manufacturer="Other",
model="Other",
)
decision = decide_identity(incoming, known)
assert decision.match_status == MatchStatus.MATCH_UNCERTAIN
assert decision.existing_id is None
assert decision.reason == "title only"
def test_fingerprint_uses_observed_keys_only():
left = spec_fingerprint({"capacity_l": 2, "color": "black"})
right = spec_fingerprint({"capacity_l": 5, "color": "black"})
assert left != right
assert "capacity_l=2" in left
assert "ram_gb" not in left
assert spec_fingerprint({"title": "ignored", "html": "<b>", "noise": 1}) == "noise=1"
def test_similar_names_do_not_merge():
known = [("a", _sig(source_external_id=None, gtin_ean=None, manufacturer_sku=None, source_url=None))]
incoming = _sig(
source_external_id=None,
gtin_ean=None,
manufacturer_sku=None,
source_url=None,
title="Example Mobile 14",
fingerprint="other",
manufacturer="Example",
model="Mobile 14",
)
decision = decide_identity(incoming, known)
assert decision.match_status == MatchStatus.NEW_REFERENCE
@@ -0,0 +1,48 @@
from decimal import Decimal
from app.discovery.ranking import rank_candidates
def test_shortlist_uses_product_score_only(profile, scoring_model, definitions):
ranked = rank_candidates(
profile=profile,
scoring_model=scoring_model,
definitions=definitions,
shortlist_size=1,
candidates=[
{
"id": "a",
"manufacturer": "Example",
"source_title": "Example Mobile 13 32GB",
"normalized_attributes": {
"ram_gb": 32,
"storage_gb": 1024,
"weight_kg": 1.2,
"battery_score": 90,
"display_score": 90,
"cpu_score": 90,
},
},
{
"id": "b",
"manufacturer": "Example",
"source_title": "Example Mobile 13 8GB",
"normalized_attributes": {
"ram_gb": 8,
"storage_gb": 1024,
"weight_kg": 1.2,
"battery_score": 90,
"display_score": 90,
"cpu_score": 90,
},
},
],
)
assert ranked[0]["id"] == "a"
assert ranked[0]["eligible"] is True
assert ranked[0]["candidate_status"] == "SHORTLISTED"
assert ranked[0]["product_score"] == Decimal("90.21")
assert ranked[1]["eligible"] is False
assert ranked[1]["candidate_status"] == "CANDIDATE"
assert "deal_score" not in ranked[0]
assert "price_score" not in ranked[0]
@@ -0,0 +1,81 @@
from app.discovery.mapping import verify_roundtrip
from app.discovery.urlgen import generate_search_url
CRITERIA = [
{"id": "ram", "criterion": "ram_gb", "operator": ">=", "value": 24, "mandatory": True},
{"id": "price", "criterion": "price_eur", "operator": "<=", "value": 2000, "mandatory": True},
{"id": "weight", "criterion": "weight_kg", "operator": "<=", "value": 1.6, "mandatory": True},
]
MAPPINGS = [
{
"criterion_key": "ram_gb",
"external_filter_key": "17023",
"source_value": "ab 24GB",
"encoding": {"url_param": "xf"},
"status": "VERIFIED",
},
{
"criterion_key": "price_eur",
"external_filter_key": "price",
"source_value": None,
"encoding": {"url_param": "maxp"},
"status": "VERIFIED",
},
]
def test_ram_and_price_url_is_reproducible():
first, internal = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=MAPPINGS,
)
second, _ = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=MAPPINGS,
)
assert first == second
assert "xf=17023_ab+24GB" in first
assert "maxp=2000" in first
assert internal == ["weight_kg"]
def test_internal_only_does_not_change_url():
without_weight, _ = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA[:2],
mappings=MAPPINGS,
)
with_weight, internal = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=MAPPINGS,
)
assert without_weight == with_weight
assert "weight_kg" in internal
def test_broken_mapping_is_not_used():
url, internal = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=[{**MAPPINGS[0], "status": "BROKEN"}, MAPPINGS[1]],
)
assert "17023" not in url
assert "ram_gb" in internal
def test_roundtrip_verifies_xf_and_maxp():
assert verify_roundtrip(
category_url="https://geizhals.de/?cat=nb",
criterion=CRITERIA[0],
mapping=MAPPINGS[0],
)
assert verify_roundtrip(
category_url="https://geizhals.de/?cat=nb",
criterion=CRITERIA[1],
mapping=MAPPINGS[1],
)
+165
View File
@@ -0,0 +1,165 @@
from datetime import timedelta
from decimal import Decimal
from uuid import uuid4
from app.domain.enums import Eligibility, PriceReferenceScope
from app.scoring.deal import classify_deal_score
from app.scoring.engine import evaluate_deal
from app.scoring.mathutil import round_score
from tests.conftest import (
AS_OF,
CUTOFF,
case_a_attributes,
case_a_prices,
offer_a,
observation,
product_a,
statement,
variant_a,
)
def _run(profile, scoring_model, definitions, attributes=None, observations=None, product=None, offer=None):
return evaluate_deal(
profile=profile,
scoring_model=scoring_model,
product=product or product_a(),
variant=variant_a(),
offer=offer or offer_a(),
attributes=attributes or case_a_attributes(),
observations=observations or case_a_prices(),
definitions=definitions,
as_of=AS_OF,
snapshot_cutoff=CUTOFF,
engine_build="test",
)
def test_reference_case_a(profile, scoring_model, definitions):
result = _run(profile, scoring_model, definitions)
assert result.product_eligibility == Eligibility.ELIGIBLE
assert result.offer_eligibility == Eligibility.ELIGIBLE
assert result.overall_eligibility == Eligibility.ELIGIBLE
assert result.eligible is True
assert result.product_score == Decimal("90.21")
assert result.price_statistics.median_30d == Decimal("1599.00")
assert result.price_statistics.median_30d_days == 2
assert result.price_statistics.median_90d == Decimal("1599.00")
assert result.price_statistics.median_90d_days == 3
assert result.price_statistics.lowest_365d == Decimal("1599.00")
assert result.price_statistics.lowest_365d_days == 3
assert result.price_score == Decimal("98.24")
assert result.deal_score == Decimal("93.42")
assert result.classification == "EXCEPTIONAL"
assert result.product_coverage == Decimal("1")
assert result.price_reference_scope == PriceReferenceScope.OFFER
assert result.price_basis.value == "BUDGET_AND_HISTORY"
assert any(reason.code == "PRICE_BELOW_MEDIAN" for reason in result.reasons)
def test_case_b_ineligible_product(profile, scoring_model, definitions):
attrs = (
statement("ram_gb", 8),
statement("storage_gb", 1024),
statement("weight_kg", Decimal("1.2")),
statement("battery_score", 90),
statement("display_score", 90),
statement("cpu_score", 90),
)
result = _run(profile, scoring_model, definitions, attributes=attrs)
assert result.product_eligibility == Eligibility.INELIGIBLE
assert result.offer_eligibility == Eligibility.ELIGIBLE
assert result.overall_eligibility == Eligibility.INELIGIBLE
assert result.eligible is False
assert result.product_score is not None
assert result.deal_score is None
assert result.classification == "INELIGIBLE"
assert any(item.constraint_id == "min_ram" and item.status == "FAIL" for item in result.constraint_results)
def test_case_c_unknown_weight(profile, scoring_model, definitions):
attrs = (
statement("ram_gb", 32),
statement("storage_gb", 1024),
statement("weight_kg", None, status="unknown"),
statement("battery_score", 90),
statement("display_score", 90),
statement("cpu_score", 90),
)
result = _run(profile, scoring_model, definitions, attributes=attrs)
assert result.product_eligibility == Eligibility.UNKNOWN
assert result.offer_eligibility == Eligibility.ELIGIBLE
assert result.overall_eligibility == Eligibility.UNKNOWN
assert result.eligible is None
assert result.product_score is not None
assert result.deal_score is None
assert result.classification == "INSUFFICIENT_DATA"
def test_case_d_over_budget_keeps_product_fit(profile, scoring_model, definitions):
prices = (
observation((9, 1), "1599.00"),
observation((9, 15), "1599.00"),
observation((10, 1), "1599.00"),
observation((10, 3), "2049.00"),
)
result = _run(profile, scoring_model, definitions, observations=prices)
assert result.product_eligibility == Eligibility.ELIGIBLE
assert result.offer_eligibility == Eligibility.INELIGIBLE
assert result.overall_eligibility == Eligibility.INELIGIBLE
assert result.product_score == Decimal("90.21")
assert result.price_score is not None
assert result.deal_score is None
def test_freshness_boundary(profile, scoring_model, definitions):
fresh = observation((10, 3), "1298.00")
fresh = fresh.__class__(**{**fresh.__dict__, "observed_at": AS_OF - timedelta(hours=48)})
result = _run(profile, scoring_model, definitions, observations=(fresh,))
assert result.offer_eligibility in {Eligibility.ELIGIBLE, Eligibility.UNKNOWN}
stale = fresh.__class__(**{**fresh.__dict__, "observed_at": AS_OF - timedelta(hours=48, seconds=1)})
result_stale = _run(profile, scoring_model, definitions, observations=(stale,))
assert result_stale.offer_eligibility == Eligibility.UNKNOWN
assert result_stale.price_score is None
def test_unknown_shipping(profile, scoring_model, definitions):
price = observation((10, 3), "1298.00")
price = price.__class__(**{**price.__dict__, "shipping_price": None})
result = _run(profile, scoring_model, definitions, observations=(price,))
assert result.offer_eligibility == Eligibility.UNKNOWN
assert result.price_score is None
def test_classification_boundaries(scoring_model):
assert classify_deal_score(Decimal("59.999"), scoring_model) == "IGNORE"
assert classify_deal_score(Decimal("60"), scoring_model) == "WATCH"
assert classify_deal_score(Decimal("74.999"), scoring_model) == "WATCH"
assert classify_deal_score(Decimal("75"), scoring_model) == "GOOD"
assert classify_deal_score(Decimal("85"), scoring_model) == "VERY_GOOD"
assert classify_deal_score(Decimal("92"), scoring_model) == "VERY_GOOD"
assert classify_deal_score(Decimal("92.01"), scoring_model) == "EXCEPTIONAL"
def test_rounding_does_not_shift_class(scoring_model):
raw = Decimal("59.996")
assert classify_deal_score(raw, scoring_model) == "IGNORE"
assert round_score(raw) == Decimal("60.00")
def test_other_category_without_engine_change(profile, scoring_model, definitions):
camera = product_a().__class__(**{**product_a().__dict__, "category": "camera"})
result = _run(profile, scoring_model, definitions, product=camera)
assert result.product_eligibility == Eligibility.INELIGIBLE
assert any(gate.reason_code == "CATEGORY_MISMATCH" for gate in result.product_gates)
def test_does_not_mix_other_offer_history(profile, scoring_model, definitions):
other = observation((9, 1), "100.00", offer_id=uuid4())
mixed = case_a_prices() + (other,)
result = _run(profile, scoring_model, definitions, observations=mixed)
# Engine is called with the provided list; the assembler in v0.1 should only
# receive one offer. Guard the function by using only same-offer rows.
same_offer = tuple(item for item in mixed if item.offer_id == offer_a().id)
result = _run(profile, scoring_model, definitions, observations=same_offer)
assert result.price_statistics.median_90d == Decimal("1599.00")
+11
View File
@@ -0,0 +1,11 @@
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health_live_does_not_touch_the_database() -> None:
response = client.get("/health/live")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
+103
View File
@@ -0,0 +1,103 @@
from datetime import datetime, timezone
from decimal import Decimal
from uuid import uuid4
from app.domain.models import Budget
from app.scoring.price import daily_series, median, score_price, select_current_observation
from tests.conftest import observation, utc
def test_budget_and_history_points(scoring_model):
budget = Budget(Decimal("1500.00"), Decimal("2000.00"))
score, budget_score, history_score, basis, warning = score_price(
current_price=Decimal("1500.00"),
reference_history=(Decimal("1500"), Decimal("1500"), Decimal("1500")),
budget=budget,
model=scoring_model,
)
assert budget_score == Decimal("100")
assert history_score == Decimal("50")
assert basis.value == "BUDGET_AND_HISTORY"
assert warning is None
over, over_budget, _, _, _ = score_price(
current_price=Decimal("2000.00"),
reference_history=(Decimal("1000"), Decimal("1000"), Decimal("1000")),
budget=budget,
model=scoring_model,
)
assert over_budget == Decimal("0")
cheap, _, hist, _, _ = score_price(
current_price=Decimal("800.00"),
reference_history=(Decimal("1000"), Decimal("1000"), Decimal("1000")),
budget=budget,
model=scoring_model,
)
assert hist == Decimal("100")
expensive, _, hist2, _, _ = score_price(
current_price=Decimal("1200.00"),
reference_history=(Decimal("1000"), Decimal("1000"), Decimal("1000")),
budget=budget,
model=scoring_model,
)
assert hist2 == Decimal("0")
def test_budget_only_with_short_history(scoring_model):
budget = Budget(Decimal("1500.00"), Decimal("2000.00"))
for days in ((), (Decimal("10"),), (Decimal("10"), Decimal("11"))):
score, budget_score, history_score, basis, warning = score_price(
current_price=Decimal("1298.00"),
reference_history=days,
budget=budget,
model=scoring_model,
)
assert score == budget_score == Decimal("100")
assert history_score is None
assert basis.value == "BUDGET_ONLY"
assert warning == "INSUFFICIENT_HISTORY"
def test_median_even_and_odd():
assert median((Decimal("1"), Decimal("3"), Decimal("5"))) == Decimal("3")
assert median((Decimal("1"), Decimal("2"), Decimal("3"), Decimal("4"))) == Decimal("2.5")
def test_daily_series_latest_then_quality_filter(scoring_model):
offer = uuid4()
morning = observation((10, 1), "10.00", offer_id=offer, hour=8)
later_unknown = observation((10, 1), "9.00", offer_id=offer, hour=12, availability="unknown")
values, _ = daily_series(
(morning, later_unknown),
window_start=datetime(2026, 9, 1, tzinfo=timezone.utc).date(),
window_end_exclusive=datetime(2026, 10, 3, tzinfo=timezone.utc).date(),
minimum_confidence=scoring_model.minimum_confidence,
)
assert values == ()
def test_current_observation_does_not_fall_back():
cheap_old = observation((10, 2), "10.00")
unknown_new = observation((10, 3), "99.00", availability="unknown")
current = select_current_observation(
(cheap_old, unknown_new),
as_of=utc(2026, 10, 3, 12),
snapshot_cutoff=utc(2026, 10, 3, 12),
)
assert current is not None
assert current.availability.value == "unknown"
assert current.item_price == Decimal("99.00")
def test_no_offer_object_required_for_price_function(scoring_model):
score, _, _, basis, _ = score_price(
current_price=Decimal("1298.00"),
reference_history=(Decimal("1599"), Decimal("1599"), Decimal("1599")),
budget=Budget(Decimal("1500"), Decimal("2000")),
model=scoring_model,
)
from app.scoring.mathutil import round_score
assert basis.value == "BUDGET_AND_HISTORY"
assert round_score(score) == Decimal("98.24")
+91
View File
@@ -0,0 +1,91 @@
from decimal import Decimal
import pytest
from app.domain.enums import ConstraintStatus, PreferenceDirection, PreferenceStrategy
from app.domain.models import ConstraintResult, Preference
from app.domain.validation import DomainValidationError, parse_profile
from app.scoring.mathutil import round_score
from app.scoring.product import score_numeric_linear, score_product
from tests.conftest import statement
def test_numeric_linear_acceptable_and_ideal():
assert score_numeric_linear(Decimal("16"), Decimal("16"), Decimal("32"), PreferenceDirection.HIGHER) == Decimal("50")
assert score_numeric_linear(Decimal("32"), Decimal("16"), Decimal("32"), PreferenceDirection.HIGHER) == Decimal("100")
assert score_numeric_linear(Decimal("1.6"), Decimal("1.6"), Decimal("1.0"), PreferenceDirection.LOWER) == Decimal("50")
assert score_numeric_linear(Decimal("1.0"), Decimal("1.6"), Decimal("1.0"), PreferenceDirection.LOWER) == Decimal("100")
def test_numeric_linear_clamps():
assert score_numeric_linear(Decimal("8"), Decimal("16"), Decimal("32"), PreferenceDirection.HIGHER) == Decimal("25")
assert score_numeric_linear(Decimal("64"), Decimal("16"), Decimal("32"), PreferenceDirection.HIGHER) == Decimal("100")
assert score_numeric_linear(Decimal("0.5"), Decimal("1.6"), Decimal("1.0"), PreferenceDirection.LOWER) == Decimal("100")
def test_weights_need_not_sum_to_one(scoring_model):
prefs = (
Preference("a", "ram_gb", PreferenceStrategy.NUMERIC_LINEAR, PreferenceDirection.HIGHER, Decimal("16"), Decimal("32"), Decimal("2")),
Preference("b", "cpu_score", PreferenceStrategy.NUMERIC_LINEAR, PreferenceDirection.HIGHER, Decimal("60"), Decimal("100"), Decimal("2")),
)
snapshot = {"ram_gb": statement("ram_gb", 32), "cpu_score": statement("cpu_score", 100)}
score, coverage, _ = score_product(prefs, snapshot, (), scoring_model)
assert score == Decimal("100")
assert coverage == Decimal("1")
def test_missing_preference_scores_zero(scoring_model):
prefs = (
Preference("ram", "ram_gb", PreferenceStrategy.NUMERIC_LINEAR, PreferenceDirection.HIGHER, Decimal("16"), Decimal("32"), Decimal("1")),
)
score, coverage, _ = score_product(prefs, {}, (), scoring_model)
assert score == Decimal("0")
assert coverage == Decimal("0")
def test_optional_penalty(scoring_model):
prefs = (
Preference("ram", "ram_gb", PreferenceStrategy.NUMERIC_LINEAR, PreferenceDirection.HIGHER, Decimal("16"), Decimal("32"), Decimal("1")),
)
snapshot = {"ram_gb": statement("ram_gb", 32)}
optional = ConstraintResult("opt", "ports", "contains", "hdmi", None, ConstraintStatus.FAIL.value, False, None, "CONSTRAINT_FAIL")
score, _, _ = score_product(prefs, snapshot, (optional,), scoring_model)
assert score == Decimal("95")
def test_unknown_strategy_rejected(definitions, scoring_model):
raw = {
"id": "x",
"version": 1,
"name": "x",
"category": "notebook",
"currency": "EUR",
"market": "DE",
"allowed_conditions": ["new"],
"budget": {"target": "100.00", "maximum": "200.00"},
"scoring_version": "generic-v1",
"constraints": [],
"preferences": [
{
"id": "ram",
"attribute": "ram_gb",
"strategy": "boolean_preferred",
"direction": "higher",
"acceptable": 16,
"ideal": 32,
"weight": 1,
}
],
}
with pytest.raises(DomainValidationError):
parse_profile(
raw,
attribute_definitions=definitions,
scoring_versions={scoring_model.version},
supported_strategies=set(scoring_model.supported_preference_strategies),
)
def test_reference_preference_scores():
assert round_score(score_numeric_linear(Decimal("1.2"), Decimal("1.6"), Decimal("1.0"), PreferenceDirection.LOWER)) == Decimal("83.33")
assert score_numeric_linear(Decimal("90"), Decimal("60"), Decimal("100"), PreferenceDirection.HIGHER) == Decimal("87.5")
@@ -0,0 +1,27 @@
from uuid import uuid4
from app.discovery.criteria import parse_project_criteria
from app.settings import Settings
def test_parse_accepts_definition_id_and_enabled_flag():
definition_id = uuid4()
parsed = parse_project_criteria(
{
"items": [
{
"criterion_definition_id": str(definition_id),
"operator": ">=",
"value": 24,
"enabled_for_discovery": True,
}
]
}
)
assert parsed["items"][0]["criterion_definition_id"] == definition_id
assert parsed["items"][0]["enabled_for_discovery"] is True
def test_cors_origins_are_split_from_environment():
settings = Settings(cors_allowed_origins="http://localhost:4173, http://127.0.0.1:4173")
assert settings.cors_origins == ["http://localhost:4173", "http://127.0.0.1:4173"]
+14
View File
@@ -0,0 +1,14 @@
from app.settings import Settings
def test_database_url_uses_psycopg_and_explicit_fields() -> None:
settings = Settings(
db_host="postgres",
db_port=5432,
db_name="buyingagent_test",
db_user="buyingagent",
db_password="secret",
)
assert settings.database_url == (
"postgresql+psycopg://buyingagent:secret@postgres:5432/buyingagent_test"
)