test: cover the new usage columns and the rollup exclusion

bucketed_totals now returns cost and cached_tokens, so the exact-equality
assertion in test_day_bucket_sums_per_day had to grow the two fields; it
pins that an unreported cache bin reads NULL rather than 0.

The per-user breakdown test was seeded with a source='schedule' row, which
is a run-level rollup and no longer counted. Reseeded on a real flow, and
both spend queries gained a test that a rollup is excluded.
This commit is contained in:
arc53-machine committed 2026-09-22 10:42:36 +01:00
1 parent c2d1893992
commit 96a930c265
2 files changed
+39 -6

No files matched your search

@@ -89,6 +89,12 @@ class TestTopUsers:
assert [row["user_id"] for row in rows] == ["heavy", "light"]
assert rows[0]["cost"] == pytest.approx(2.0)
def test_rollup_rows_are_not_billed_twice(self, pg_conn, since):
_insert(pg_conn, user_id="u1", source="agent_stream", cost=1.0)
_insert(pg_conn, user_id="u1", source="schedule", cost=1.0)
rows = AdminStatsRepository(pg_conn).top_token_users(since=since)
assert rows[0]["cost"] == pytest.approx(1.0)
class TestLatencySummary:
def test_percentiles_over_measured_calls(self, pg_conn, since):
@@ -124,7 +130,7 @@ class TestLatencySummary:
class TestUserUsageBreakdown:
def test_totals_and_splits(self, pg_conn, since):
_insert(pg_conn, user_id="u1", model_id="a", source="agent_stream", cost=1.0)
_insert(pg_conn, user_id="u1", model_id="b", source="schedule", cost=2.0)
_insert(pg_conn, user_id="u1", model_id="b", source="webhook", cost=2.0)
_insert(pg_conn, user_id="other", model_id="a", cost=99.0)
detail = AdminStatsRepository(pg_conn).user_usage_breakdown("u1", since=since)
assert detail["totals"]["calls"] == 2
@@ -132,9 +138,18 @@ class TestUserUsageBreakdown:
assert {row["key"] for row in detail["by_model"]} == {"a", "b"}
assert {row["key"] for row in detail["by_source"]} == {
"agent_stream",
"schedule",
"webhook",
}
def test_rollup_rows_are_not_billed_twice(self, pg_conn, since):
"""A scheduled run's rollup duplicates its own per-call rows."""
_insert(pg_conn, user_id="u1", source="agent_stream", cost=1.0)
_insert(pg_conn, user_id="u1", source="schedule", cost=1.0)
detail = AdminStatsRepository(pg_conn).user_usage_breakdown("u1", since=since)
assert detail["totals"]["calls"] == 1
assert detail["totals"]["cost"] == pytest.approx(1.0)
assert [row["key"] for row in detail["by_source"]] == ["agent_stream"]
def test_a_user_with_no_usage_reports_zeroes(self, pg_conn, since):
detail = AdminStatsRepository(pg_conn).user_usage_breakdown("ghost", since=since)
assert detail["totals"] == {"tokens": 0, "cost": 0.0, "calls": 0}
@@ -279,8 +279,12 @@ class TestBucketedTotals:
t1 = datetime(2026, 4, 10, 10, 0, tzinfo=timezone.utc)
t2 = datetime(2026, 4, 10, 23, 30, tzinfo=timezone.utc)
t3 = datetime(2026, 4, 11, 0, 15, tzinfo=timezone.utc)
repo.insert(user_id="u-day", prompt_tokens=10, generated_tokens=5, timestamp=t1)
repo.insert(user_id="u-day", prompt_tokens=20, generated_tokens=7, timestamp=t2)
repo.insert(
user_id="u-day", prompt_tokens=10, generated_tokens=5, cost=0.5, timestamp=t1
)
repo.insert(
user_id="u-day", prompt_tokens=20, generated_tokens=7, cost=0.25, timestamp=t2
)
repo.insert(user_id="u-day", prompt_tokens=1, generated_tokens=1, timestamp=t3)
rows = repo.bucketed_totals(
bucket_unit="day",
@@ -288,9 +292,23 @@ class TestBucketedTotals:
timestamp_gte=datetime(2026, 4, 10, tzinfo=timezone.utc),
timestamp_lt=datetime(2026, 4, 12, tzinfo=timezone.utc),
)
# ``cached_tokens`` is None when no row in the bucket reported a cache
# breakdown: "the provider said nothing", not "no cache hits".
assert rows == [
{"bucket": "2026-04-10", "prompt_tokens": 30, "generated_tokens": 12},
{"bucket": "2026-04-11", "prompt_tokens": 1, "generated_tokens": 1},
{
"bucket": "2026-04-10",
"prompt_tokens": 30,
"generated_tokens": 12,
"cost": 0.75,
"cached_tokens": None,
},
{
"bucket": "2026-04-11",
"prompt_tokens": 1,
"generated_tokens": 1,
"cost": 0.0,
"cached_tokens": None,
},
]
def test_hour_bucket(self, pg_conn):