Skip to content

Commit 1f7e2a9

Browse files
update: make cmab_uuid parameter optional in _send_impression_event method
1 parent 9d63477 commit 1f7e2a9

File tree

2 files changed

+3
-63
lines changed

2 files changed

+3
-63
lines changed

optimizely/optimizely.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def _validate_user_inputs(
261261
def _send_impression_event(
262262
self, project_config: project_config.ProjectConfig, experiment: Optional[entities.Experiment],
263263
variation: Optional[entities.Variation], flag_key: str, rule_key: str, rule_type: str,
264-
enabled: bool, user_id: str, attributes: Optional[UserAttributes], cmab_uuid: Optional[str]
264+
enabled: bool, user_id: str, attributes: Optional[UserAttributes], cmab_uuid: Optional[str] = None
265265
) -> None:
266266
""" Helper method to send impression event.
267267

tests/test_optimizely.py

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4890,66 +4890,6 @@ def test_odp_events_not_sent_with_legacy_apis(self):
48904890

48914891
client.close()
48924892

4893-
def test_activate_with_cmab_uuid(self):
4894-
""" Test that activate includes CMAB UUID when available from CMAB service. """
4895-
expected_cmab_uuid = "test-cmab-uuid-123"
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('time.time', return_value=42), mock.patch(
4907-
'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
4908-
), mock.patch(
4909-
'optimizely.event.event_processor.BatchEventProcessor.process'
4910-
) as mock_process:
4911-
result = self.optimizely.activate('test_experiment', 'test_user')
4912-
self.assertEqual('variation', result)
4913-
4914-
# Verify the impression event includes CMAB UUID
4915-
impression_event = mock_process.call_args[0][0]
4916-
self.assertEqual(impression_event.cmab_uuid, expected_cmab_uuid)
4917-
4918-
# Verify the log event includes CMAB UUID in metadata
4919-
log_event = EventFactory.create_log_event(impression_event, self.optimizely.logger)
4920-
metadata = log_event.params['visitors'][0]['snapshots'][0]['decisions'][0]['metadata']
4921-
self.assertIn('cmab_uuid', metadata)
4922-
self.assertEqual(metadata['cmab_uuid'], expected_cmab_uuid)
4923-
4924-
def test_activate_without_cmab_uuid(self):
4925-
""" Test that activate works correctly when CMAB service returns None. """
4926-
variation_result = {
4927-
'variation': self.project_config.get_variation_from_id('test_experiment', '111129'),
4928-
'cmab_uuid': None,
4929-
'reasons': [],
4930-
'error': False
4931-
}
4932-
4933-
with mock.patch(
4934-
'optimizely.decision_service.DecisionService.get_variation',
4935-
return_value=variation_result,
4936-
), mock.patch('time.time', return_value=42), mock.patch(
4937-
'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
4938-
), mock.patch(
4939-
'optimizely.event.event_processor.BatchEventProcessor.process'
4940-
) as mock_process:
4941-
result = self.optimizely.activate('test_experiment', 'test_user')
4942-
self.assertEqual('variation', result)
4943-
4944-
# Verify the impression event has no CMAB UUID
4945-
impression_event = mock_process.call_args[0][0]
4946-
self.assertIsNone(impression_event.cmab_uuid)
4947-
4948-
# Verify the log event does not include CMAB UUID in metadata
4949-
log_event = EventFactory.create_log_event(impression_event, self.optimizely.logger)
4950-
metadata = log_event.params['visitors'][0]['snapshots'][0]['decisions'][0]['metadata']
4951-
self.assertNotIn('cmab_uuid', metadata)
4952-
49534893
def test_get_variation_with_cmab_uuid(self):
49544894
""" Test that get_variation works correctly with CMAB UUID. """
49554895
expected_cmab_uuid = "get-variation-cmab-uuid"
@@ -4965,7 +4905,7 @@ def test_get_variation_with_cmab_uuid(self):
49654905
return_value=variation_result,
49664906
), mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_broadcast:
49674907
variation = self.optimizely.get_variation('test_experiment', 'test_user')
4968-
self.assertEqual('variation', variation['variation'].key)
4908+
self.assertEqual('variation', variation)
49694909

49704910
# Verify decision notification is sent with correct parameters
49714911
mock_broadcast.assert_any_call(
@@ -4990,7 +4930,7 @@ def test_get_variation_without_cmab_uuid(self):
49904930
return_value=variation_result,
49914931
), mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_broadcast:
49924932
variation = self.optimizely.get_variation('test_experiment', 'test_user')
4993-
self.assertEqual('variation', variation['variation'].key)
4933+
self.assertEqual('variation', variation)
49944934

49954935
# Verify decision notification is sent correctly
49964936
mock_broadcast.assert_any_call(

0 commit comments

Comments
 (0)