Skip to content

Commit 2aadf93

Browse files
Refactor ProjectConfig to add attribute ID to key mapping and implement retrieval methods; update test for cmab field population
1 parent 0330a6d commit 2aadf93

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

optimizely/entities.py

-1
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,3 @@ def __init__(self, key: str, host: Optional[str] = None, publicKey: Optional[str
194194
self.key = key
195195
self.host = host
196196
self.publicKey = publicKey
197-

optimizely/helpers/types.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class IntegrationDict(BaseEntity):
109109
key: str
110110
host: str
111111
publicKey: str
112-
112+
113+
113114
class CmabDict(BaseEntity):
114115
"""Cmab dict from parsed datafile json."""
115116
attributeIds: list[str]

optimizely/project_config.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any):
9494
self.attribute_key_map: dict[str, entities.Attribute] = self._generate_key_map(
9595
self.attributes, 'key', entities.Attribute
9696
)
97-
97+
self.attribute_id_to_key_map: dict[str, str] = {}
98+
for attribute in self.attributes:
99+
self.attribute_id_to_key_map[attribute['id']] = attribute['key']
98100
self.audience_id_map: dict[str, entities.Audience] = self._generate_key_map(
99101
self.audiences, 'id', entities.Audience
100102
)
@@ -510,6 +512,34 @@ def get_attribute_id(self, attribute_key: str) -> Optional[str]:
510512
self.error_handler.handle_error(exceptions.InvalidAttributeException(enums.Errors.INVALID_ATTRIBUTE))
511513
return None
512514

515+
def get_attribute_by_key(self, key: str) -> Optional[entities.Attribute]:
516+
""" Get attribute for the provided attribute key.
517+
518+
Args:
519+
key: Attribute key for which attribute is to be fetched.
520+
521+
Returns:
522+
Attribute corresponding to the provided attribute key.
523+
"""
524+
if key in self.attribute_key_map:
525+
return self.attribute_key_map[key]
526+
self.logger.error(f'Attribute with key:"{key}" is not in datafile.')
527+
return None
528+
529+
def get_attribute_key_by_id(self, id: str) -> Optional[str]:
530+
""" Get attribute key for the provided attribute id.
531+
532+
Args:
533+
id: Attribute id for which attribute is to be fetched.
534+
535+
Returns:
536+
Attribute key corresponding to the provided attribute id.
537+
"""
538+
if id in self.attribute_id_to_key_map:
539+
return self.attribute_id_to_key_map[id]
540+
self.logger.error(f'Attribute with id:"{id}" is not in datafile.')
541+
return None
542+
513543
def get_feature_from_key(self, feature_key: str) -> Optional[entities.FeatureFlag]:
514544
""" Get feature for the provided feature key.
515545

tests/test_config.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_init(self):
155155
self.assertEqual(expected_variation_id_map, self.project_config.variation_id_map)
156156

157157
def test_cmab_field_population(self):
158-
""" Test that the cmab field is populated correctly in experiments and attributes are parsed as attribute_ids. """
158+
""" Test that the cmab field is populated correctly in experiments."""
159159

160160
# Deep copy existing datafile and add cmab config to the first experiment
161161
config_dict = copy.deepcopy(self.config_dict_with_multiple_experiments)
@@ -167,11 +167,9 @@ def test_cmab_field_population(self):
167167

168168
experiment = project_config.get_experiment_from_key('test_experiment')
169169
self.assertEqual(experiment.cmab, {'attributeIds': ['808797688', '808797689'], 'trafficAllocation': 4000})
170-
170+
171171
experiment_2 = project_config.get_experiment_from_key('test_experiment_2')
172172
self.assertIsNone(experiment_2.cmab)
173-
174-
print(project_config.attributes)
175173

176174
def test_init__with_v4_datafile(self):
177175
""" Test that on creating object, properties are initiated correctly for version 4 datafile. """

0 commit comments

Comments
 (0)