|
| 1 | +import pytest |
| 2 | +from typing import Dict, Any |
| 3 | + |
| 4 | +from stream_chat.async_chat import StreamChatAsync |
| 5 | +from stream_chat.types.stream_response import StreamResponse |
| 6 | + |
| 7 | +@pytest.mark.incremental |
| 8 | +class TestQueryThreads: |
| 9 | + @pytest.mark.asyncio |
| 10 | + async def test_query_threads(self, client: StreamChatAsync, channel, random_user: Dict): |
| 11 | + # Create a thread with some messages |
| 12 | + parent_message = await channel.send_message({"text": "Parent message"}, random_user["id"]) |
| 13 | + thread_message = await channel.send_message( |
| 14 | + {"text": "Thread message", "parent_id": parent_message["message"]["id"]}, |
| 15 | + random_user["id"] |
| 16 | + ) |
| 17 | + |
| 18 | + # Query threads with filter and sort |
| 19 | + filter_conditions = {"parent_id": parent_message["message"]["id"]} |
| 20 | + sort_conditions = [{"field": "created_at", "direction": -1}] |
| 21 | + |
| 22 | + response = await client.query_threads( |
| 23 | + filter=filter_conditions, |
| 24 | + sort=sort_conditions |
| 25 | + ) |
| 26 | + |
| 27 | + assert isinstance(response, StreamResponse) |
| 28 | + assert "threads" in response |
| 29 | + assert len(response["threads"]) > 0 |
| 30 | + |
| 31 | + # Verify the thread message is in the response |
| 32 | + thread = response["threads"][0] |
| 33 | + assert "latest_replies" in thread |
| 34 | + assert len(thread["latest_replies"]) > 0 |
| 35 | + assert thread["latest_replies"][0]["text"] == thread_message["message"]["text"] |
| 36 | + |
| 37 | + @pytest.mark.asyncio |
| 38 | + async def test_query_threads_with_options(self, client: StreamChatAsync, channel, random_user: Dict): |
| 39 | + # Create a thread with multiple messages |
| 40 | + parent_message = await channel.send_message({"text": "Parent message"}, random_user["id"]) |
| 41 | + thread_messages = [] |
| 42 | + for i in range(3): |
| 43 | + msg = await channel.send_message( |
| 44 | + {"text": f"Thread message {i}", "parent_id": parent_message["message"]["id"]}, |
| 45 | + random_user["id"] |
| 46 | + ) |
| 47 | + thread_messages.append(msg) |
| 48 | + |
| 49 | + # Query threads with limit and offset |
| 50 | + filter_conditions = {"parent_id": parent_message["message"]["id"]} |
| 51 | + sort_conditions = [{"field": "created_at", "direction": -1}] |
| 52 | + |
| 53 | + response = await client.query_threads( |
| 54 | + filter=filter_conditions, |
| 55 | + sort=sort_conditions, |
| 56 | + limit=1, |
| 57 | + ) |
| 58 | + |
| 59 | + assert isinstance(response, StreamResponse) |
| 60 | + assert "threads" in response |
| 61 | + assert len(response["threads"]) == 1 |
| 62 | + assert "next" in response |
0 commit comments