-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
273 lines (206 loc) · 7.33 KB
/
Copy pathtest.py
File metadata and controls
273 lines (206 loc) · 7.33 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import uuid
import requests
from typing import Dict, Any
BASE_URL = "http://127.0.0.1:8000"
AUTH = "/auth/login"
USERS = "/users"
FLAGS = "/flags"
METRICS = "/metrics"
EXPERIMENTS = "/experiments"
REPORTS = "/reports"
DECIDE = "/decide"
EVENTS = "/events"
ADMIN_EMAIL = os.getenv("ADMIN_EMAIL", "admin@mail.ru")
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin123")
TIMEOUT = 10
# -------------------------
# utils
# -------------------------
def assert_ok(cond, msg):
if not cond:
raise AssertionError(msg)
def post(path, token=None, json=None):
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
return requests.post(BASE_URL + path, json=json, headers=headers, timeout=TIMEOUT)
def get(path, token=None):
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
return requests.get(BASE_URL + path, headers=headers, timeout=TIMEOUT)
def login(email, password):
r = post(AUTH, json={"email": email, "password": password})
assert_ok(r.status_code == 200, f"Login failed {r.text}")
return r.json()["access_token"]
# -------------------------
# helpers
# -------------------------
def create_flag(admin_token) -> Dict[str, Any]:
key = f"flag_{uuid.uuid4().hex[:8]}"
r = post(FLAGS, token=admin_token, json={
"key": key,
"type": "bool",
"default_value": False,
"description": "test flag"
})
assert_ok(r.status_code in (200, 201), r.text)
return r.json()
def get_flag_key(admin_token, flag_id):
r = get(f"{FLAGS}/{flag_id}", token=admin_token)
assert_ok(r.status_code == 200, r.text)
return r.json()["key"]
def create_metric(admin_token, key, aggregation_type,
numerator=None, denominator=None):
r = post(METRICS, token=admin_token, json={
"key": key,
"name": key,
"aggregation_type": aggregation_type,
"numerator_event": numerator,
"denominator_event": denominator
})
assert_ok(r.status_code in (200, 201), r.text)
return r.json()
def experiment_payload(feature_flag_id: str, *, traffic: float = 100.0) -> dict:
return {
"name": f"Experiment {uuid.uuid4().hex[:8]}",
"description": "test experiment",
"feature_flag_id": feature_flag_id,
"traffic_percentage": traffic,
"targeting_rule": None,
"variants": [
{
"name": "control",
"value": "false",
"weight": traffic * 0.5,
"is_control": True,
},
{
"name": "treatment",
"value": "true",
"weight": traffic * 0.5,
"is_control": False,
},
],
}
def create_experiment(exp_token, admin_token):
flag = create_flag(admin_token)
payload = {
"name": "exp_" + uuid.uuid4().hex[:6],
"description": "report test",
"feature_flag_id": flag["id"],
"traffic_percentage": 100.0,
"targeting_rule": None,
"variants": [
{"name": "control", "value": "false", "weight": 50.0, "is_control": True},
{"name": "treatment", "value": "true", "weight": 50.0, "is_control": False},
],
}
r = post(EXPERIMENTS, token=exp_token, json=payload)
assert_ok(r.status_code in (200, 201), r.text)
exp = r.json()
# submit
r = post(f"{EXPERIMENTS}/{exp['id']}/submit", token=exp_token, json={})
assert_ok(r.status_code == 200, r.text)
# review (admin assumed APPROVER or allowed)
r = post(f"{EXPERIMENTS}/{exp['id']}/review",
token=admin_token,
json={"decision": "approve", "comment": "ok"})
assert_ok(r.status_code == 200, r.text)
# start
r = post(f"{EXPERIMENTS}/{exp['id']}/start", token=exp_token)
assert_ok(r.status_code == 200, r.text)
return exp, flag
def decide(flag_key, subject):
r = post(DECIDE, json={
"subject_id": subject,
"flags": [flag_key],
"attributes": {}
})
assert_ok(r.status_code == 200, r.text)
d = r.json()["decisions"][0]
assert_ok(d["meta"]["is_default"] is False, "Experiment not applied")
return d["meta"]["decision_id"]
def ingest(decision_id, name, ts):
return post(EVENTS, json={
"events": [{
"idempotency_key": uuid.uuid4().hex,
"decision_id": decision_id,
"event_name": name,
"ts": ts
}]
})
def report(exp_id, payload, token):
r = post(f"{REPORTS}/experiments/{exp_id}", json=payload, token=token)
assert_ok(r.status_code == 200, r.text)
return r.json()
# -------------------------
# TESTS
# -------------------------
def test_basic_count_and_rate(admin_token, exp_token):
exp, flag = create_experiment(exp_token, admin_token)
try:
m_click = create_metric(admin_token, "m_click", "count", numerator="click")
m_ctr = create_metric(
admin_token, "m_ctr", "rate",
numerator="click",
denominator="exposure"
)
except Exception:
# при повторном создании может вылезти ошибка
pass
flag_key = get_flag_key(admin_token, flag["id"])
subject = "user_" + uuid.uuid4().hex[:6]
decision_id = decide(flag_key, subject)
ingest(decision_id, "exposure", "2026-02-24T10:00:00Z")
ingest(decision_id, "exposure", "2026-02-24T10:01:00Z")
ingest(decision_id, "click", "2026-02-24T10:02:00Z")
rep = report(exp["id"], {
"from_ts": "2026-02-24T09:00:00Z",
"to_ts": "2026-02-24T11:00:00Z",
"metric_keys": ["m_click", "m_ctr"],
"include_timeseries": False
}, token=admin_token)
variants = rep["variants"]
print(rep)
# найдём вариант с кликом
v = next(v for v in variants if v["metrics"]["m_click"]["raw"] == 1.0)
assert_ok(v["metrics"]["m_click"]["raw"] == 1.0, "Click count mismatch")
assert_ok(abs(v["metrics"]["m_ctr"]["raw"] - 0.5) < 1e-9, "CTR mismatch")
print("✅ test_basic_count_and_rate")
def test_timeseries(admin_token, exp_token):
exp, flag = create_experiment(exp_token, admin_token)
try:
create_metric(admin_token, "m_click_ts", "count", numerator="click")
except Exception:
pass
flag_key = get_flag_key(admin_token, flag["id"])
subject = "user_" + uuid.uuid4().hex[:6]
decision_id = decide(flag_key, subject)
ingest(decision_id, "click", "2026-02-24T10:10:00Z")
ingest(decision_id, "click", "2026-02-24T11:10:00Z")
rep = report(exp["id"], {
"from_ts": "2026-02-24T10:00:00Z",
"to_ts": "2026-02-24T12:00:00Z",
"metric_keys": ["m_click_ts"],
"include_timeseries": True,
"granularity": "hour"
}, token=admin_token)
ts = rep["timeseries"]
assert_ok(ts is not None, "Timeseries missing")
found = False
for v in ts:
for s in v["series"]:
vals = [p["value"] for p in s["points"]]
if sum(vals) == 2.0:
found = True
assert_ok(found, "Timeseries aggregation incorrect")
print("✅ test_timeseries")
def main():
admin_token = login(ADMIN_EMAIL, ADMIN_PASSWORD)
exp_token = admin_token
test_basic_count_and_rate(admin_token, exp_token)
test_timeseries(admin_token, exp_token)
print("\n✅ ALL REPORT TESTS PASSED")
main()