8000 [FSSDK-11184] Update: Send CMAB uuid in impression events by FarhanAnjum-opti · Pull Request #458 · optimizely/python-sdk · GitHub
[go: up one dir, main page]

Skip to content

[FSSDK-11184] Update: Send CMAB uuid in impression events #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
88b4f1e
update: integrate CMAB components into OptimizelyFactory
FarhanAnjum-opti Jun 4, 2025
2563c7b
update: add cmab_service parameter to Optimizely constructor for CMAB…
FarhanAnjum-opti Jun 4, 2025
fac8946
update: add docstring to DefaultCmabService class for improved docume…
FarhanAnjum-opti Jun 4, 2025
f74bc8c
update: implement CMAB support in bucketer and decision service, reve…
FarhanAnjum-opti Jun 13, 2025
6d1f73d
linting fix
FarhanAnjum-opti Jun 13, 2025
91d53b6
update: add cmab_uuid handling in DecisionService and related tests
FarhanAnjum-opti Jun 16, 2025
3eb755f
- updated function bucket_to_entity_id
FarhanAnjum-opti Jun 16, 2025
a5e4993
update: add None parameter to Decision constructor in user context tests
FarhanAnjum-opti Jun 16, 2025
c1cd97a
update: enhance CMAB decision handling and add related tests
FarhanAnjum-opti Jun 16, 2025
fd7c723
update: fix logger message formatting in CMAB experiment tests
FarhanAnjum-opti Jun 16, 2025
ec19c3b
mypy fix
FarhanAnjum-opti Jun 16, 2025
029262d
update: refine traffic allocation type hints and key naming in bucket…
FarhanAnjum-opti Jun 16, 2025
180fdee
update: remove unused import of cast in bucketer.py
FarhanAnjum-opti Jun 16, 2025
cd5ba39
update: fix return type for numeric_metric_value in get_numeric_value…
FarhanAnjum-opti Jun 16, 2025
92a3258
update: specify type hint for numeric_metric_value in get_numeric_val…
FarhanAnjum-opti Jun 16, 2025
fe100cb
update: fix logger reference in DefaultCmabClient initialization and …
FarhanAnjum-opti Jun 17, 2025
60a4ada
update: enhance error logging for CMAB fetch failures with detailed m…
FarhanAnjum-opti Jun 20, 2025
265d82b
update: enhance decision result handling by introducing VariationResu…
FarhanAnjum-opti Jun 20, 2025
6ca1102
update: refactor get_variation return structure and change tests acco…
FarhanAnjum-opti Jun 20, 2025
c2b3d96
-Error propagated to optimizely.py
FarhanAnjum-opti Jun 23, 2025
0e25622
update: add cmab_uuid parameter to impression events
FarhanAnjum-opti Jun 27, 2025
088f4af
update: add None parameter to impression events in decision tests
FarhanAnjum-opti Jun 27, 2025
b901c5f
update: modify get_variation to return VariationResult and adjust rel…
FarhanAnjum-opti Jun 27, 2025
fa77cca
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jun 27, 2025
d2fc631
update: unit test fixes
FarhanAnjum-opti Jun 27, 2025
752a030
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jun 27, 2025
cbf2c2c
update: include CMAB UUID in activation and add corresponding tests
FarhanAnjum-opti Jun 27, 2025
fdcdfbf
update: add tests for get_variation with and without CMAB UUID
FarhanAnjum-opti Jun 27, 2025
b9a8555
Revert "update: unit test fixes"
FarhanAnjum-opti Jun 30, 2025
a129854
Revert "update: modify get_variation to return VariationResult and ad…
FarhanAnjum-opti Jun 30, 2025
9d63477
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jun 30, 2025
1f7e2a9
update: make cmab_uuid parameter optional in _send_impression_event m…
FarhanAnjum-opti Jun 30, 2025
73a2802
chore: trigger CI by turning on python flag
FarhanAnjum-opti Jun 30, 2025
a6d9771
update: new class method to handle optimizely error decisions
FarhanAnjum-opti Jun 30, 2025
4743376
fix unit test
FarhanAnjum-opti Jun 30, 2025
6d79053
fix: update error logging format for CMAB fetch failures
FarhanAnjum-opti Jun 30, 2025
3c903c7
chore: trigger CI
FarhanAnjum-opti Jul 1, 2025
c637878
update: enhance decision service to handle error states and improve b…
FarhanAnjum-opti Jul 3, 2025
62bbf64
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
0bc4fbd
update: remove debug print statement from Optimizely class
FarhanAnjum-opti Jul 3, 2025
8668565
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
fcdad1f
update: enhance bucketing logic to support CMAB traffic allocations
FarhanAnjum-opti Jul 3, 2025
dd4790d
8000 Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
aca7df4
update: improve error logging for CMAB decision fetch failures
FarhanAnjum-opti Jul 3, 2025
c14b768
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
72955a0
update: improve logging and error handling in bucketer and decision s…
FarhanAnjum-opti Jul 7, 2025
265ea3c
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 7, 2025
b88c08b
Merge branch 'master' into farhan-anjum/FSSDK-11184-update-impression…
FarhanAnjum-opti Jul 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update: enhance CMAB decision handling and add related tests
  • Loading branch information
