test(auth): isolate prefetch assertions from cache expiry

The three Postgres prefetch tests warm the cache, then run a series of
per-object getters against a MagicMock stand-in for the DB, asserting
'getters must be served from the prefetched cache'. When the test's
real-time DB writes and prefetch round-trip exceed DEFAULT_IN_MEMORY_TTL
(5s), the org cache entry expires and get_org_object falls through to
the MagicMock, producing 'TypeError: object MagicMock can't be used in
await expression' at the assertion point.

Freeze the in-memory cache clock with clock=lambda: 0.0 so entries never
tick past their TTL during the test. The behaviour under test (join +
prefetch + cache serve) is unaffected.

Co-authored-by: Krrish Dholakia <krrish-berri-2@users.noreply.github.com>
This commit is contained in:
Cursor Agent 2026-09-13 21:20:17 +00:00
parent 9ab4d4fb15
commit 7fd2c3bb8a
No known key found for this signature in database

View file

@ -58,7 +58,7 @@ async def test_join_binds_the_membership_to_the_requested_team(prisma):
data={"user_id": user_id, "team_id": team_b, "litellm_budget_table": {"connect": {"budget_id": f"b-{run}"}}}
)
cache = UserApiKeyCache(in_memory_cache=InMemoryCache(), redis_cache=None)
cache = UserApiKeyCache(in_memory_cache=InMemoryCache(clock=lambda: 0.0), redis_cache=None)
refs = AuthObjectRefs(user_id=user_id, team_id=team_a, membership_user_id=user_id, organization_id=org_id)
await prefetch_auth_objects(refs=refs, user_api_key_cache=cache, prisma_client=prisma)
@ -100,7 +100,7 @@ async def test_join_reads_team_model_aliases_from_the_mapped_column(prisma):
where={"team_id": team_id}, include={"litellm_model_table": True}
)
cache = UserApiKeyCache(in_memory_cache=InMemoryCache(), redis_cache=None)
cache = UserApiKeyCache(in_memory_cache=InMemoryCache(clock=lambda: 0.0), redis_cache=None)
refs = AuthObjectRefs(user_id=None, team_id=team_id, membership_user_id=None, organization_id=None)
await prefetch_auth_objects(refs=refs, user_api_key_cache=cache, prisma_client=prisma)
@ -144,7 +144,7 @@ async def test_join_reads_null_nested_lists_the_way_prisma_does(prisma):
where={"user_id_team_id": {"user_id": user_id, "team_id": team_id}}, include={"litellm_budget_table": True}
)
cache = UserApiKeyCache(in_memory_cache=InMemoryCache(), redis_cache=None)
cache = UserApiKeyCache(in_memory_cache=InMemoryCache(clock=lambda: 0.0), redis_cache=None)
refs = AuthObjectRefs(user_id=user_id, team_id=team_id, membership_user_id=user_id, organization_id=None)
await prefetch_auth_objects(refs=refs, user_api_key_cache=cache, prisma_client=prisma)