-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cmab_ups_exclusion.py
More file actions
312 lines (272 loc) · 10.6 KB
/
test_cmab_ups_exclusion.py
File metadata and controls
312 lines (272 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Copyright 2026, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Tests to verify that CMAB experiments are excluded from User Profile Service.
CMAB experiments should not use UPS for sticky bucketing because:
- UPS maintains decisions across experiment lifetime without considering TTL
- UPS doesn't account for changing user attributes
- This contradicts CMAB's dynamic decision-making nature
"""
import unittest
from unittest import mock
from optimizely import decision_service, entities, user_profile
from optimizely.cmab.cmab_service import DefaultCmabService
class TestCmabUpsExclusion(unittest.TestCase):
"""Test suite to verify CMAB experiments are excluded from UPS."""
def test_cmab_experiment_skips_ups_lookup(self):
"""
Test that get_variation skips UPS lookup for CMAB experiments.
This test verifies that the condition `not experiment.cmab` prevents
calling get_stored_variation when processing CMAB experiments.
"""
# Create mocks
mock_logger = mock.MagicMock()
mock_ups = mock.MagicMock(spec=user_profile.UserProfileService)
mock_cmab_service = mock.MagicMock(spec=DefaultCmabService)
mock_project_config = mock.MagicMock()
mock_user_context = mock.MagicMock()
mock_user_context.user_id = 'test_user'
mock_user_context.get_user_attributes.return_value = {}
# Create decision service
ds = decision_service.DecisionService(
mock_logger,
mock_ups,
mock_cmab_service
)
# Create a CMAB experiment
variation = entities.Variation('1', 'var1')
cmab_experiment = entities.Experiment(
'123',
'cmab_test',
'Running',
['1'],
[variation],
{},
[],
'1',
cmab={'attributeIds': []}
)
# Create user profile tracker with a stored variation
user_profile_tracker = user_profile.UserProfileTracker(
'test_user',
mock_ups,
mock_logger
)
user_profile_tracker.user_profile = user_profile.UserProfile(
'test_user',
{'123': {'variation_id': 'stored_variation'}}
)
# Mock get_stored_variation to verify it's not called for CMAB
# Also mock audience helper to pass audience conditions
with mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions',
return_value=(True, [])):
with mock.patch.object(
ds,
'get_stored_variation',
return_value=None
) as mock_get_stored:
# Mock CMAB decision
mock_cmab_service.get_decision.return_value = (
{'variation_id': '1', 'cmab_uuid': 'uuid-123'},
[]
)
mock_project_config.get_variation_from_id.return_value = variation
# Mock bucketer to say user is in traffic
with mock.patch.object(ds.bucketer, 'bucket_to_entity_id') as m:
m.return_value = ('1', [])
# Get variation
result = ds.get_variation(
mock_project_config,
cmab_experiment,
mock_user_context,
user_profile_tracker
)
# Verify get_stored_variation was NOT called
# because experiment.cmab is True
mock_get_stored.assert_not_called()
def test_cmab_experiment_skips_ups_save(self):
"""
Test that get_variation skips UPS save for CMAB experiments.
This test verifies that the condition `not experiment.cmab` prevents
calling update_user_profile when processing CMAB experiments.
"""
# Create mocks
mock_logger = mock.MagicMock()
mock_ups = mock.MagicMock(spec=user_profile.UserProfileService)
mock_cmab_service = mock.MagicMock(spec=DefaultCmabService)
mock_project_config = mock.MagicMock()
mock_user_context = mock.MagicMock()
mock_user_context.user_id = 'test_user'
mock_user_context.get_user_attributes.return_value = {}
# Create decision service
ds = decision_service.DecisionService(
mock_logger,
mock_ups,
mock_cmab_service
)
# Create a CMAB experiment with variation
variation = entities.Variation('1', 'var1')
cmab_experiment = entities.Experiment(
'123',
'cmab_test',
'Running',
['1'],
[variation],
{},
[],
'1',
cmab={'attributeIds': []}
)
# Mock get_variation_from_id to return the variation
mock_project_config.get_variation_from_id.return_value = variation
# Create user profile tracker
user_profile_tracker = user_profile.UserProfileTracker(
'test_user',
mock_ups,
mock_logger
)
# Mock CMAB decision
mock_cmab_service.get_decision.return_value = (
{'variation_id': '1', 'cmab_uuid': 'uuid-123'},
[]
)
# Mock audience helper to pass audience conditions
with mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions',
return_value=(True, [])):
# Mock bucketer to say user is in traffic
with mock.patch.object(ds.bucketer, 'bucket_to_entity_id') as m:
m.return_value = ('1', [])
# Get variation
result = ds.get_variation(
mock_project_config,
cmab_experiment,
mock_user_context,
user_profile_tracker
)
# Verify profile was NOT updated
self.assertFalse(
user_profile_tracker.profile_updated,
"CMAB should not update user profile"
)
def test_non_cmab_experiment_uses_ups_lookup(self):
"""
Test that regular experiments still use UPS lookup.
"""
# Create mocks
mock_logger = mock.MagicMock()
mock_ups = mock.MagicMock(spec=user_profile.UserProfileService)
mock_cmab_service = mock.MagicMock(spec=DefaultCmabService)
mock_project_config = mock.MagicMock()
mock_user_context = mock.MagicMock()
mock_user_context.user_id = 'test_user'
# Create decision service
ds = decision_service.DecisionService(
mock_logger,
mock_ups,
mock_cmab_service
)
# Create a regular (non-CMAB) experiment
variation = entities.Variation('1', 'var1')
regular_experiment = entities.Experiment(
'123',
'regular_test',
'Running',
['1'],
[variation],
{},
[],
'1'
)
# Create user profile tracker with stored variation
user_profile_tracker = user_profile.UserProfileTracker(
'test_user',
mock_ups,
mock_logger
)
# Mock get_stored_variation to return the variation
with mock.patch.object(
ds,
'get_stored_variation',
return_value=variation
) as mock_get_stored:
# Get variation
result = ds.get_variation(
mock_project_config,
regular_experiment,
mock_user_context,
user_profile_tracker
)
# Verify get_stored_variation WAS called for regular experiments
mock_get_stored.assert_called_once()
# Verify returned variation is the stored one
self.assertEqual(result['variation'], variation)
def test_non_cmab_experiment_uses_ups_save(self):
"""
Test that regular experiments still save to UPS.
This verifies our changes don't break existing UPS behavior.
"""
# Create mocks
mock_logger = mock.MagicMock()
mock_ups = mock.MagicMock(spec=user_profile.UserProfileService)
mock_cmab_service = mock.MagicMock(spec=DefaultCmabService)
mock_project_config = mock.MagicMock()
mock_user_context = mock.MagicMock()
mock_user_context.user_id = 'test_user'
mock_user_context.get_user_attributes.return_value = {}
# Create decision service
ds = decision_service.DecisionService(
mock_logger,
mock_ups,
mock_cmab_service
)
# Create a regular experiment with variation
variation = entities.Variation('1', 'var1')
regular_experiment = entities.Experiment(
'123',
'regular_test',
'Running',
['1'],
[variation],
{},
[],
'1'
)
# Create user profile tracker
user_profile_tracker = user_profile.UserProfileTracker(
'test_user',
mock_ups,
mock_logger
)
# Mock audience helper to pass audience conditions
with mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions',
return_value=(True, [])):
# Mock bucketer to return variation
with mock.patch.object(
ds.bucketer,
'bucket',
return_value=(variation, [])
):
# Get variation
result = ds.get_variation(
mock_project_config,
regular_experiment,
mock_user_context,
user_profile_tracker
)
# Verify profile WAS updated for regular experiments
self.assertTrue(
user_profile_tracker.profile_updated,
"Regular experiments should update user profile"
)
if __name__ == '__main__':
unittest.main()