|
| 1 | +import uuid |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from stream_chat.async_chat.channel import Channel |
| 7 | +from stream_chat.async_chat.client import StreamChatAsync |
| 8 | +from stream_chat.types.base import SortOrder |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.incremental |
| 12 | +class TestDraft: |
| 13 | + async def test_create_draft(self, channel: Channel, random_user: Dict): |
| 14 | + draft_message = {"text": "This is a draft message"} |
| 15 | + response = await channel.create_draft(draft_message, random_user["id"]) |
| 16 | + |
| 17 | + assert "draft" in response |
| 18 | + assert response["draft"]["message"]["text"] == "This is a draft message" |
| 19 | + assert response["draft"]["channel_cid"] == channel.cid |
| 20 | + |
| 21 | + async def test_get_draft(self, channel: Channel, random_user: Dict): |
| 22 | + # First create a draft |
| 23 | + draft_message = {"text": "This is a draft to retrieve"} |
| 24 | + await channel.create_draft(draft_message, random_user["id"]) |
| 25 | + |
| 26 | + # Then get the draft |
| 27 | + response = await channel.get_draft(random_user["id"]) |
| 28 | + |
| 29 | + assert "draft" in response |
| 30 | + assert response["draft"]["message"]["text"] == "This is a draft to retrieve" |
| 31 | + assert response["draft"]["channel_cid"] == channel.cid |
| 32 | + |
| 33 | + async def test_delete_draft(self, channel: Channel, random_user: Dict): |
| 34 | + # First create a draft |
| 35 | + draft_message = {"text": "This is a draft to delete"} |
| 36 | + await channel.create_draft(draft_message, random_user["id"]) |
| 37 | + |
| 38 | + # Then delete the draft |
| 39 | + await channel.delete_draft(random_user["id"]) |
| 40 | + |
| 41 | + # Verify it's deleted by trying to get it |
| 42 | + try: |
| 43 | + await channel.get_draft(random_user["id"]) |
| 44 | + raise AssertionError("Draft should be deleted") |
| 45 | + except Exception: |
| 46 | + # Expected behavior, draft should not be found |
| 47 | + pass |
| 48 | + |
| 49 | + async def test_thread_draft(self, channel: Channel, random_user: Dict): |
| 50 | + # First create a parent message |
| 51 | + msg = await channel.send_message({"text": "Parent message"}, random_user["id"]) |
| 52 | + parent_id = msg["message"]["id"] |
| 53 | + |
| 54 | + # Create a draft reply |
| 55 | + draft_reply = {"text": "This is a draft reply", "parent_id": parent_id} |
| 56 | + response = await channel.create_draft(draft_reply, random_user["id"]) |
| 57 | + |
| 58 | + assert "draft" in response |
| 59 | + assert response["draft"]["message"]["text"] == "This is a draft reply" |
| 60 | + assert response["draft"]["parent_id"] == parent_id |
| 61 | + |
| 62 | + # Get the draft reply |
| 63 | + response = await channel.get_draft(random_user["id"], parent_id=parent_id) |
| 64 | + |
| 65 | + assert "draft" in response |
| 66 | + assert response["draft"]["message"]["text"] == "This is a draft reply" |
| 67 | + assert response["draft"]["parent_id"] == parent_id |
| 68 | + |
| 69 | + # Delete the draft reply |
| 70 | + await channel.delete_draft(random_user["id"], parent_id=parent_id) |
| 71 | + |
| 72 | + # Verify it's deleted |
| 73 | + try: |
| 74 | + await channel.get_draft(random_user["id"], parent_id=parent_id) |
| 75 | + raise AssertionError("Thread draft should be deleted") |
| 76 | + except Exception: |
| 77 | + # Expected behavior |
| 78 | + pass |
| 79 | + |
| 80 | + async def test_query_drafts( |
| 81 | + self, client: StreamChatAsync, channel: Channel, random_user: Dict |
| 82 | + ): |
| 83 | + # Create multiple drafts in different channels |
| 84 | + draft1 = {"text": "Draft in channel 1"} |
| 85 | + await channel.create_draft(draft1, random_user["id"]) |
| 86 | + |
| 87 | + # Create another channel with a draft |
| 88 | + channel2 = client.channel("messaging", str(uuid.uuid4())) |
| 89 | + await channel2.create(random_user["id"]) |
| 90 | + |
| 91 | + draft2 = {"text": "Draft in channel 2"} |
| 92 | + await channel2.create_draft(draft2, random_user["id"]) |
| 93 | + |
| 94 | + # Query all drafts for the user |
| 95 | + response = await client.query_drafts(random_user["id"]) |
| 96 | + |
| 97 | + assert "drafts" in response |
| 98 | + assert len(response["drafts"]) == 2 |
| 99 | + |
| 100 | + # Query drafts for a specific channel |
| 101 | + response = await client.query_drafts( |
| 102 | + random_user["id"], filter={"channel_cid": channel2.cid} |
| 103 | + ) |
| 104 | + |
| 105 | + assert "drafts" in response |
| 106 | + assert len(response["drafts"]) == 1 |
| 107 | + draft = response["drafts"][0] |
| 108 | + assert draft["channel_cid"] == channel2.cid |
| 109 | + assert draft["message"]["text"] == "Draft in channel 2" |
| 110 | + |
| 111 | + # Query drafts with sort |
| 112 | + response = await client.query_drafts( |
| 113 | + random_user["id"], |
| 114 | + sort=[{"field": "created_at", "direction": SortOrder.ASC}], |
| 115 | + ) |
| 116 | + |
| 117 | + assert "drafts" in response |
| 118 | + assert len(response["drafts"]) == 2 |
| 119 | + assert response["drafts"][0]["channel_cid"] == channel.cid |
| 120 | + assert response["drafts"][1]["channel_cid"] == channel2.cid |
| 121 | + |
| 122 | + # Query drafts with pagination |
| 123 | + response = await client.query_drafts( |
| 124 | + random_user["id"], |
| 125 | + options={"limit": 1}, |
| 126 | + ) |
| 127 | + |
| 128 | + assert "drafts" in response |
| 129 | + assert len(response["drafts"]) == 1 |
| 130 | + assert response["drafts"][0]["channel_cid"] == channel2.cid |
| 131 | + |
| 132 | + assert response["next"] is not None |
| 133 | + |
| 134 | + # Query drafts with pagination |
| 135 | + response = await client.query_drafts( |
| 136 | + random_user["id"], |
| 137 | + options={"limit": 1, "next": response["next"]}, |
| 138 | + ) |
| 139 | + |
| 140 | + assert "drafts" in response |
| 141 | + assert len(response["drafts"]) == 1 |
| 142 | + assert response["drafts"][0]["channel_cid"] == channel.cid |
| 143 | + |
| 144 | + # Cleanup |
| 145 | + try: |
| 146 | + await channel2.delete() |
| 147 | + except Exception: |
| 148 | + pass |
0 commit comments