示例#1
0
 def test_does_not_log_bucketing_event(self):
     event_queue = mock.Mock(spec=EventQueue)
     filewatcher = mock.Mock(spec=FileWatcher)
     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_cant_find_experiment(self):
     self.mock_filewatcher.get_data.return_value = {
         "other_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,
     )
     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)
示例#3
0
    def test_bucketing_event_fields_without_baseplate_user_with_cfg_data(self):
        cfg_data = {
            "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",
            cfg_data=cfg_data,
            global_cache={},
            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")
        self.assertEqual(event_fields["logged_in"], True)
        self.assertEqual(event_fields["app_name"], "r2")
        self.assertEqual(event_fields["event_type"], EventType.BUCKET)
        self.assertNotEqual(event_fields["span"], None)

        self.assertEqual(getattr(event_fields["experiment"], "id"), 1)
        self.assertEqual(getattr(event_fields["experiment"], "name"), "test")
        self.assertEqual(getattr(event_fields["experiment"], "owner"),
                         "test_owner")
        self.assertEqual(getattr(event_fields["experiment"], "version"), "1")
 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)
 def test_that_bucketing_events_not_sent_if_cant_load_config(self):
     self.mock_filewatcher.get_data.side_effect = WatchedFileNotAvailableError(
         "path", None)  # noqa
     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)
示例#6
0
 def test_that_bucketing_events_not_sent_if_config_is_empty_with_cfg_data(
         self):
     experiments = Experiments(
         config_watcher=self.mock_filewatcher,
         server_span=self.mock_span,
         context_name="test",
         cfg_data={},
         global_cache={},
         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)
 def test_none_returned_on_variant_call_with_no_times(self):
     self.mock_filewatcher.get_data.return_value = {
         "test": {
             "id": 1,
             "name": "test",
             "owner": "test_owner",
             "type": "r2",
             "version": "1",
             "experiment": {
                 "id": 1,
                 "name": "test",
                 "variants": {
                     "active": 50,
                     "control_1": 25,
                     "control_2": 25
                 },
             },
         }
     }
     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)
示例#8
0
 def test_none_returned_on_variant_call_with_bad_id_with_cfg_data(self):
     cfg_data = {
         "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": 50,
                     "control_1": 25,
                     "control_2": 25
                 },
             },
         }
     }
     experiments = Experiments(
         config_watcher=self.mock_filewatcher,
         server_span=self.mock_span,
         context_name="test",
         cfg_data=cfg_data,
         global_cache={},
         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)
示例#9
0
    def test_that_override_true_has_no_effect_with_cfg_data(self):
        cfg_data = {
            "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",
            cfg_data=cfg_data,
            global_cache={},
            event_logger=self.event_logger,
        )

        with mock.patch(
                "baseplate.lib.experiments.providers.r2.R2Experiment.variant"
        ) as p:
            p.return_value = "active"
            self.assertEqual(self.event_logger.log.call_count, 0)
            experiments.variant("test",
                                user=self.user,
                                bucketing_event_override=True)
            self.assertEqual(self.event_logger.log.call_count, 1)
            experiments.variant("test",
                                user=self.user,
                                bucketing_event_override=True)
            self.assertEqual(self.event_logger.log.call_count, 1)
    def test_that_we_only_send_bucketing_event_once(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": {
                    "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)
 def test_that_bucketing_events_are_not_sent_with_override_false(self):
     """Don't send events when override is False."""
     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"
         self.assertEqual(self.event_logger.log.call_count, 0)
         experiments.variant("test",
                             user=self.user,
                             bucketing_event_override=False)
         self.assertEqual(self.event_logger.log.call_count, 0)
         experiments.variant("test",
                             user=self.user,
                             bucketing_event_override=False)
         self.assertEqual(self.event_logger.log.call_count, 0)
         p.return_value = None
         experiments.variant("test",
                             user=self.user,
                             bucketing_event_override=False)
         self.assertEqual(self.event_logger.log.call_count, 0)