|
| 1 | +import uuid |
| 2 | +from datetime import datetime, timedelta, timezone |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from stream_chat.async_chat import StreamChatAsync |
| 7 | +from stream_chat.base.exceptions import StreamAPIException |
| 8 | + |
| 9 | + |
| 10 | +class TestReminders: |
| 11 | + @pytest.mark.asyncio |
| 12 | + async def test_create_reminder(self, client: StreamChatAsync, channel, random_user): |
| 13 | + # First, send a message to create a reminder for |
| 14 | + message_data = { |
| 15 | + "text": "This is a test message for reminder", |
| 16 | + } |
| 17 | + response = await channel.send_message(message_data, random_user["id"]) |
| 18 | + message_id = response["message"]["id"] |
| 19 | + |
| 20 | + # Create a reminder without remind_at |
| 21 | + response = await client.create_reminder(message_id, random_user["id"]) |
| 22 | + # Verify the response contains the expected data |
| 23 | + assert response is not None |
| 24 | + assert "reminder" in response |
| 25 | + assert response["reminder"]["message_id"] == message_id |
| 26 | + assert "user_id" in response["reminder"] |
| 27 | + |
| 28 | + # Clean up - try to delete the reminder |
| 29 | + try: |
| 30 | + await client.delete_reminder(message_id, random_user["id"]) |
| 31 | + except StreamAPIException: |
| 32 | + pass # It's okay if deletion fails |
| 33 | + |
| 34 | + @pytest.mark.asyncio |
| 35 | + async def test_create_reminder_with_remind_at( |
| 36 | + self, client: StreamChatAsync, channel, random_user |
| 37 | + ): |
| 38 | + # First, send a message to create a reminder for |
| 39 | + message_data = { |
| 40 | + "text": "This is a test message for reminder with time", |
| 41 | + } |
| 42 | + response = await channel.send_message(message_data, random_user["id"]) |
| 43 | + message_id = response["message"]["id"] |
| 44 | + |
| 45 | + # Create a reminder with remind_at |
| 46 | + remind_at = datetime.now(timezone.utc) + timedelta(days=1) |
| 47 | + response = await client.create_reminder( |
| 48 | + message_id, random_user["id"], remind_at |
| 49 | + ) |
| 50 | + # Verify the response contains the expected data |
| 51 | + assert response is not None |
| 52 | + assert "reminder" in response |
| 53 | + assert response["reminder"]["message_id"] == message_id |
| 54 | + assert "user_id" in response["reminder"] |
| 55 | + assert "remind_at" in response["reminder"] |
| 56 | + |
| 57 | + # Clean up - try to delete the reminder |
| 58 | + try: |
| 59 | + await client.delete_reminder(message_id, random_user["id"]) |
| 60 | + except StreamAPIException: |
| 61 | + pass # It's okay if deletion fails |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_update_reminder(self, client: StreamChatAsync, channel, random_user): |
| 65 | + # First, send a message to create a reminder for |
| 66 | + message_data = { |
| 67 | + "text": "This is a test message for updating reminder", |
| 68 | + } |
| 69 | + response = await channel.send_message(message_data, random_user["id"]) |
| 70 | + message_id = response["message"]["id"] |
| 71 | + |
| 72 | + # Create a reminder |
| 73 | + await client.create_reminder(message_id, random_user["id"]) |
| 74 | + |
| 75 | + # Update the reminder with a remind_at time |
| 76 | + remind_at = datetime.now(timezone.utc) + timedelta(days=2) |
| 77 | + response = await client.update_reminder( |
| 78 | + message_id, random_user["id"], remind_at |
| 79 | + ) |
| 80 | + # Verify the response contains the expected data |
| 81 | + assert response is not None |
| 82 | + assert "reminder" in response |
| 83 | + assert response["reminder"]["message_id"] == message_id |
| 84 | + assert "user_id" in response["reminder"] |
| 85 | + assert "remind_at" in response["reminder"] |
| 86 | + |
| 87 | + # Clean up - try to delete the reminder |
| 88 | + try: |
| 89 | + await client.delete_reminder(message_id, random_user["id"]) |
| 90 | + except StreamAPIException: |
| 91 | + pass # It's okay if deletion fails |
| 92 | + |
| 93 | + @pytest.mark.asyncio |
| 94 | + async def test_delete_reminder(self, client: StreamChatAsync, channel, random_user): |
| 95 | + # First, send a message to create a reminder for |
| 96 | + message_data = { |
| 97 | + "text": "This is a test message for deleting reminder", |
| 98 | + } |
| 99 | + response = await channel.send_message(message_data, random_user["id"]) |
| 100 | + message_id = response["message"]["id"] |
| 101 | + |
| 102 | + # Create a reminder |
| 103 | + await client.create_reminder(message_id, random_user["id"]) |
| 104 | + |
| 105 | + # Delete the reminder |
| 106 | + response = await client.delete_reminder(message_id, random_user["id"]) |
| 107 | + # Verify the response contains the expected data |
| 108 | + assert response is not None |
| 109 | + # The delete response may not include the reminder object |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_query_reminders(self, client: StreamChatAsync, channel, random_user): |
| 113 | + # First, send messages to create reminders for |
| 114 | + message_ids = [] |
| 115 | + channel_cid = channel.cid |
| 116 | + |
| 117 | + for i in range(3): |
| 118 | + message_data = { |
| 119 | + "text": f"This is test message {i} for querying reminders", |
| 120 | + } |
| 121 | + response = await channel.send_message(message_data, random_user["id"]) |
| 122 | + message_id = response["message"]["id"] |
| 123 | + message_ids.append(message_id) |
| 124 | + |
| 125 | + # Create a reminder with different remind_at times |
| 126 | + remind_at = datetime.now(timezone.utc) + timedelta(hours=i + 1) |
| 127 | + await client.create_reminder(message_id, random_user["id"], remind_at) |
| 128 | + |
| 129 | + # Test case 1: Query reminders without filters |
| 130 | + response = await client.query_reminders(random_user["id"]) |
| 131 | + assert response is not None |
| 132 | + assert "reminders" in response |
| 133 | + # Check that we have at least our 3 reminders |
| 134 | + assert len(response["reminders"]) >= 3 |
| 135 | + |
| 136 | + # Check that at least some of our message IDs are in the results |
| 137 | + found_ids = [ |
| 138 | + r["message_id"] |
| 139 | + for r in response["reminders"] |
| 140 | + if r["message_id"] in message_ids |
| 141 | + ] |
| 142 | + assert len(found_ids) > 0 |
| 143 | + |
| 144 | + # Test case 2: Query reminders by message ID |
| 145 | + if len(message_ids) > 0: |
| 146 | + filter_conditions = {"message_id": {"$in": [message_ids[0]]}} |
| 147 | + response = await client.query_reminders( |
| 148 | + random_user["id"], filter_conditions |
| 149 | + ) |
| 150 | + assert response is not None |
| 151 | + |
| 152 | + # Test case 3: Query reminders by channel CID |
| 153 | + filter_conditions = {"channel_cid": channel_cid} |
| 154 | + response = await client.query_reminders(random_user["id"], filter_conditions) |
| 155 | + assert response is not None |
| 156 | + |
| 157 | + # Clean up - try to delete the reminders |
| 158 | + for message_id in message_ids: |
| 159 | + try: |
| 160 | + await client.delete_reminder(message_id, random_user["id"]) |
| 161 | + except StreamAPIException: |
| 162 | + pass # It's okay if deletion fails |
0 commit comments