Skip to content

Commit

Permalink
Done, Danadan
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Oct 24, 2018
1 parent a6447ff commit e2c2c59
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 82 deletions.
3 changes: 2 additions & 1 deletion optimizely/helpers/audience.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions optimizely/helpers/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
16 changes: 8 additions & 8 deletions tests/helpers_tests/test_audience.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down
146 changes: 73 additions & 73 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e2c2c59

Please sign in to comment.