@@ -2397,6 +2397,122 @@ def test_get_feature_variable__returns_none_if_invalid_variable_key(self):
2397
2397
mock .call ('Variable with key "invalid_variable" not found in the datafile.' )
2398
2398
])
2399
2399
2400
+ def test_get_feature_variable__returns_default_value_if_feature_not_enabled (self ):
2401
+ """ Test that get_feature_variable_* returns default value if feature is not enabled for the user. """
2402
+
2403
+ opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
2404
+ mock_experiment = opt_obj .config .get_experiment_from_key ('test_experiment' )
2405
+ mock_variation = opt_obj .config .get_variation_from_id ('test_experiment' , '111128' )
2406
+
2407
+ # Boolean
2408
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2409
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2410
+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2411
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2412
+
2413
+ self .assertTrue (opt_obj .get_feature_variable_boolean ('test_feature_in_experiment' , 'is_working' , 'test_user' ))
2414
+
2415
+ mock_client_logger .info .assert_called_once_with (
2416
+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2417
+ 'Returning the default variable value "true".'
2418
+ )
2419
+
2420
+ # Double
2421
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2422
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2423
+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2424
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2425
+ self .assertEqual (10.99 ,
2426
+ opt_obj .get_feature_variable_double ('test_feature_in_experiment' , 'cost' , 'test_user' ))
2427
+
2428
+ mock_client_logger .info .assert_called_once_with (
2429
+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2430
+ 'Returning the default variable value "10.99".'
2431
+ )
2432
+
2433
+ # Integer
2434
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2435
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2436
+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2437
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2438
+ self .assertEqual (999 ,
2439
+ opt_obj .get_feature_variable_integer ('test_feature_in_experiment' , 'count' , 'test_user' ))
2440
+
2441
+ mock_client_logger .info .assert_called_once_with (
2442
+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2443
+ 'Returning the default variable value "999".'
2444
+ )
2445
+
2446
+ # String
2447
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2448
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2449
+ decision_service .DECISION_SOURCE_EXPERIMENT )), \
2450
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2451
+ self .assertEqual ('devel' ,
2452
+ opt_obj .get_feature_variable_string ('test_feature_in_experiment' , 'environment' , 'test_user' ))
2453
+
2454
+ mock_client_logger .info .assert_called_once_with (
2455
+ 'Feature "test_feature_in_experiment" for variation "control" is not enabled. '
2456
+ 'Returning the default variable value "devel".'
2457
+ )
2458
+
2459
+ def test_get_feature_variable__returns_default_value_if_feature_not_enabled_in_rollout (self ):
2460
+ """ Test that get_feature_variable_* returns default value if feature is not enabled for the user. """
2461
+
2462
+ opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
2463
+ mock_experiment = opt_obj .config .get_experiment_from_key ('211127' )
2464
+ mock_variation = opt_obj .config .get_variation_from_id ('211127' , '211229' )
2465
+
2466
+ # Boolean
2467
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2468
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2469
+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2470
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2471
+ self .assertFalse (opt_obj .get_feature_variable_boolean ('test_feature_in_rollout' , 'is_running' , 'test_user' ))
2472
+
2473
+ mock_client_logger .info .assert_called_once_with (
2474
+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2475
+ 'Returning the default variable value "false".'
2476
+ )
2477
+
2478
+ # Double
2479
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2480
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2481
+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2482
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2483
+ self .assertEqual (99.99 ,
2484
+ opt_obj .get_feature_variable_double ('test_feature_in_rollout' , 'price' , 'test_user' ))
2485
+
2486
+ mock_client_logger .info .assert_called_once_with (
2487
+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2488
+ 'Returning the default variable value "99.99".'
2489
+ )
2490
+
2491
+ # Integer
2492
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2493
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2494
+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2495
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2496
+ self .assertEqual (999 ,
2497
+ opt_obj .get_feature_variable_integer ('test_feature_in_rollout' , 'count' , 'test_user' ))
2498
+
2499
+ mock_client_logger .info .assert_called_once_with (
2500
+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2501
+ 'Returning the default variable value "999".'
2502
+ )
2503
+
2504
+ # String
2505
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
2506
+ return_value = decision_service .Decision (mock_experiment , mock_variation ,
2507
+ decision_service .DECISION_SOURCE_ROLLOUT )), \
2508
+ mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2509
+ self .assertEqual ('Hello' ,
2510
+ opt_obj .get_feature_variable_string ('test_feature_in_rollout' , 'message' , 'test_user' ))
2511
+ mock_client_logger .info .assert_called_once_with (
2512
+ 'Feature "test_feature_in_rollout" for variation "211229" is not enabled. '
2513
+ 'Returning the default variable value "Hello".'
2514
+ )
2515
+
2400
2516
def test_get_feature_variable__returns_none_if_type_mismatch (self ):
2401
2517
""" Test that get_feature_variable_* returns None if type mismatch. """
2402
2518
@@ -2439,15 +2555,25 @@ def test_get_feature_variable_returns__variable_value__typed_audience_match(self
2439
2555
opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_typed_audiences ))
2440
2556
2441
2557
# Should be included in the feature test via greater-than match audience with id '3468206647'
2442
- self .assertEqual (
2443
- 'xyz' ,
2444
- opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'lasers' : 71 })
2558
+ with mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2559
+ self .assertEqual (
2560
+ 'xyz' ,
2561
+ opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'lasers' : 71 })
2562
+ )
2563
+
2564
+ mock_client_logger .info .assert_called_once_with (
2565
+ 'Got variable value "xyz" for variable "x" of feature flag "feat_with_var".'
2445
2566
)
2446
2567
2447
2568
# Should be included in the feature test via exact match boolean audience with id '3468206643'
2448
- self .assertEqual (
2449
- 'xyz' ,
2450
- opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'should_do_it' : True })
2569
+ with mock .patch .object (opt_obj , 'logger' ) as mock_client_logger :
2570
+ self .assertEqual (
2571
+ 'xyz' ,
2572
+ opt_obj .get_feature_variable_string ('feat_with_var' , 'x' , 'user1' , {'should_do_it' : True })
2573
+ )
2574
+
2575
+ mock_client_logger .info .assert_called_once_with (
2576
+ 'Got variable value "xyz" for variable "x" of feature flag "feat_with_var".'
2451
2577
)
2452
2578
2453
2579
def test_get_feature_variable_returns__default_value__typed_audience_match (self ):
0 commit comments