def stub_config(): """Builds a standardized Configuration object and returns it, but does not load it as the active configuration returned by dallinger.config.get_config() """ defaults = { u'aws_region': u'us-east-1', u'base_port': 5000, u'clock_on': True, u'dallinger_email_address': u'*****@*****.**', u'database_url': u'postgresql://postgres@localhost/dallinger', u'dyno_type': u'standard-2x', u'heroku_team': u'dallinger', u'host': u'localhost', u'logfile': u'server.log', u'loglevel': 0, u'mode': u'debug', u'num_dynos_web': 2, u'num_dynos_worker': 2, u'threads': u'1', u'whimsical': True } from dallinger.config import default_keys from dallinger.config import Configuration config = Configuration() for key in default_keys: config.register(*key) config.extend(defaults.copy()) config.ready = True return config
def test_setting_values_supports_synonyms(self): config = Configuration() config.register('num_participants', int, synonyms={ 'n', }) config.ready = True config.extend({'n': 1}) assert config.get('num_participants') == 1
def test_layering_of_configs(self): config = Configuration() config.register('num_participants', int) config.extend({'num_participants': 1}) config.ready = True assert config.get('num_participants', 1) == 1 config.extend({'num_participants': 2}) assert config.get('num_participants', 1) == 2
def stub_config(): """Builds a standardized Configuration object and returns it, but does not load it as the active configuration returned by dallinger.config.get_config() """ defaults = { u'ad_group': u'Test ad group', u'approve_requirement': 95, u'assign_qualifications': True, u'auto_recruit': True, u'aws_access_key_id': u'fake aws key', u'aws_secret_access_key': u'fake aws secret', u'aws_region': u'us-east-1', u'base_payment': 0.01, u'base_port': 5000, u'browser_exclude_rule': u'MSIE, mobile, tablet', u'clock_on': True, u'contact_email_on_error': u'*****@*****.**', u'dallinger_email_address': u'*****@*****.**', u'database_size': u'standard-0', u'redis_size': u'premium-0', u'database_url': u'postgresql://postgres@localhost/dallinger', u'description': u'fake HIT description', u'duration': 1.0, u'dyno_type': u'free', u'heroku_auth_token': u'heroku secret', u'heroku_team': u'', u'host': u'0.0.0.0', u'id': u'some experiment uid', u'keywords': u'kw1, kw2, kw3', u'lifetime': 1, u'logfile': u'-', u'loglevel': 0, u'mode': u'debug', u'notification_url': u'https://url-of-notification-route', u'num_dynos_web': 1, u'num_dynos_worker': 1, u'organization_name': u'Monsters University', u'sentry': True, u'smtp_host': u'smtp.fakehost.com:587', u'smtp_username': u'fake email username', u'smtp_password': u'fake email password', u'threads': u'1', u'title': u'fake experiment title', u'us_only': True, u'webdriver_type': u'phantomjs', u'whimsical': True } from dallinger.config import default_keys from dallinger.config import Configuration config = Configuration() for key in default_keys: config.register(*key) config.extend(defaults.copy()) config.ready = True return config
def test_type_casts_follow_file_pointers(self): config = Configuration() config.register("data", six.text_type) config.ready = True with NamedTemporaryFile() as data_file: data_file.write("hello".encode("utf-8")) data_file.flush() config.extend({"data": "file:" + data_file.name}, cast_types=True) assert config.get("data") == "hello"
def test_loading_keys_from_environment_variables(self): config = Configuration() config.register("num_participants", int, synonyms={"n"}) os.environ["num_participants"] = "1" try: config.load_from_environment() finally: del os.environ["num_participants"] config.ready = True assert config.get("num_participants") == 1
def test_loading_keys_from_environment_variables(self): config = Configuration() config.register('num_participants', int, synonyms={'n', }) os.environ['num_participants'] = '1' try: config.load_from_environment() finally: del os.environ['num_participants'] config.ready = True assert config.get('num_participants') == 1
def test_setting_value_that_doesnt_validate_fails(self): config = Configuration() def is_purple(val): if val != "purple": raise ValueError config.register("fave_colour", six.text_type, validators=[is_purple]) config.ready = True config.set("fave_colour", "purple") with pytest.raises(ValueError): config.set("fave_colour", "red")
def test_loading_keys_from_config_file(self): config = Configuration() config.register('num_participants', int, synonyms={'n', }) config.register('deploy_worldwide', bool, synonyms={'worldwide', }) with NamedTemporaryFile() as configfile: configfile.write(""" [Example Section] num_participants = 10 worldwide = false """) configfile.flush() config.load_from_config_file(configfile.name) config.ready = True assert config.get('num_participants') == 10 assert config.get('deploy_worldwide') is False
def test_loading_keys_from_config_file(self): config = Configuration() config.register("mode", six.text_type) config.register("num_participants", int, synonyms={"n"}) config.register("deploy_worldwide", bool, synonyms={"worldwide"}) mode_with_trailing_whitespace = "live " contents = """ [Example Section] mode = {} num_participants = 10 worldwide = false """.format(mode_with_trailing_whitespace) with NamedTemporaryFile() as configfile: configfile.write(contents.encode("utf-8")) configfile.flush() config.load_from_file(configfile.name) config.ready = True assert config.get("mode") == "live" # whitespace stripped assert config.get("num_participants") == 10 assert config.get("deploy_worldwide") is False
def test_setting_unknown_key_is_ignored(self): config = Configuration() config.ready = True config.extend({'num_participants': 1}) config.get('num_participants', None)
def test_get_strips_strings(self): config = Configuration() config.register("test_string", six.text_type) config.ready = True config.extend({"test_string": " something "}) assert config.get("test_string") == "something"
def test_get_before_ready_is_not_possible(self): config = Configuration() config.register('num_participants', int) config.extend({'num_participants': 1}) with pytest.raises(RuntimeError): config.get('num_participants', 1)
def test_type_cast_types_failure_raises(self): config = Configuration() config.register('num_participants', int) config.ready = True with pytest.raises(TypeError): config.extend({'num_participants': 'A NUMBER'}, cast_types=True)
def stub_config(): """Builds a standardized Configuration object and returns it, but does not load it as the active configuration returned by dallinger.config.get_config() """ defaults = { u"ad_group": u"Test ad group", u"approve_requirement": 95, u"assign_qualifications": True, u"auto_recruit": True, u"aws_access_key_id": u"fake aws key", u"aws_secret_access_key": u"fake aws secret", u"aws_region": u"us-east-1", u"base_payment": 0.01, u"base_port": 5000, u"browser_exclude_rule": u"MSIE, mobile, tablet", u"clock_on": False, u"contact_email_on_error": u"*****@*****.**", u"dallinger_email_address": u"*****@*****.**", u"database_size": u"standard-0", u"redis_size": u"premium-0", u"database_url": u"postgresql://postgres@localhost/dallinger", u"description": u"fake HIT description", u"duration": 1.0, u"dyno_type": u"free", u"heroku_auth_token": u"heroku secret", u"heroku_team": u"", u"host": u"0.0.0.0", u"id": u"some experiment uid", u"keywords": u"kw1, kw2, kw3", u"lifetime": 1, u"logfile": u"-", u"loglevel": 0, u"mode": u"debug", u"num_dynos_web": 1, u"num_dynos_worker": 1, u"organization_name": u"Monsters University", u"sentry": True, u"smtp_host": u"smtp.fakehost.com:587", u"smtp_username": u"fake email username", u"smtp_password": u"fake email password", u"threads": u"1", u"title": u"fake experiment title", u"us_only": True, u"webdriver_type": u"phantomjs", u"whimsical": True, u"replay": False, u"worker_multiplier": 1.5, } from dallinger.config import default_keys from dallinger.config import Configuration config = Configuration() for key in default_keys: config.register(*key) config.extend(defaults.copy()) # Patch load() so we don't update any key/value pairs from actual files: config.load = mock.Mock(side_effect=lambda: setattr(config, "ready", True)) config.ready = True return config
def test_register_duplicate_variable_raises(self): config = Configuration() config.register('num_participants', int) with pytest.raises(KeyError): config.register('num_participants', int)
def test_get_without_default_raises(self): config = Configuration() config.register('num_participants', int) config.ready = True with pytest.raises(KeyError): config.get('num_participants')
def test_setting_values_supports_synonyms(self): config = Configuration() config.register("num_participants", int, synonyms={"n"}) config.ready = True config.extend({"n": 1}) assert config.get("num_participants") == 1
def test_dict_access(self): config = Configuration() config.register("num_participants", int) config.ready = True config.extend({"num_participants": 1}) assert config["num_participants"] == 1
def test_strict_extending_blocks_unknown_keys(self): config = Configuration() config.register('num_participants', int) config.ready = True with pytest.raises(KeyError): config.extend({'unknown_key': 1}, strict=True)
def test_attribute_setting(self): config = Configuration() config.register('num_participants', int) config.ready = True config.num_participants = 1 assert config.num_participants == 1
def test_attribute_access(self): config = Configuration() config.register('num_participants', int) config.ready = True config.extend({'num_participants': 1}) assert config.num_participants == 1
def test_setting_by_set(self): config = Configuration() config.ready = True config.set("mode", "live")
def test_register_unknown_type_raises(self): config = Configuration() with pytest.raises(TypeError): config.register('num_participants', object)
def test_setting_by_assignment(self): config = Configuration() config.ready = True config["mode"] = "live"
def test_type_mismatch(self): config = Configuration() config.register('num_participants', int) with pytest.raises(TypeError): config.extend({'num_participants': 1.0})
def test_get_has_default_value(self): config = Configuration() config.register('num_participants', int) config.ready = True assert config.get('num_participants', 10) == 10
def test_type_mismatch_with_cast_types(self): config = Configuration() config.register('num_participants', int) config.ready = True config.extend({'num_participants': 1.0}, cast_types=True) assert config.get('num_participants', 1) == 1
def test_register_new_variable(self): config = Configuration() config.register('num_participants', int) config.extend({'num_participants': 1}) config.ready = True assert config.get('num_participants', 1)
def test_type_mismatch_on_extend(self): config = Configuration() config.register("num_participants", int) with pytest.raises(TypeError): config.extend({"num_participants": 1.0})