Add regression corpus and opt-in catalog test route
This commit is contained in:
1 parent
11e1a4c4a2
commit
e23fdcfc0d
6 files changed
+475
-12
No files matched your search
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from app.matcher import CatalogMatcher
|
||||
|
||||
|
||||
class CatalogItemInfo(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
sku: str
|
||||
name: str
|
||||
unit: str
|
||||
price: Decimal
|
||||
|
||||
|
||||
def register_test_info_route(
|
||||
application: FastAPI, matcher: CatalogMatcher
|
||||
) -> None:
|
||||
items_by_sku = {item.sku: item for item in matcher.items}
|
||||
|
||||
@application.get(
|
||||
"/get_info_by/{sku}",
|
||||
response_model=CatalogItemInfo,
|
||||
include_in_schema=False,
|
||||
)
|
||||
def get_info_by(sku: str) -> CatalogItemInfo:
|
||||
item = items_by_sku.get(sku.strip().upper())
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="SKU not found")
|
||||
return CatalogItemInfo(
|
||||
sku=item.sku,
|
||||
name=item.name,
|
||||
unit=item.unit,
|
||||
price=item.price,
|
||||
)
|
||||
+42
-11
@@ -4,6 +4,7 @@ import os
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.matcher import CatalogMatcher
|
||||
from app.models import MatchRequest, MatchResponse
|
||||
@@ -11,18 +12,48 @@ from app.models import MatchRequest, MatchResponse
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
CATALOG_PATH = Path(os.environ.get("CATALOG_PATH", BASE_DIR / "catalog_excel.csv"))
|
||||
MATCHER = CatalogMatcher(CATALOG_PATH)
|
||||
|
||||
# The task asks for one application endpoint. OpenAPI/Swagger routes are disabled
|
||||
# so the running service exposes only POST /match.
|
||||
app = FastAPI(
|
||||
title="Offline catalog matcher",
|
||||
version="0.2.0",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
openapi_url=None,
|
||||
)
|
||||
TEST_INFO_ROUTE_ENV = "ENABLE_TEST_INFO_ROUTE"
|
||||
|
||||
|
||||
@app.post("/match", response_model=MatchResponse)
|
||||
def match(request: MatchRequest) -> MatchResponse:
|
||||
return MatchResponse(results=[MATCHER.match(message) for message in request.messages])
|
||||
|
||||
|
||||
def create_app(*, enable_test_info_route: bool = False) -> FastAPI:
|
||||
# OpenAPI/Swagger routes are disabled so the production application exposes
|
||||
# exactly the single endpoint required by the task.
|
||||
application = FastAPI(
|
||||
title="Offline catalog matcher",
|
||||
version="0.2.0",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
openapi_url=None,
|
||||
)
|
||||
application.add_api_route(
|
||||
"/match", match, methods=["POST"], response_model=MatchResponse
|
||||
)
|
||||
|
||||
if enable_test_info_route:
|
||||
from app.dev_routes import register_test_info_route
|
||||
|
||||
application.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["GET", "POST"],
|
||||
allow_headers=["Content-Type"],
|
||||
)
|
||||
register_test_info_route(application, MATCHER)
|
||||
|
||||
return application
|
||||
|
||||
|
||||
def _test_info_route_enabled() -> bool:
|
||||
return os.environ.get(TEST_INFO_ROUTE_ENV, "").strip().lower() in {
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
"on",
|
||||
}
|
||||
|
||||
|
||||
app = create_app(enable_test_info_route=_test_info_route_enabled())
|
||||
Reference in new issue
Block a user