FarhanAnjum-opti committed Jun 16, 2025
commit c1cd97ab6ac2c950e3c7f393fb7cb7a10c145a78
3 changes: 2 additions & 1 deletion optimizely/decision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ def get_variation(
options)
decide_reasons += decision_variation_value.get('reasons', [])
cmab_decision = decision_variation_value.get('result')
if not cmab_decision:
if not cmab_decision or decision_variation_value['error']:
self.logger.error(Errors.CMAB_FETCH_FAILED.format(decide_reasons[0]))
return None, decide_reasons, cmab_uuid
variation_id = cmab_decision['variation_id']
cmab_uuid = cmab_decision['cmab_uuid']
Expand Down
270 changes: 270 additions & 0 deletions tests/test_decision_service.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,276 @@ def test_get_variation__ignore_user_profile_when_specified(self):
self.assertEqual(0, mock_lookup.call_count)
self.assertEqual(0, mock_save.call_count)

def test_get_variation_cmab_experiment_user_in_traffic_allocation(self):
"""Test get_variation with CMAB experiment where user is in traffic allocation."""

# Create a user context
user = optimizely_user_context.OptimizelyUserContext(
optimizely_client=None,
logger=None,
user_id="test_user",
user_attributes={}
)

# Create a CMAB experiment
cmab_experiment = entities.Experiment(
'111150',
'cmab_experiment',
'Running',
'111150',
[], # No audience IDs
{},
[
entities.Variation('111151', 'variation_1'),
entities.Variation('111152', 'variation_2')
],
[
{'entityId': '111151', 'endOfRange': 5000},
{'entityId': '111152', 'endOfRange': 10000}
],
cmab={'trafficAllocation': 5000}
)

cmab_decision = {
'variation_id': '111151',
'cmab_uuid': 'test-cmab-uuid-123'
}

with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \
mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions', return_value=[True, []]), \
mock.patch('optimizely.bucketer.Bucketer.bucket_to_entity_id', return_value=['$', []]), \
mock.patch('optimizely.decision_service.DecisionService._get_decision_for_cmab_experiment',
return_value={'error': False, 'result': cmab_decision, 'reasons': []}), \
mock.patch.object(self.project_config, 'get_variation_from_id',
return_value=entities.Variation('111151', 'variation_1')), \
mock.patch.object(self.decision_service,
'logger') as mock_logger:

# Call get_variation with the CMAB experiment
variation, reasons, cmab_uuid = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
None
)

# Verify the variation and cmab_uuid
self.assertEqual(entities.Variation('111151', 'variation_1'), variation)
self.assertEqual('test-cmab-uuid-123', cmab_uuid)

# Verify logger was called
mock_logger.info.assert_any_call('User "test_user" is in variation\
"variation_1" of experiment cmab_experiment.')

def test_get_variation_cmab_experiment_user_not_in_traffic_allocation(self):
"""Test get_variation with CMAB experiment where user is not in traffic allocation."""

# Create a user context
user = optimizely_user_context.OptimizelyUserContext(
optimizely_client=None,
logger=None,
user_id="test_user",
user_attributes={}
)

# Create a CMAB experiment
cmab_experiment = entities.Experiment(
'111150',
'cmab_experiment',
'Running',
'111150',
[], # No audience IDs
{},
[entities.Variation('111151', 'variation_1')],
[{'entityId': '111151', 'endOfRange': 10000}],
cmab={'trafficAllocation': 5000}
)

with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \
mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions', return_value=[True, []]), \
mock.patch('optimizely.bucketer.Bucketer.bucket_to_entity_id', return_value=['not_in_allocation', []]), \
mock.patch('optimizely.decision_service.DecisionService._get_decision_for_cmab_experiment'
) as mock_cmab_decision, \
mock.patch.object(self.decision_service,
'logger') as mock_logger:

# Call get_variation with the CMAB experiment
variation, reasons, cmab_uuid = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
None
)

# Verify we get no variation and CMAB service wasn't called
self.assertIsNone(variation)
self.assertIsNone(cmab_uuid)
mock_cmab_decision.assert_not_called()

# Verify logger was called
mock_logger.info.assert_any_call('User "test_user" not in CMAB\
experiment "cmab_experiment" due to traffic allocation.')

def test_get_variation_cmab_experiment_service_error(self):
"""Test get_variation with CMAB experiment when the CMAB service returns an error."""

# Create a user context
user = optimizely_user_context.OptimizelyUserContext(
optimizely_client=None,
logger=None,
user_id="test_user",
user_attributes={}
)

# Create a CMAB experiment
cmab_experiment = entities.Experiment(
'111150',
'cmab_experiment',
'Running',
'111150',
[], # No audience IDs
{},
[entities.Variation('111151', 'variation_1')],
[{'entityId': '111151', 'endOfRange': 10000}],
cmab={'trafficAllocation': 5000}
)

