Skip to content

Commit 0330a6d

Browse files
Add CmabDict type and update Experiment class to include cmab field
1 parent f8da261 commit 0330a6d

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

optimizely/entities.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
if TYPE_CHECKING:
2424
# prevent circular dependenacy by skipping import at runtime
25-
from .helpers.types import ExperimentDict, TrafficAllocation, VariableDict, VariationDict
25+
from .helpers.types import ExperimentDict, TrafficAllocation, VariableDict, VariationDict, CmabDict
2626

2727

2828
class BaseEntity:
@@ -84,6 +84,7 @@ def __init__(
8484
audienceConditions: Optional[Sequence[str | list[str]]] = None,
8585
groupId: Optional[str] = None,
8686
groupPolicy: Optional[str] = None,
87+
cmab: Optional[CmabDict] = None,
8788
**kwargs: Any
8889
):
8990
self.id = id
@@ -97,6 +98,7 @@ def __init__(
9798
self.layerId = layerId
9899
self.groupId = groupId
99100
self.groupPolicy = groupPolicy
101+
self.cmab = cmab
100102

101103
def get_audience_conditions_or_ids(self) -> Sequence[str | list[str]]:
102104
""" Returns audienceConditions if present, otherwise audienceIds. """
@@ -192,3 +194,4 @@ def __init__(self, key: str, host: Optional[str] = None, publicKey: Optional[str
192194
self.key = key
193195
self.host = host
194196
self.publicKey = publicKey
197+

optimizely/helpers/types.py

+5
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@ class IntegrationDict(BaseEntity):
109109
key: str
110110
host: str
111111
publicKey: str
112+
113+
class CmabDict(BaseEntity):
114+
"""Cmab dict from parsed datafile json."""
115+
attributeIds: list[str]
116+
trafficAllocation: int

tests/test_config.py

+19
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,25 @@ def test_init(self):
154154
self.assertEqual(expected_variation_key_map, self.project_config.variation_key_map)
155155
self.assertEqual(expected_variation_id_map, self.project_config.variation_id_map)
156156

157+
def test_cmab_field_population(self):
158+
""" Test that the cmab field is populated correctly in experiments and attributes are parsed as attribute_ids. """
159+
160+
# Deep copy existing datafile and add cmab config to the first experiment
161+
config_dict = copy.deepcopy(self.config_dict_with_multiple_experiments)
162+
config_dict['experiments'][0]['cmab'] = {'attributeIds': ['808797688', '808797689'], 'trafficAllocation': 4000}
163+
config_dict['experiments'][0]['trafficAllocation'] = []
164+
165+
opt_obj = optimizely.Optimizely(json.dumps(config_dict))
166+
project_config = opt_obj.config_manager.get_config()
167+
168+
experiment = project_config.get_experiment_from_key('test_experiment')
169+
self.assertEqual(experiment.cmab, {'attributeIds': ['808797688', '808797689'], 'trafficAllocation': 4000})
170+
171+
experiment_2 = project_config.get_experiment_from_key('test_experiment_2')
172+
self.assertIsNone(experiment_2.cmab)
173+
174+
print(project_config.attributes)
175+
157176
def test_init__with_v4_datafile(self):
158177
""" Test that on creating object, properties are initiated correctly for version 4 datafile. """
159178

0 commit comments

Comments
 (0)