@@ -887,6 +887,17 @@ def test_get_rollout_from_id__valid_rollout_id(self):
887
887
}])
888
888
self .assertEqual (expected_rollout , project_config .get_rollout_from_id ('211111' ))
889
889
890
+ def test_get_rollout_from_id__invalid_rollout_id (self ):
891
+ """ Test that None is returned for an unknown Rollout ID """
892
+
893
+ opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ),
894
+ logger = logger .NoOpLogger ())
895
+ project_config = opt_obj .config
896
+ with mock .patch ('optimizely.logger.NoOpLogger.log' ) as mock_logging :
897
+ self .assertIsNone (project_config .get_rollout_from_id ('aabbccdd' ))
898
+
899
+ mock_logging .assert_called_once_with (enums .LogLevels .ERROR , 'Rollout with ID "aabbccdd" is not in datafile.' )
900
+
890
901
def test_get_variable_value_for_variation__returns_valid_value (self ):
891
902
""" Test that the right value is returned. """
892
903
opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
@@ -971,6 +982,29 @@ def test_get_forced_variation__invalid_experiment_key(self):
971
982
self .assertIsNone (self .project_config .get_forced_variation (None , 'test_user' ))
972
983
self .assertIsNone (self .project_config .get_forced_variation ('' , 'test_user' ))
973
984
985
+ def test_get_forced_variation_with_none_set_for_user (self ):
986
+ """ Test get_forced_variation when none set for user ID in forced variation map. """
987
+ self .project_config .forced_variation_map = {}
988
+ self .project_config .forced_variation_map ['test_user' ] = {}
989
+
990
+ with mock .patch ('optimizely.logger.NoOpLogger.log' ) as mock_logging :
991
+ self .assertIsNone (self .project_config .get_forced_variation ('test_experiment' , 'test_user' ))
992
+ mock_logging .assert_called_once_with (
993
+ enums .LogLevels .DEBUG ,
994
+ 'No experiment "test_experiment" mapped to user "test_user" in the forced variation map.' )
995
+
996
+ def test_get_forced_variation_missing_variation_mapped_to_experiment (self ):
997
+ """ Test get_forced_variation when no variation found against given experiment for the user. """
998
+ self .project_config .forced_variation_map = {}
999
+ self .project_config .forced_variation_map ['test_user' ] = {}
1000
+ self .project_config .forced_variation_map ['test_user' ]['test_experiment' ] = None
1001
+
1002
+ with mock .patch ('optimizely.logger.NoOpLogger.log' ) as mock_logging :
1003
+ self .assertIsNone (self .project_config .get_forced_variation ('test_experiment' , 'test_user' ))
1004
+ mock_logging .assert_called_once_with (
1005
+ enums .LogLevels .DEBUG ,
1006
+ 'No variation mapped to experiment "test_experiment" in the forced variation map.' )
1007
+
974
1008
# set_forced_variation tests
975
1009
def test_set_forced_variation__invalid_user_id (self ):
976
1010
""" Test invalid user IDs set fail to set a forced variation """
@@ -1017,6 +1051,28 @@ def test_set_forced_variation__multiple_sets(self):
1017
1051
self .assertEqual (self .project_config .get_forced_variation ('test_experiment' , 'test_user_1' ).key , 'control' )
1018
1052
self .assertEqual (self .project_config .get_forced_variation ('group_exp_1' , 'test_user_1' ).key , 'group_exp_1_control' )
1019
1053
1054
+ def test_set_forced_variation_when_called_to_remove_forced_variation (self ):
1055
+ """ Test set_forced_variation when no variation is given. """
1056
+ # Test case where both user and experiment are present in the forced variation map
1057
+ self .project_config .forced_variation_map = {}
1058
+ self .project_config .set_forced_variation ('test_experiment' , 'test_user' , 'variation' )
1059
+
1060
+ with mock .patch ('optimizely.logger.NoOpLogger.log' ) as mock_logging :
1061
+ self .assertTrue (self .project_config .set_forced_variation ('test_experiment' , 'test_user' , None ))
1062
+ mock_logging .assert_called_once_with (
1063
+ enums .LogLevels .DEBUG ,
1064
+ 'Variation mapped to experiment "test_experiment" has been removed for user "test_user".' )
1065
+
1066
+ # Test case where user is present in the forced variation map, but the given experiment isn't
1067
+ self .project_config .forced_variation_map = {}
1068
+ self .project_config .set_forced_variation ('test_experiment' , 'test_user' , 'variation' )
1069
+
1070
+ with mock .patch ('optimizely.logger.NoOpLogger.log' ) as mock_logging :
1071
+ self .assertTrue (self .project_config .set_forced_variation ('group_exp_1' , 'test_user' , None ))
1072
+ mock_logging .assert_called_once_with (
1073
+ enums .LogLevels .DEBUG ,
1074
+ 'Nothing to remove. Variation mapped to experiment "group_exp_1" for user "test_user" does not exist.' )
1075
+
1020
1076
1021
1077
class ConfigLoggingTest (base .BaseTest ):
1022
1078
def setUp (self ):
0 commit comments