@@ -4890,57 +4890,6 @@ def test_odp_events_not_sent_with_legacy_apis(self):
4890
4890
4891
4891
client .close ()
4892
4892
4893
- def test_get_variation_with_cmab_uuid (self ):
4894
- """ Test that get_variation works correctly with CMAB UUID. """
4895
- expected_cmab_uuid = "get-variation-cmab-uuid"
4896
- variation_result = {
4897
- 'variation' : self .project_config .get_variation_from_id ('test_experiment' , '111129' ),
4898
- 'cmab_uuid' : expected_cmab_uuid ,
4899
- 'reasons' : [],
4900
- 'error' : False
4901
- }
4902
-
4903
- with mock .patch (
4904
- 'optimizely.decision_service.DecisionService.get_variation' ,
4905
- return_value = variation_result ,
4906
- ), mock .patch ('optimizely.notification_center.NotificationCenter.send_notifications' ) as mock_broadcast :
4907
- variation = self .optimizely .get_variation ('test_experiment' , 'test_user' )
4908
- self .assertEqual ('variation' , variation )
4909
-
4910
- # Verify decision notification is sent with correct parameters
4911
- mock_broadcast .assert_any_call (
4912
- enums .NotificationTypes .DECISION ,
4913
- 'ab-test' ,
4914
- 'test_user' ,
4915
- {},
4916
- {'experiment_key' : 'test_experiment' , 'variation_key' : 'variation' },
4917
- )
4918
-
4919
- def test_get_variation_without_cmab_uuid (self ):
4920
- """ Test that get_variation works correctly when CMAB UUID is None. """
4921
- variation_result = {
4922
- 'variation' : self .project_config .get_variation_from_id ('test_experiment' , '111129' ),
4923
- 'cmab_uuid' : None ,
4924
- 'reasons' : [],
4925
- 'error' : False
4926
- }
4927
-
4928
- with mock .patch (
4929
- 'optimizely.decision_service.DecisionService.get_variation' ,
4930
- return_value = variation_result ,
4931
- ), mock .patch ('optimizely.notification_center.NotificationCenter.send_notifications' ) as mock_broadcast :
4932
- variation = self .optimizely .get_variation ('test_experiment' , 'test_user' )
4933
- self .assertEqual ('variation' , variation )
4934
-
4935
- # Verify decision notification is sent correctly
4936
- mock_broadcast .assert_any_call (
4937
- enums .NotificationTypes .DECISION ,
4938
- 'ab-test' ,
4939
- 'test_user' ,
4940
- {},
4941
- {'experiment_key' : 'test_experiment' , 'variation_key' : 'variation' },
4942
- )
4943
-
4944
4893
4945
4894
class OptimizelyWithExceptionTest (base .BaseTest ):
4946
4895
def setUp (self ):
@@ -5801,3 +5750,77 @@ def test_decide_returns_error_decision_when_decision_service_fails(self):
5801
5750
self .assertIsNone (decision .rule_key )
5802
5751
self .assertEqual (decision .flag_key , 'test_feature_in_experiment' )
5803
5752
self .assertIn ('CMAB service failed to fetch decision' , decision .reasons )
5753
+
5754
+ def test_decide_includes_cmab_uuid_in_dispatched_event (self ):
5755
+ """Test that decide calls UserEventFactory.create_impression_event with correct CMAB UUID."""
5756
+ import copy
5757
+ config_dict = copy .deepcopy (self .config_dict_with_features )
5758
+ config_dict ['experiments' ][0 ]['cmab' ] = {'attributeIds' : ['808797688' , '808797689' ], 'trafficAllocation' : 4000 }
5759
+ config_dict ['experiments' ][0 ]['trafficAllocation' ] = []
5760
+ opt_obj = optimizely .Optimizely (json .dumps (config_dict ))
5761
+ user_context = opt_obj .create_user_context ('test_user' )
5762
+ project_config = opt_obj .config_manager .get_config ()
5763
+
5764
+ # Mock decision service to return a CMAB result
5765
+ expected_cmab_uuid = 'uuid-cmab'
5766
+ mock_experiment = project_config .get_experiment_from_key ('test_experiment' )
5767
+ mock_variation = project_config .get_variation_from_id ('test_experiment' , '111129' )
5768
+ decision_result = {
5769
+ 'decision' : decision_service .Decision (
5770
+ mock_experiment ,
5771
+ mock_variation ,
5772
+ enums .DecisionSources .FEATURE_TEST ,
5773
+ expected_cmab_uuid
5774
+ ),
5775
+ 'reasons' : [],
5776
+ 'error' : False
5777
+ }
5778
+
5779
+ with mock .patch .object (
5780
+ opt_obj .decision_service , 'get_variations_for_feature_list' ,
5781
+ return_value = [decision_result ]
5782
+ ), mock .patch (
5783
+ 'optimizely.event.user_event_factory.UserEventFactory.create_impression_event'
5784
+ ) as mock_create_impression , mock .patch (
5785
+ 'time.time' , return_value = 42
5786
+ ), mock .patch (
5787
+ 'uuid.uuid4' , return_value = 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
5788
+ ):
5789
+ # Call decide
5790
+ decision = user_context .decide ('test_feature_in_experiment' )
5791
+
5792
+ # Verify the decision contains the expected information
5793
+ self .assertTrue (decision .enabled )
5794
+ self .assertEqual (decision .variation_key , 'variation' )
5795
+ self .assertEqual (decision .rule_key , 'test_experiment' )
5796
+ self .assertEqual (decision .flag_key , 'test_feature_in_experiment' )
5797
+
5798
+ # Verify that create_impression_event was called once
5799
+ mock_create_impression .assert_called_once ()
5800
+
5801
+ # Get the call arguments
5802
+ call_args = mock_create_impression .call_args [0 ]
5803
+
5804
+ # Verify the correct parameters were passed
5805
+ project_config_arg = call_args [0 ]
5806
+ experiment_arg = call_args [1 ]
5807
+ variation_id_arg = call_args [2 ]
5808
+ flag_key_arg = call_args [3 ]
5809
+ rule_key_arg = call_args [4 ]
5810
+ rule_type_arg = call_args [5 ]
5811
+ enabled_arg = call_args [6 ]
5812
+ user_id_arg = call_args [7 ]
5813
+ attributes_arg = call_args [8 ]
5814
+ cmab_uuid_arg = call_args [9 ]
5815
+
5816
+ # Verify all parameters
5817
+ self .assertEqual (project_config_arg , project_config )
5818
+ self .assertEqual (experiment_arg , mock_experiment )
5819
+ self .assertEqual (variation_id_arg , '111129' ) # variation.id
5820
+ self .assertEqual (flag_key_arg , 'test_feature_in_experiment' )
5821
+ self .assertEqual (rule_key_arg , 'test_experiment' )
5822
+ self .assertEqual (rule_type_arg , str (enums .DecisionSources .FEATURE_TEST ))
5823
+ self .assertTrue (enabled_arg )
5824
+ self .assertEqual (user_id_arg , 'test_user' )
5825
+ self .assertEqual (attributes_arg , {})
5826
+ self .assertEqual (cmab_uuid_arg , expected_cmab_uuid )
0 commit comments