Skip to content

Commit fdcdfbf

Browse files
update: add tests for get_variation with and without CMAB UUID
1 parent cbf2c2c commit fdcdfbf

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/test_optimizely.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,6 +4950,57 @@ def test_activate_without_cmab_uuid(self):
49504950
metadata = log_event.params['visitors'][0]['snapshots'][0]['decisions'][0]['metadata']
49514951
self.assertNotIn('cmab_uuid', metadata)
49524952

4953+
def test_get_variation_with_cmab_uuid(self):
4954+
""" Test that get_variation works correctly with CMAB UUID. """
4955+
expected_cmab_uuid = "get-variation-cmab-uuid"
4956+
variation_result = {
4957+
'variation': self.project_config.get_variation_from_id('test_experiment', '111129'),
4958+
'cmab_uuid': expected_cmab_uuid,
4959+
'reasons': [],
4960+
'error': False
4961+
}
4962+
4963+
with mock.patch(
4964+
'optimizely.decision_service.DecisionService.get_variation',
4965+
return_value=variation_result,
4966+
), mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_broadcast:
4967+
variation = self.optimizely.get_variation('test_experiment', 'test_user')
4968+
self.assertEqual('variation', variation['variation'].key)
4969+
4970+
# Verify decision notification is sent with correct parameters
4971+
mock_broadcast.assert_any_call(
4972+
enums.NotificationTypes.DECISION,
4973+
'ab-test',
4974+
'test_user',
4975+
{},
4976+
{'experiment_key': 'test_experiment', 'variation_key': 'variation'},
4977+
)
4978+
4979+
def test_get_variation_without_cmab_uuid(self):
4980+
""" Test that get_variation works correctly when CMAB UUID is None. """
4981+
variation_result = {
4982+
'variation': self.project_config.get_variation_from_id('test_experiment', '111129'),
4983+
'cmab_uuid': None,
4984+
'reasons': [],
4985+
'error': False
4986+
}
4987+
4988+
with mock.patch(
4989+
'optimizely.decision_service.DecisionService.get_variation',
4990+
return_value=variation_result,
4991+
), mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_broadcast:
4992+
variation = self.optimizely.get_variation('test_experiment', 'test_user')
4993+
self.assertEqual('variation', variation['variation'].key)
4994+
4995+
# Verify decision notification is sent correctly
4996+
mock_broadcast.assert_any_call(
4997+
enums.NotificationTypes.DECISION,
4998+
'ab-test',
4999+
'test_user',
5000+
{},
5001+
{'experiment_key': 'test_experiment', 'variation_key': 'variation'},
5002+
)
5003+
49535004

49545005
class OptimizelyWithExceptionTest(base.BaseTest):
49555006
def setUp(self):

0 commit comments

Comments
 (0)