Skip to content

Commit b725be0

Browse files
update: Refactor attribute handling in get_decision and add test for CMAB attribute filtering
1 parent 44b145e commit b725be0

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

tests/test_cmab_service.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def test_new_decision_when_hash_changes(self):
124124
}
125125
self.mock_cmab_client.fetch_decision.return_value = "varE"
126126

127-
user_attrs = {"age": 25, "location": "USA"}
128-
expected_hash = self.cmab_service._hash_attributes(user_attrs)
127+
expected_attribute = {"age": 25, "location": "USA"}
128+
expected_hash = self.cmab_service._hash_attributes(expected_attribute)
129129
expected_key = self.cmab_service._get_cache_key("user123", "exp1")
130130

131131
decision = self.cmab_service.get_decision(self.mock_project_config, self.mock_user_context, "exp1", [])
@@ -139,6 +139,12 @@ def test_new_decision_when_hash_changes(self):
139139
}
140140
)
141141
self.assertEqual(decision["variation_id"], "varE")
142+
self.mock_cmab_client.fetch_decision.assert_called_once_with(
143+
"exp1",
144+
self.mock_user_context.user_id,
145+
expected_attribute,
146+
decision["cmab_uuid"]
147+
)
142148

143149
def test_filter_attributes_returns_correct_subset(self):
144150
filtered = self.cmab_service._filter_attributes(self.mock_project_config, self.mock_user_context, "exp1")
@@ -155,3 +161,27 @@ def test_hash_attributes_produces_stable_output(self):
155161
hash1 = self.cmab_service._hash_attributes(attrs)
156162
hash2 = self.cmab_service._hash_attributes({"a": 1, "b": 2})
157163
self.assertEqual(hash1, hash2)
164+
165+
def test_only_cmab_attributes_passed_to_client(self):
166+
self.mock_user_context.get_user_attributes.return_value = {
167+
'age': 25,
168+
'location': 'USA',
169+
'extra_attr': 'value', # This shouldn't be passed to CMAB
170+
'another_extra': 123 # This shouldn't be passed to CMAB
171+
}
172+
self.mock_cmab_client.fetch_decision.return_value = "varF"
173+
174+
decision = self.cmab_service.get_decision(
175+
self.mock_project_config,
176+
self.mock_user_context,
177+
"exp1",
178+
[OptimizelyDecideOption.IGNORE_CMAB_CACHE]
179+
)
180+
181+
# Verify only age and location are passed (attributes configured in setUp)
182+
self.mock_cmab_client.fetch_decision.assert_called_once_with(
183+
"exp1",
184+
self.mock_user_context.user_id,
185+
{"age": 25, "location": "USA"},
186+
decision["cmab_uuid"]
187+
)

0 commit comments

Comments
 (0)