with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \
mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions', return_value=[True, []]), \
mock.patch('optimizely.bucketer.Bucketer.bucket_to_entity_id', return_value=['$', []]), \
mock.patch('optimizely.decision_service.DecisionService._get_decision_for_cmab_experiment',
return_value={'error': True, 'result': None, 'reasons': ['CMAB service error']}), \
mock.patch.object(self.decision_service,
'logger') as mock_logger:

# Call get_variation with the CMAB experiment
variation, reasons, cmab_uuid = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
None
)

# Verify we get no variation due to CMAB service error
self.assertIsNone(variation)
self.assertIsNone(cmab_uuid)
self.assertIn('CMAB service error', reasons)

# Verify logger was called
mock_logger.error.assert_any_call('CMAB decision fetch failed with status: CMAB service error')

def test_get_variation_cmab_experiment_forced_variation(self):
"""Test get_variation with CMAB experiment when user has a forced variation."""

# Create a user context
user = optimizely_user_context.OptimizelyUserContext(
optimizely_client=None,
logger=None,
user_id="test_user",
user_attributes={}
)

# Create a CMAB experiment
cmab_experiment = entities.Experiment(
'111150',
'cmab_experiment',
'Running',
'111150',
[], # No audience IDs
{},
[
entities.Variation('111151', 'variation_1'),
entities.Variation('111152', 'variation_2')
],
[
{'entityId': '111151', 'endOfRange': 5000},
{'entityId': '111152', 'endOfRange': 10000}
],
cmab={'trafficAllocation': 5000}
)

forced_variation = entities.Variation('111152', 'variation_2')

with mock.patch('optimizely.decision_service.DecisionService.get_forced_variation',
return_value=[forced_variation, ['User is forced into variation']]), \
mock.patch('optimizely.bucketer.Bucketer.bucket_to_entity_id') as mock_bucket, \
mock.patch('optimizely.decision_service.DecisionService._get_decision_for_cmab_experiment'
) as mock_cmab_decision:

# Call get_variation with the CMAB experiment
variation, reasons, cmab_uuid = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
None
)

# Verify we get the forced variation
self.assertEqual(forced_variation, variation)
self.assertIsNone(cmab_uuid)
self.assertIn('User is forced into variation', reasons)

# Verify CMAB-specific methods weren't called
mock_bucket.assert_not_called()
mock_cmab_decision.assert_not_called()

def test_get_variation_cmab_experiment_with_whitelisted_variation(self):
"""Test get_variation with CMAB experiment when user has a whitelisted variation."""

# Create a user context
user = optimizely_user_context.OptimizelyUserContext(
optimizely_client=None,
logger=None,
user_id="test_user",
user_attributes={}
)

# Create a CMAB experiment with forced variations
cmab_experiment = entities.Experiment(
'111150',
'cmab_experiment',
'Running',
'111150',
[], # No audience IDs
{'test_user': 'variation_2'},
[
entities.Variation('111151', 'variation_1'),
entities.Variation('111152', 'variation_2')
],
[
{'entityId': '111151', 'endOfRange': 5000},
{'entityId': '111152', 'endOfRange': 10000}
],
cmab={'trafficAllocation': 5000}
)

whitelisted_variation = entities.Variation('111152', 'variation_2')

with mock.patch('optimizely.decision_service.DecisionService.get_forced_variation',
return_value=[None, []]), \
mock.patch('optimizely.decision_service.DecisionService.get_whitelisted_variation',
return_value=[whitelisted_variation, ['User is whitelisted into variation']]), \
mock.patch('optimizely.bucketer.Bucketer.bucket_to_entity_id') as mock_bucket, \
mock.patch('optimizely.decision_service.DecisionService._get_decision_for_cmab_experiment'
) as mock_cmab_decision:

# Call get_variation with the CMAB experiment
variation, reasons, cmab_uuid = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
None
)

# Verify we get the whitelisted variation
self.assertEqual(whitelisted_variation, variation)
self.assertIsNone(cmab_uuid)
self.assertIn('User is whitelisted into variation', reasons)

# Verify CMAB-specific methods weren't called
mock_bucket.assert_not_called()
mock_cmab_decision.assert_not_called()


class FeatureFlagDecisionTests(base.BaseTest):
def setUp(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ def on_activate(experiment, user_id, attributes, variation, event):
with mock.patch(
'optimizely.decision_service.DecisionService.get_variation_for_feature',
return_value=(
decision_service.Decision(mock_experiment, mock_variation, enums.DecisionSources.FEATURE_TEST, None), []),
decision_service.Decision(mock_experiment, mock_variation,
enums.DecisionSources.FEATURE_TEST, None), []),
) as mock_decision, mock.patch('optimizely.event.event_processor.ForwardingEventProcessor.process'):
self.assertTrue(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user'))

Expand Down
Loading
0