-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconftest.py
More file actions
143 lines (114 loc) · 4.31 KB
/
Copy pathconftest.py
File metadata and controls
143 lines (114 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import asyncio
import os
import uuid
from typing import Dict, List
import pytest
from stream_chat.async_chat import StreamChatAsync
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail(f"previous test failed ({previousfailed.name})")
def pytest_configure(config):
config.addinivalue_line("markers", "incremental: mark test incremental")
@pytest.fixture(scope="module")
def event_loop():
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="function", autouse=True)
@pytest.mark.asyncio
async def client():
base_url = os.environ.get("STREAM_HOST")
options = {"base_url": base_url} if base_url else {}
async with StreamChatAsync(
api_key=os.environ["STREAM_KEY"],
api_secret=os.environ["STREAM_SECRET"],
timeout=10,
**options,
) as stream_client:
yield stream_client
@pytest.fixture(scope="function")
async def random_user(client: StreamChatAsync):
user = {"id": str(uuid.uuid4())}
response = await client.upsert_user(user)
assert "users" in response
assert user["id"] in response["users"]
yield user
await hard_delete_users(client, [user["id"]])
@pytest.fixture(scope="function")
async def server_user(client: StreamChatAsync):
user = {"id": str(uuid.uuid4())}
response = await client.upsert_user(user)
assert "users" in response
assert user["id"] in response["users"]
yield user
await hard_delete_users(client, [user["id"]])
@pytest.fixture(scope="function")
async def random_users(client: StreamChatAsync):
user1 = {"id": str(uuid.uuid4())}
user2 = {"id": str(uuid.uuid4())}
user3 = {"id": str(uuid.uuid4())}
await client.upsert_users([user1, user2, user3])
yield [user1, user2, user3]
await hard_delete_users(client, [user1["id"], user2["id"], user3["id"]])
@pytest.fixture(scope="function")
async def channel(client: StreamChatAsync, random_user: Dict):
channel = client.channel(
"messaging", str(uuid.uuid4()), {"test": True, "language": "python"}
)
await channel.create(random_user["id"])
yield channel
try:
await client.delete_channels([channel.cid], hard_delete=True)
except Exception:
pass
@pytest.fixture(scope="function")
async def command(client: StreamChatAsync):
response = await client.create_command(
dict(name=str(uuid.uuid4()), description="My command")
)
yield response["command"]
await client.delete_command(response["command"]["name"])
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def fellowship_of_the_ring(client: StreamChatAsync):
members: List[Dict] = [
{"id": "frodo-baggins", "name": "Frodo Baggins", "race": "Hobbit", "age": 50},
{"id": "sam-gamgee", "name": "Samwise Gamgee", "race": "Hobbit", "age": 38},
{"id": "gandalf", "name": "Gandalf the Grey", "race": "Istari"},
{"id": "legolas", "name": "Legolas", "race": "Elf", "age": 500},
{"id": "gimli", "name": "Gimli", "race": "Dwarf", "age": 139},
{"id": "aragorn", "name": "Aragorn", "race": "Man", "age": 87},
{"id": "boromir", "name": "Boromir", "race": "Man", "age": 40},
{
"id": "meriadoc-brandybuck",
"name": "Meriadoc Brandybuck",
"race": "Hobbit",
"age": 36,
},
{"id": "peregrin-took", "name": "Peregrin Took", "race": "Hobbit", "age": 28},
]
await client.upsert_users(members)
channel = client.channel(
"team", "fellowship-of-the-ring", {"members": [m["id"] for m in members]}
)
await channel.create("gandalf")
yield
try:
await channel.delete(hard=True)
await hard_delete_users(client, [m["id"] for m in members])
except Exception:
pass
async def hard_delete_users(client: StreamChatAsync, user_ids: List[str]):
try:
await client.delete_users(
user_ids, "hard", conversations="hard", messages="hard"
)
except Exception:
pass