Skip to content

Commit

Permalink
refact: nits - ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Nov 22, 2018
1 parent 008a3ce commit 50b95a7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 103 deletions.
2 changes: 1 addition & 1 deletion optimizely/helpers/audience.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def is_user_in_experiment(config, experiment, attributes):
if not experiment.audienceIds:
return True

if not attributes:
if attributes is None:
attributes = {}

# Return True if conditions for any one audience are met
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ def test_get_audience__invalid_id(self):

self.assertIsNone(self.project_config.get_audience('42'))

def test_get_audience__retrieves_audiences_first_from_typedAudiences_before_audiences(self):
def test_get_audience__prefers_typedAudiences_over_audiences(self):
opt = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))
config = opt.config

Expand Down
202 changes: 101 additions & 101 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,53 @@ def test_activate__with_attributes_of_different_types(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__typed_audience_match(self):
""" Test that activate calls dispatch_event with right params and returns expected
variation when attributes are provided and typed audience conditions are met. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
# Should be included via exact match string audience with id '3468206642'
self.assertEqual('A', opt_obj.activate('typed_audience_experiment', 'test_user',
{'house': 'Gryffindor'}))
expected_attr = {
'type': 'custom',
'value': 'Gryffindor',
'entity_id': '594015',
'key': 'house'
}

self.assertTrue(
expected_attr in mock_dispatch_event.call_args[0][0].params['visitors'][0]['attributes']
)

mock_dispatch_event.reset()

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
# Should be included via exact match number audience with id '3468206646'
self.assertEqual('A', opt_obj.activate('typed_audience_experiment', 'test_user',
{'lasers': 45.5}))
expected_attr = {
'type': 'custom',
'value': 45.5,
'entity_id': '594016',
'key': 'lasers'
}

self.assertTrue(
expected_attr in mock_dispatch_event.call_args[0][0].params['visitors'][0]['attributes']
)

def test_activate__with_attributes__typed_audience_mismatch(self):
""" Test that activate returns None when typed audience conditions do not match. """
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
self.assertIsNone(opt_obj.activate('typed_audience_experiment', 'test_user',
{'house': 'Hufflepuff'}))
self.assertEqual(0, mock_dispatch_event.call_count)

def test_activate__with_attributes__audience_match__forced_bucketing(self):
""" Test that activate calls dispatch_event with right params and returns expected
variation when attributes are provided and audience conditions are met after a
Expand Down Expand Up @@ -890,6 +937,39 @@ def test_track__with_attributes(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_track__with_attributes__typed_audience_match(self):
""" Test that track calls dispatch_event with right params when attributes are provided
and it's a typed audience match. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
# Should be included via substring match string audience with id '3988293898'
opt_obj.track('item_bought', 'test_user', {'house': 'Welcome to Slytherin!'})

self.assertEqual(1, mock_dispatch_event.call_count)

expected_attr = {
'type': 'custom',
'value': 'Welcome to Slytherin!',
'entity_id': '594015',
'key': 'house'
}

self.assertTrue(
expected_attr in mock_dispatch_event.call_args[0][0].params['visitors'][0]['attributes']
)

def test_track__with_attributes__typed_audience_mismatch(self):
""" Test that track does not call dispatch_event when typed audience conditions do not match. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
opt_obj.track('item_bought', 'test_user', {'house': 'Welcome to Hufflepuff!'})

self.assertEqual(0, mock_dispatch_event.call_count)

def test_track__with_attributes__bucketing_id_provided(self):
""" Test that track calls dispatch_event with right params when
attributes (including bucketing ID) are provided. """
Expand Down Expand Up @@ -1347,6 +1427,27 @@ def test_is_feature_enabled__returns_false_for__invalid_attributes(self):
mock_validator.assert_called_once_with('invalid')
mock_client_logging.error.assert_called_once_with('Provided attributes are in an invalid format.')

def test_is_feature_enabled__in_rollout__typed_audience_match(self):
""" Test that is_feature_enabled returns True for feature rollout with typed audience match. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

# Should be included via exists match audience with id '3988293899'
self.assertTrue(opt_obj.is_feature_enabled('feat', 'test_user', {'favorite_ice_cream': 'chocolate'}))

# Should be included via less-than match audience with id '3468206644'
self.assertTrue(opt_obj.is_feature_enabled('feat', 'test_user', {'lasers': -3}))

def test_is_feature_enabled__in_rollout__typed_audience_mismatch(self):
""" Test that is_feature_enabled returns False for feature rollout with typed audience mismatch. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

self.assertIs(
opt_obj.is_feature_enabled('feat', 'test_user', {}),
False
)

def test_is_feature_enabled__returns_false_for_invalid_feature(self):
""" Test that the feature is not enabled for the user if the provided feature key is invalid. """

Expand Down Expand Up @@ -2046,107 +2147,6 @@ def test_get_feature_variable__returns_none_if_unable_to_cast(self):

mock_client_logger.error.assert_called_with('Unable to cast value. Returning None.')

def test_activate__with_attributes__typed_audience_match(self):
""" Test that activate calls dispatch_event with right params and returns expected
variation when attributes are provided and typed audience conditions are met. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
# Should be included via exact match string audience with id '3468206642'
self.assertEqual('A', opt_obj.activate('typed_audience_experiment', 'test_user',
{'house': 'Gryffindor'}))
expected_attr = {
'type': 'custom',
'value': 'Gryffindor',
'entity_id': '594015',
'key': 'house'
}

self.assertTrue(
expected_attr in mock_dispatch_event.call_args[0][0].params['visitors'][0]['attributes']
)

mock_dispatch_event.reset()

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
# Should be included via exact match number audience with id '3468206646'
self.assertEqual('A', opt_obj.activate('typed_audience_experiment', 'test_user',
{'lasers': 45.5}))
expected_attr = {
'type': 'custom',
'value': 45.5,
'entity_id': '594016',
'key': 'lasers'
}

self.assertTrue(
expected_attr in mock_dispatch_event.call_args[0][0].params['visitors'][0]['attributes']
)

def test_activate__with_attributes__typed_audience_mismatch(self):
""" Test that activate returns None when typed audience conditions do not match. """
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
self.assertIsNone(opt_obj.activate('typed_audience_experiment', 'test_user',
{'house': 'Hufflepuff'}))
self.assertEqual(0, mock_dispatch_event.call_count)

def test_track__with_attributes__typed_audience_match(self):
""" Test that track calls dispatch_event with right params when attributes are provided
and it's a typed audience match. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
# Should be included via substring match string audience with id '3988293898'
opt_obj.track('item_bought', 'test_user', {'house': 'Welcome to Slytherin!'})

self.assertEqual(1, mock_dispatch_event.call_count)

expected_attr = {
'type': 'custom',
'value': 'Welcome to Slytherin!',
'entity_id': '594015',
'key': 'house'
}

self.assertTrue(
expected_attr in mock_dispatch_event.call_args[0][0].params['visitors'][0]['attributes']
)

def test_track__with_attributes__typed_audience_mismatch(self):
""" Test that track does not call dispatch_event when typed audience conditions do not match. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

with mock.patch('optimizely.event_dispatcher.EventDispatcher.dispatch_event') as mock_dispatch_event:
opt_obj.track('item_bought', 'test_user', {'house': 'Welcome to Hufflepuff!'})

self.assertEqual(0, mock_dispatch_event.call_count)

def test_is_feature_enabled__in_rollout__typed_audience_match(self):
""" Test that is_feature_enabled returns True for feature rollout with typed audience match. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

# Should be included via exists match audience with id '3988293899'
self.assertTrue(opt_obj.is_feature_enabled('feat', 'test_user', {'favorite_ice_cream': 'chocolate'}))

# Should be included via less-than match audience with id '3468206644'
self.assertTrue(opt_obj.is_feature_enabled('feat', 'test_user', {'lasers': -3}))

def test_is_feature_enabled__in_rollout__typed_audience_mismatch(self):
""" Test that is_feature_enabled returns False for feature rollout with typed audience mismatch. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_typed_audiences))

self.assertIs(
opt_obj.is_feature_enabled('feat', 'test_user', {}),
False
)

def test_get_feature_variable_returns__variable_value__typed_audience_match(self):
""" Test that get_feature_variable_* return variable value with typed audience match. """

Expand Down

0 comments on commit 50b95a7

Please sign in to comment.