Put a number on what an endpoint may fetch, not just a column list

test_egress.py asserted which columns a statement names, which is the shape
both of this project's egress blowouts took. It would all still pass if a
response grew tenfold within the columns it is allowed to read -- and a story
that keeps getting longer does exactly that. Production's longest adventure is
607 actions where the plan assumed 200.

So dbmeter, which was built to be importable from tests and was not yet used
by any, now backs four byte ceilings: the page load, the action list, and one
action's snapshot fetched on demand. Budgets are per action rather than
absolute, so they mean the same thing whatever size the fixture is set to, and
generous -- 3 kB against a real 994 B. They are there to catch an order of
magnitude, not to freeze a byte count.

The fourth test is the one that keeps the other three honest. A ceiling proves
nothing unless the thing it excludes would breach it, so it undefers the
snapshot on purpose and asserts the same twelve rows cost more than ten times
the budget. If the fixture ever shrinks below the point where that holds, that
test fails rather than the ceilings quietly passing on nothing.

Meter grows detach() and a context manager. A script exits and takes the
wrapping with it; a test does not, and one test leaving the shared engine
metered would charge bytes to a scope nobody opened.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Dvvqn9ZDR4ixeFPHNbww7
This commit is contained in:
parththakkar106
2026-08-17 13:51:10 +05:30
co-authored by Claude Opus 5
parent be66780a26
commit 12d57afdac
2 changed files with 147 additions and 5 deletions
+20 -1
View File
@@ -174,7 +174,26 @@ class Meter:
metered_creator._dbmeter = self
pool._creator = metered_creator
self._attached_pools.append(pool)
self._attached_pools.append((engine, pool, creator))
def detach(self) -> None:
"""Put every metered engine back as it was.
A script exits and takes the wrapping with it; a test does not, and one
test leaving the shared engine metered would go on charging bytes to a
scope nobody opened. Pooled connections are dropped again on the way
out for the same reason attach drops them on the way in.
"""
while self._attached_pools:
engine, pool, creator = self._attached_pools.pop()
pool._creator = creator
engine.dispose()
def __enter__(self) -> "Meter":
return self
def __exit__(self, *exc) -> None:
self.detach()
# ------------------------------------------------------------- reporting