Skip to content

Updating CHANGELOG and introducing some more unit tests #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.0
- Introduced support for forced bucketing.
- Introduced support for numeric metrics.
- Updated event builder to support new endpoint.

## 1.2.1
- Removed older feature flag parsing.

Expand Down
65 changes: 29 additions & 36 deletions optimizely/event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ def _add_account_id(self):

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

@abstractmethod
def _add_user_id(self, user_id):
""" Add user ID to the event. """
""" Add user ID to the event.

self.params[self.EventParams.END_USER_ID] = user_id
Args:
user_id: ID of the user.
"""
pass

@abstractmethod
def _add_attributes(self, attributes):
Expand All @@ -70,15 +74,25 @@ def _add_source(self):
""" Add source information to the event. """
pass

@abstractmethod
def _add_time(self):
""" Add time information to the event. """
pass

def _add_revision(self):
""" Add datafile revision information to the event. """
pass

def _add_anonymize_ip(self):
""" Add IP anonymization bool to the event """

self.params[self.EventParams.ANONYMIZE_IP] = self.config.get_anonymize_ip_value()

@abstractmethod
def _get_time(self):
""" Get time in milliseconds to be added to the event.

Returns:
Current time in milliseconds.
"""

return int(round(time.time() * 1000))

def _add_common_params(self, user_id, attributes):
""" Add params which are used same in both conversion and impression events.

Expand All @@ -92,8 +106,7 @@ def _add_common_params(self, user_id, attributes):
self._add_user_id(user_id)
self._add_attributes(attributes)
self._add_source()
self._add_revision()
self._add_time()
self._add_anonymize_ip()


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

def _add_time(self):
""" Add time information to the event. """

self.params[self.EventParams.TIME] = int(round(time.time() * 1000))
def _add_user_id(self, user_id):
""" Add user ID to the event.

def _add_visitor(self, user_id):
""" Add user to the event """
Args:
user_id: ID of the user.
"""

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

def _add_anonymize_ip(self):
""" Add IP anonymization bool to the event """

self.params[self.EventParams.ANONYMIZE_IP] = self.config.get_anonymize_ip_value()

def _add_common_params(self, user_id, attributes):
""" Add params which are used same in both conversion and impression events.

Args:
user_id: ID for user.
attributes: Dict representing user attributes and values which need to be recorded.
"""
self._add_project_id()
self._add_account_id()
self._add_visitor(user_id)
self._add_attributes(attributes)
self._add_source()
self._add_anonymize_ip()

def _add_required_params_for_impression(self, experiment, variation_id):
""" Add parameters that are required for the impression event to register.

Expand All @@ -209,7 +202,7 @@ def _add_required_params_for_impression(self, experiment, variation_id):

snapshot[self.EventParams.EVENTS] = [{
self.EventParams.EVENT_ID: experiment.layerId,
self.EventParams.TIME: int(round(time.time() * 1000)),
self.EventParams.TIME: self._get_time(),
self.EventParams.KEY: 'campaign_activated',
self.EventParams.UUID: str(uuid.uuid4())
}]
Expand Down Expand Up @@ -241,7 +234,7 @@ def _add_required_params_for_conversion(self, event_key, event_tags, decisions):

event_dict = {
self.EventParams.EVENT_ID: self.config.get_event(event_key).id,
self.EventParams.TIME: int(round(time.time() * 1000)),
self.EventParams.TIME: self._get_time(),
self.EventParams.KEY: event_key,
self.EventParams.UUID: str(uuid.uuid4())
}
Expand Down
2 changes: 1 addition & 1 deletion optimizely/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def lookup(self, user_id):
Returns:
Dict representing the user's profile.
"""
return dict(UserProfile(user_id))
return UserProfile(user_id).__dict__

def save(self, user_profile):
""" Save the user profile dict sent to this method.
Expand Down
2 changes: 1 addition & 1 deletion optimizely/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version_info = (1, 2, 0)
version_info = (1, 3, 0)
__version__ = '.'.join(str(v) for v in version_info)
15 changes: 15 additions & 0 deletions tests/test_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ def test_set_variation_for_experiment__previous_decision_available(self):

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': {}}))