From e2c2c591079dcb0e39fddd6e245cf9b9aa400443 Mon Sep 17 00:00:00 2001 From: Owais Date: Wed, 24 Oct 2018 16:03:51 +0500 Subject: [PATCH] Done, Danadan --- optimizely/helpers/audience.py | 3 +- optimizely/helpers/condition.py | 6 ++ tests/helpers_tests/test_audience.py | 16 +-- tests/test_optimizely.py | 146 +++++++++++++-------------- 4 files changed, 89 insertions(+), 82 deletions(-) diff --git a/optimizely/helpers/audience.py b/optimizely/helpers/audience.py index 3309794c..89407eb0 100644 --- a/optimizely/helpers/audience.py +++ b/optimizely/helpers/audience.py @@ -24,7 +24,8 @@ def is_match(audience, attributes): Return: Boolean representing if user satisfies audience conditions or not. """ - + print(audience.conditionList) + print(audience.conditionStructure) condition_tree_evaluator = condition_helper.ConditionTreeEvaluator() custom_attr_condition_evaluator = condition_helper.CustomAttributeConditionEvaluator( audience.conditionList, attributes) diff --git a/optimizely/helpers/condition.py b/optimizely/helpers/condition.py index ac6ce5a4..461f16f6 100644 --- a/optimizely/helpers/condition.py +++ b/optimizely/helpers/condition.py @@ -153,9 +153,15 @@ def is_value_valid_for_exact_conditions(self, value): def exact_evaluator(self, condition): condition_value = self.condition_data[condition][1] + if isinstance(condition_value, string_types): + condition_value = condition_value.encode() + condition_value_type = type(condition_value) user_value = self.attributes.get(self.condition_data[condition][0]) + if isinstance(user_value, string_types): + user_value = user_value.encode() + user_value_type = type(user_value) if not self.is_value_valid_for_exact_conditions(condition_value) or \ diff --git a/tests/helpers_tests/test_audience.py b/tests/helpers_tests/test_audience.py index d0179947..6302ad8a 100644 --- a/tests/helpers_tests/test_audience.py +++ b/tests/helpers_tests/test_audience.py @@ -19,16 +19,16 @@ class AudienceTest(base.BaseTest): - # def test_is_match__audience_condition_matches(self): - # """ Test that is_match returns True when audience conditions are met. """ + def test_is_match__audience_condition_matches(self): + """ Test that is_match returns True when audience conditions are met. """ - # user_attributes = { - # 'test_attribute': 'test_value_1', - # 'browser_type': 'firefox', - # 'location': 'San Francisco' - # } + user_attributes = { + 'test_attribute': 'test_value_1', + 'browser_type': 'firefox', + 'location': 'San Francisco' + } - # self.assertTrue(audience.is_match(self.optimizely.config.get_audience('11154'), user_attributes)) + self.assertTrue(audience.is_match(self.optimizely.config.get_audience('11154'), user_attributes)) def test_is_match__audience_condition_does_not_match(self): """ Test that is_match returns False when audience conditions are not met. """ diff --git a/tests/test_optimizely.py b/tests/test_optimizely.py index aea4eb84..dd27af2e 100644 --- a/tests/test_optimizely.py +++ b/tests/test_optimizely.py @@ -596,79 +596,79 @@ def test_activate__with_attributes__audience_match(self): self._validate_event_object(mock_dispatch_event.call_args[0][0], 'https://logx.optimizely.com/v1/events', expected_params, 'POST', {'Content-Type': 'application/json'}) - # def test_activate__with_attributes_of_different_types(self): - # """ Test that activate calls dispatch_event with right params and returns expected - # variation when different types of attributes are provided and audience conditions are met. """ - - # with mock.patch( - # 'optimizely.bucketer.Bucketer.bucket', - # return_value=self.project_config.get_variation_from_id('test_experiment', '111129')) \ - # as mock_bucket, \ - # 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: - - # attributes = { - # 'test_attribute': 'test_value_1', - # 'boolean_key': False, - # 'integer_key': 0, - # 'double_key': 0.0 - # } - - # self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user', attributes)) - - # expected_params = { - # 'account_id': '12001', - # 'project_id': '111001', - # 'visitors': [{ - # 'visitor_id': 'test_user', - # 'attributes': [{ - # 'type': 'custom', - # 'value': False, - # 'entity_id': '111196', - # 'key': 'boolean_key' - # }, { - # 'type': 'custom', - # 'value': 0.0, - # 'entity_id': '111198', - # 'key': 'double_key' - # }, { - # 'type': 'custom', - # 'value': 0, - # 'entity_id': '111197', - # 'key': 'integer_key' - # }, { - # 'type': 'custom', - # 'value': 'test_value_1', - # 'entity_id': '111094', - # 'key': 'test_attribute' - # }], - # 'snapshots': [{ - # 'decisions': [{ - # 'variation_id': '111129', - # 'experiment_id': '111127', - # 'campaign_id': '111182' - # }], - # 'events': [{ - # 'timestamp': 42000, - # 'entity_id': '111182', - # 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', - # 'key': 'campaign_activated', - # }] - # }] - # }], - # 'client_version': version.__version__, - # 'client_name': 'python-sdk', - # 'anonymize_ip': False, - # 'revision': '42' - # } - - # mock_bucket.assert_called_once_with( - # self.project_config.get_experiment_from_key('test_experiment'), 'test_user', 'test_user' - # ) - # self.assertEqual(1, mock_dispatch_event.call_count) - # self._validate_event_object(mock_dispatch_event.call_args[0][0], 'https://logx.optimizely.com/v1/events', - # expected_params, 'POST', {'Content-Type': 'application/json'}) + def test_activate__with_attributes_of_different_types(self): + """ Test that activate calls dispatch_event with right params and returns expected + variation when different types of attributes are provided and audience conditions are met. """ + + with mock.patch( + 'optimizely.bucketer.Bucketer.bucket', + return_value=self.project_config.get_variation_from_id('test_experiment', '111129')) \ + as mock_bucket, \ + 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: + + attributes = { + 'test_attribute': 'test_value_1', + 'boolean_key': False, + 'integer_key': 0, + 'double_key': 0.0 + } + + self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user', attributes)) + + expected_params = { + 'account_id': '12001', + 'project_id': '111001', + 'visitors': [{ + 'visitor_id': 'test_user', + 'attributes': [{ + 'type': 'custom', + 'value': False, + 'entity_id': '111196', + 'key': 'boolean_key' + }, { + 'type': 'custom', + 'value': 0.0, + 'entity_id': '111198', + 'key': 'double_key' + }, { + 'type': 'custom', + 'value': 0, + 'entity_id': '111197', + 'key': 'integer_key' + }, { + 'type': 'custom', + 'value': 'test_value_1', + 'entity_id': '111094', + 'key': 'test_attribute' + }], + 'snapshots': [{ + 'decisions': [{ + 'variation_id': '111129', + 'experiment_id': '111127', + 'campaign_id': '111182' + }], + 'events': [{ + 'timestamp': 42000, + 'entity_id': '111182', + 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + 'key': 'campaign_activated', + }] + }] + }], + 'client_version': version.__version__, + 'client_name': 'python-sdk', + 'anonymize_ip': False, + 'revision': '42' + } + + mock_bucket.assert_called_once_with( + self.project_config.get_experiment_from_key('test_experiment'), 'test_user', 'test_user' + ) + self.assertEqual(1, mock_dispatch_event.call_count) + self._validate_event_object(mock_dispatch_event.call_args[0][0], 'https://logx.optimizely.com/v1/events', + expected_params, 'POST', {'Content-Type': 'application/json'}) def test_activate__with_attributes__audience_match__forced_bucketing(self): """ Test that activate calls dispatch_event with right params and returns expected