|
15 | 15 | import mock
|
16 | 16 |
|
17 | 17 | from optimizely.decision.optimizely_decision import OptimizelyDecision
|
| 18 | +from optimizely.decision.optimizely_decide_option import OptimizelyDecideOption as DecideOption |
18 | 19 | from optimizely.helpers import enums
|
19 | 20 | from . import base
|
20 | 21 | from optimizely import optimizely, decision_service
|
@@ -60,6 +61,23 @@ def test_user_context(self):
|
60 | 61 | self.assertEqual("firefox", uc.get_user_attributes()["browser"])
|
61 | 62 | self.assertEqual("red", uc.get_user_attributes()["color"])
|
62 | 63 |
|
| 64 | + def test_user_and_attributes_as_json(self): |
| 65 | + """ |
| 66 | + tests user context as json |
| 67 | + """ |
| 68 | + uc = OptimizelyUserContext(self.optimizely, "test_user") |
| 69 | + |
| 70 | + # set an attribute |
| 71 | + uc.set_attribute("browser", "safari") |
| 72 | + |
| 73 | + # set expected json obj |
| 74 | + expected_json = { |
| 75 | + "user_id": uc.user_id, |
| 76 | + "attributes": uc.get_user_attributes(), |
| 77 | + } |
| 78 | + |
| 79 | + self.assertEqual(uc.as_json(), expected_json) |
| 80 | + |
63 | 81 | def test_attributes_are_cloned_when_passed_to_user_context(self):
|
64 | 82 | user_id = 'test_user'
|
65 | 83 | attributes = {"browser": "chrome"}
|
@@ -1247,3 +1265,34 @@ def test_decide_reasons__whitelisted_variation(self):
|
1247 | 1265 | expected_reasons = ['User "user_1" is forced in variation "control".']
|
1248 | 1266 |
|
1249 | 1267 | self.assertEqual(expected_reasons, actual.reasons)
|
| 1268 | + |
| 1269 | + def test_init__invalid_default_decide_options(self): |
| 1270 | + """ |
| 1271 | + Test to confirm that default decide options passed not as a list will trigger setting |
| 1272 | + self.deafulat_decide_options as an empty list. |
| 1273 | + """ |
| 1274 | + invalid_decide_options = {"testKey": "testOption"} |
| 1275 | + |
| 1276 | + mock_client_logger = mock.MagicMock() |
| 1277 | + with mock.patch('optimizely.logger.reset_logger', return_value=mock_client_logger): |
| 1278 | + opt_obj = optimizely.Optimizely(default_decide_options=invalid_decide_options) |
| 1279 | + |
| 1280 | + self.assertEqual(opt_obj.default_decide_options, []) |
| 1281 | + |
| 1282 | + def test_decide_experiment(self): |
| 1283 | + """ Test that the feature is enabled for the user if bucketed into variation of a rollout. |
| 1284 | + Also confirm that no impression event is processed. """ |
| 1285 | + |
| 1286 | + opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features)) |
| 1287 | + project_config = opt_obj.config_manager.get_config() |
| 1288 | + |
| 1289 | + mock_experiment = project_config.get_experiment_from_key('test_experiment') |
| 1290 | + mock_variation = project_config.get_variation_from_id('test_experiment', '111129') |
| 1291 | + with mock.patch( |
| 1292 | + 'optimizely.decision_service.DecisionService.get_variation_for_feature', |
| 1293 | + return_value=(decision_service.Decision(mock_experiment, |
| 1294 | + mock_variation, enums.DecisionSources.FEATURE_TEST), []), |
| 1295 | + ): |
| 1296 | + user_context = opt_obj.create_user_context('test_user') |
| 1297 | + decision = user_context.decide('test_feature_in_experiment', [DecideOption.DISABLE_DECISION_EVENT]) |
| 1298 | + self.assertTrue(decision.enabled, "decision should be enabled") |
0 commit comments