add demo data for Swagger
This commit is contained in:
4 files changed
+132
-2
No files matched your search
@@ -14,6 +14,10 @@ docker compose up --build
|
||||
|
||||
پس از healthy شدن سرویس، مستندات تعاملی در `http://localhost:8080/docs` و متریکها در `http://localhost:8080/metrics` در دسترساند.
|
||||
|
||||
در استقرار عمومی، نمونه `Runnable demo quote` داخل Swagger با موجودی نمایشی
|
||||
هماهنگ شده و `reserveInventory: false` دارد؛ بنابراین میتوان آن را چند بار بدون
|
||||
کاهش موجودی اجرا کرد.
|
||||
|
||||
ابتدا موجودی projection را ثبت کنید:
|
||||
|
||||
```bash
|
||||
|
||||
@@ -4,12 +4,16 @@ from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Annotated, cast
|
||||
|
||||
from fastapi import Depends, FastAPI, Header, HTTPException, Request, Response, status
|
||||
from fastapi import Body, Depends, FastAPI, Header, HTTPException, Request, Response, status
|
||||
from fastapi.responses import JSONResponse
|
||||
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
|
||||
from redis.asyncio import Redis
|
||||
from starlette.middleware.base import RequestResponseEndpoint
|
||||
|
||||
from warehouse_allocation.api_examples import (
|
||||
DEMO_ALLOCATION_REQUEST,
|
||||
DEMO_ALLOCATION_RESPONSE,
|
||||
)
|
||||
from warehouse_allocation.application import AllocationService
|
||||
from warehouse_allocation.config import Settings, get_settings
|
||||
from warehouse_allocation.domain.exceptions import (
|
||||
@@ -122,13 +126,36 @@ def create_app(
|
||||
"/v1/allocations",
|
||||
response_model=AllocationResponse,
|
||||
responses={
|
||||
200: {
|
||||
"description": "Warehouse selected successfully.",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"example": DEMO_ALLOCATION_RESPONSE,
|
||||
}
|
||||
},
|
||||
},
|
||||
400: {"model": ErrorResponse},
|
||||
409: {"model": ErrorResponse},
|
||||
503: {"model": ErrorResponse},
|
||||
},
|
||||
)
|
||||
async def allocate(
|
||||
payload: AllocationRequest,
|
||||
payload: Annotated[
|
||||
AllocationRequest,
|
||||
Body(
|
||||
openapi_examples={
|
||||
"demoQuote": {
|
||||
"summary": "Runnable demo quote",
|
||||
"description": (
|
||||
"Uses inventory pre-seeded in the public demo. "
|
||||
"It does not reserve or decrement stock, "
|
||||
"so it is safe to run repeatedly."
|
||||
),
|
||||
"value": DEMO_ALLOCATION_REQUEST,
|
||||
}
|
||||
}
|
||||
),
|
||||
],
|
||||
service: Annotated[AllocationService, Depends(get_service)],
|
||||
) -> AllocationResponse:
|
||||
started = time.perf_counter()
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
from typing import Final
|
||||
|
||||
DEMO_SKU: Final = "DEMO-SKU-001"
|
||||
DEMO_WAREHOUSE_IDS: Final = ("demo-tehran", "demo-karaj")
|
||||
|
||||
DEMO_ALLOCATION_REQUEST: Final[dict[str, object]] = {
|
||||
"requestId": "swagger-demo-quote",
|
||||
"customerLocation": {
|
||||
"latitude": 35.7219,
|
||||
"longitude": 51.3347,
|
||||
},
|
||||
"items": [
|
||||
{
|
||||
"sku": DEMO_SKU,
|
||||
"quantity": 1,
|
||||
}
|
||||
],
|
||||
"transportationCosts": [
|
||||
{
|
||||
"warehouseId": "demo-tehran",
|
||||
"fixedCost": 180000,
|
||||
"distanceKm": 12,
|
||||
"costPerKm": 3500,
|
||||
"baseDeliveryMinutes": 55,
|
||||
"surcharge": 0,
|
||||
},
|
||||
{
|
||||
"warehouseId": "demo-karaj",
|
||||
"fixedCost": 60000,
|
||||
"distanceKm": 48,
|
||||
"costPerKm": 2500,
|
||||
"baseDeliveryMinutes": 105,
|
||||
"surcharge": 0,
|
||||
},
|
||||
],
|
||||
"logisticsConstraints": [
|
||||
{
|
||||
"warehouseId": "demo-tehran",
|
||||
"trafficMultiplier": 1.25,
|
||||
"fleetPriority": 1.1,
|
||||
"handlingMinutes": 8,
|
||||
"available": True,
|
||||
},
|
||||
{
|
||||
"warehouseId": "demo-karaj",
|
||||
"trafficMultiplier": 1.05,
|
||||
"fleetPriority": 1,
|
||||
"handlingMinutes": 12,
|
||||
"available": True,
|
||||
},
|
||||
],
|
||||
"weights": {
|
||||
"cost": 0.7,
|
||||
"deliveryTime": 0.3,
|
||||
},
|
||||
"promisedDeliveryMinutes": 180,
|
||||
# A Swagger demo should remain repeatable and must not consume shared stock.
|
||||
"reserveInventory": False,
|
||||
}
|
||||
|
||||
DEMO_ALLOCATION_RESPONSE: Final[dict[str, object]] = {
|
||||
"requestId": "swagger-demo-quote",
|
||||
"warehouseId": "demo-karaj",
|
||||
"estimatedDeliveryTime": 122,
|
||||
"totalCost": 181350,
|
||||
"routeOptimizationScore": 70,
|
||||
"inventoryReserved": False,
|
||||
"scoreBreakdown": {
|
||||
"transportCost": 181350,
|
||||
"normalizedCost": 0,
|
||||
"normalizedDeliveryTime": 1,
|
||||
"slaPenalty": 0,
|
||||
"feasibleWarehouseCount": 2,
|
||||
},
|
||||
"calculatedAt": "2026-07-23T04:00:00Z",
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import httpx
|
||||
|
||||
from warehouse_allocation.api_examples import DEMO_SKU, DEMO_WAREHOUSE_IDS
|
||||
from warehouse_allocation.infrastructure.inventory import InMemoryInventoryRepository
|
||||
|
||||
|
||||
@@ -83,3 +84,25 @@ async def test_validation_error_is_structured(
|
||||
|
||||
assert response.status_code == 422
|
||||
assert response.json()["detail"]
|
||||
|
||||
|
||||
async def test_openapi_demo_example_is_runnable_and_non_reserving(
|
||||
client: httpx.AsyncClient,
|
||||
inventory: InMemoryInventoryRepository,
|
||||
) -> None:
|
||||
for warehouse_id in DEMO_WAREHOUSE_IDS:
|
||||
await inventory.set_levels(warehouse_id, {DEMO_SKU: 10})
|
||||
|
||||
openapi = (await client.get("/openapi.json")).json()
|
||||
media_type = openapi["paths"]["/v1/allocations"]["post"]["requestBody"]["content"][
|
||||
"application/json"
|
||||
]
|
||||
example = media_type["examples"]["demoQuote"]["value"]
|
||||
|
||||
first = await client.post("/v1/allocations", json=example)
|
||||
second = await client.post("/v1/allocations", json=example)
|
||||
|
||||
assert first.status_code == 200
|
||||
assert second.status_code == 200
|
||||
assert first.json()["inventoryReserved"] is False
|
||||
assert first.json()["warehouseId"] in DEMO_WAREHOUSE_IDS
|
||||
Reference in new issue
Block a user