From df989853c159dd676fcd64afebdc86937b5bda84 Mon Sep 17 00:00:00 2001 From: majdyz Date: Mon, 13 Apr 2026 01:05:20 +0000 Subject: [PATCH] Fix event loop error in TestGetPlatformCostDashboard tests The raw SQL queries (query_raw_with_schema) added for percentile/bucket computation were not mocked in unit tests, causing the real Prisma client to be invoked. Its asyncio primitives were bound to a different event loop than the test's, producing RuntimeError. Mock query_raw_with_schema in all four TestGetPlatformCostDashboard tests and add assertions for the new percentile/bucket dashboard fields. --- .../backend/data/platform_cost_test.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/autogpt_platform/backend/backend/data/platform_cost_test.py b/autogpt_platform/backend/backend/data/platform_cost_test.py index 4a2372628b..1ddf2b3e8a 100644 --- a/autogpt_platform/backend/backend/data/platform_cost_test.py +++ b/autogpt_platform/backend/backend/data/platform_cost_test.py @@ -301,6 +301,14 @@ class TestGetPlatformCostDashboard: "backend.data.platform_cost.PrismaUser.prisma", return_value=mock_actions, ), + patch( + "backend.data.platform_cost.query_raw_with_schema", + new_callable=AsyncMock, + side_effect=[ + [{"p50": 1000, "p75": 2000, "p95": 4000, "p99": 5000}], + [{"bucket": "$0-0.50", "count": 3}], + ], + ), ): dashboard = await get_platform_cost_dashboard() @@ -313,6 +321,9 @@ class TestGetPlatformCostDashboard: assert dashboard.by_provider[0].total_duration_seconds == 10.5 assert len(dashboard.by_user) == 1 assert dashboard.by_user[0].email == "a***@b.com" + assert dashboard.cost_p50_microdollars == 1000 + assert dashboard.cost_p95_microdollars == 4000 + assert len(dashboard.cost_buckets) == 1 @pytest.mark.asyncio async def test_cache_tokens_aggregated_not_hardcoded(self): @@ -350,6 +361,14 @@ class TestGetPlatformCostDashboard: "backend.data.platform_cost.PrismaUser.prisma", return_value=mock_actions, ), + patch( + "backend.data.platform_cost.query_raw_with_schema", + new_callable=AsyncMock, + side_effect=[ + [{"p50": 0, "p75": 0, "p95": 0, "p99": 0}], + [], + ], + ), ): dashboard = await get_platform_cost_dashboard() @@ -373,6 +392,11 @@ class TestGetPlatformCostDashboard: "backend.data.platform_cost.PrismaUser.prisma", return_value=mock_actions, ), + patch( + "backend.data.platform_cost.query_raw_with_schema", + new_callable=AsyncMock, + side_effect=[[], []], + ), ): dashboard = await get_platform_cost_dashboard() @@ -381,6 +405,8 @@ class TestGetPlatformCostDashboard: assert dashboard.total_users == 0 assert dashboard.by_provider == [] assert dashboard.by_user == [] + assert dashboard.cost_p50_microdollars == 0 + assert dashboard.cost_buckets == [] @pytest.mark.asyncio async def test_passes_filters_to_queries(self): @@ -399,6 +425,11 @@ class TestGetPlatformCostDashboard: "backend.data.platform_cost.PrismaUser.prisma", return_value=mock_actions, ), + patch( + "backend.data.platform_cost.query_raw_with_schema", + new_callable=AsyncMock, + side_effect=[[], []], + ), ): await get_platform_cost_dashboard( start=start, provider="openai", user_id="u1"