@@ -251,7 +251,7 @@ def test_activate__bucketer_returns_none(self):
251
251
def test_activate__invalid_object (self ):
252
252
""" Test that activate logs error if Optimizely object is not created correctly. """
253
253
254
- opt_obj = optimizely .Optimizely ('invalid_file ' )
254
+ opt_obj = optimizely .Optimizely ('invalid_datafile ' )
255
255
256
256
with mock .patch ('optimizely.logger.SimpleLogger.log' ) as mock_logging :
257
257
self .assertIsNone (opt_obj .activate ('test_experiment' , 'test_user' ))
@@ -538,7 +538,7 @@ def test_track__whitelisted_user_overrides_audience_check(self):
538
538
def test_track__invalid_object (self ):
539
539
""" Test that track logs error if Optimizely object is not created correctly. """
540
540
541
- opt_obj = optimizely .Optimizely ('invalid_file ' )
541
+ opt_obj = optimizely .Optimizely ('invalid_datafile ' )
542
542
543
543
with mock .patch ('optimizely.logger.SimpleLogger.log' ) as mock_logging :
544
544
opt_obj .track ('test_event' , 'test_user' )
@@ -548,7 +548,7 @@ def test_track__invalid_object(self):
548
548
def test_get_variation__invalid_object (self ):
549
549
""" Test that get_variation logs error if Optimizely object is not created correctly. """
550
550
551
- opt_obj = optimizely .Optimizely ('invalid_file ' )
551
+ opt_obj = optimizely .Optimizely ('invalid_datafile ' )
552
552
553
553
with mock .patch ('optimizely.logger.SimpleLogger.log' ) as mock_logging :
554
554
self .assertIsNone (opt_obj .get_variation ('test_experiment' , 'test_user' ))
@@ -557,34 +557,42 @@ def test_get_variation__invalid_object(self):
557
557
558
558
def test_is_feature_enabled__returns_false_for_invalid_feature (self ):
559
559
""" Test that the feature is not enabled for the user if the provided feature key is invalid. """
560
- optimizely_instance = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
561
- project_config = optimizely_instance .config
562
560
563
- with mock . patch (
564
- 'optimizely.decision_service.DecisionService.get_variation_for_feature'
565
- ) as mock_decision :
566
- self .assertFalse (optimizely_instance .is_feature_enabled ('invalid_feature' , 'user1' ))
561
+ opt_obj = optimizely . Optimizely ( json . dumps ( self . config_dict_with_features ))
562
+
563
+ with mock . patch ( 'optimizely.decision_service.DecisionService.get_variation_for_feature' ) as mock_decision :
564
+ self .assertFalse (opt_obj .is_feature_enabled ('invalid_feature' , 'user1' ))
567
565
568
566
self .assertFalse (mock_decision .called )
569
567
570
568
def test_is_feature_enabled__returns_true_if_user_is_bucketed_into_a_variation (self ):
571
569
""" Test that the feature is not enabled for the user if the provided feature key is invalid. """
572
- optimizely_instance = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
573
- project_config = optimizely_instance .config
570
+
571
+ opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
572
+ project_config = opt_obj .config
574
573
feature = project_config .get_feature_from_key ('test_feature_1' )
575
574
576
- with mock .patch (
577
- 'optimizely.decision_service.DecisionService.get_variation_for_feature' ,
578
- return_value = project_config .get_variation_from_id ('test_experiment' , '111129' )
579
- ) as mock_decision :
580
- self .assertTrue (optimizely_instance .is_feature_enabled ('test_feature_1' , 'user1' ))
575
+ with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ,
576
+ return_value = project_config .get_variation_from_id ('test_experiment' , '111129' )) as mock_decision :
577
+ self .assertTrue (opt_obj .is_feature_enabled ('test_feature_1' , 'user1' ))
581
578
582
579
mock_decision .assert_called_once_with (feature , 'user1' , None )
583
580
581
+ def test_is_feature_enabled__invalid_object (self ):
582
+ """ Test that is_feature_enabled returns False if Optimizely object is not valid. """
583
+
584
+ opt_obj = optimizely .Optimizely ('invalid_file' )
585
+
586
+ with mock .patch ('optimizely.logger.SimpleLogger.log' ) as mock_logging :
587
+ self .assertFalse (opt_obj .is_feature_enabled ('test_feature_1' , 'user_1' ))
588
+
589
+ mock_logging .assert_called_once_with (enums .LogLevels .ERROR ,
590
+ 'Datafile has invalid format. Failing "is_feature_enabled".' )
591
+
584
592
def test_get_enabled_features (self ):
585
593
""" Test that get_enabled_features only returns features that are enabled for the specified user. """
586
- optimizely_instance = optimizely . Optimizely ( json . dumps ( self . config_dict_with_features ))
587
- project_config = optimizely_instance . config
594
+
595
+ opt_obj = optimizely . Optimizely ( json . dumps ( self . config_dict_with_features ))
588
596
589
597
def side_effect (* args , ** kwargs ):
590
598
feature_key = args [0 ]
@@ -593,10 +601,9 @@ def side_effect(*args, **kwargs):
593
601
594
602
return False
595
603
596
- with mock .patch (
597
- 'optimizely.optimizely.Optimizely.is_feature_enabled' ,
598
- side_effect = side_effect ) as mock_is_feature_enabled :
599
- received_features = optimizely_instance .get_enabled_features ('user_1' )
604
+ with mock .patch ('optimizely.optimizely.Optimizely.is_feature_enabled' ,
605
+ side_effect = side_effect ) as mock_is_feature_enabled :
606
+ received_features = opt_obj .get_enabled_features ('user_1' )
600
607
601
608
expected_enabled_features = ['test_feature_1' , 'test_feature_2' ]
602
609
self .assertEqual (sorted (expected_enabled_features ), sorted (received_features ))
@@ -605,6 +612,17 @@ def side_effect(*args, **kwargs):
605
612
mock_is_feature_enabled .assert_any_call ('test_feature_in_group' , 'user_1' , None )
606
613
mock_is_feature_enabled .assert_any_call ('test_feature_in_experiment_and_rollout' , 'user_1' , None )
607
614
615
+ def test_get_enabled_features__invalid_object (self ):
616
+ """ Test that get_enabled_features returns empty list if Optimizely object is not valid. """
617
+
618
+ opt_obj = optimizely .Optimizely ('invalid_file' )
619
+
620
+ with mock .patch ('optimizely.logger.SimpleLogger.log' ) as mock_logging :
621
+ self .assertEqual ([], opt_obj .get_enabled_features ('user_1' ))
622
+
623
+ mock_logging .assert_called_once_with (enums .LogLevels .ERROR ,
624
+ 'Datafile has invalid format. Failing "get_enabled_features".' )
625
+
608
626
609
627
class OptimizelyWithExceptionTest (base .BaseTest ):
610
628
0 commit comments