Skip to content

Commit 4aeb853

Browse files
dry distinct id
1 parent ed0f59e commit 4aeb853

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

‎mixpanel/flags/test_local_feature_flags.py‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .local_feature_flags import LocalFeatureFlagsProvider
1111

1212
TEST_FLAG_KEY = "test_flag"
13+
DISTINCT_ID = "user123"
1314

1415
def create_test_flag(
1516
flag_key: str = TEST_FLAG_KEY,
@@ -106,7 +107,7 @@ async def setup_flags_with_polling(self, flags_in_order: List[List[Experimentati
106107
@respx.mock
107108
async def test_get_variant_value_returns_fallback_when_no_flag_definitions(self):
108109
await self.setup_flags([])
109-
result = self._flags.get_variant_value("nonexistent_flag", "control", {"distinct_id": "user123"})
110+
result = self._flags.get_variant_value("nonexistent_flag", "control", {"distinct_id": DISTINCT_ID})
110111
assert result == "control"
111112

112113
@respx.mock
@@ -116,14 +117,14 @@ async def test_get_variant_value_returns_fallback_if_flag_definition_call_fails(
116117
)
117118

118119
await self._flags.astart_polling_for_definitions()
119-
result = self._flags.get_variant_value("nonexistent_flag", "control", {"distinct_id": "user123"})
120+
result = self._flags.get_variant_value("nonexistent_flag", "control", {"distinct_id": DISTINCT_ID})
120121
assert result == "control"
121122

122123
@respx.mock
123124
async def test_get_variant_value_returns_fallback_when_flag_does_not_exist(self):
124125
other_flag = create_test_flag("other_flag")
125126
await self.setup_flags([other_flag])
126-
result = self._flags.get_variant_value("nonexistent_flag", "control", {"distinct_id": "user123"})
127+
result = self._flags.get_variant_value("nonexistent_flag", "control", {"distinct_id": DISTINCT_ID})
127128
assert result == "control"
128129

129130
@respx.mock
@@ -137,7 +138,7 @@ async def test_get_variant_value_returns_fallback_when_no_context(self):
137138
async def test_get_variant_value_returns_fallback_when_wrong_context_key(self):
138139
flag = create_test_flag(context="user_id")
139140
await self.setup_flags([flag])
140-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
141+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
141142
assert result == "fallback"
142143

143144
@respx.mock
@@ -175,14 +176,14 @@ async def test_get_variant_value_returns_fallback_when_test_user_variant_not_con
175176
async def test_get_variant_value_returns_fallback_when_rollout_percentage_zero(self):
176177
flag = create_test_flag(rollout_percentage=0.0)
177178
await self.setup_flags([flag])
178-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
179+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
179180
assert result == "fallback"
180181

181182
@respx.mock
182183
async def test_get_variant_value_returns_variant_when_rollout_percentage_hundred(self):
183184
flag = create_test_flag(rollout_percentage=100.0)
184185
await self.setup_flags([flag])
185-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
186+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
186187
assert result != "fallback"
187188

188189
# TODO Joshua start here
@@ -195,7 +196,7 @@ async def test_get_variant_value_respects_runtime_evaluation_rule_satisfied(self
195196
flag = create_test_flag(runtime_evaluation_rule=runtime_eval)
196197
await self.setup_flags([flag])
197198
context = {
198-
"distinct_id": "user123",
199+
"distinct_id": DISTINCT_ID,
199200
"custom_properties": {
200201
"plan": "premium",
201202
"region": "US"
@@ -212,7 +213,7 @@ async def test_get_variant_value_respects_runtime_evaluation_rule_not_satisfied(
212213
flag = create_test_flag(runtime_evaluation_rule=runtime_eval)
213214
await self.setup_flags([flag])
214215
context = {
215-
"distinct_id": "user123",
216+
"distinct_id": DISTINCT_ID,
216217
"custom_properties": {
217218
"plan": "premium",
218219
"region": "US"
@@ -227,7 +228,7 @@ async def test_get_variant_value_respects_runtime_evaluation_satisfied(self):
227228
flag = create_test_flag(runtime_evaluation_legacy_definition=runtime_eval)
228229
await self.setup_flags([flag])
229230
context = {
230-
"distinct_id": "user123",
231+
"distinct_id": DISTINCT_ID,
231232
"custom_properties": {
232233
"plan": "premium",
233234
"region": "US"
@@ -242,7 +243,7 @@ async def test_get_variant_value_returns_fallback_when_runtime_evaluation_not_sa
242243
flag = create_test_flag(runtime_evaluation_legacy_definition=runtime_eval)
243244
await self.setup_flags([flag])
244245
context = {
245-
"distinct_id": "user123",
246+
"distinct_id": DISTINCT_ID,
246247
"custom_properties": {
247248
"plan": "basic",
248249
"region": "US"
@@ -260,7 +261,7 @@ async def test_get_variant_value_picks_correct_variant_with_hundred_percent_spli
260261
]
261262
flag = create_test_flag(variants=variants, rollout_percentage=100.0)
262263
await self.setup_flags([flag])
263-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
264+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
264265
assert result == "variant_a"
265266

266267
@respx.mock
@@ -273,7 +274,7 @@ async def test_get_variant_value_picks_correct_variant_with_half_migrated_group_
273274
variant_splits = {"A": 0.0, "B": 100.0, "C": 0.0}
274275
flag = create_test_flag(variants=variants, rollout_percentage=100.0, variant_splits=variant_splits)
275276
await self.setup_flags([flag])
276-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
277+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
277278
assert result == "variant_b"
278279

279280
@respx.mock
@@ -286,7 +287,7 @@ async def test_get_variant_value_picks_correct_variant_with_full_migrated_group_
286287
variant_splits = {"A": 0.0, "B": 0.0, "C": 100.0}
287288
flag = create_test_flag(variants=variants, rollout_percentage=100.0, variant_splits=variant_splits)
288289
await self.setup_flags([flag])
289-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
290+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
290291
assert result == "variant_c"
291292

292293
@respx.mock
@@ -297,7 +298,7 @@ async def test_get_variant_value_picks_overriden_variant(self):
297298
]
298299
flag = create_test_flag(variants=variants, variant_override=VariantOverride(key="B"))
299300
await self.setup_flags([flag])
300-
result = self._flags.get_variant_value(TEST_FLAG_KEY, "control", {"distinct_id": "user123"})
301+
result = self._flags.get_variant_value(TEST_FLAG_KEY, "control", {"distinct_id": DISTINCT_ID})
301302
assert result == "variant_b"
302303

303304
@respx.mock
@@ -306,7 +307,7 @@ async def test_get_variant_value_tracks_exposure_when_variant_selected(self):
306307
await self.setup_flags([flag])
307308
with patch('mixpanel.flags.utils.normalized_hash') as mock_hash:
308309
mock_hash.return_value = 0.5
309-
_ = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
310+
_ = self._flags.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
310311
self._mock_tracker.assert_called_once()
311312

312313
@respx.mock
@@ -349,7 +350,7 @@ async def test_get_variant_value_tracks_exposure_with_correct_properties(self, e
349350
@respx.mock
350351
async def test_get_variant_value_does_not_track_exposure_on_fallback(self):
351352
await self.setup_flags([])
352-
_ = self._flags.get_variant_value("nonexistent_flag", "fallback", {"distinct_id": "user123"})
353+
_ = self._flags.get_variant_value("nonexistent_flag", "fallback", {"distinct_id": DISTINCT_ID})
353354
self._mock_tracker.assert_not_called()
354355

355356
@respx.mock
@@ -365,7 +366,7 @@ async def test_get_all_variants_returns_all_variants_when_user_in_rollout(self):
365366
flag2 = create_test_flag(flag_key="flag2", rollout_percentage=100.0)
366367
await self.setup_flags([flag1, flag2])
367368

368-
result = self._flags.get_all_variants({"distinct_id": "user123"})
369+
result = self._flags.get_all_variants({"distinct_id": DISTINCT_ID})
369370

370371
assert len(result) == 2 and "flag1" in result and "flag2" in result
371372

@@ -375,15 +376,15 @@ async def test_get_all_variants_returns_partial_variants_when_user_in_some_rollo
375376
flag2 = create_test_flag(flag_key="flag2", rollout_percentage=0.0)
376377
await self.setup_flags([flag1, flag2])
377378

378-
result = self._flags.get_all_variants({"distinct_id": "user123"})
379+
result = self._flags.get_all_variants({"distinct_id": DISTINCT_ID})
379380

380381
assert len(result) == 1 and "flag1" in result and "flag2" not in result
381382

382383
@respx.mock
383384
async def test_get_all_variants_returns_empty_dict_when_no_flags_configured(self):
384385
await self.setup_flags([])
385386

386-
result = self._flags.get_all_variants({"distinct_id": "user123"})
387+
result = self._flags.get_all_variants({"distinct_id": DISTINCT_ID})
387388

388389
assert result == {}
389390

@@ -393,7 +394,7 @@ async def test_get_all_variants_does_not_track_exposure_events(self):
393394
flag2 = create_test_flag(flag_key="flag2", rollout_percentage=100.0)
394395
await self.setup_flags([flag1, flag2])
395396

396-
_ = self._flags.get_all_variants({"distinct_id": "user123"})
397+
_ = self._flags.get_all_variants({"distinct_id": DISTINCT_ID})
397398

398399
self._mock_tracker.assert_not_called()
399400

@@ -403,7 +404,7 @@ async def test_track_exposure_event_successfully_tracks(self):
403404
await self.setup_flags([flag])
404405

405406
variant = SelectedVariant(key="treatment", variant_value="treatment")
406-
self._flags.track_exposure_event(TEST_FLAG_KEY, variant, {"distinct_id": "user123"})
407+
self._flags.track_exposure_event(TEST_FLAG_KEY, variant, {"distinct_id": DISTINCT_ID})
407408

408409
self._mock_tracker.assert_called_once()
409410

@@ -423,7 +424,7 @@ async def test_are_flags_ready_returns_true_when_empty_flags_loaded(self):
423424
@respx.mock
424425
async def test_is_enabled_returns_false_for_nonexistent_flag(self):
425426
await self.setup_flags([])
426-
result = self._flags.is_enabled("nonexistent_flag", {"distinct_id": "user123"})
427+
result = self._flags.is_enabled("nonexistent_flag", {"distinct_id": DISTINCT_ID})
427428
assert result == False
428429

429430
@respx.mock
@@ -433,7 +434,7 @@ async def test_is_enabled_returns_true_for_true_variant_value(self):
433434
]
434435
flag = create_test_flag(variants=variants, rollout_percentage=100.0)
435436
await self.setup_flags([flag])
436-
result = self._flags.is_enabled(TEST_FLAG_KEY, {"distinct_id": "user123"})
437+
result = self._flags.is_enabled(TEST_FLAG_KEY, {"distinct_id": DISTINCT_ID})
437438
assert result == True
438439

439440
@respx.mock
@@ -458,7 +459,7 @@ async def track_fetch_calls(self):
458459
async with polling_limit_check:
459460
await polling_limit_check.wait_for(lambda: polling_iterations >= len(flags_in_order))
460461

461-
result2 = self._flags_with_polling.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
462+
result2 = self._flags_with_polling.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
462463
assert result2 != "fallback"
463464

464465
class TestLocalFeatureFlagsProviderSync:
@@ -504,5 +505,5 @@ def track_fetch_calls(self):
504505
self.setup_flags_with_polling(flags_in_order)
505506
polling_event.wait(timeout=5.0)
506507
assert (polling_iterations >= 3 )
507-
result2 = self._flags_with_polling.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": "user123"})
508+
result2 = self._flags_with_polling.get_variant_value(TEST_FLAG_KEY, "fallback", {"distinct_id": DISTINCT_ID})
508509
assert result2 != "fallback"

0 commit comments

Comments
 (0)