88from stream_chat .async_chat .channel import Channel
99from stream_chat .async_chat .client import StreamChatAsync
1010from stream_chat .base .exceptions import StreamAPIException
11+ from stream_chat .tests .async_chat .conftest import hard_delete_users
1112
1213
1314@pytest .mark .incremental
@@ -34,6 +35,7 @@ async def test_create_without_id(
3435
3536 await channel .create (random_users [0 ]["id" ])
3637 assert channel .id is not None
38+ await channel .delete (hard = True )
3739
3840 async def test_create_with_options (
3941 self , client : StreamChatAsync , random_users : List [Dict ]
@@ -45,6 +47,7 @@ async def test_create_with_options(
4547
4648 await channel .create (random_users [0 ]["id" ], hide_for_creator = True )
4749 assert channel .id is not None
50+ await channel .delete (hard = True )
4851
4952 async def test_send_message_with_options (self , channel : Channel , random_user : Dict ):
5053 response = await channel .send_message (
@@ -54,26 +57,23 @@ async def test_send_message_with_options(self, channel: Channel, random_user: Di
5457 assert response ["message" ]["text" ] == "hi"
5558
5659 async def test_send_message_with_restricted_visibility (
57- self , client : StreamChatAsync , channel : Channel , random_user : Dict
60+ self , channel : Channel , random_users : List [ Dict ]
5861 ):
59- # Create test users first
60- restricted_users = [
61- {"id" : "amy" , "name" : "Amy" },
62- {"id" : "paul" , "name" : "Paul" },
63- ]
64- await client .upsert_users (restricted_users )
62+ amy = random_users [0 ]["id" ]
63+ paul = random_users [1 ]["id" ]
64+ user = random_users [2 ]["id" ]
6565
6666 # Add users to channel
67- await channel .add_members ([u [ "id" ] for u in restricted_users ])
67+ await channel .add_members ([amy , paul ])
6868
6969 # Send message with restricted visibility
7070 response = await channel .send_message (
71- {"text" : "hi" , "restricted_visibility" : [" amy" , " paul" ]}, random_user [ "id" ]
71+ {"text" : "hi" , "restricted_visibility" : [amy , paul ]}, user
7272 )
7373
7474 assert "message" in response
7575 assert response ["message" ]["text" ] == "hi"
76- assert response ["message" ]["restricted_visibility" ] == [" amy" , " paul" ]
76+ assert response ["message" ]["restricted_visibility" ] == [amy , paul ]
7777
7878 async def test_send_event (self , channel : Channel , random_user : Dict ):
7979 response = await channel .send_event ({"type" : "typing.start" }, random_user ["id" ])
@@ -303,39 +303,44 @@ async def test_channel_hide_show(
303303 )
304304 assert len (response ["channels" ]) == 1
305305
306- async def test_invites (self , client : StreamChatAsync , channel : Channel ):
307- members = ["john" , "paul" , "george" , "pete" , "ringo" , "eric" ]
308- await client .upsert_users ([{"id" : m } for m in members ])
306+ async def test_invites (self , client : StreamChatAsync , random_users : List [Dict ]):
307+ john = random_users [0 ]["id" ]
308+ ringo = random_users [1 ]["id" ]
309+ eric = random_users [2 ]["id" ]
310+
309311 channel = client .channel (
310312 "team" ,
311313 "beatles-" + str (uuid .uuid4 ()),
312- {"members" : members , "invites" : [" ringo" , " eric" ]},
314+ {"members" : [ john ] , "invites" : [ringo , eric ]},
313315 )
314- await channel .create (" john" )
316+ await channel .create (john )
315317 # accept the invite when not a member
316318 with pytest .raises (StreamAPIException ):
317- await channel .accept_invite ("brian" )
319+ await channel .accept_invite ("brian" + str ( uuid . uuid4 ()) )
318320 # accept the invite when a member
319- accept = await channel .accept_invite (" ringo" )
321+ accept = await channel .accept_invite (ringo )
320322 for m in accept ["members" ]:
321- if m ["user_id" ] == " ringo" :
323+ if m ["user_id" ] == ringo :
322324 assert m ["invited" ] is True
323325 assert "invite_accepted_at" in m
324326 # can accept again, noop
325- await channel .accept_invite (" ringo" )
327+ await channel .accept_invite (ringo )
326328
327- reject = await channel .reject_invite (" eric" )
329+ reject = await channel .reject_invite (eric )
328330 for m in reject ["members" ]:
329- if m ["user_id" ] == " eric" :
331+ if m ["user_id" ] == eric :
330332 assert m ["invited" ] is True
331333 assert "invite_rejected_at" in m
332334 # can reject again, noop
333- await channel .reject_invite ("eric" )
335+ await channel .reject_invite (eric )
336+ await channel .delete (hard = True )
334337
335338 async def test_query_members (self , client : StreamChatAsync , channel : Channel ):
336- members = ["paul" , "george" , "john" , "jessica" , "john2" ]
337- await client .upsert_users ([{"id" : m , "name" : m } for m in members ])
338- for member in members :
339+ rand = str (uuid .uuid4 ())
340+ user_ids = ["paul" , "george" , "john" , "jessica" , "john2" ]
341+ user_ids = [f"{ n } -{ rand } " for n in user_ids ]
342+ await client .upsert_users ([{"id" : m , "name" : m } for m in user_ids ])
343+ for member in user_ids :
339344 await channel .add_members ([member ])
340345
341346 response = await channel .query_members (
@@ -346,8 +351,10 @@ async def test_query_members(self, client: StreamChatAsync, channel: Channel):
346351 )
347352
348353 assert len (response ) == 2
349- assert response [0 ]["user" ]["id" ] == "jessica"
350- assert response [1 ]["user" ]["id" ] == "john2"
354+ assert response [0 ]["user" ]["id" ] == f"jessica-{ rand } "
355+ assert response [1 ]["user" ]["id" ] == f"john2-{ rand } "
356+
357+ await hard_delete_users (client , user_ids )
351358
352359 async def test_mute_unmute (
353360 self , client : StreamChatAsync , channel : Channel , random_users : List [Dict ]
0 commit comments