Skip to content

Commit 0d07788

Browse files
author
Rafael Marinho
committed
fix unit tests
1 parent ba01362 commit 0d07788

File tree

4 files changed

+171
-24
lines changed

4 files changed

+171
-24
lines changed

stream_chat/tests/async_chat/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,25 @@ async def hard_delete_users(client: StreamChatAsync, user_ids: List[str]):
141141
)
142142
except Exception:
143143
pass
144+
145+
146+
async def cleanup_blocklists_async(client: StreamChatAsync):
147+
"""Clean up test blocklists"""
148+
try:
149+
# Delete common test blocklist names
150+
test_blocklists = ["Foo", "TestBlocklist", "TestBlocklistAsync"]
151+
for blocklist_name in test_blocklists:
152+
try:
153+
await client.delete_blocklist(blocklist_name)
154+
except Exception:
155+
pass # Blocklist might not exist
156+
except Exception:
157+
pass
158+
159+
160+
@pytest.fixture(autouse=True, scope="session")
161+
async def cleanup_test_data():
162+
"""Global cleanup for test data"""
163+
yield
164+
# This runs after all tests complete
165+
# Add any global cleanup here if needed

stream_chat/tests/async_chat/test_client.py

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -722,26 +722,77 @@ async def test_query_channels_members_in(
722722
assert len(response["channels"][0]["members"]) == 9
723723

724724
async def test_create_blocklist(self, client: StreamChatAsync):
725-
await client.create_blocklist(name="Foo", words=["fudge", "heck"], type="word")
725+
blocklist_name = f"TestBlocklistAsync_{uuid.uuid4().hex[:8]}"
726+
await client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
727+
728+
# Clean up after test
729+
try:
730+
await client.delete_blocklist(blocklist_name)
731+
except Exception:
732+
pass
726733

727734
async def test_list_blocklists(self, client: StreamChatAsync):
728-
response = await client.list_blocklists()
729-
assert len(response["blocklists"]) == 2
730-
blocklist_names = {blocklist["name"] for blocklist in response["blocklists"]}
731-
assert "Foo" in blocklist_names
735+
# First, clean up any existing test blocklists
736+
await cleanup_blocklists_async(client)
737+
738+
# Create a test blocklist
739+
blocklist_name = f"TestBlocklistAsync_{uuid.uuid4().hex[:8]}"
740+
await client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
741+
742+
try:
743+
response = await client.list_blocklists()
744+
# Should have at least 1 blocklist (the one we just created)
745+
assert len(response["blocklists"]) >= 1
746+
blocklist_names = {blocklist["name"] for blocklist in response["blocklists"]}
747+
assert blocklist_name in blocklist_names
748+
finally:
749+
# Clean up
750+
try:
751+
await client.delete_blocklist(blocklist_name)
752+
except Exception:
753+
pass
732754

733755
async def test_get_blocklist(self, client: StreamChatAsync):
734-
response = await client.get_blocklist("Foo")
735-
assert response["blocklist"]["name"] == "Foo"
736-
assert response["blocklist"]["words"] == ["fudge", "heck"]
756+
blocklist_name = f"TestBlocklistAsync_{uuid.uuid4().hex[:8]}"
757+
await client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
758+
759+
try:
760+
response = await client.get_blocklist(blocklist_name)
761+
assert response["blocklist"]["name"] == blocklist_name
762+
assert response["blocklist"]["words"] == ["fudge", "heck"]
763+
finally:
764+
# Clean up
765+
try:
766+
await client.delete_blocklist(blocklist_name)
767+
except Exception:
768+
pass
737769

738770
async def test_update_blocklist(self, client: StreamChatAsync):
739-
await client.update_blocklist("Foo", words=["dang"])
740-
response = await client.get_blocklist("Foo")
741-
assert response["blocklist"]["words"] == ["dang"]
771+
blocklist_name = f"TestBlocklistAsync_{uuid.uuid4().hex[:8]}"
772+
await client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
773+
774+
try:
775+
await client.update_blocklist(blocklist_name, words=["dang"])
776+
response = await client.get_blocklist(blocklist_name)
777+
assert response["blocklist"]["words"] == ["dang"]
778+
finally:
779+
# Clean up
780+
try:
781+
await client.delete_blocklist(blocklist_name)
782+
except Exception:
783+
pass
742784

743785
async def test_delete_blocklist(self, client: StreamChatAsync):
744-
await client.delete_blocklist("Foo")
786+
blocklist_name = f"TestBlocklistAsync_{uuid.uuid4().hex[:8]}"
787+
await client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
788+
await client.delete_blocklist(blocklist_name)
789+
790+
# Verify it's deleted
791+
try:
792+
await client.get_blocklist(blocklist_name)
793+
pytest.fail("Blocklist should have been deleted")
794+
except Exception:
795+
pass # Expected - blocklist should not exist
745796

746797
async def test_check_push(
747798
self, client: StreamChatAsync, channel: Channel, random_user: Dict

stream_chat/tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,25 @@ def hard_delete_users(client: StreamChat, user_ids: List[str]):
131131
client.delete_users(user_ids, "hard", conversations="hard", messages="hard")
132132
except Exception:
133133
pass
134+
135+
136+
def cleanup_blocklists(client: StreamChat):
137+
"""Clean up test blocklists"""
138+
try:
139+
# Delete common test blocklist names
140+
test_blocklists = ["Foo", "TestBlocklist", "TestBlocklistAsync"]
141+
for blocklist_name in test_blocklists:
142+
try:
143+
client.delete_blocklist(blocklist_name)
144+
except Exception:
145+
pass # Blocklist might not exist
146+
except Exception:
147+
pass
148+
149+
150+
@pytest.fixture(autouse=True, scope="session")
151+
def cleanup_test_data():
152+
"""Global cleanup for test data"""
153+
yield
154+
# This runs after all tests complete
155+
# Add any global cleanup here if needed

stream_chat/tests/test_client.py

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -699,26 +699,78 @@ def test_query_channels_members_in(
699699
assert len(response["channels"][0]["members"]) == 9
700700

701701
def test_create_blocklist(self, client: StreamChat):
702-
client.create_blocklist(name="Foo", words=["fudge", "heck"], type="word")
702+
# Use a unique name to avoid conflicts
703+
blocklist_name = f"TestBlocklist_{uuid.uuid4().hex[:8]}"
704+
client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
705+
706+
# Clean up after test
707+
try:
708+
client.delete_blocklist(blocklist_name)
709+
except Exception:
710+
pass
703711

704712
def test_list_blocklists(self, client: StreamChat):
705-
response = client.list_blocklists()
706-
assert len(response["blocklists"]) == 2
707-
blocklist_names = {blocklist["name"] for blocklist in response["blocklists"]}
708-
assert "Foo" in blocklist_names
713+
# First, clean up any existing test blocklists
714+
cleanup_blocklists(client)
715+
716+
# Create a test blocklist
717+
blocklist_name = f"TestBlocklist_{uuid.uuid4().hex[:8]}"
718+
client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
719+
720+
try:
721+
response = client.list_blocklists()
722+
# Should have at least 1 blocklist (the one we just created)
723+
assert len(response["blocklists"]) >= 1
724+
blocklist_names = {blocklist["name"] for blocklist in response["blocklists"]}
725+
assert blocklist_name in blocklist_names
726+
finally:
727+
# Clean up
728+
try:
729+
client.delete_blocklist(blocklist_name)
730+
except Exception:
731+
pass
709732

710733
def test_get_blocklist(self, client: StreamChat):
711-
response = client.get_blocklist("Foo")
712-
assert response["blocklist"]["name"] == "Foo"
713-
assert response["blocklist"]["words"] == ["fudge", "heck"]
734+
blocklist_name = f"TestBlocklist_{uuid.uuid4().hex[:8]}"
735+
client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
736+
737+
try:
738+
response = client.get_blocklist(blocklist_name)
739+
assert response["blocklist"]["name"] == blocklist_name
740+
assert response["blocklist"]["words"] == ["fudge", "heck"]
741+
finally:
742+
# Clean up
743+
try:
744+
client.delete_blocklist(blocklist_name)
745+
except Exception:
746+
pass
714747

715748
def test_update_blocklist(self, client: StreamChat):
716-
client.update_blocklist("Foo", words=["dang"])
717-
response = client.get_blocklist("Foo")
718-
assert response["blocklist"]["words"] == ["dang"]
749+
blocklist_name = f"TestBlocklist_{uuid.uuid4().hex[:8]}"
750+
client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
751+
752+
try:
753+
client.update_blocklist(blocklist_name, words=["dang"])
754+
response = client.get_blocklist(blocklist_name)
755+
assert response["blocklist"]["words"] == ["dang"]
756+
finally:
757+
# Clean up
758+
try:
759+
client.delete_blocklist(blocklist_name)
760+
except Exception:
761+
pass
719762

720763
def test_delete_blocklist(self, client: StreamChat):
721-
client.delete_blocklist("Foo")
764+
blocklist_name = f"TestBlocklist_{uuid.uuid4().hex[:8]}"
765+
client.create_blocklist(name=blocklist_name, words=["fudge", "heck"], type="word")
766+
client.delete_blocklist(blocklist_name)
767+
768+
# Verify it's deleted
769+
try:
770+
client.get_blocklist(blocklist_name)
771+
pytest.fail("Blocklist should have been deleted")
772+
except Exception:
773+
pass # Expected - blocklist should not exist
722774

723775
def test_check_push(self, client: StreamChat, channel: Channel, random_user: Dict):
724776
msg = {"id": str(uuid.uuid4()), "text": "/giphy wave"}

0 commit comments

Comments
 (0)