def test_conversion_event(self): project_config = self.project_config user_id = 'test_user' event_key = 'test_event' user_attributes = {'test_attribute': 'test_value', 'boolean_key': True} conversion_event = UserEventFactory.create_conversion_event( project_config, event_key, user_id, user_attributes, None) expected_attrs = EventFactory.build_attribute_list( user_attributes, project_config) self.assertEqual(self.project_config.project_id, conversion_event.event_context.project_id) self.assertEqual(self.project_config.revision, conversion_event.event_context.revision) self.assertEqual(self.project_config.account_id, conversion_event.event_context.account_id) self.assertEqual( self.project_config.anonymize_ip, conversion_event.event_context.anonymize_ip, ) self.assertEqual(self.project_config.bot_filtering, conversion_event.bot_filtering) self.assertEqual(self.project_config.get_event(event_key), conversion_event.event) self.assertEqual(user_id, conversion_event.user_id) self.assertEqual( [x.__dict__ for x in expected_attrs], [x.__dict__ for x in conversion_event.visitor_attributes], )
def test_create_conversion_event__with_user_agent_when_bot_filtering_is_disabled(self,): """ Test that create_conversion_event creates Event object with right params when user agent attribute is provided and bot filtering is disabled """ expected_params = { 'account_id': '12001', 'project_id': '111001', 'visitors': [ { 'visitor_id': 'test_user', 'attributes': [ { 'type': 'custom', 'value': 'Chrome', 'entity_id': '$opt_user_agent', 'key': '$opt_user_agent', }, { 'type': 'custom', 'value': False, 'entity_id': '$opt_bot_filtering', 'key': '$opt_bot_filtering', }, ], 'snapshots': [ { 'events': [ { 'timestamp': 42123, 'entity_id': '111095', 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'key': 'test_event', } ] } ], } ], 'client_name': 'python-sdk', 'client_version': version.__version__, 'enrich_decisions': True, 'anonymize_ip': False, 'revision': '42', } with mock.patch('time.time', return_value=42.123), mock.patch( 'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c' ), mock.patch( 'optimizely.project_config.ProjectConfig.get_bot_filtering_value', return_value=False, ): event_obj = UserEventFactory.create_conversion_event( self.project_config, 'test_event', 'test_user', {'$opt_user_agent': 'Chrome'}, None, ) log_event = EventFactory.create_log_event(event_obj, self.logger) self._validate_event_object( log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS, )
def test_create_conversion_event__when_event_is_used_in_multiple_experiments(self): """ Test that create_conversion_event creates Event object with right params when multiple experiments use the same event. """ expected_params = { 'client_version': version.__version__, 'project_id': '111001', 'visitors': [ { 'attributes': [ {'entity_id': '111094', 'type': 'custom', 'value': 'test_value', 'key': 'test_attribute'} ], 'visitor_id': 'test_user', 'snapshots': [ { 'events': [ { 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'tags': {'non-revenue': 'abc', 'revenue': 4200, 'value': 1.234}, 'timestamp': 42123, 'revenue': 4200, 'value': 1.234, 'key': 'test_event', 'entity_id': '111095', } ] } ], } ], 'account_id': '12001', 'client_name': 'python-sdk', 'enrich_decisions': True, 'anonymize_ip': False, 'revision': '42', } with mock.patch('time.time', return_value=42.123), mock.patch( 'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c' ): event_obj = UserEventFactory.create_conversion_event( self.project_config, 'test_event', 'test_user', {'test_attribute': 'test_value'}, {'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'}, ) log_event = EventFactory.create_log_event(event_obj, self.logger) self._validate_event_object( log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS, )
def test_create_conversion_event(self): """ Test that create_conversion_event creates Event object with right params when no attributes are provided. """ expected_params = { 'account_id': '12001', 'project_id': '111001', 'visitors': [ { 'visitor_id': 'test_user', 'attributes': [], 'snapshots': [ { 'events': [ { 'timestamp': 42123, 'entity_id': '111095', 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'key': 'test_event', } ] } ], } ], 'client_name': 'python-sdk', 'client_version': version.__version__, 'enrich_decisions': True, 'anonymize_ip': False, 'revision': '42', } with mock.patch('time.time', return_value=42.123), mock.patch( 'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c' ): event_obj = UserEventFactory.create_conversion_event( self.project_config, 'test_event', 'test_user', None, None ) log_event = EventFactory.create_log_event(event_obj, self.logger) self._validate_event_object( log_event, EventFactory.EVENT_ENDPOINT, expected_params, EventFactory.HTTP_VERB, EventFactory.HTTP_HEADERS, )
def _build_conversion_event(self, event_name): return UserEventFactory.create_conversion_event( self.project_config, event_name, self.test_user_id, {}, {})