Skip to content

Commit cbf2c2c

Browse files
update: include CMAB UUID in activation and add corresponding tests
1 parent 752a030 commit cbf2c2c

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

optimizely/optimizely.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from __future__ import annotations
1515

1616
from typing import TYPE_CHECKING, Any, Optional
17-
from unittest import mock
17+
1818

1919
from . import decision_service
2020
from . import entities
@@ -540,8 +540,11 @@ def activate(self, experiment_key: str, user_id: str, attributes: Optional[UserA
540540

541541
variation_result = self.get_variation(experiment_key, user_id, attributes)
542542
variation_key = None
543+
cmab_uuid = None
543544
if variation_result and variation_result['variation']:
544545
variation_key = variation_result['variation'].key
546+
if variation_result and variation_result['cmab_uuid']:
547+
cmab_uuid = variation_result['cmab_uuid']
545548
if not variation_key:
546549
self.logger.info(f'Not activating user "{user_id}".')
547550
return None
@@ -555,7 +558,7 @@ def activate(self, experiment_key: str, user_id: str, attributes: Optional[UserA
555558
# Create and dispatch impression event
556559
self.logger.info(f'Activating user "{user_id}" in experiment "{experiment.key}".')
557560
self._send_impression_event(project_config, experiment, variation, '', experiment.key,
558-
enums.DecisionSources.EXPERIMENT, True, user_id, attributes, None)
561+
enums.DecisionSources.EXPERIMENT, True, user_id, attributes, cmab_uuid)
559562

560563
return variation.key
561564

tests/test_optimizely.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4890,6 +4890,66 @@ 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+
48934953

48944954
class OptimizelyWithExceptionTest(base.BaseTest):
48954955
def setUp(self):

0 commit comments

Comments
 (0)