def test_impression_event(self):
        project_config = self.project_config
        experiment = self.project_config.get_experiment_from_key(
            'test_experiment')
        variation = self.project_config.get_variation_from_id(
            experiment.key, '111128')
        user_id = 'test_user'

        impression_event = UserEventFactory.create_impression_event(
            project_config, experiment, '111128', 'flag_key', 'rule_key',
            'rule_type', True, user_id, None)

        self.assertEqual(self.project_config.project_id,
                         impression_event.event_context.project_id)
        self.assertEqual(self.project_config.revision,
                         impression_event.event_context.revision)
        self.assertEqual(self.project_config.account_id,
                         impression_event.event_context.account_id)
        self.assertEqual(
            self.project_config.anonymize_ip,
            impression_event.event_context.anonymize_ip,
        )
        self.assertEqual(self.project_config.bot_filtering,
                         impression_event.bot_filtering)
        self.assertEqual(experiment, impression_event.experiment)
        self.assertEqual(variation, impression_event.variation)
        self.assertEqual(user_id, impression_event.user_id)
    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,
        )
Пример #4
0
    def test_impression_event__with_attributes(self):
        project_config = self.project_config
        experiment = self.project_config.get_experiment_from_key(
            'test_experiment')
        variation = self.project_config.get_variation_from_id(
            experiment.key, '111128')
        user_id = 'test_user'

        user_attributes = {'test_attribute': 'test_value', 'boolean_key': True}

        impression_event = UserEventFactory.create_impression_event(
            project_config, experiment, '111128', user_id, user_attributes)

        expected_attrs = EventFactory.build_attribute_list(
            user_attributes, project_config)

        self.assertEqual(self.project_config.project_id,
                         impression_event.event_context.project_id)
        self.assertEqual(self.project_config.revision,
                         impression_event.event_context.revision)
        self.assertEqual(self.project_config.account_id,
                         impression_event.event_context.account_id)
        self.assertEqual(
            self.project_config.anonymize_ip,
            impression_event.event_context.anonymize_ip,
        )
        self.assertEqual(self.project_config.bot_filtering,
                         impression_event.bot_filtering)
        self.assertEqual(experiment, impression_event.experiment)
        self.assertEqual(variation, impression_event.variation)
        self.assertEqual(user_id, impression_event.user_id)
        self.assertEqual(
            [x.__dict__ for x in expected_attrs],
            [x.__dict__ for x in impression_event.visitor_attributes],
        )
    def test_create_impression_event(self):
        """ Test that create_impression_event creates LogEvent object with right params. """

        expected_params = {
            'account_id': '12001',
            'project_id': '111001',
            'visitors': [
                {
                    'visitor_id': 'test_user',
                    'attributes': [],
                    'snapshots': [
                        {
                            'decisions': [
                                {'variation_id': '111129', 'experiment_id': '111127', 'campaign_id': '111182',
                                 'metadata': {'flag_key': '',
                                              'rule_key': 'rule_key',
                                              'rule_type': 'experiment',
                                              'variation_key': 'variation',
                                              'enabled': False}}
                            ],
                            'events': [
                                {
                                    'timestamp': 42123,
                                    'entity_id': '111182',
                                    'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
                                    'key': 'campaign_activated',
                                }
                            ],
                        }
                    ],
                }
            ],
            '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_impression_event(
                self.project_config,
                self.project_config.get_experiment_from_key('test_experiment'),
                '111129',
                '',
                'rule_key',
                'experiment',
                False,
                'test_user',
                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 side_effect(*args, **kwargs):
            attribute_key = args[0]
            if attribute_key == 'boolean_key' or attribute_key == 'double_key':
                return True

            return False

            attributes = {
                'test_attribute': 'test_value',
                'boolean_key': True,
                'integer_key': 0,
                'double_key': 5.5,
            }

            with mock.patch('time.time', return_value=42.123), mock.patch(
                'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
            ), mock.patch(
                'optimizely.helpers.validator.is_attribute_valid', side_effect=side_effect,
            ):

                event_obj = UserEventFactory.create_impression_event(
                    self.project_config,
                    self.project_config.get_experiment_from_key('test_experiment'),
                    '111129',
                    '',
                    'experiment',
                    'test_user',
                    attributes,
                )

            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, {}, {})