app/main.py
One file, one FastAPI app. Owns the in-memory order store, the total calculation, and both HTTP routes.
ORDERS — 3 hardcoded orders across 2 accounts (Account A ×2, Account B ×1, one with no line items)order_total() — sums a single order's linestests/test_orders.py
5 tests: 3 hit the HTTP layer via TestClient, 2 call order_total() directly.
| Method | Path | Input | Output |
|---|---|---|---|
GET |
/orders/{order_id} |
path param order_id (string) |
order object + computed total, or 404 |
GET |
/accounts/{account}/total |
path param account (exact string match) |
{account, orders, total}, or 404 |
pytest -q, 5 collected| Test | Result | Notes |
|---|---|---|
test_get_order_ok | pass | 200, correct id |
test_get_order_missing | pass | 404 as expected |
test_total_two_lines | fail | expected 1650.0, got 1256.0 — this is the planted bug |
test_empty_order_total_is_zero | pass | passes regardless of the bug — an empty line list makes the broken formula irrelevant |
test_account_total | pass | only checks order count (2), never checks the total value, so it doesn't catch the bug either |
This is the planted bug. order_total() does qty + unit where it should do qty × unit. Every order with line items gets the wrong total, and every account rollup inherits the error. On the real data, Account A's total is 2458.0 instead of the correct 4050.0 — off by 40%. Caught by test_total_two_lines; missed by the other two total-related tests because one uses an empty order and the other never checks the number.
Line items are read with bare line["qty"] / line["unit"] lookups against a plain dict — there's no Pydantic model, despite Pydantic already being a FastAPI dependency. Verified directly: a line missing "unit" raises an unhandled KeyError, which FastAPI turns into a bare 500 Internal Server Error instead of a controlled 4xx with a useful message.
Both routes hand back full order line items and account revenue totals to any caller — no API key, no auth dependency, no rate limiting. Fine for a lab sample; a real risk the moment this fronts anything with actual customer data.
--- a/app/main.py +++ b/app/main.py @@ order_total() @@ for line in order["lines"]: - total += line["qty"] + line["unit"] + total += line["qty"] * line["unit"] return round(total, 2)
| Order | Account | Lines | Total (shipped) |
|---|
Numbers come straight from the ORDERS dict in app/main.py — nothing invented. Toggling recomputes with qty × unit instead of qty + unit.
2458.0, fixed 4050.0. Add up A-1001 (2 × 1200) and A-1002 (10 × 45 + 1 × 1200) yourself against data/sample-service/app/main.py and confirm it lands on 4050.