Skip to content

Commit 6b3bf40

Browse files
[OASIS-7757] - update copyright years and add more testcases for sdk_key and environment_key. Move decide tests to test_user_context from test_optimizely.
1 parent 4f27743 commit 6b3bf40

6 files changed

+91
-36
lines changed

Diff for: optimizely/optimizely_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020, Optimizely
1+
# Copyright 2020-2021, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

Diff for: optimizely/optimizely_factory.py

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ def default_instance_with_config_manager(config_manager):
113113
def custom_instance(sdk_key, datafile=None, event_dispatcher=None, logger=None, error_handler=None,
114114
skip_json_validation=None, user_profile_service=None, config_manager=None,
115115
notification_center=None):
116-
117116
""" Returns a new optimizely instance.
118117
if max_event_batch_size and max_event_flush_interval are None then default batch_size and flush_interval
119118
will be used to setup BatchEventProcessor.

Diff for: optimizely/project_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016-2019, Optimizely
1+
# Copyright 2016-2019, 2021, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

Diff for: tests/test_optimizely.py

-32
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from optimizely import optimizely_config
2727
from optimizely import project_config
2828
from optimizely import version
29-
from optimizely.decision.optimizely_decide_option import OptimizelyDecideOption as DecideOption
3029
from optimizely.event.event_factory import EventFactory
3130
from optimizely.helpers import enums
3231
from . import base
@@ -266,19 +265,6 @@ def test_init__sdk_key_and_datafile_access_token(self):
266265

267266
self.assertIs(type(opt_obj.config_manager), config_manager.AuthDatafilePollingConfigManager)
268267

269-
def test_init__invalid_default_decide_options(self):
270-
"""
271-
Test to confirm that default decide options passed not as a list will trigger setting
272-
self.deafulat_decide_options as an empty list.
273-
"""
274-
invalid_decide_options = {"testKey": "testOption"}
275-
276-
mock_client_logger = mock.MagicMock()
277-
with mock.patch('optimizely.logger.reset_logger', return_value=mock_client_logger):
278-
opt_obj = optimizely.Optimizely(default_decide_options=invalid_decide_options)
279-
280-
self.assertEqual(opt_obj.default_decide_options, [])
281-
282268
def test_invalid_json_raises_schema_validation_off(self):
283269
""" Test that invalid JSON logs error if schema validation is turned off. """
284270

@@ -690,24 +676,6 @@ def on_activate(experiment, user_id, attributes, variation, event):
690676
self.assertEqual(1, mock_process.call_count)
691677
self.assertEqual(True, access_callback[0])
692678

693-
def test_decide_experiment(self):
694-
""" Test that the feature is enabled for the user if bucketed into variation of a rollout.
695-
Also confirm that no impression event is processed. """
696-
697-
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
698-
project_config = opt_obj.config_manager.get_config()
699-
700-
mock_experiment = project_config.get_experiment_from_key('test_experiment')
701-
mock_variation = project_config.get_variation_from_id('test_experiment', '111129')
702-
with mock.patch(
703-
'optimizely.decision_service.DecisionService.get_variation_for_feature',
704-
return_value=(decision_service.Decision(mock_experiment,
705-
mock_variation, enums.DecisionSources.FEATURE_TEST), []),
706-
):
707-
user_context = opt_obj.create_user_context('test_user')
708-
decision = user_context.decide('test_feature_in_experiment', [DecideOption.DISABLE_DECISION_EVENT])
709-
self.assertTrue(decision.enabled, "decision should be enabled")
710-
711679
def test_activate__with_attributes__audience_match(self):
712680
""" Test that activate calls process with right params and returns expected
713681
variation when attributes are provided and audience conditions are met. """

Diff for: tests/test_optimizely_config.py

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020, Optimizely
1+
# Copyright 2020-2021, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -734,3 +734,59 @@ def test__get_datafile(self):
734734
actual_datafile = self.actual_config.get_datafile()
735735

736736
self.assertEqual(expected_datafile, actual_datafile)
737+
738+
def test__get_sdk_key(self):
739+
""" Test that get_sdk_key returns the expected value. """
740+
741+
config = optimizely_config.OptimizelyConfig(
742+
revision='101',
743+
experiments_map={},
744+
features_map={},
745+
sdk_key='testSdkKey',
746+
)
747+
748+
expected_value = 'testSdkKey'
749+
750+
self.assertEqual(expected_value, config.get_sdk_key())
751+
752+
def test__get_sdk_key_invalid(self):
753+
""" Negative Test that tests get_sdk_key does not return the expected value. """
754+
755+
config = optimizely_config.OptimizelyConfig(
756+
revision='101',
757+
experiments_map={},
758+
features_map={},
759+
sdk_key='testSdkKey',
760+
)
761+
762+
invalid_value = 123
763+
764+
self.assertNotEqual(invalid_value, config.get_sdk_key())
765+
766+
def test__get_environment_key(self):
767+
""" Test that get_environment_key returns the expected value. """
768+
769+
config = optimizely_config.OptimizelyConfig(
770+
revision='101',
771+
experiments_map={},
772+
features_map={},
773+
environment_key='TestEnvironmentKey'
774+
)
775+
776+
expected_value = 'TestEnvironmentKey'
777+
778+
self.assertEqual(expected_value, config.get_environment_key())
779+
780+
def test__get_environment_key_invalid(self):
781+
""" Negative Test that tests get_environment_key does not return the expected value. """
782+
783+
config = optimizely_config.OptimizelyConfig(
784+
revision='101',
785+
experiments_map={},
786+
features_map={},
787+
environment_key='testEnvironmentKey'
788+
)
789+
790+
invalid_value = 321
791+
792+
self.assertNotEqual(invalid_value, config.get_environment_key())

Diff for: tests/test_user_context.py

+32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import mock
1616

1717
from optimizely.decision.optimizely_decision import OptimizelyDecision
18+
from optimizely.decision.optimizely_decide_option import OptimizelyDecideOption as DecideOption
1819
from optimizely.helpers import enums
1920
from . import base
2021
from optimizely import optimizely, decision_service
@@ -1264,3 +1265,34 @@ def test_decide_reasons__whitelisted_variation(self):
12641265
expected_reasons = ['User "user_1" is forced in variation "control".']
12651266

12661267
self.assertEqual(expected_reasons, actual.reasons)
1268+
1269+
def test_init__invalid_default_decide_options(self):
1270+
"""
1271+
Test to confirm that default decide options passed not as a list will trigger setting
1272+
self.deafulat_decide_options as an empty list.
1273+
"""
1274+
invalid_decide_options = {"testKey": "testOption"}
1275+
1276+
mock_client_logger = mock.MagicMock()
1277+
with mock.patch('optimizely.logger.reset_logger', return_value=mock_client_logger):
1278+
opt_obj = optimizely.Optimizely(default_decide_options=invalid_decide_options)
1279+
1280+
self.assertEqual(opt_obj.default_decide_options, [])
1281+
1282+
def test_decide_experiment(self):
1283+
""" Test that the feature is enabled for the user if bucketed into variation of a rollout.
1284+
Also confirm that no impression event is processed. """
1285+
1286+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1287+
project_config = opt_obj.config_manager.get_config()
1288+
1289+
mock_experiment = project_config.get_experiment_from_key('test_experiment')
1290+
mock_variation = project_config.get_variation_from_id('test_experiment', '111129')
1291+
with mock.patch(
1292+
'optimizely.decision_service.DecisionService.get_variation_for_feature',
1293+
return_value=(decision_service.Decision(mock_experiment,
1294+
mock_variation, enums.DecisionSources.FEATURE_TEST), []),
1295+
):
1296+
user_context = opt_obj.create_user_context('test_user')
1297+
decision = user_context.decide('test_feature_in_experiment', [DecideOption.DISABLE_DECISION_EVENT])
1298+
self.assertTrue(decision.enabled, "decision should be enabled")

0 commit comments

Comments
 (0)