"""Statement budgets for the reads a sale makes of the catalogue (every statement is a round trip on the live pg wire, ~100 ms):
GET /products/{id}, /price, /convert and /rounding. Reference rows (units, roles, unit rows, measures, the list head) are read once
per request, and the count does not grow with the number of other products in the catalogue."""
from conftest import CATALOGUE, DEFAULT, SALES, api, mk_list, mk_product, must, row
from erp_platform.testing import count_statements
READS = [("GET", "/products/HOST-M", {}),
("GET", "/products/HOST-M/price", {"currency": "GBP", "bp_id": "7"}),
("GET", "/products/HOST-M/convert", {"quantity": "2", "from_unit": "MON", "to_unit": "MON"}),
("GET", "/products/HOST-M/rounding", {"role": "delivery", "quantity": "2", "unit_code": "MON"})]
BUDGET = {"/products/HOST-M": 11, "/products/HOST-M/price": 14, "/products/HOST-M/convert": 6, "/products/HOST-M/rounding": 6}
# was (2026-10-25, the o2c sale on SQLite: 15, 19, 10, 6) - unit roles read one role at a time, unit rows and units re-read per helper
def _counts(engine):
out = {}
for method, path, params in READS:
with count_statements(engine) as c:
must(api(method, path, SALES, params))
out[path] = c.statements
return out
def test_the_sale_reads_of_a_product_stay_within_budget_and_flat_in_other_products(engine):
mk_product("HOST-M")
mk_list("LIST-GBP", rows=[row("HOST-M", "2026-11-01", "120.00")], default_from=DEFAULT)
few = _counts(engine)
for i in range(20):
mk_product(f"OTHER-{i:02}", name=f"Other {i}")
many = _counts(engine)
print("BUDGET product", few)
assert few == many, (few, many)
for path, n in few.items():
assert n <= BUDGET[path], (path, n)
assert must(api("GET", "/products/HOST-M", CATALOGUE))["product_id"] == "HOST-M"