diff --git a/tests/helpers_tests/test_audience.py b/tests/helpers_tests/test_audience.py index 813c9950..0453b1c3 100644 --- a/tests/helpers_tests/test_audience.py +++ b/tests/helpers_tests/test_audience.py @@ -56,11 +56,13 @@ def test_is_user_in_experiment__no_audience(self): def test_is_user_in_experiment__no_attributes(self): """ Test that is_user_in_experiment returns True when experiment is using no audience. """ - self.assertFalse(audience.is_user_in_experiment(self.project_config, - self.project_config.get_experiment_from_key('test_experiment'), None)) + self.assertFalse(audience.is_user_in_experiment( + self.project_config, self.project_config.get_experiment_from_key('test_experiment'), None) + ) - self.assertFalse(audience.is_user_in_experiment(self.project_config, - self.project_config.get_experiment_from_key('test_experiment'), {})) + self.assertFalse(audience.is_user_in_experiment( + self.project_config, self.project_config.get_experiment_from_key('test_experiment'), {}) + ) def test_is_user_in_experiment__audience_conditions_are_met(self): """ Test that is_user_in_experiment returns True when audience conditions are met. """ diff --git a/tests/test_bucketing.py b/tests/test_bucketing.py index ac5328f6..0db43d3c 100644 --- a/tests/test_bucketing.py +++ b/tests/test_bucketing.py @@ -38,11 +38,12 @@ def test_bucket(self): # Variation 1 with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=42) as mock_generate_bucket_value: - self.assertEqual(entities.Variation('111128', 'control'), - self.bucketer.bucket( - self.project_config.get_experiment_from_key('test_experiment'), - 'test_user', 'test_user' - )) + self.assertEqual( + entities.Variation('111128', 'control'), + self.bucketer.bucket( + self.project_config.get_experiment_from_key('test_experiment'), + 'test_user', 'test_user' + )) mock_generate_bucket_value.assert_called_once_with('test_user111127') # Empty entity ID @@ -56,12 +57,11 @@ def test_bucket(self): # Variation 2 with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=5042) as mock_generate_bucket_value: - self.assertEqual(entities.Variation('111129', 'variation'), - self.bucketer.bucket( - self.project_config.get_experiment_from_key('test_experiment'), - 'test_user', - 'test_user' - )) + self.assertEqual( + entities.Variation('111129', 'variation'), + self.bucketer.bucket( + self.project_config.get_experiment_from_key('test_experiment'), 'test_user', 'test_user' + )) mock_generate_bucket_value.assert_called_once_with('test_user111127') # No matching variation @@ -165,7 +165,7 @@ def test_bucket(self): # Variation 1 with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=42),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertEqual(entities.Variation('111128', 'control'), self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'), 'test_user', @@ -196,7 +196,7 @@ def test_bucket(self): # Variation 2 with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=5042),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertEqual(entities.Variation('111129', 'variation'), self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'), 'test_user', @@ -211,7 +211,7 @@ def test_bucket(self): # No matching variation with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=424242),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertIsNone(self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'), 'test_user', 'test_user')) @@ -227,7 +227,7 @@ def test_bucket__experiment_in_group(self): # In group, matching experiment and variation with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', side_effect=[42, 4242]),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertEqual(entities.Variation('28902', 'group_exp_1_variation'), self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'), 'test_user', @@ -248,7 +248,7 @@ def test_bucket__experiment_in_group(self): # In group, but in no experiment with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', side_effect=[8400, 9500]),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertIsNone(self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'), 'test_user', 'test_user')) @@ -261,7 +261,7 @@ def test_bucket__experiment_in_group(self): # In group, no matching experiment with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', side_effect=[42, 9500]),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertIsNone(self.bucketer.bucket( self.project_config.get_experiment_from_key('group_exp_1'), 'test_user', 'test_user')) self.assertEqual(4, mock_logging.call_count) @@ -277,7 +277,7 @@ def test_bucket__experiment_in_group(self): # In group, experiment does not match with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', side_effect=[42, 4242]),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertIsNone(self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_2'), 'test_user', 'test_user')) @@ -292,7 +292,7 @@ def test_bucket__experiment_in_group(self): # In group no matching variation with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', side_effect=[42, 424242]),\ - mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: + mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging: self.assertIsNone(self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'), 'test_user', 'test_user')) diff --git a/tests/test_config.py b/tests/test_config.py index 49563ca6..c3dd3544 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -62,8 +62,7 @@ def test_init(self): }, { 'entityId': '111129', 'endOfRange': 9000 - }], - '111182'), + }], '111182'), 'group_exp_1': entities.Experiment( '32222', 'group_exp_1', 'Running', [], [{ 'key': 'group_exp_1_control', @@ -425,8 +424,7 @@ def test_init__with_v4_datafile(self): }, { 'entityId': '111129', 'endOfRange': 9000 - }], - '111182'), + }], '111182'), 'group_exp_1': entities.Experiment( '32222', 'group_exp_1', 'Running', [], [{ 'key': 'group_exp_1_control', diff --git a/tests/test_decision_service.py b/tests/test_decision_service.py index 7ad4035b..ebdd6ff6 100644 --- a/tests/test_decision_service.py +++ b/tests/test_decision_service.py @@ -171,9 +171,9 @@ def test_get_variation__user_has_stored_decision(self): # Assert that stored variation is returned and bucketing service is not involved mock_get_forced_variation.assert_called_once_with(experiment, 'test_user') mock_lookup.assert_called_once_with('test_user') - mock_get_stored_variation.assert_called_once_with(experiment, - user_profile.UserProfile('test_user', - {'111127': {'variation_id': '111128'}})) + mock_get_stored_variation.assert_called_once_with( + experiment, user_profile.UserProfile('test_user', {'111127': {'variation_id': '111128'}}) + ) self.assertEqual(0, mock_audience_check.call_count) self.assertEqual(0, mock_bucket.call_count) self.assertEqual(0, mock_save.call_count) @@ -459,7 +459,8 @@ def test_get_variation_for_rollout__skips_to_everyone_else_rule(self, mock_loggi mock.call(enums.LogLevels.DEBUG, 'User "test_user" is not in the traffic group for the targeting else. ' 'Checking "Everyone Else" rule now.'), mock.call(enums.LogLevels.DEBUG, 'User "test_user" meets conditions for targeting rule "Everyone Else".')], - mock_logging.call_args_list) + mock_logging.call_args_list + ) def test_get_variation_for_rollout__returns_none_for_user_not_in_rollout(self, mock_logging): """ Test that get_variation_for_rollout returns None for the user not in the associated rollout. """ @@ -604,11 +605,10 @@ def test_get_variation_for_feature__returns_none_for_invalid_group_id(self, mock feature = self.project_config.get_feature_from_key('test_feature_in_group') feature.groupId = 'aabbccdd' - self.assertEqual(decision_service.Decision(None, - None, - decision_service.DECISION_SOURCE_EXPERIMENT), - self.decision_service.get_variation_for_feature(feature, 'test_user') - ) + self.assertEqual( + decision_service.Decision(None, None, decision_service.DECISION_SOURCE_EXPERIMENT), + self.decision_service.get_variation_for_feature(feature, 'test_user') + ) mock_logging.assert_called_with(enums.LogLevels.ERROR, enums.Errors.INVALID_GROUP_ID_ERROR.format('_get_variation_for_feature')) diff --git a/tests/test_event_dispatcher.py b/tests/test_event_dispatcher.py index 69263b6c..923609b7 100644 --- a/tests/test_event_dispatcher.py +++ b/tests/test_event_dispatcher.py @@ -1,4 +1,4 @@ -# Copyright 2016, Optimizely +# Copyright 2016, 2018, Optimizely # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -72,7 +72,7 @@ def test_dispatch_event__handle_request_exception(self): with mock.patch('requests.post', side_effect=request_exception.RequestException('Failed Request')) as mock_request_post,\ - mock.patch('logging.error') as mock_log_error: + mock.patch('logging.error') as mock_log_error: event_dispatcher.EventDispatcher.dispatch_event(event) mock_request_post.assert_called_once_with(url, data=json.dumps(params), diff --git a/tests/test_optimizely.py b/tests/test_optimizely.py index c15f44f2..c223a73a 100644 --- a/tests/test_optimizely.py +++ b/tests/test_optimizely.py @@ -159,9 +159,9 @@ def test_activate(self): with mock.patch( 'optimizely.decision_service.DecisionService.get_variation', return_value=self.project_config.get_variation_from_id('test_experiment', '111129')) as mock_decision, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user')) expected_params = { @@ -209,8 +209,9 @@ def on_activate(experiment, user_id, attributes, variation, event): print("Activated experiment {0}".format(experiment.key)) callbackhit[0] = True - notification_id = self.optimizely.notification_center.add_notification_listener(enums.NotificationTypes.ACTIVATE, - on_activate) + notification_id = self.optimizely.notification_center.add_notification_listener( + enums.NotificationTypes.ACTIVATE, on_activate + ) with mock.patch( 'optimizely.decision_service.DecisionService.get_variation', return_value=self.project_config.get_variation_from_id('test_experiment', '111129')), \ @@ -308,7 +309,6 @@ def on_custom_event(test_string): notification_center.add_notification_listener(custom_type, on_custom_event) notification_center.send_notifications(custom_type, 1, 2, "5", 6) - #self.assertTrue(custom_called[0]) mock_logging.assert_called_once_with(enums.LogLevels.ERROR, self._expected_notification_failure) def test_add_invalid_listener(self): @@ -346,10 +346,8 @@ def test_activate_listener(self): with mock.patch( 'optimizely.decision_service.DecisionService.get_variation', return_value=self.project_config.get_variation_from_id('test_experiment', '111129')), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ - mock.patch( - 'optimizely.notification_center.NotificationCenter.send_notifications') \ - as mock_broadcast_activate: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ + mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_broadcast_activate: self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user')) mock_broadcast_activate.assert_called_once_with(enums.NotificationTypes.ACTIVATE, @@ -365,10 +363,8 @@ def test_activate_listener_with_attr(self): with mock.patch( 'optimizely.decision_service.DecisionService.get_variation', return_value=self.project_config.get_variation_from_id('test_experiment', '111129')), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ - mock.patch( - 'optimizely.notification_center.NotificationCenter.send_notifications') \ - as mock_broadcast_activate: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ + mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_broadcast_activate: self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user', {'test_attribute': 'test_value'})) @@ -388,9 +384,8 @@ def test_track_listener(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ - mock.patch( - 'optimizely.notification_center.NotificationCenter.send_notifications') as mock_event_tracked: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ + mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_event_tracked: self.optimizely.track('test_event', 'test_user') mock_event_tracked.assert_called_once_with(enums.NotificationTypes.TRACK, "test_event", @@ -403,9 +398,8 @@ def test_track_listener_with_attr(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ - mock.patch( - 'optimizely.notification_center.NotificationCenter.send_notifications') as mock_event_tracked: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ + mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_event_tracked: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}) mock_event_tracked.assert_called_once_with(enums.NotificationTypes.TRACK, "test_event", 'test_user', @@ -419,9 +413,8 @@ def test_track_listener_with_attr_with_event_tags(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ - mock.patch( - 'optimizely.notification_center.NotificationCenter.send_notifications') as mock_event_tracked: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch, \ + mock.patch('optimizely.notification_center.NotificationCenter.send_notifications') as mock_event_tracked: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}, event_tags={'value': 1.234, 'non-revenue': 'abc'}) @@ -454,9 +447,9 @@ def on_activate(experiment, user_id, attributes, variation, event): mock_variation, decision_service.DECISION_SOURCE_EXPERIMENT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertTrue(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -485,9 +478,9 @@ def on_activate(experiment, user_id, attributes, variation, event): mock_variation, decision_service.DECISION_SOURCE_ROLLOUT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertTrue(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -504,9 +497,9 @@ def test_activate__with_attributes__audience_match(self): 'optimizely.decision_service.DecisionService.get_variation', return_value=self.project_config.get_variation_from_id('test_experiment', '111129')) \ as mock_get_variation, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user', {'test_attribute': 'test_value'})) expected_params = { @@ -623,11 +616,11 @@ def test_activate__with_attributes__audience_match__bucketing_id_provided(self): 'experiment_id': '111127', 'campaign_id': '111182' }], - 'events': [{ - 'timestamp': 42000, - 'entity_id': '111182', - 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', - 'key': 'campaign_activated', + 'events': [{ + 'timestamp': 42000, + 'entity_id': '111182', + 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + 'key': 'campaign_activated', }] }] }], @@ -657,7 +650,7 @@ def test_activate__with_attributes__invalid_attributes(self): """ Test that activate returns None and does not bucket or dispatch event when attributes are invalid. """ with mock.patch('optimizely.bucketer.Bucketer.bucket') as mock_bucket, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertIsNone(self.optimizely.activate('test_experiment', 'test_user', attributes='invalid')) self.assertEqual(0, mock_bucket.call_count) @@ -667,10 +660,10 @@ def test_activate__experiment_not_running(self): """ Test that activate returns None and does not dispatch event when experiment is not Running. """ with mock.patch('optimizely.helpers.audience.is_user_in_experiment', return_value=True) as mock_audience_check, \ - mock.patch('optimizely.helpers.experiment.is_experiment_running', - return_value=False) as mock_is_experiment_running, \ - mock.patch('optimizely.bucketer.Bucketer.bucket') as mock_bucket, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.helpers.experiment.is_experiment_running', + return_value=False) as mock_is_experiment_running, \ + mock.patch('optimizely.bucketer.Bucketer.bucket') as mock_bucket, \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertIsNone(self.optimizely.activate('test_experiment', 'test_user', attributes={'test_attribute': 'test_value'})) @@ -693,8 +686,8 @@ def test_activate__bucketer_returns_none(self): """ Test that activate returns None and does not dispatch event when user is in no variation. """ with mock.patch('optimizely.helpers.audience.is_user_in_experiment', return_value=True), \ - mock.patch('optimizely.bucketer.Bucketer.bucket', return_value=None) as mock_bucket, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.bucketer.Bucketer.bucket', return_value=None) as mock_bucket, \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertIsNone(self.optimizely.activate('test_experiment', 'test_user', attributes={'test_attribute': 'test_value'})) mock_bucket.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'), @@ -719,9 +712,9 @@ def test_track__with_attributes(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}) expected_params = { @@ -775,7 +768,7 @@ def test_track__with_attributes__bucketing_id_provided(self): '$opt_bucketing_id': 'user_bucket_value'}) expected_params = { - 'account_id': '12001', + 'account_id': '12001', 'project_id': '111001', 'visitors': [{ 'visitor_id': 'test_user', @@ -791,11 +784,11 @@ def test_track__with_attributes__bucketing_id_provided(self): 'experiment_id': '111127', 'campaign_id': '111182' }], - 'events': [{ - 'timestamp': 42000, - 'entity_id': '111095', - 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', - 'key': 'test_event', + 'events': [{ + 'timestamp': 42000, + 'entity_id': '111095', + 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + 'key': 'test_event', }] }] }], @@ -818,8 +811,8 @@ def test_track__with_attributes__no_audience_match(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_bucket, \ - mock.patch('time.time', return_value=42), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'wrong_test_value'}) self.assertEqual(0, mock_bucket.call_count) @@ -829,7 +822,7 @@ def test_track__with_attributes__invalid_attributes(self): """ Test that track does not bucket or dispatch event if attributes are invalid. """ with mock.patch('optimizely.bucketer.Bucketer.bucket') as mock_bucket, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes='invalid') self.assertEqual(0, mock_bucket.call_count) @@ -842,9 +835,9 @@ def test_track__with_event_tags(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}, event_tags={'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'}) @@ -899,9 +892,9 @@ def test_track__with_event_tags_revenue(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}, event_tags={'revenue': 4200, 'non-revenue': 'abc'}) @@ -954,8 +947,8 @@ def test_track__with_event_tags_numeric_metric(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('time.time', return_value=42), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}, event_tags={'value': 1.234, 'non-revenue': 'abc'}) @@ -982,8 +975,8 @@ def test_track__with_event_tags__forced_bucketing(self): after a forced bucket. """ with mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertTrue(self.optimizely.set_forced_variation('test_experiment', 'test_user', 'variation')) self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}, event_tags={'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'}) @@ -1038,9 +1031,9 @@ def test_track__with_invalid_event_tags(self): return_value=self.project_config.get_variation_from_id( 'test_experiment', '111128' )) as mock_get_variation, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user', attributes={'test_attribute': 'test_value'}, event_tags={'revenue': '4200', 'value': True}) @@ -1089,8 +1082,8 @@ def test_track__experiment_not_running(self): with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=False) as mock_is_experiment_running, \ - mock.patch('time.time', return_value=42), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('time.time', return_value=42), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'test_user') mock_is_experiment_running.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment')) @@ -1100,7 +1093,7 @@ def test_track_invalid_event_key(self): """ Test that track does not call dispatch_event when event does not exist. """ with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event,\ - mock.patch('optimizely.logger.NoOpLogger.log') as mock_logging: + mock.patch('optimizely.logger.NoOpLogger.log') as mock_logging: self.optimizely.track('aabbcc_event', 'test_user') self.assertEqual(0, mock_dispatch_event.call_count) @@ -1114,11 +1107,11 @@ def test_track__whitelisted_user_overrides_audience_check(self): with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True) as mock_is_experiment_running, \ - mock.patch('optimizely.helpers.audience.is_user_in_experiment', - return_value=False) as mock_audience_check, \ - mock.patch('time.time', return_value=42), \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.helpers.audience.is_user_in_experiment', + return_value=False) as mock_audience_check, \ + mock.patch('time.time', return_value=42), \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.optimizely.track('test_event', 'user_1') mock_is_experiment_running.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment')) @@ -1181,7 +1174,7 @@ def test_is_feature_enabled__returns_false_for_invalid_feature(self): opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features)) with mock.patch('optimizely.decision_service.DecisionService.get_variation_for_feature') as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertFalse(opt_obj.is_feature_enabled('invalid_feature', 'user1')) self.assertFalse(mock_decision.called) @@ -1210,9 +1203,9 @@ def test_is_feature_enabled__returns_true_for_feature_experiment_if_property_fea mock_variation, decision_service.DECISION_SOURCE_EXPERIMENT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertTrue(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -1269,9 +1262,9 @@ def test_is_feature_enabled__returns_false_for_feature_experiment_if_property_fe mock_variation, decision_service.DECISION_SOURCE_EXPERIMENT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertFalse(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -1300,9 +1293,9 @@ def test_is_feature_enabled__returns_true_for_feature_rollout_if_property_featur mock_variation, decision_service.DECISION_SOURCE_ROLLOUT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertTrue(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -1331,9 +1324,9 @@ def test_is_feature_enabled__returns_false_for_feature_rollout_if_property_featu mock_variation, decision_service.DECISION_SOURCE_ROLLOUT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertFalse(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -1356,9 +1349,9 @@ def test_is_feature_enabled__returns_false_when_user_is_not_bucketed_into_any_va None, decision_service.DECISION_SOURCE_EXPERIMENT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertFalse(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -1373,9 +1366,9 @@ def test_is_feature_enabled__returns_false_when_user_is_not_bucketed_into_any_va None, decision_service.DECISION_SOURCE_ROLLOUT )) as mock_decision, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ - mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ - mock.patch('time.time', return_value=42): + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event, \ + mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'), \ + mock.patch('time.time', return_value=42): self.assertFalse(opt_obj.is_feature_enabled('test_feature_in_experiment', 'test_user')) mock_decision.assert_called_once_with(feature, 'test_user', None) @@ -1389,7 +1382,7 @@ def test_is_feature_enabled__invalid_object(self): opt_obj = optimizely.Optimizely('invalid_file') with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging, \ - mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: + mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event: self.assertFalse(opt_obj.is_feature_enabled('test_feature_in_experiment', 'user_1')) mock_logging.assert_called_once_with(enums.LogLevels.ERROR, @@ -1958,7 +1951,7 @@ def test_get_variation__no_audience_match(self): attributes={'test_attribute': 'wrong_test_value'}) self.assertEqual(2, mock_logging.call_count) - self.assertEqual(mock.call(enums.LogLevels.DEBUG, 'User "%s" is not in the forced variation map.' % user_id), \ + self.assertEqual(mock.call(enums.LogLevels.DEBUG, 'User "%s" is not in the forced variation map.' % user_id), mock_logging.call_args_list[0]) self.assertEqual(mock.call(enums.LogLevels.INFO, 'User "%s" does not meet conditions to be in experiment "%s".' diff --git a/tox.ini b/tox.ini index deb283de..c962d441 100644 --- a/tox.ini +++ b/tox.ini @@ -2,16 +2,7 @@ # E111 - indentation is not a multiple of four # E114 - indentation is not a multiple of four (comment) # E121 - continuation line indentation is not a multiple of four -# E122 - continuation line missing indentation or outdented -# E124 - closing bracket does not match visual indentation -# E125 - continuation line does not distinguish itself from next logical line -# E126 - continuation line over-indented for hanging indent # E127 - continuation line over-indented for visual indent -# E128 - continuation line under-indented for visual indent -# E129 - visually indented line with same indent as next logical line -# E131 - continuation line unaligned for hanging indent -# E265 - block comment should start with '# ' -# E502 - the backslash is redundant between brackets -ignore = E111,E114,E121,E122,E123,E124,E125,E126,E127,E128,E129,E131,E265,E502 +ignore = E111,E114,E121,E127 exclude = optimizely/lib/pymmh3.py,*virtualenv* max-line-length = 120