Skip to content

Commit 750a4fb

Browse files
Updating CHANGELOG and introducing some more unit tests (#89)
1 parent 4cbf7c5 commit 750a4fb

File tree

5 files changed

+51
-38
lines changed

5 files changed

+51
-38
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.0
2+
- Introduced support for forced bucketing.
3+
- Introduced support for numeric metrics.
4+
- Updated event builder to support new endpoint.
5+
16
## 1.2.1
27
- Removed older feature flag parsing.
38

optimizely/event_builder.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ def _add_account_id(self):
5151

5252
self.params[self.EventParams.ACCOUNT_ID] = self.config.get_account_id()
5353

54+
@abstractmethod
5455
def _add_user_id(self, user_id):
55-
""" Add user ID to the event. """
56+
""" Add user ID to the event.
5657
57-
self.params[self.EventParams.END_USER_ID] = user_id
58+
Args:
59+
user_id: ID of the user.
60+
"""
61+
pass
5862

5963
@abstractmethod
6064
def _add_attributes(self, attributes):
@@ -70,15 +74,25 @@ def _add_source(self):
7074
""" Add source information to the event. """
7175
pass
7276

73-
@abstractmethod
74-
def _add_time(self):
75-
""" Add time information to the event. """
76-
pass
77-
7877
def _add_revision(self):
7978
""" Add datafile revision information to the event. """
8079
pass
8180

81+
def _add_anonymize_ip(self):
82+
""" Add IP anonymization bool to the event """
83+
84+
self.params[self.EventParams.ANONYMIZE_IP] = self.config.get_anonymize_ip_value()
85+
86+
@abstractmethod
87+
def _get_time(self):
88+
""" Get time in milliseconds to be added to the event.
89+
90+
Returns:
91+
Current time in milliseconds.
92+
"""
93+
94+
return int(round(time.time() * 1000))
95+
8296
def _add_common_params(self, user_id, attributes):
8397
""" Add params which are used same in both conversion and impression events.
8498
@@ -92,8 +106,7 @@ def _add_common_params(self, user_id, attributes):
92106
self._add_user_id(user_id)
93107
self._add_attributes(attributes)
94108
self._add_source()
95-
self._add_revision()
96-
self._add_time()
109+
self._add_anonymize_ip()
97110

98111

99112
class EventBuilder(BaseEventBuilder):
@@ -158,13 +171,12 @@ def _add_source(self):
158171
self.params[self.EventParams.SOURCE_SDK_TYPE] = 'python-sdk'
159172
self.params[self.EventParams.SOURCE_SDK_VERSION] = version.__version__
160173

161-
def _add_time(self):
162-
""" Add time information to the event. """
163-
164-
self.params[self.EventParams.TIME] = int(round(time.time() * 1000))
174+
def _add_user_id(self, user_id):
175+
""" Add user ID to the event.
165176
166-
def _add_visitor(self, user_id):
167-
""" Add user to the event """
177+
Args:
178+
user_id: ID of the user.
179+
"""
168180

169181
self.params[self.EventParams.USERS] = []
170182
# Add a single visitor
@@ -173,25 +185,6 @@ def _add_visitor(self, user_id):
173185
visitor[self.EventParams.SNAPSHOTS] = []
174186
self.params[self.EventParams.USERS].append(visitor)
175187

176-
def _add_anonymize_ip(self):
177-
""" Add IP anonymization bool to the event """
178-
179-
self.params[self.EventParams.ANONYMIZE_IP] = self.config.get_anonymize_ip_value()
180-
181-
def _add_common_params(self, user_id, attributes):
182-
""" Add params which are used same in both conversion and impression events.
183-
184-
Args:
185-
user_id: ID for user.
186-
attributes: Dict representing user attributes and values which need to be recorded.
187-
"""
188-
self._add_project_id()
189-
self._add_account_id()
190-
self._add_visitor(user_id)
191-
self._add_attributes(attributes)
192-
self._add_source()
193-
self._add_anonymize_ip()
194-
195188
def _add_required_params_for_impression(self, experiment, variation_id):
196189
""" Add parameters that are required for the impression event to register.
197190
@@ -209,7 +202,7 @@ def _add_required_params_for_impression(self, experiment, variation_id):
209202

210203
snapshot[self.EventParams.EVENTS] = [{
211204
self.EventParams.EVENT_ID: experiment.layerId,
212-
self.EventParams.TIME: int(round(time.time() * 1000)),
205+
self.EventParams.TIME: self._get_time(),
213206
self.EventParams.KEY: 'campaign_activated',
214207
self.EventParams.UUID: str(uuid.uuid4())
215208
}]
@@ -241,7 +234,7 @@ def _add_required_params_for_conversion(self, event_key, event_tags, decisions):
241234

242235
event_dict = {
243236
self.EventParams.EVENT_ID: self.config.get_event(event_key).id,
244-
self.EventParams.TIME: int(round(time.time() * 1000)),
237+
self.EventParams.TIME: self._get_time(),
245238
self.EventParams.KEY: event_key,
246239
self.EventParams.UUID: str(uuid.uuid4())
247240
}

optimizely/user_profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def lookup(self, user_id):
7171
Returns:
7272
Dict representing the user's profile.
7373
"""
74-
return dict(UserProfile(user_id))
74+
return UserProfile(user_id).__dict__
7575

7676
def save(self, user_profile):
7777
""" Save the user profile dict sent to this method.

optimizely/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
version_info = (1, 2, 0)
14+
version_info = (1, 3, 0)
1515
__version__ = '.'.join(str(v) for v in version_info)

tests/test_user_profile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ def test_set_variation_for_experiment__previous_decision_available(self):
5050

5151
self.profile.save_variation_for_experiment('199912', '1224525')
5252
self.assertEqual({'199912': {'variation_id': '1224525'}}, self.profile.experiment_bucket_map)
53+
54+
55+
class UserProfileServiceTest(unittest.TestCase):
56+
57+
def test_lookup(self):
58+
""" Test that lookup returns user profile in expected format. """
59+
60+
user_profile_service = user_profile.UserProfileService()
61+
self.assertEqual({'user_id': 'test_user', 'experiment_bucket_map': {}}, user_profile_service.lookup('test_user'))
62+
63+
def test_save(self):
64+
""" Test that nothing happens on calling save. """
65+
66+
user_profile_service = user_profile.UserProfileService()
67+
self.assertIsNone(user_profile_service.save({'user_id': 'test_user', 'experiment_bucket_map': {}}))

0 commit comments

Comments
 (0)