-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconftest.py
More file actions
159 lines (128 loc) · 4.47 KB
/
conftest.py
File metadata and controls
159 lines (128 loc) · 4.47 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import uuid
from typing import Dict, List
import pytest
from stream_chat import StreamChat
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 client():
base_url = os.environ.get("STREAM_HOST")
options = {"base_url": base_url} if base_url else {}
return StreamChat(
api_key=os.environ["STREAM_KEY"],
api_secret=os.environ["STREAM_SECRET"],
timeout=10,
**options,
)
@pytest.fixture(scope="function")
def random_user(client: StreamChat):
user = {"id": str(uuid.uuid4())}
response = client.upsert_user(user)
assert "users" in response
assert user["id"] in response["users"]
yield user
hard_delete_users(client, [user["id"]])
@pytest.fixture(scope="function")
def server_user(client: StreamChat):
user = {"id": str(uuid.uuid4())}
response = client.upsert_user(user)
assert "users" in response
assert user["id"] in response["users"]
yield user
hard_delete_users(client, [user["id"]])
@pytest.fixture(scope="function")
def random_users(client: StreamChat):
user1 = {"id": str(uuid.uuid4())}
user2 = {"id": str(uuid.uuid4())}
user3 = {"id": str(uuid.uuid4())}
client.upsert_users([user1, user2, user3])
yield [user1, user2, user3]
hard_delete_users(client, [user1["id"], user2["id"], user3["id"]])
@pytest.fixture(scope="function")
def channel(client: StreamChat, random_user: Dict):
channel = client.channel(
"messaging", str(uuid.uuid4()), {"test": True, "language": "python"}
)
channel.create(random_user["id"])
yield channel
try:
client.delete_channels([channel.cid], hard_delete=True)
except Exception:
pass
@pytest.fixture(scope="function")
def command(client: StreamChat):
try:
commands = client.list_commands()
for cmd in commands.get("commands", []):
if cmd.get("name") not in (
"giphy",
"imgur",
"flag",
"ban",
"unban",
"mute",
"unmute",
):
try:
client.delete_command(cmd["name"])
except Exception:
pass
except Exception:
pass
response = client.create_command(
dict(name=str(uuid.uuid4()), description="My command")
)
yield response["command"]
try:
client.delete_command(response["command"]["name"])
except Exception:
pass
@pytest.fixture(scope="module")
def fellowship_of_the_ring(client: StreamChat):
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},
]
try:
client.restore_users([m["id"] for m in members])
except Exception:
pass
client.upsert_users(members)
channel = client.channel(
"team", "fellowship-of-the-ring", {"members": [m["id"] for m in members]}
)
channel.create("gandalf")
yield
try:
channel.delete(hard=True)
hard_delete_users(client, [m["id"] for m in members])
except Exception:
pass
def hard_delete_users(client: StreamChat, user_ids: List[str]):
try:
client.delete_users(user_ids, "hard", conversations="hard", messages="hard")
except Exception:
pass