litellm/tests/unit/repositories/test_daily_activity_sql.py
jesus-berri aa58384947
feat(usage-export): include key and user metadata objects in daily activity export rows (#49360)
* feat(usage-export): include key and user metadata objects in daily activity export rows

* fix(usage-export): only populate key and user metadata for admin view and strip credential and SCIM slots

* feat(usage-export): redact credential-named values inside exported key and user metadata

* test(usage-export): freeze e2e models, compare raw non-admin metadata, mark query locals Final

* test(usage-export): mark redaction test locals Final

* fix(usage-export): drop callback and SCIM slots from both metadata objects at any depth and mask camelCase credential names

* fix(masker): keep odd-cased plain names like sEt-CoOkIe sensitive alongside the camelCase split

* test(masker): mark new test locals Final

* fix(masker): make the camelCase split opt-in and enable it only for the usage export masker

* fix(usage-export): mask acronym-cased and unseparated credential names in exported metadata

* test(e2e): assert acronym-cased and upper-case credential names are masked in the usage export

* test(integration): cover usage export metadata across routes, callers and a worker kill

* test(integration): retry burst calls dropped by the worker kill and fail on any chat error

* fix(usage-export): redact credential-shaped metadata values whatever their key is called

The export masked key and user metadata by key name only, so secrets under
names the masker does not know (tokens, apiKeys, passphrase, database_url,
sentry_dsn, slackWebhookUrl) or under ordinary names (an sk- key in a note,
a postgres URL with a password, a Bearer header, a JWT, a Slack or Discord
webhook URL), including inside lists, were exported verbatim to admin-view
callers. redact_credentials_in_payload gains an opt-in is_credential hook,
and the export passes one that matches those key names and credential value
shapes.

* fix(usage-export): mask password-only credential URLs and bound the value scan

The URL branch required a username, so redis://:pw@host reached exports. The sk- lookahead and the URL scheme prefix scanned the rest of the string at every match attempt, which made a long metadata string quadratic (54s for 150k chars locally, 0.2s now).

* fix(usage-export): make every credential pattern linear, including JWTs

Builds on the bounded sk- and URL scan. The JWT branch still rescanned to the
end of the string at every eyJ, so eyJ- repeated 30k times took seconds. JWTs
now match on their eyJ header run, the sk- digit check moved out of the
lookahead into one pass over the sk- runs, and URL userinfo is length bounded.
The adversarial-input test covers sk-, JWT, scheme and userinfo runs.

* fix(usage-export): redact credential URLs whatever the username or password length

The 256-character userinfo bounds let a longer generated password through
unredacted. The username and password classes already stop at the next
separator, so the scan stays linear without them.

* fix(usage-export): redact credential URLs with any scheme casing and embedded Authorization headers

Postgresql:// and HTTPS:// userinfo passwords passed through because the
scheme class was lowercase only, and an Authorization: Bearer line inside a
longer string missed the anchored header pattern.

* fix(usage-export): redact quoted Authorization keys in serialized metadata strings

* fix(usage-export): redact padded Bearer values and prefixed or escaped Authorization keys

* fix(usage-export): redact prefixed key shapes and webhook URL variants

Key prefixes now only need a non-alphanumeric character before them, so
id_AKIA..., stripe_sk_live_... and my_ghp_... are redacted while risk-2024 and
disk-1234 stay intact. Slack and Discord webhooks match any scheme casing,
http, Discord subdomains and versioned /api/vN/webhooks/ paths.

* feat(usage-export): make key and user metadata opt-in with include_metadata

The export now fills key_metadata and user_metadata only when an admin-view
caller passes include_metadata=true. Without it, and for non-admins, both
columns stay '{}' so the CSV shape is unchanged. The Usage export dialog gets
an unchecked "Include key and user metadata" checkbox that sends the flag.

* test(usage-export): opt the integration and e2e export calls into metadata, regenerate API types

The integration and e2e export helpers now pass include_metadata=true, and the
admin-viewer integration test also checks that an admin export without the flag
returns empty metadata objects.

* chore(usage-export): regenerate the lazy OpenAPI snapshot for include_metadata

* fix(usage-export): keep the default export shape and gate the metadata option to admins

The key_metadata and user_metadata columns and fields now appear only when an
admin-view caller passes include_metadata=true. Every other export, CSV and
JSON, keeps the merge-base shape. The Usage export dialog shows the metadata
checkbox only to proxy admins and viewers, with a note that stored metadata may
carry credentials the redaction cannot catch.

* test(usage-export): expect no metadata fields on the owner path of the export burst test

* fix(usage-export): require the metadata fields on ExportRow instead of an unpicklable default

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-20 03:58:01 +00:00

540 lines
20 KiB
Python

from dataclasses import replace
from datetime import datetime, timezone
from typing import Final
import pytest
from litellm import constants
from litellm.constants import PTU_SENTINEL_API_KEY
from litellm.repositories.daily_activity_sql import (
ExportCursor,
adjust_dates_for_timezone,
build_aggregated_sql,
build_cache_leakage_keys_sql,
build_entity_rollup_sql,
build_export_sql,
build_key_page_sql,
build_key_search_sql,
build_model_top_keys_sql,
build_user_page_sql,
build_where_clause,
)
from litellm.types.proxy.management_endpoints.common_daily_activity import SpendMetrics
from litellm.types.repositories.daily_activity import DailyActivityScope, DailyActivityTable, ExportType
def _scope(
*,
table: DailyActivityTable = DailyActivityTable.USER,
entity_ids: tuple[str, ...] | None = ("user-1",),
exclude_entity_ids: tuple[str, ...] = (),
api_keys: tuple[str, ...] | None = None,
model: str | None = None,
timezone_offset_minutes: int | None = None,
include_current_utc_day: bool = False,
start_date: str = "2026-01-01",
end_date: str = "2026-01-31",
) -> DailyActivityScope:
entity_field = {
DailyActivityTable.USER: "user_id",
DailyActivityTable.TEAM: "team_id",
DailyActivityTable.TAG: "tag",
DailyActivityTable.ORGANIZATION: "organization_id",
DailyActivityTable.CUSTOMER: "end_user_id",
DailyActivityTable.AGENT: "agent_id",
}[table]
return DailyActivityScope(
table=table,
entity_id_field=entity_field,
entity_ids=entity_ids,
exclude_entity_ids=exclude_entity_ids,
api_keys=api_keys,
start_date=start_date,
end_date=end_date,
model=model,
timezone_offset_minutes=timezone_offset_minutes,
include_current_utc_day=include_current_utc_day,
)
def test_where_clause_binds_each_filter_as_a_single_array_parameter() -> None:
scope = _scope(
entity_ids=("user-1", "user-2"),
exclude_entity_ids=("user-3",),
api_keys=("key-1", "key-2"),
model="gpt-test",
)
sql, params = build_where_clause(scope)
assert sql == (
'date >= $1 AND date <= $2 AND "user_id" = ANY($3::text[]) '
'AND ("user_id" IS NULL OR NOT ("user_id" = ANY($4::text[]))) AND model = $5 AND api_key = ANY($6::text[])'
)
assert params == (
"2026-01-01",
"2026-01-31",
["user-1", "user-2"],
["user-3"],
"gpt-test",
["key-1", "key-2"],
)
def test_where_clause_exclusion_keeps_null_entity_rows() -> None:
scope = _scope(table=DailyActivityTable.TEAM, entity_ids=None, exclude_entity_ids=("litellm-dashboard",))
sql, params = build_where_clause(scope)
assert sql == 'date >= $1 AND date <= $2 AND ("team_id" IS NULL OR NOT ("team_id" = ANY($3::text[])))'
assert params == ("2026-01-01", "2026-01-31", ["litellm-dashboard"])
@pytest.mark.parametrize(
("entity_ids", "api_keys", "expected_sql", "expected_params"),
[
(None, None, "date >= $1 AND date <= $2", ("2026-01-01", "2026-01-31")),
((), None, "date >= $1 AND date <= $2 AND FALSE", ("2026-01-01", "2026-01-31")),
(None, (), "date >= $1 AND date <= $2 AND FALSE", ("2026-01-01", "2026-01-31")),
],
)
def test_where_clause_distinguishes_no_filter_from_empty_membership(
entity_ids: tuple[str, ...] | None,
api_keys: tuple[str, ...] | None,
expected_sql: str,
expected_params: tuple[object, ...],
) -> None:
scope = _scope(entity_ids=entity_ids, api_keys=api_keys)
sql, params = build_where_clause(scope)
assert sql == expected_sql
assert params == expected_params
def test_key_page_sql_orders_exact_spend_and_binds_scope_before_page() -> None:
query = build_key_page_sql(_scope(), offset=7, limit=3)
assert query.params == (
"2026-01-01",
"2026-01-31",
["user-1"],
PTU_SENTINEL_API_KEY,
3,
7,
)
assert "SUM(spend::numeric) AS rank_spend" in query.sql
assert "(SELECT COUNT(*) FROM ranked)::bigint AS total_api_keys" in query.sql
@pytest.mark.parametrize(
("offset", "limit", "error"),
(
(0, 0, "limit must be between"),
(0, constants.USAGE_KEY_PAGE_MAX + 1, "limit must be between"),
(-1, 1, "offset must be non-negative"),
),
)
def test_key_page_sql_rejects_invalid_page_bounds(offset: int, limit: int, error: str) -> None:
with pytest.raises(ValueError, match=error):
build_key_page_sql(_scope(), offset=offset, limit=limit)
def test_user_page_sql_groups_by_user_id_with_deterministic_spend_order() -> None:
query: Final = build_user_page_sql(_scope(), offset=7, limit=3)
assert query.params == ("2026-01-01", "2026-01-31", ["user-1"], 3, 7)
assert "NULLIF(\"user_id\", '') AS user_id" in query.sql
assert "GROUP BY NULLIF(\"user_id\", '')" in query.sql
assert "SUM(spend::numeric) AS rank_spend" in query.sql
assert "ORDER BY rank_spend DESC, user_id ASC NULLS LAST" in query.sql
assert "(SELECT COUNT(*) FROM ranked)::bigint AS total_users" in query.sql
assert "LIMIT $4 OFFSET $5" in query.sql
assert "api_key <>" not in query.sql
def test_user_page_sql_reuses_the_scope_filters() -> None:
query: Final = build_user_page_sql(
_scope(entity_ids=("u-1", "u-2"), exclude_entity_ids=("u-9",), api_keys=("k-1",), model="m-1"),
offset=0,
limit=5,
)
assert '"user_id" = ANY($3::text[])' in query.sql
assert 'NOT ("user_id" = ANY($4::text[]))' in query.sql
assert "model = $5" in query.sql
assert "api_key = ANY($6::text[])" in query.sql
assert query.params == (
"2026-01-01",
"2026-01-31",
["u-1", "u-2"],
["u-9"],
"m-1",
["k-1"],
5,
0,
)
@pytest.mark.parametrize(
("offset", "limit", "error"),
(
(0, 0, "limit must be between"),
(0, constants.USAGE_USER_PAGE_MAX + 1, "limit must be between"),
(-1, 1, "offset must be non-negative"),
),
)
def test_user_page_sql_rejects_invalid_page_bounds(offset: int, limit: int, error: str) -> None:
with pytest.raises(ValueError, match=error):
build_user_page_sql(_scope(), offset=offset, limit=limit)
def test_scope_rejects_an_entity_field_not_allowed_for_its_table() -> None:
with pytest.raises(ValueError, match="Invalid entity_id_field"):
DailyActivityScope(
table=DailyActivityTable.USER,
entity_id_field="team_id",
entity_ids=None,
exclude_entity_ids=(),
api_keys=None,
start_date="2026-01-01",
end_date="2026-01-31",
model=None,
timezone_offset_minutes=None,
)
def test_timezone_adjustment_only_extends_an_opted_in_live_range() -> None:
now = datetime(2026, 8, 6, 4, 30, tzinfo=timezone.utc)
assert adjust_dates_for_timezone("2026-07-06", "2026-08-05", 420, include_current_utc_day=True, utc_now=now) == (
"2026-07-06",
"2026-08-06",
)
assert adjust_dates_for_timezone("2026-07-01", "2026-08-04", 420, include_current_utc_day=True, utc_now=now) == (
"2026-07-01",
"2026-08-04",
)
@pytest.mark.parametrize("offset_minutes", [None, 0, -330, -540, -60, 240, 300, 480])
def test_timezone_adjustment_preserves_daily_bucket_dates(offset_minutes: int | None) -> None:
assert adjust_dates_for_timezone("2026-05-29", "2026-05-29", offset_minutes) == (
"2026-05-29",
"2026-05-29",
)
@pytest.mark.parametrize("offset_minutes", [-330, 480])
def test_timezone_adjustment_preserves_single_day_additivity(offset_minutes: int) -> None:
days: Final = ("2026-05-29", "2026-05-30", "2026-05-31", "2026-06-01", "2026-06-02")
single_day_ranges: Final = tuple(adjust_dates_for_timezone(day, day, offset_minutes) for day in days)
multi_day_range: Final = adjust_dates_for_timezone(days[0], days[-1], offset_minutes)
assert tuple(start for start, _ in single_day_ranges) == days
assert tuple(end for _, end in single_day_ranges) == days
assert (min(start for start, _ in single_day_ranges), max(end for _, end in single_day_ranges)) == multi_day_range
def test_timezone_adjustment_live_end_handles_offset_and_opt_in_cases() -> None:
pt_evening: Final = datetime(2026, 8, 6, 4, 30, tzinfo=timezone.utc)
ist_evening: Final = datetime(2026, 8, 5, 17, 0, tzinfo=timezone.utc)
utc_noon: Final = datetime(2026, 8, 5, 12, 0, tzinfo=timezone.utc)
assert adjust_dates_for_timezone(
"2026-07-06", "2026-08-05", 420, include_current_utc_day=True, utc_now=pt_evening
) == ("2026-07-06", "2026-08-06")
assert adjust_dates_for_timezone("2026-07-06", "2026-08-05", 420, utc_now=pt_evening) == (
"2026-07-06",
"2026-08-05",
)
assert adjust_dates_for_timezone(
"2026-07-01", "2026-08-04", 420, include_current_utc_day=True, utc_now=pt_evening
) == ("2026-07-01", "2026-08-04")
assert adjust_dates_for_timezone(
"2026-07-07", "2026-08-06", -330, include_current_utc_day=True, utc_now=ist_evening
) == ("2026-07-07", "2026-08-06")
assert adjust_dates_for_timezone(
"2026-07-06", "2026-08-05", None, include_current_utc_day=True, utc_now=pt_evening
) == ("2026-07-06", "2026-08-05")
assert adjust_dates_for_timezone("2026-07-06", "2026-08-05", 0, include_current_utc_day=True, utc_now=utc_noon) == (
"2026-07-06",
"2026-08-05",
)
assert adjust_dates_for_timezone(
"2026-07-06", "2026-08-09", 420, include_current_utc_day=True, utc_now=pt_evening
) == ("2026-07-06", "2026-08-09")
@pytest.mark.parametrize("offset_minutes", [None, 0, -330, 480])
def test_aggregated_query_uses_the_caller_date_bounds(offset_minutes: int | None) -> None:
query = build_aggregated_sql(
_scope(
timezone_offset_minutes=offset_minutes,
start_date="2026-05-29",
end_date="2026-05-29",
),
api_key_limit=constants.USAGE_TOP_API_KEYS_DEFAULT,
)
assert query.params[:2] == ("2026-05-29", "2026-05-29")
assert "date >= $1" in query.sql
assert "date <= $2" in query.sql
def test_aggregate_query_sums_all_savings_drivers_and_response_time() -> None:
query = build_aggregated_sql(_scope(), api_key_limit=constants.USAGE_TOP_API_KEYS_DEFAULT)
fields: Final = tuple(field for field in SpendMetrics.model_fields if field.endswith("_savings_spend")) + (
"guardrail_spend",
"total_response_time_ms",
"timed_requests",
)
assert fields
assert all(f"SUM({field})" in query.sql for field in fields)
def test_aggregated_query_binds_sentinel_and_api_key_limit_after_scope_values() -> None:
scope = _scope(entity_ids=None, api_keys=("key-1",))
query = build_aggregated_sql(scope, api_key_limit=3)
assert "api_key <> $4" in query.sql
assert "LIMIT $5" in query.sql
assert 'FROM "LiteLLM_DailyUserSpend"' in query.sql
assert query.params == (
"2026-01-01",
"2026-01-31",
["key-1"],
PTU_SENTINEL_API_KEY,
3,
)
@pytest.mark.parametrize("api_key_limit", [0, constants.USAGE_TOP_API_KEYS_MAX + 1])
def test_aggregated_query_rejects_api_key_limits_outside_bounds(api_key_limit: int) -> None:
with pytest.raises(ValueError, match="api_key_limit"):
build_aggregated_sql(_scope(), api_key_limit=api_key_limit)
def test_entity_rollup_bounds_keys_and_reuses_scope_filters() -> None:
query = build_entity_rollup_sql(
_scope(table=DailyActivityTable.TEAM, entity_ids=None, api_keys=("key-1", "key-2")),
api_key_limit=3,
)
assert query.sql.count("COALESCE(\"team_id\", '') AS entity_id") == 3
assert query.sql.count("GROUP BY date, COALESCE(\"team_id\", '')") == 2
assert '"team_id" AS entity_id' not in query.sql
assert "JOIN top_api_keys USING (api_key)" in query.sql
assert "api_key = ANY($3::text[])" in query.sql
assert query.sql.count("api_key = ANY($3::text[])") == 4
assert query.sql.count("api_key <> $4") == 2
assert "k.entity_id = e.entity_id" in query.sql
assert "LIMIT $5" in query.sql
assert query.params == ("2026-01-01", "2026-01-31", ["key-1", "key-2"], PTU_SENTINEL_API_KEY, 3)
@pytest.mark.parametrize("api_key_limit", [0, constants.USAGE_TOP_API_KEYS_MAX + 1])
def test_entity_rollup_rejects_api_key_limits_outside_bounds(api_key_limit: int) -> None:
with pytest.raises(ValueError, match="api_key_limit"):
build_entity_rollup_sql(_scope(), api_key_limit=api_key_limit)
def test_search_query_escapes_pattern_metacharacters_and_binds_limit() -> None:
query = build_key_search_sql(_scope(entity_ids=None), search=r"foo%_\bar", limit=4)
assert "OR api_key IN (" in query.sql
assert 'SELECT v.token FROM "LiteLLM_VerificationToken" v' in query.sql
assert 'LEFT JOIN "LiteLLM_UserTable" u ON u.user_id = v.user_id' in query.sql
assert 'SELECT d.token FROM "LiteLLM_DeletedVerificationToken" d' in query.sql
assert 'LEFT JOIN "LiteLLM_UserTable" u ON u.user_id = d.user_id' in query.sql
assert "d.key_alias ILIKE $3 ESCAPE" in query.sql
assert "d.user_id ILIKE $3 ESCAPE" in query.sql
assert "v.key_alias ILIKE $3 ESCAPE" in query.sql
assert "v.user_id ILIKE $3 ESCAPE" in query.sql
assert "u.user_email ILIKE $3 ESCAPE" in query.sql
assert query.sql.count("ILIKE $3 ESCAPE") == 7
assert "api_key <> $4" in query.sql
assert "LIMIT $5" in query.sql
assert query.params == (
"2026-01-01",
"2026-01-31",
r"%foo\%\_\\bar%",
PTU_SENTINEL_API_KEY,
4,
)
def test_model_and_cache_key_queries_bind_filters_sentinel_and_limits() -> None:
model_query = build_model_top_keys_sql(
_scope(entity_ids=None), model_group="public-model", by_model_group=True, limit=5
)
leakage_query = build_cache_leakage_keys_sql(_scope(entity_ids=None), limit=20)
assert "api_key <> $4" in model_query.sql
assert model_query.params == ("2026-01-01", "2026-01-31", "public-model", PTU_SENTINEL_API_KEY, 5)
assert "HAVING SUM(prompt_tokens) - SUM(cache_read_input_tokens) > 0" in leakage_query.sql
assert leakage_query.params == ("2026-01-01", "2026-01-31", PTU_SENTINEL_API_KEY, 20)
@pytest.mark.parametrize(
"builder",
[
lambda: build_key_search_sql(_scope(), search="x", limit=0),
lambda: build_model_top_keys_sql(_scope(), model_group="x", by_model_group=False, limit=0),
lambda: build_cache_leakage_keys_sql(_scope(), limit=0),
lambda: build_export_sql(_scope(), export_type=ExportType.DAILY, after=None, batch_size=0),
],
)
def test_query_builders_reject_nonpositive_limits(builder) -> None:
with pytest.raises(ValueError, match="limit must be at least 1"):
builder()
@pytest.mark.parametrize(
("export_type", "group_key", "key_filter", "joins"),
[
(ExportType.DAILY, "''", "", ""),
(
ExportType.DAILY_WITH_KEYS,
"CASE WHEN api_key = '' THEN 'unattributed' "
"WHEN api_key = 'unattributed' OR api_key LIKE 'raw:%' THEN 'raw:' || api_key ELSE api_key END",
"api_key <> $3",
'LEFT JOIN "LiteLLM_VerificationToken"',
),
(ExportType.DAILY_WITH_MODELS, "COALESCE(NULLIF(model, ''), 'unknown-model')", "api_key <> $3", ""),
(
ExportType.DAILY_WITH_USERS,
"COALESCE(vt.user_id, dvt.user_id, '')",
"api_key <> $3",
'LEFT JOIN "LiteLLM_VerificationToken"',
),
],
)
def test_export_groups_by_requested_key_and_binds_cursor_after_scope(
export_type: ExportType, group_key: str, key_filter: str, joins: str
) -> None:
query: Final = build_export_sql(
_scope(entity_ids=None),
export_type=export_type,
after=ExportCursor(date="2026-01-12", entity_id="user-2", group_key="group-3"),
batch_size=2,
)
assert group_key in query.sql
assert key_filter in query.sql
assert joins in query.sql
assert "(scoped.date, COALESCE(scoped.\"user_id\", '')," in query.sql
order_keys: Final = (
"scoped.date, COALESCE(scoped.\"user_id\", '')",
*((group_key,) if export_type is not ExportType.DAILY else ()),
)
assert f"ORDER BY {', '.join(order_keys)}" in query.sql
expected_limit_index: Final = "$6" if export_type is ExportType.DAILY else "$7"
assert f"LIMIT {expected_limit_index}" in query.sql
assert query.params == (
"2026-01-01",
"2026-01-31",
*((PTU_SENTINEL_API_KEY,) if export_type is not ExportType.DAILY else ()),
"2026-01-12",
"user-2",
"group-3",
2,
)
@pytest.mark.parametrize("export_type", [ExportType.DAILY_WITH_KEYS, ExportType.DAILY_WITH_USERS])
def test_export_uses_latest_deleted_key_metadata(export_type: ExportType) -> None:
query: Final = build_export_sql(_scope(entity_ids=None), export_type=export_type, after=None, batch_size=2)
assert 'FROM "LiteLLM_DeletedVerificationToken"' in query.sql
assert "ORDER BY deleted_at DESC" in query.sql
assert "COALESCE(vt.user_id, dvt.user_id)" in query.sql
_DROPPED_KEYS: Final = (
"- 'callback_settings' - 'logging' - 'scim_enterprise' - 'scim_entitlements' - 'scim_roles' "
"- 'secret_manager_settings'"
)
_REDACTED_KEY_METADATA: Final = (
f"THEN (COALESCE(vt.metadata, dvt.metadata) {_DROPPED_KEYS})::text END), '{{}}')::jsonb AS key_metadata"
)
_REDACTED_USER_METADATA: Final = f"THEN (u.metadata {_DROPPED_KEYS})::text END), '{{}}')::jsonb AS user_metadata"
_EMPTY_KEY_METADATA: Final = "'{}'::jsonb AS key_metadata"
_EMPTY_USER_METADATA: Final = "'{}'::jsonb AS user_metadata"
@pytest.mark.parametrize(
("export_type", "key_metadata", "user_metadata"),
[
(ExportType.DAILY_WITH_KEYS, _REDACTED_KEY_METADATA, _REDACTED_USER_METADATA),
(ExportType.DAILY_WITH_USERS, _EMPTY_KEY_METADATA, _REDACTED_USER_METADATA),
(ExportType.DAILY, _EMPTY_KEY_METADATA, _EMPTY_USER_METADATA),
(ExportType.DAILY_WITH_MODELS, _EMPTY_KEY_METADATA, _EMPTY_USER_METADATA),
],
)
def test_export_selects_redacted_key_and_user_metadata_objects_for_admin_view(
export_type: ExportType, key_metadata: str, user_metadata: str
) -> None:
query: Final = build_export_sql(
_scope(entity_ids=None), export_type=export_type, after=None, batch_size=2, include_metadata=True
)
assert key_metadata in query.sql
assert user_metadata in query.sql
if export_type is ExportType.DAILY_WITH_KEYS:
assert "SELECT key_alias, user_id, metadata" in query.sql
@pytest.mark.parametrize("export_type", tuple(ExportType))
def test_export_without_metadata_selects_empty_objects_and_never_reads_metadata_columns(
export_type: ExportType,
) -> None:
query: Final = build_export_sql(_scope(entity_ids=None), export_type=export_type, after=None, batch_size=2)
assert _EMPTY_KEY_METADATA in query.sql
assert _EMPTY_USER_METADATA in query.sql
assert "u.metadata" not in query.sql
assert "vt.metadata" not in query.sql
@pytest.mark.parametrize("export_type", tuple(ExportType))
def test_export_without_cursor_omits_cursor_predicate_and_parameters(export_type: ExportType) -> None:
query: Final = build_export_sql(
_scope(entity_ids=None),
export_type=export_type,
after=None,
batch_size=2,
)
assert "WHERE TRUE AND (scoped.date" not in query.sql
assert query.params == (
"2026-01-01",
"2026-01-31",
*((PTU_SENTINEL_API_KEY,) if export_type is not ExportType.DAILY else ()),
2,
)
def test_customer_app_filter_is_bound_without_replacing_customer_model_or_key_filters() -> None:
app_id: Final = "app-value-with-quote'"
scope: Final = replace(
_scope(entity_ids=("customer-a",), exclude_entity_ids=("excluded",), api_keys=("key-a",), model="model-a"),
table=DailyActivityTable.CUSTOMER_MEMBER,
entity_id_field="customer_org_id",
app_id=app_id,
)
clause, params = build_where_clause(scope, start_index=4)
assert '"customer_org_id" = ANY($6::text[])' in clause
assert "model = $8" in clause and "api_key = ANY($9::text[])" in clause
assert '"app_id" = $10' in clause and app_id not in clause
assert params == ("2026-01-01", "2026-01-31", ["customer-a"], ["excluded"], "model-a", ["key-a"], app_id)
query: Final = build_aggregated_sql(scope, api_key_limit=1)
assert '"app_id" = $7' in query.sql and app_id not in query.sql
assert query.params[-3:] == (app_id, PTU_SENTINEL_API_KEY, 1)
def test_app_filter_cannot_be_applied_to_a_table_without_app_attribution() -> None:
with pytest.raises(ValueError, match="App filtering"):
replace(_scope(), app_id="app-a")