Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_is_valid_experiment(self):
self.mock_filewatcher.get_data.return_value = {
"test": {
"id": 1,
"name": "test",
"owner": "test",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {"variants": {"active": 10, "control_1": 10, "control_2": 10}},
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
with mock.patch("baseplate.lib.experiments.providers.r2.R2Experiment.variant") as p:
p.return_value = "active"
is_valid = experiments.is_valid_experiment("test")
self.assertEqual(is_valid, True)
is_valid = experiments.is_valid_experiment("test2")
self.assertEqual(is_valid, False)
"test": {
"id": 1,
"name": "test",
"owner": "test_owner",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {
"id": 1,
"name": "test",
"variants": {"active": 10, "control_1": 10, "control_2": 10},
},
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
with mock.patch(
"baseplate.lib.experiments.providers.r2.R2Experiment.variant", return_value="active"
):
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user_id="t2_2", app_name="r2", logged_in=True)
self.assertEqual(self.event_logger.log.call_count, 1)
event_fields = self.event_logger.log.call_args[1]
self.assertEqual(event_fields["variant"], "active")
self.assertEqual(event_fields["user_id"], "t2_2")
"test": {
"id": 1,
"name": "test",
"owner": "test",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {
"id": 1,
"name": "test",
"variants": {"active": 10, "control_1": 10, "control_2": 10},
},
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
with mock.patch(
"baseplate.lib.experiments.providers.r2.R2Experiment.variant", return_value="active"
):
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 1)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 1)
def test_none_returned_on_variant_call_with_no_experiment(self):
self.mock_filewatcher.get_data.return_value = {
"test": {
"id": 1,
"name": "test",
"owner": "test_owner",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
self.assertEqual(self.event_logger.log.call_count, 0)
variant = experiments.variant("test", user=self.user)
self.assertEqual(variant, None)
span = mock.MagicMock(spec=ServerSpan)
filewatcher.get_data.return_value = {
"test": {
"id": 1,
"name": "test",
"type": "feature_flag",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {
"targeting": {"logged_in": [True, False]},
"variants": {"active": 100},
},
}
}
experiments = Experiments(config_watcher=filewatcher, server_span=span, context_name="test")
self.assertEqual(event_queue.put.call_count, 0)
variant = experiments.variant("test", user_id=self.user_id, logged_in=True)
self.assertEqual(variant, "active")
self.assertEqual(event_queue.put.call_count, 0)
experiments.variant("test", user_id=self.user_id, logged_in=True)
self.assertEqual(event_queue.put.call_count, 0)
def test_that_bucketing_events_not_sent_if_no_variant(self):
self.mock_filewatcher.get_data.return_value = {
"test": {
"id": 1,
"name": "test",
"owner": "test",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {"variants": {"active": 10, "control_1": 10, "control_2": 10}},
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
with mock.patch(
"baseplate.lib.experiments.providers.r2.R2Experiment.variant", return_value=None
):
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 0)
def test_that_bucketing_events_not_sent_if_experiment_disables(self):
self.mock_filewatcher.get_data.return_value = {
"test": {
"id": 1,
"name": "test",
"owner": "test",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {"variants": {"active": 10, "control_1": 10, "control_2": 10}},
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
with mock.patch(
"baseplate.lib.experiments.providers.r2.R2Experiment.variant", return_value="active"
), mock.patch(
"baseplate.lib.experiments.providers.r2.R2Experiment.should_log_bucketing",
return_value=False,
):
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {"variants": {"active": 10, "control_1": 10, "control_2": 10}},
},
"test2": {
"id": 1,
"name": "test",
"owner": "test",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {"variants": {"active": 10, "control_1": 10, "control_2": 10}},
},
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
with mock.patch("baseplate.lib.experiments.providers.r2.R2Experiment.variant") as p:
p.return_value = "active"
experiment_names = experiments.get_all_experiment_names()
self.assertEqual(len(experiment_names), 2)
self.assertEqual("test" in experiment_names, True)
self.assertEqual("test2" in experiment_names, True)
"test": {
"id": 1,
"name": "test",
"owner": "test_owner",
"type": "r2",
"version": "1",
"start_ts": time.time() - THIRTY_DAYS,
"stop_ts": time.time() + THIRTY_DAYS,
"experiment": {
"id": 1,
"name": "test",
"variants": {"active": 10, "control_1": 10, "control_2": 10},
},
}
}
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.expose("test", variant_name="control_1", user=self.user, app_name="r2")
self.assertEqual(self.event_logger.log.call_count, 1)
event_fields = self.event_logger.log.call_args[1]
self.assertEqual(event_fields["variant"], "control_1")
self.assertEqual(event_fields["user_id"], "t2_1")
self.assertEqual(event_fields["logged_in"], True)
self.assertEqual(event_fields["app_name"], "r2")
def test_that_bucketing_events_not_sent_if_cant_parse_config(self):
self.mock_filewatcher.get_data.side_effect = TypeError()
experiments = Experiments(
config_watcher=self.mock_filewatcher,
server_span=self.mock_span,
context_name="test",
event_logger=self.event_logger,
)
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 0)
experiments.variant("test", user=self.user)
self.assertEqual(self.event_logger.log.call_count, 0)