@@ -2242,6 +2242,122 @@ def test_get_feature_variable__returns_none_if_invalid_variable_key(self):
22422242 mock .call ('Variable with key "invalid_variable" not found in the datafile.' )
22432243 ])
22442244
2245+ def test_get_feature_variable__returns_default_value_if_feature_not_enabled (self ):
2246+ """ Test that get_feature_variable_* returns default value if feature is not enabled for the user. """
2247+
2248+ opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
2249+ mock_experiment = opt_obj .config .get_experiment_from_key ('test_experiment' )
2250+ mock_variation = opt_obj .config .get_variation_from_id ('test_experiment' , '111128' )
2251+
2252+ # Boolean
2253+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2254+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2255+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2256+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2257+
2258+ self .assertTrue (opt_obj .get_feature_variable_boolean ('test_feature_in_experiment' , 'is_working' , 'test_user' ))
2259+
2260+ mock_client_logger .info .assert_called_once_with (
2261+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2262+ 'Returning the default variable value "true".'
2263+ )
2264+
2265+ # Double
2266+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2267+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2268+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2269+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2270+ self .assertEqual (10.99 ,
2271+ opt_obj .get_feature_variable_double ('test_feature_in_experiment' , 'cost' , 'test_user' ))
2272+
2273+ mock_client_logger .info .assert_called_once_with (
2274+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2275+ 'Returning the default variable value "10.99".'
2276+ )
2277+
2278+ # Integer
2279+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2280+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2281+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2282+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2283+ self .assertEqual (999 ,
2284+ opt_obj .get_feature_variable_integer ('test_feature_in_experiment' , 'count' , 'test_user' ))
2285+
2286+ mock_client_logger .info .assert_called_once_with (
2287+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2288+ 'Returning the default variable value "999".'
2289+ )
2290+
2291+ # String
2292+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2293+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2294+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2295+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2296+ self .assertEqual ('devel' ,
2297+ opt_obj .get_feature_variable_string ('test_feature_in_experiment' , 'environment' , 'test_user' ))
2298+
2299+ mock_client_logger .info .assert_called_once_with (
2300+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2301+ 'Returning the default variable value "devel".'
2302+ )
2303+
2304+ def test_get_feature_variable__returns_default_value_if_feature_not_enabled_in_rollout (self ):
2305+ """ Test that get_feature_variable_* returns default value if feature is not enabled for the user. """
2306+
2307+ opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
2308+ mock_experiment = opt_obj .config .get_experiment_from_key ('211127' )
2309+ mock_variation = opt_obj .config .get_variation_from_id ('211127' , '211229' )
2310+
2311+ # Boolean
2312+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2313+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2314+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2315+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2316+ self .assertFalse (opt_obj .get_feature_variable_boolean ('test_feature_in_rollout' , 'is_running' , 'test_user' ))
2317+
2318+ mock_client_logger .info .assert_called_once_with (
2319+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2320+ 'Returning the default variable value "false".'
2321+ )
2322+
2323+ # Double
2324+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2325+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2326+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2327+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2328+ self .assertEqual (99.99 ,
2329+ opt_obj .get_feature_variable_double ('test_feature_in_rollout' , 'price' , 'test_user' ))
2330+
2331+ mock_client_logger .info .assert_called_once_with (
2332+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2333+ 'Returning the default variable value "99.99".'
2334+ )
2335+
2336+ # Integer
2337+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2338+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2339+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2340+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2341+ self .assertEqual (999 ,
2342+ opt_obj .get_feature_variable_integer ('test_feature_in_rollout' , 'count' , 'test_user' ))
2343+
2344+ mock_client_logger .info .assert_called_once_with (
2345+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2346+ 'Returning the default variable value "999".'
2347+ )
2348+
2349+ # String
2350+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2351+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2352+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2353+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2354+ self .assertEqual ('Hello' ,
2355+ opt_obj .get_feature_variable_string ('test_feature_in_rollout' , 'message' , 'test_user' ))
2356+ mock_client_logger .info .assert_called_once_with (
2357+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2358+ 'Returning the default variable value "Hello".'
2359+ )
2360+
22452361 def test_get_feature_variable__returns_none_if_type_mismatch (self ):
22462362 """ Test that get_feature_variable_* returns None if type mismatch. """
22472363
@@ -2284,15 +2400,25 @@ def test_get_feature_variable_returns__variable_value__typed_audience_match(self
22842400 opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_typed_audiences ))
22852401
22862402 # Should be included in the feature test via greater-than match audience with id '3468206647'
2287- self .assertEqual (
2288- 'xyz' ,
2289- opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'lasers' : 71 })
2403+ with mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2404+ self .assertEqual (
2405+ 'xyz' ,
2406+ opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'lasers' : 71 })
2407+ )
2408+
2409+ mock_client_logger .info .assert_called_once_with (
2410+ 'Got variable value "xyz" for variable "x" of feature flag "feat_with_var".'
22902411 )
22912412
22922413 # Should be included in the feature test via exact match boolean audience with id '3468206643'
2293- self .assertEqual (
2294- 'xyz' ,
2295- opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'should_do_it' : True })
2414+ with mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2415+ self .assertEqual (
2416+ 'xyz' ,
2417+ opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'should_do_it' : True })
2418+ )
2419+
2420+ mock_client_logger .info .assert_called_once_with (
2421+ 'Got variable value "xyz" for variable "x" of feature flag "feat_with_var".'
22962422 )
22972423
22982424 def test_get_feature_variable_returns__default_value__typed_audience_match (self ):
0 commit comments