Skip to content

Commit

Permalink
fix: addressed more feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mjamal@folio3.com authored and mnoman09 committed Aug 21, 2019
1 parent c3c9d46 commit 23ab6ce
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 49 deletions.
26 changes: 24 additions & 2 deletions optimizely/event/event_payload.py → optimizely/event/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json


class EventBatch(object):
""" Class respresenting Event Batch. """
Expand All @@ -26,6 +28,26 @@ def __init__(self, account_id, project_id, revision, client_name, client_version
self.enrich_decisions = enrich_decisions
self.visitors = visitors

def __eq__(self, other):
batch_obj = json.loads(json.dumps(self.__dict__, default=lambda o: o.__dict__),
object_pairs_hook=self._dict_clean)

print(batch_obj)
print(other)

return batch_obj == other

def _dict_clean(self, obj):
""" Helper method to remove keys from dictionary with None values. """

result = {}
for k, v in obj:
if v is None and k in ['revenue', 'value', 'tags', 'decisions']:
continue
else:
result[k] = v
return result


class Decision(object):
""" Class respresenting Decision. """
Expand Down Expand Up @@ -69,8 +91,8 @@ def __init__(self, snapshots, attributes, visitor_id):
class VisitorAttribute(object):
""" Class representing Visitor Attribute. """

def __init__(self, entity_id, key, event_type, value):
def __init__(self, entity_id, key, attribute_type, value):
self.entity_id = entity_id
self.key = key
self.type = event_type
self.type = attribute_type
self.value = value
4 changes: 2 additions & 2 deletions optimizely/event/user_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from optimizely import version

SDK_TYPE = 'python-sdk'
CLIENT_NAME = 'python-sdk'


class UserEvent(object):
Expand Down Expand Up @@ -67,6 +67,6 @@ def __init__(self, account_id, project_id, revision, anonymize_ip):
self.account_id = account_id
self.project_id = project_id
self.revision = revision
self.client_name = SDK_TYPE
self.client_name = CLIENT_NAME
self.client_version = version.__version__
self.anonymize_ip = anonymize_ip
59 changes: 14 additions & 45 deletions tests/test_event_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from operator import itemgetter

from optimizely import version
from optimizely.event.event_payload import Decision, EventBatch, Snapshot, SnapshotEvent, Visitor, VisitorAttribute
from optimizely.event.payload import Decision, EventBatch, Snapshot, SnapshotEvent, Visitor, VisitorAttribute
from . import base


class EventPayloadTest(base.BaseTest):
def _validate_event_object(self, expected_params, event_obj):
""" Helper method to validate properties of the event object. """

expected_params['visitors'][0]['attributes'] = \
sorted(expected_params['visitors'][0]['attributes'], key=itemgetter('key'))
event_obj['visitors'][0]['attributes'] = \
sorted(event_obj['visitors'][0]['attributes'], key=itemgetter('key'))
self.assertEqual(expected_params, event_obj)

def _dict_clean(self, obj):
""" Helper method to remove keys from dictionary with None values. """

result = {}
for k, v in obj:
if v is None and k in ['revenue', 'value', 'tags', 'decisions']:
continue
else:
result[k] = v
return result

def test_impression_event_equals_serialized_payload(self):
expected_params = {
Expand Down Expand Up @@ -73,24 +51,19 @@ def test_impression_event_equals_serialized_payload(self):
'revision': '42'
}

batch = EventBatch("12001", "111001", "42", "python-sdk", version.__version__,
batch = EventBatch('12001', '111001', '42', 'python-sdk', version.__version__,
False, True)
visitor_attr = VisitorAttribute("111094", "test_attribute", "custom", "test_value")
event = SnapshotEvent("111182", "a68cf1ad-0393-4e18-af87-efe8f01a7c9c", "campaign_activated",
visitor_attr = VisitorAttribute('111094', 'test_attribute', 'custom', 'test_value')
event = SnapshotEvent('111182', 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'campaign_activated',
42123)
event_decision = Decision("111182", "111127", "111129")
event_decision = Decision('111182', '111127', '111129')

snapshots = Snapshot([event], [event_decision])
user = Visitor([snapshots], [visitor_attr], "test_user")
user = Visitor([snapshots], [visitor_attr], 'test_user')

batch.visitors = [user]

self.maxDiff = None
self._validate_event_object(expected_params,
json.loads(
json.dumps(batch.__dict__, default=lambda o: o.__dict__),
object_pairs_hook=self._dict_clean
))
self.assertEqual(batch, expected_params)

def test_conversion_event_equals_serialized_payload(self):
expected_params = {
Expand Down Expand Up @@ -132,20 +105,16 @@ def test_conversion_event_equals_serialized_payload(self):
'revision': '42'
}

batch = EventBatch("12001", "111001", "42", "python-sdk", version.__version__,
False, True)
visitor_attr_1 = VisitorAttribute("111094", "test_attribute", "custom", "test_value")
visitor_attr_2 = VisitorAttribute("111095", "test_attribute2", "custom", "test_value2")
event = SnapshotEvent("111182", "a68cf1ad-0393-4e18-af87-efe8f01a7c9c", "campaign_activated",
batch = EventBatch('12001', '111001', '42', 'python-sdk', version.__version__,
False, True)
visitor_attr_1 = VisitorAttribute('111094', 'test_attribute', 'custom', 'test_value')
visitor_attr_2 = VisitorAttribute('111095', 'test_attribute2', 'custom', 'test_value2')
event = SnapshotEvent('111182', 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'campaign_activated',
42123, 4200, 1.234, {'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'})

snapshots = Snapshot([event])
user = Visitor([snapshots], [visitor_attr_1, visitor_attr_2], "test_user")
user = Visitor([snapshots], [visitor_attr_1, visitor_attr_2], 'test_user')

batch.visitors = [user]

self._validate_event_object(expected_params,
json.loads(
json.dumps(batch.__dict__, default=lambda o: o.__dict__),
object_pairs_hook=self._dict_clean
))
self.assertEqual(batch, expected_params)

0 comments on commit 23ab6ce

Please sign in to comment.