-
Notifications
You must be signed in to change notification settings - Fork 36
fix: decision service for group and multiple experiments. #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
3992620
62987b0
3655fc6
10431d2
8d69a02
f4c9e7e
5ef8e26
cd59fa8
569720a
8fb6b73
777e975
c2d6752
790ee40
bad76e7
804fdb6
f0d9f60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1309,58 +1309,41 @@ def test_get_variation_for_feature__returns_variation_if_user_not_in_experiment_ | |
| mock_decision_service_logging, | ||
| ) | ||
|
|
||
| def test_get_variation_for_feature__returns_variation_for_feature_in_group(self): | ||
| """ Test that get_variation_for_feature returns the variation of | ||
| the experiment the user is bucketed in the feature's group. """ | ||
| def test_get_variation_for_feature__returns_none_for_user_not_in_experiment(self): | ||
| """ Test that get_variation_for_feature returns None for user not in the associated experiment. """ | ||
|
|
||
| feature = self.project_config.get_feature_from_key("test_feature_in_group") | ||
| feature = self.project_config.get_feature_from_key("test_feature_in_experiment") | ||
|
|
||
| expected_experiment = self.project_config.get_experiment_from_key("group_exp_1") | ||
| expected_variation = self.project_config.get_variation_from_id( | ||
| "group_exp_1", "28901" | ||
| ) | ||
| with mock.patch( | ||
| "optimizely.decision_service.DecisionService.get_experiment_in_group", | ||
| return_value=(self.project_config.get_experiment_from_key("group_exp_1"), []), | ||
| ) as mock_get_experiment_in_group, mock.patch( | ||
| "optimizely.decision_service.DecisionService.get_variation", | ||
| return_value=(expected_variation, []), | ||
| return_value=[None, []], | ||
| ) as mock_decision: | ||
| variation_received, _ = self.decision_service.get_variation_for_feature( | ||
| self.project_config, feature, "test_user" | ||
| ) | ||
| self.assertEqual( | ||
| decision_service.Decision( | ||
| expected_experiment, | ||
| expected_variation, | ||
| enums.DecisionSources.FEATURE_TEST, | ||
| ), | ||
| decision_service.Decision(None, None, enums.DecisionSources.ROLLOUT), | ||
| variation_received, | ||
| ) | ||
|
|
||
| mock_get_experiment_in_group.assert_called_once_with( | ||
| self.project_config, self.project_config.get_group("19228"), 'test_user') | ||
|
|
||
| mock_decision.assert_called_once_with( | ||
| self.project_config, | ||
| self.project_config.get_experiment_from_key("group_exp_1"), | ||
| self.project_config.get_experiment_from_key("test_experiment"), | ||
| "test_user", | ||
| None, | ||
| False | ||
| ) | ||
|
|
||
| def test_get_variation_for_feature__returns_none_for_user_not_in_group(self): | ||
| """ Test that get_variation_for_feature returns None for | ||
| user not in group and the feature is not part of a rollout. """ | ||
| user not in group and the feature is not part of a rollout. """ | ||
|
|
||
| feature = self.project_config.get_feature_from_key("test_feature_in_group") | ||
|
|
||
| with mock.patch( | ||
| "optimizely.decision_service.DecisionService.get_experiment_in_group", | ||
| return_value=[None, []], | ||
| ) as mock_get_experiment_in_group, mock.patch( | ||
| "optimizely.decision_service.DecisionService.get_variation" | ||
| ) as mock_decision: | ||
| "optimizely.decision_service.DecisionService.get_variation", | ||
|
||
| return_value=[None, []], | ||
| ): | ||
| variation_received, _ = self.decision_service.get_variation_for_feature( | ||
| self.project_config, feature, "test_user" | ||
| ) | ||
|
|
@@ -1369,65 +1352,50 @@ def test_get_variation_for_feature__returns_none_for_user_not_in_group(self): | |
| variation_received, | ||
| ) | ||
|
|
||
| mock_get_experiment_in_group.assert_called_once_with( | ||
| self.project_config, self.project_config.get_group("19228"), "test_user") | ||
|
|
||
| self.assertFalse(mock_decision.called) | ||
|
|
||
| def test_get_variation_for_feature__returns_none_for_user_not_in_experiment(self): | ||
| """ Test that get_variation_for_feature returns None for user not in the associated experiment. """ | ||
| def test_get_variation_for_feature__returns_variation_for_feature_in_group(self): | ||
| """ Test that get_variation_for_feature returns the variation of | ||
| the experiment the user is bucketed in the feature's group. """ | ||
|
|
||
| feature = self.project_config.get_feature_from_key("test_feature_in_experiment") | ||
| feature = self.project_config.get_feature_from_key("test_feature_in_group") | ||
|
|
||
| expected_experiment = self.project_config.get_experiment_from_key("group_exp_1") | ||
| expected_variation = self.project_config.get_variation_from_id( | ||
| "group_exp_1", "28901" | ||
| ) | ||
| with mock.patch( | ||
| "optimizely.decision_service.DecisionService.get_variation", | ||
| return_value=[None, []], | ||
| return_value=(expected_variation, []), | ||
| ) as mock_decision: | ||
| variation_received, _ = self.decision_service.get_variation_for_feature( | ||
| self.project_config, feature, "test_user" | ||
| ) | ||
| self.assertEqual( | ||
| decision_service.Decision(None, None, enums.DecisionSources.ROLLOUT), | ||
| decision_service.Decision( | ||
| expected_experiment, | ||
| expected_variation, | ||
| enums.DecisionSources.FEATURE_TEST, | ||
| ), | ||
| variation_received, | ||
| ) | ||
|
|
||
| mock_decision.assert_called_once_with( | ||
| self.project_config, | ||
| self.project_config.get_experiment_from_key("test_experiment"), | ||
| self.project_config.get_experiment_from_key("group_exp_1"), | ||
| "test_user", | ||
| None, | ||
| False | ||
| ) | ||
|
|
||
| def test_get_variation_for_feature__returns_none_for_invalid_group_id(self): | ||
| """ Test that get_variation_for_feature returns None for unknown group ID. """ | ||
|
|
||
| feature = self.project_config.get_feature_from_key("test_feature_in_group") | ||
| feature.groupId = "aabbccdd" | ||
|
|
||
| with self.mock_decision_logger as mock_decision_service_logging: | ||
| variation_received, _ = self.decision_service.get_variation_for_feature( | ||
| self.project_config, feature, "test_user" | ||
| ) | ||
| self.assertEqual( | ||
| decision_service.Decision(None, None, enums.DecisionSources.ROLLOUT), | ||
| variation_received, | ||
| ) | ||
| mock_decision_service_logging.error.assert_called_once_with( | ||
| enums.Errors.INVALID_GROUP_ID.format("_get_variation_for_feature") | ||
| ) | ||
|
|
||
| def test_get_variation_for_feature__returns_none_for_user_in_group_experiment_not_associated_with_feature( | ||
| self, | ||
| ): | ||
| """ Test that if a user is in the mutex group but the experiment is | ||
| not targeting a feature, then None is returned. """ | ||
| not targeting a feature, then None is returned. """ | ||
|
|
||
| feature = self.project_config.get_feature_from_key("test_feature_in_group") | ||
|
|
||
| with mock.patch( | ||
| "optimizely.decision_service.DecisionService.get_experiment_in_group", | ||
| return_value=[self.project_config.get_experiment_from_key("group_exp_2"), []], | ||
| "optimizely.decision_service.DecisionService.get_variation", | ||
| return_value=[None, []], | ||
| ) as mock_decision: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same test as above. |
||
| variation_received, _ = self.decision_service.get_variation_for_feature( | ||
| self.project_config, feature, "test_user" | ||
|
|
@@ -1438,43 +1406,19 @@ def test_get_variation_for_feature__returns_none_for_user_in_group_experiment_no | |
| ) | ||
|
|
||
| mock_decision.assert_called_once_with( | ||
| self.project_config, self.project_config.get_group("19228"), "test_user" | ||
| self.project_config, self.project_config.get_experiment_from_id("32222"), "test_user", None, False | ||
| ) | ||
|
|
||
| def test_get_experiment_in_group(self): | ||
| """ Test that get_experiment_in_group returns the bucketed experiment for the user. """ | ||
| def test_get_variation_for_feature__returns_none_for_invalid_group_id(self): | ||
| """ Test that get_variation_for_feature returns None for unknown group ID. """ | ||
|
|
||
| group = self.project_config.get_group("19228") | ||
| experiment = self.project_config.get_experiment_from_id("32222") | ||
| with mock.patch( | ||
| "optimizely.bucketer.Bucketer.find_bucket", return_value="32222" | ||
| ), self.mock_decision_logger as mock_decision_service_logging: | ||
| variation_received, _ = self.decision_service.get_experiment_in_group( | ||
| self.project_config, group, "test_user" | ||
| ) | ||
| self.assertEqual( | ||
| experiment, | ||
| variation_received, | ||
| ) | ||
| feature = self.project_config.get_feature_from_key("test_feature_in_group") | ||
| feature.groupId = "aabbccdd" | ||
|
|
||
| mock_decision_service_logging.info.assert_called_once_with( | ||
| 'User with bucketing ID "test_user" is in experiment group_exp_1 of group 19228.' | ||
| variation_received, _ = self.decision_service.get_variation_for_feature( | ||
| self.project_config, feature, "test_user" | ||
| ) | ||
|
|
||
| def test_get_experiment_in_group__returns_none_if_user_not_in_group(self): | ||
| """ Test that get_experiment_in_group returns None if the user is not bucketed into the group. """ | ||
|
|
||
| group = self.project_config.get_group("19228") | ||
| with mock.patch( | ||
| "optimizely.bucketer.Bucketer.find_bucket", return_value=None | ||
| ), self.mock_decision_logger as mock_decision_service_logging: | ||
| variation_received, _ = self.decision_service.get_experiment_in_group( | ||
| self.project_config, group, "test_user" | ||
| ) | ||
| self.assertIsNone( | ||
| variation_received | ||
| ) | ||
|
|
||
| mock_decision_service_logging.info.assert_called_once_with( | ||
| 'User with bucketing ID "test_user" is not in any experiments of group 19228.' | ||
| self.assertEqual( | ||
| decision_service.Decision(None, None, enums.DecisionSources.ROLLOUT), | ||
| variation_received, | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent like before.