forked from optimizely/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_profile.py
More file actions
139 lines (103 loc) · 5.67 KB
/
test_user_profile.py
File metadata and controls
139 lines (103 loc) · 5.67 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
# Copyright 2017, 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.
import unittest
from optimizely import user_profile
from unittest import mock
class UserProfileTest(unittest.TestCase):
def setUp(self):
user_id = 'test_user'
experiment_bucket_map = {'199912': {'variation_id': '14512525'}}
self.profile = user_profile.UserProfile(user_id, experiment_bucket_map=experiment_bucket_map)
def test_get_variation_for_experiment__decision_exists(self):
""" Test that variation ID is retrieved correctly if a decision exists in the experiment bucket map. """
self.assertEqual('14512525', self.profile.get_variation_for_experiment('199912'))
def test_get_variation_for_experiment__no_decision_exists(self):
""" Test that None is returned if no decision exists in the experiment bucket map. """
self.assertIsNone(self.profile.get_variation_for_experiment('199924'))
def test_set_variation_for_experiment__no_previous_decision(self):
""" Test that decision for new experiment/variation is stored correctly. """
self.profile.save_variation_for_experiment('1993412', '118822')
self.assertEqual(
{'199912': {'variation_id': '14512525'}, '1993412': {'variation_id': '118822'}},
self.profile.experiment_bucket_map,
)
def test_set_variation_for_experiment__previous_decision_available(self):
""" Test that decision for is updated correctly if new experiment/variation combination is available. """
self.profile.save_variation_for_experiment('199912', '1224525')
self.assertEqual({'199912': {'variation_id': '1224525'}}, self.profile.experiment_bucket_map)
class UserProfileServiceTest(unittest.TestCase):
def test_lookup(self):
""" Test that lookup returns user profile in expected format. """
user_profile_service = user_profile.UserProfileService()
self.assertEqual(
{'user_id': 'test_user', 'experiment_bucket_map': {}}, user_profile_service.lookup('test_user'),
)
def test_save(self):
""" Test that nothing happens on calling save. """
user_profile_service = user_profile.UserProfileService()
self.assertIsNone(user_profile_service.save({'user_id': 'test_user', 'experiment_bucket_map': {}}))
class UserProfileTrackerTest(unittest.TestCase):
def test_load_user_profile_failure(self):
"""Test that load_user_profile handles exceptions gracefully."""
mock_user_profile_service = mock.MagicMock()
mock_logger = mock.MagicMock()
user_profile_tracker = user_profile.UserProfileTracker(
user_id="test_user",
user_profile_service=mock_user_profile_service,
logger=mock_logger
)
mock_user_profile_service.lookup.side_effect = Exception("Lookup failure")
user_profile_tracker.load_user_profile()
# Verify that the logger recorded the exception
mock_logger.exception.assert_called_once_with(
'Unable to retrieve user profile for user "test_user" as lookup failed.'
)
# Verify that the user profile is reset to an empty profile
self.assertEqual(user_profile_tracker.user_profile.user_id, "test_user")
self.assertEqual(user_profile_tracker.user_profile.experiment_bucket_map, {})
def test_load_user_profile__user_profile_invalid(self):
"""Test that load_user_profile handles an invalid user profile format."""
mock_user_profile_service = mock.MagicMock()
mock_logger = mock.MagicMock()
user_profile_tracker = user_profile.UserProfileTracker(
user_id="test_user",
user_profile_service=mock_user_profile_service,
logger=mock_logger
)
mock_user_profile_service.lookup.return_value = {"invalid_key": "value"}
reasons = []
user_profile_tracker.load_user_profile(reasons=reasons)
# Verify that the logger recorded a warning for the missing keys
missing_keys_message = "User profile is missing keys: user_id, experiment_bucket_map"
self.assertIn(missing_keys_message, reasons)
# Ensure the logger logs the invalid format
mock_logger.info.assert_not_called()
self.assertEqual(user_profile_tracker.user_profile.user_id, "test_user")
self.assertEqual(user_profile_tracker.user_profile.experiment_bucket_map, {})
# Verify the reasons list was updated
self.assertIn(missing_keys_message, reasons)
def test_save_user_profile_failure(self):
"""Test that save_user_profile handles exceptions gracefully."""
mock_user_profile_service = mock.MagicMock()
mock_logger = mock.MagicMock()
user_profile_tracker = user_profile.UserProfileTracker(
user_id="test_user",
user_profile_service=mock_user_profile_service,
logger=mock_logger
)
user_profile_tracker.profile_updated = True
mock_user_profile_service.save.side_effect = Exception("Save failure")
user_profile_tracker.save_user_profile()
mock_logger.warning.assert_called_once_with(
'Failed to save user profile of user "test_user" for exception:Save failure".'
)