Skip to content

Commit

Permalink
moving variations to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
aliabbasrizvi committed Sep 23, 2016
1 parent b9098ca commit f1cefe6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 69 deletions.
48 changes: 27 additions & 21 deletions tests/test_bucketing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random

from optimizely import bucketer
from optimizely import entities
from optimizely import logger
from optimizely import optimizely
from optimizely.helpers import enums
Expand All @@ -24,15 +25,18 @@ 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('111128', self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))
self.assertEqual(entities.Variation('111128', 'control'),
self.bucketer.bucket(
self.project_config.get_experiment_from_key('test_experiment'), 'test_user'
))
mock_generate_bucket_value.assert_called_once_with('test_user111127')

# Variation 2
with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value',
return_value=4242) as mock_generate_bucket_value:
self.assertEqual('111129', self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))
self.assertEqual(entities.Variation('111129', 'variation'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))
mock_generate_bucket_value.assert_called_once_with('test_user111127')

# No matching variation
Expand All @@ -52,8 +56,8 @@ def test_bucket__user_in_forced_variation(self):
""" Test that bucket returns variation ID for variation user is forced in. """

with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value') as mock_generate_bucket_value:
self.assertEqual('111128', self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'user_1'))
self.assertEqual(entities.Variation('111128', 'control'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'), 'user_1'))

# Confirm that bucket value generation did not happen
self.assertEqual(0, mock_generate_bucket_value.call_count)
Expand All @@ -62,7 +66,7 @@ def test_bucket__user_in_forced_variation__invalid_variation_id(self):
""" Test that bucket returns None when variation user is forced in is invalid. """

with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value') as mock_generate_bucket_value, \
mock.patch('optimizely.project_config.ProjectConfig.get_variation_id',
mock.patch('optimizely.project_config.ProjectConfig.get_variation_from_key',
return_value=None) as mock_get_variation_id:
self.assertIsNone(self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'user_1'))
Expand All @@ -77,8 +81,8 @@ 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]) as mock_generate_bucket_value:
self.assertEqual('28902', self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'),
'test_user'))
self.assertEqual(entities.Variation('28902', 'group_exp_1_variation'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'), 'test_user'))

self.assertEqual([mock.call('test_user19228'), mock.call('test_user32222')],
mock_generate_bucket_value.call_args_list)
Expand Down Expand Up @@ -110,8 +114,8 @@ def test_bucket__experiment_in_group__user_in_forced_variation(self):
""" Test that bucket returns variation ID for variation user is forced in. """

with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value') as mock_generate_bucket_value:
self.assertEqual('28905', self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_2'),
'user_1'))
self.assertEqual(entities.Variation('28905', 'group_exp_2_control'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_2'), 'user_1'))

# Confirm that bucket value generation did not happen
self.assertEqual(0, mock_generate_bucket_value.call_count)
Expand Down Expand Up @@ -151,8 +155,9 @@ 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:
self.assertEqual('111128', self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))
self.assertEqual(entities.Variation('111128', 'control'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))

self.assertEqual(2, mock_logging.call_count)
self.assertEqual(mock.call(enums.LogLevels.DEBUG, 'Assigned bucket 42 to user "test_user".'),
Expand All @@ -165,8 +170,9 @@ def test_bucket(self):
# Variation 2
with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=4242),\
mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
self.assertEqual('111129', self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))
self.assertEqual(entities.Variation('111129', 'variation'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'test_user'))
self.assertEqual(2, mock_logging.call_count)
self.assertEqual(mock.call(enums.LogLevels.DEBUG, 'Assigned bucket 4242 to user "test_user".'),
mock_logging.call_args_list[0])
Expand All @@ -191,8 +197,8 @@ def test_bucket__user_in_forced_variation(self):

with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value'),\
mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
self.assertEqual('111128', self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
'user_1'))
self.assertEqual(entities.Variation('111128', 'control'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'), 'user_1'))

mock_logging.assert_called_with(enums.LogLevels.INFO, 'User "user_1" is forced in variation "control".')

Expand All @@ -203,8 +209,8 @@ def test_bucket__experiment_in_group(self):
with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value',
side_effect=[42, 4242]),\
mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
self.assertEqual('28902', self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'),
'test_user'))
self.assertEqual(entities.Variation('28902', 'group_exp_1_variation'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_1'), 'test_user'))
self.assertEqual(4, mock_logging.call_count)
self.assertEqual(mock.call(enums.LogLevels.DEBUG, 'Assigned bucket 42 to user "test_user".'),
mock_logging.call_args_list[0])
Expand Down Expand Up @@ -277,8 +283,8 @@ def test_bucket__experiment_in_group__user_in_forced_variation(self):

with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value'),\
mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
self.assertEqual('28905', self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_2'),
'user_1'))
self.assertEqual(entities.Variation('28905', 'group_exp_2_control'),
self.bucketer.bucket(self.project_config.get_experiment_from_key('group_exp_2'), 'user_1'))

# Confirm that bucket value generation did not happen
mock_logging.assert_called_with(enums.LogLevels.INFO, 'User "user_1" is forced in variation "group_exp_2_control".')
60 changes: 12 additions & 48 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,66 +108,30 @@ def test_init(self):
}
expected_variation_key_map = {
'test_experiment': {
'control': {
'key': 'control',
'id': '111128'
},
'variation': {
'key': 'variation',
'id': '111129'
}
'control': entities.Variation('111128', 'control'),
'variation': entities.Variation('111129', 'variation')
},
'group_exp_1': {
'group_exp_1_control': {
'key': 'group_exp_1_control',
'id': '28901'
},
'group_exp_1_variation': {
'key': 'group_exp_1_variation',
'id': '28902'
}
'group_exp_1_control': entities.Variation('28901', 'group_exp_1_control'),
'group_exp_1_variation': entities.Variation('28902', 'group_exp_1_variation')
},
'group_exp_2': {
'group_exp_2_control': {
'key': 'group_exp_2_control',
'id': '28905'
},
'group_exp_2_variation': {
'key': 'group_exp_2_variation',
'id': '28906'
}
'group_exp_2_control': entities.Variation('28905', 'group_exp_2_control'),
'group_exp_2_variation': entities.Variation('28906', 'group_exp_2_variation')
}
}
expected_variation_id_map = {
'test_experiment': {
'111128': {
'key': 'control',
'id': '111128'
},
'111129': {
'key': 'variation',
'id': '111129'
}
'111128': entities.Variation('111128', 'control'),
'111129': entities.Variation('111129', 'variation')
},
'group_exp_1': {
'28901': {
'key': 'group_exp_1_control',
'id': '28901'
},
'28902': {
'key': 'group_exp_1_variation',
'id': '28902'
}
'28901': entities.Variation('28901', 'group_exp_1_control'),
'28902': entities.Variation('28902', 'group_exp_1_variation')
},
'group_exp_2': {
'28905': {
'key': 'group_exp_2_control',
'id': '28905'
},
'28906': {
'key': 'group_exp_2_variation',
'id': '28906'
}
'28905': entities.Variation('28905', 'group_exp_2_control'),
'28906': entities.Variation('28906', 'group_exp_2_variation')
}
}

Expand Down

0 comments on commit f1cefe6

Please sign in to comment.