|
| 1 | +from datetime import date, timedelta |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from stream_chat.async_chat import StreamChatAsync |
| 6 | + |
| 7 | + |
| 8 | +class TestTeamUsageStats: |
| 9 | + @pytest.mark.asyncio |
| 10 | + async def test_query_team_usage_stats_default(self, client: StreamChatAsync): |
| 11 | + """Test querying team usage stats with default options.""" |
| 12 | + response = await client.query_team_usage_stats() |
| 13 | + assert "teams" in response |
| 14 | + assert isinstance(response["teams"], list) |
| 15 | + |
| 16 | + @pytest.mark.asyncio |
| 17 | + async def test_query_team_usage_stats_with_month(self, client: StreamChatAsync): |
| 18 | + """Test querying team usage stats with month parameter.""" |
| 19 | + current_month = date.today().strftime("%Y-%m") |
| 20 | + response = await client.query_team_usage_stats(month=current_month) |
| 21 | + assert "teams" in response |
| 22 | + assert isinstance(response["teams"], list) |
| 23 | + |
| 24 | + @pytest.mark.asyncio |
| 25 | + async def test_query_team_usage_stats_with_date_range( |
| 26 | + self, client: StreamChatAsync |
| 27 | + ): |
| 28 | + """Test querying team usage stats with date range.""" |
| 29 | + end_date = date.today() |
| 30 | + start_date = end_date - timedelta(days=7) |
| 31 | + response = await client.query_team_usage_stats( |
| 32 | + start_date=start_date.strftime("%Y-%m-%d"), |
| 33 | + end_date=end_date.strftime("%Y-%m-%d"), |
| 34 | + ) |
| 35 | + assert "teams" in response |
| 36 | + assert isinstance(response["teams"], list) |
| 37 | + |
| 38 | + @pytest.mark.asyncio |
| 39 | + async def test_query_team_usage_stats_with_pagination( |
| 40 | + self, client: StreamChatAsync |
| 41 | + ): |
| 42 | + """Test querying team usage stats with pagination.""" |
| 43 | + response = await client.query_team_usage_stats(limit=10) |
| 44 | + assert "teams" in response |
| 45 | + assert isinstance(response["teams"], list) |
| 46 | + |
| 47 | + # If there's a next cursor, test fetching the next page |
| 48 | + if response.get("next"): |
| 49 | + next_response = await client.query_team_usage_stats( |
| 50 | + limit=10, next=response["next"] |
| 51 | + ) |
| 52 | + assert "teams" in next_response |
| 53 | + assert isinstance(next_response["teams"], list) |
| 54 | + |
| 55 | + @pytest.mark.asyncio |
| 56 | + async def test_query_team_usage_stats_response_structure( |
| 57 | + self, client: StreamChatAsync |
| 58 | + ): |
| 59 | + """Test that response contains expected metric fields when data exists.""" |
| 60 | + # Query last year to maximize chance of getting data |
| 61 | + end_date = date.today() |
| 62 | + start_date = end_date - timedelta(days=365) |
| 63 | + response = await client.query_team_usage_stats( |
| 64 | + start_date=start_date.strftime("%Y-%m-%d"), |
| 65 | + end_date=end_date.strftime("%Y-%m-%d"), |
| 66 | + ) |
| 67 | + |
| 68 | + assert "teams" in response |
| 69 | + teams = response["teams"] |
| 70 | + |
| 71 | + if teams: |
| 72 | + team = teams[0] |
| 73 | + # Verify team identifier |
| 74 | + assert "team" in team |
| 75 | + |
| 76 | + # Verify daily activity metrics |
| 77 | + assert "users_daily" in team |
| 78 | + assert "messages_daily" in team |
| 79 | + assert "translations_daily" in team |
| 80 | + assert "image_moderations_daily" in team |
| 81 | + |
| 82 | + # Verify peak metrics |
| 83 | + assert "concurrent_users" in team |
| 84 | + assert "concurrent_connections" in team |
| 85 | + |
| 86 | + # Verify rolling/cumulative metrics |
| 87 | + assert "users_total" in team |
| 88 | + assert "users_last_24_hours" in team |
| 89 | + assert "users_last_30_days" in team |
| 90 | + assert "users_month_to_date" in team |
| 91 | + assert "users_engaged_last_30_days" in team |
| 92 | + assert "users_engaged_month_to_date" in team |
| 93 | + assert "messages_total" in team |
| 94 | + assert "messages_last_24_hours" in team |
| 95 | + assert "messages_last_30_days" in team |
| 96 | + assert "messages_month_to_date" in team |
| 97 | + |
| 98 | + # Verify metric structure |
| 99 | + assert "total" in team["users_daily"] |
0 commit comments