def test_deprecate_option(caplog): """Test deprecating an option.""" config = Configuration() config.add_option('option', option_type=str, default='hello', deprecate=dict(version='v1.0', alternative='None! T_T')) config.add_option('ok', option_type=str, default='hillo') # Access the deprecated option and trigger a warning. with caplog.at_level(logging.WARNING, logger="orion.core.io.config"): assert config.option == 'hello' assert caplog.record_tuples == [( 'orion.core.io.config', logging.WARNING, '(DEPRECATED) Option `option` will be removed in v1.0. Use `None! T_T` instead.' )] caplog.clear() # Access the non-deprecated option and trigger no warnings. with caplog.at_level(logging.WARNING, logger="orion.core.io.config"): assert config.ok == 'hillo' assert caplog.record_tuples == []
def test_deprecate_option(caplog): """Test deprecating an option.""" config = Configuration() config.add_option( "option", option_type=str, default="hello", deprecate=dict(version="v1.0", alternative="None! T_T"), ) config.add_option("ok", option_type=str, default="hillo") # Access the deprecated option and trigger a warning. with caplog.at_level(logging.WARNING, logger="orion.core.io.config"): assert config.option == "hello" assert caplog.record_tuples == [( "orion.core.io.config", logging.WARNING, "(DEPRECATED) Option `option` will be removed in v1.0. Use `None! T_T` instead.", )] caplog.clear() # Access the non-deprecated option and trigger no warnings. with caplog.at_level(logging.WARNING, logger="orion.core.io.config"): assert config.ok == "hillo" assert caplog.record_tuples == []
def define_config(): """Create and define the fields of the configuration object.""" config = Configuration() define_storage_config(config) define_experiment_config(config) define_worker_config(config) define_evc_config(config) define_frontends_uri_config(config) config.add_option( "user_script_config", option_type=str, default="config", deprecate=dict(version="v0.3", alternative="worker.user_script_config"), ) config.add_option( "debug", option_type=bool, default=False, help= "Turn Oríon into debug mode. Storage will be overriden to in-memory EphemeralDB.", ) return config
def test_access_config_without_values(): """Test that access to config without values raises ConfigurationError""" config = Configuration() config.add_option('test', option_type=int) with pytest.raises(ConfigurationError) as exc: config.test assert 'Configuration not set and no default provided: test.' in str(exc.value)
def test_yaml_precedence(yaml_path): """Test that yaml definition has precedence over default values""" config = Configuration() config.add_option('test', option_type=str, default="voici_voila", env_var="TOP_SECRET_MESSAGE") assert config.test == "voici_voila" config.load_yaml(yaml_path) assert config.test == "from my yaml!"
def test_help_option_with_default(): """Verify adding documentation to options with default value.""" config = Configuration() config.add_option("option", option_type=str, default="a", help="A useless option!") assert config.help("option") == "A useless option! (default: a)"
def test_deprecate_option_missing_version(): """Verify option deprecation if version is missing.""" config = Configuration() with pytest.raises(ValueError) as exc: config.add_option("option", option_type=str, deprecate=dict(alternative="None! T_T")) assert exc.match("`version` is missing in deprecate option")
def test_set_subconfig_over_option(): """Test that overwritting an option with a subconfig is not possible""" config = Configuration() config.add_option('test', option_type=int) config.test = 1 assert config.test == 1 with pytest.raises(TypeError) as exc: config.test = Configuration() assert "Cannot overwrite option test with a configuration" in str(exc.value)
def define_config(): """Create and define the fields of the configuration object.""" config = Configuration() define_database_config(config) define_worker_config(config) config.add_option('user_script_config', option_type=str, default='config') return config
def test_deprecate_option_help(): """Verify help message of a deprecated option.""" config = Configuration() config.add_option('option', option_type=str, deprecate=dict(version='v1.0', alternative='None! T_T'), help='A useless option!') assert config.help('option') == '(DEPRECATED) A useless option!'
def test_help_option_with_default(): """Verify adding documentation to options with default value.""" config = Configuration() config.add_option('option', option_type=str, default='a', help='A useless option!') assert config.help('option') == 'A useless option! (default: a)'
def test_load_yaml_unknown_option(broken_yaml_path): """Test error message when yaml config contains unknown options""" config = Configuration() config.add_option("test", option_type=str, default="hello") assert config.test == "hello" with pytest.raises(ConfigurationError) as exc: config.load_yaml(broken_yaml_path) assert exc.match("Configuration does not have an attribute 'coucou'")
def test_env_var_list(yaml_path): """Test that env_var lists are correctly handled""" config = Configuration() config.add_option("test", option_type=list, default=["voici"], env_var="TOP_SECRET_LIST") assert config.test == ["voici"] os.environ["TOP_SECRET_LIST"] = "voila:voici:voila" assert config.test == ["voila", "voici", "voila"]
def test_help_nested_option(): """Verify adding documentation to a nested option.""" config = Configuration() config.add_option('option', option_type=str, help='A useless option!') config.nested = Configuration() config.nested.add_option('option', option_type=str, help='A useless nested option!') assert config.help('nested.option') == 'A useless nested option!' assert config.nested.help('option') == 'A useless nested option!'
def test_deprecate_option_help(): """Verify help message of a deprecated option.""" config = Configuration() config.add_option( "option", option_type=str, deprecate=dict(version="v1.0", alternative="None! T_T"), help="A useless option!", ) assert config.help("option") == "(DEPRECATED) A useless option!"
def test_argument_parser_ignore_default(): """Verify the argument parser does not get default values.""" config = Configuration() config.add_option("option", option_type=str, default="b") parser = argparse.ArgumentParser() config.add_arguments(parser) options = parser.parse_args([]) assert options.option is None
def test_argument_parser(): """Verify the argument parser built based on config.""" config = Configuration() config.add_option("option", option_type=str) parser = argparse.ArgumentParser() config.add_arguments(parser) options = parser.parse_args(["--option", "a"]) assert options.option == "a"
def test_help_nested_option(): """Verify adding documentation to a nested option.""" config = Configuration() config.add_option("option", option_type=str, help="A useless option!") config.nested = Configuration() config.nested.add_option("option", option_type=str, help="A useless nested option!") assert config.help("nested.option") == "A useless nested option!" assert config.nested.help("option") == "A useless nested option!"
def define_storage_config(config): """Create and define the fields of the storage configuration.""" storage_config = Configuration() storage_config.add_option( 'type', option_type=str, default='legacy', env_var='ORION_STORAGE_TYPE') config.storage = storage_config define_database_config(config.storage) # Backward compatibility, should be removed in v0.3.0, or not? config.database = config.storage.database
def test_set_str_value(): """Test that a string option can have its value set""" config = Configuration() config.add_option("test", option_type=str) with pytest.raises(ConfigurationError): config.test config.test = "1" assert config.test == "1" config.test = 1 assert config.test == "1"
def test_set_value_like_dict(): """Test that we can set values like a dictionary""" config = Configuration() config.add_option("test", option_type=str) with pytest.raises(ConfigurationError): config.test config["test"] = "1" assert config.test == "1" config["test"] = 1 assert config.test == "1"
def test_overwrite_subconfig(): """Test that subconfig cannot be overwritten""" config = Configuration() config.nested = Configuration() with pytest.raises(ValueError) as exc: config.add_option("nested", option_type=str) assert "Configuration already contains nested" == str(exc.value) with pytest.raises(ValueError) as exc: config.nested = Configuration() assert "Configuration already contains subconfiguration nested" == str( exc.value)
def test_get_deprecated_key_ignore_warning(caplog): """Verify deprecation warning using get(deprecated='ignore').""" config = Configuration() config.add_option('option', option_type=str, default='hello', deprecate=dict(version='v1.0', alternative='None! T_T')) # Access the deprecated option and trigger a warning. with caplog.at_level(logging.WARNING, logger="orion.core.io.config"): assert config.get('option', deprecated='ignore') == 'hello' assert caplog.record_tuples == []
def test_load_yaml_with_dict_items(subdict_yaml_path): """Test that yaml config with items assigned with dicts is supported""" config = Configuration() default = {"default": "sub-dict"} config.add_option("test", option_type=dict, default=default) assert config.test == default config.test2 = Configuration() config.test2.add_option("test", option_type=dict, default=default) assert config.test2.test == default config.load_yaml(subdict_yaml_path) assert config.test == {"i am": "a sub-dict"} assert config.test2.test == {"me is": "sub-conf sub-dict"}
def define_database_config(config): """Create and define the fields of the database configuration.""" database_config = Configuration() try: default_host = socket.gethostbyname(socket.gethostname()) except socket.gaierror: default_host = 'localhost' database_config.add_option('name', option_type=str, default='orion', env_var='ORION_DB_NAME') database_config.add_option('type', option_type=str, default='MongoDB', env_var='ORION_DB_TYPE') database_config.add_option('host', option_type=str, default=default_host, env_var='ORION_DB_ADDRESS') database_config.add_option('port', option_type=int, default=27017, env_var='ORION_DB_PORT') config.database = database_config
def test_env_var_precedence(yaml_path): """Test that env_var has precedence over yaml values""" config = Configuration() config.add_option('test', option_type=str, default="voici_voila", env_var="TOP_SECRET_MESSAGE") assert config.test == "voici_voila" config.load_yaml(yaml_path) assert config.test == "from my yaml!" os.environ['TOP_SECRET_MESSAGE'] = 'coussi_coussa' assert config.test == "coussi_coussa" del os.environ['TOP_SECRET_MESSAGE'] assert config.test == "from my yaml!"
def define_database_config(config): """Create and define the fields of the database configuration.""" database_config = Configuration() database_config.add_option( "name", option_type=str, default="orion", env_var="ORION_DB_NAME", help="Name of the database.", ) database_config.add_option( "type", option_type=str, default="PickledDB", env_var="ORION_DB_TYPE", help=("Type of database. Builtin backends are ``mongodb``, " "``pickleddb`` and ``ephemeraldb``."), ) database_config.add_option( "host", option_type=str, default="", env_var="ORION_DB_ADDRESS", help="URI for ``mongodb``, or file path for ``pickleddb``.", ) database_config.add_option( "port", option_type=int, default=27017, env_var="ORION_DB_PORT", help="Port address for ``mongodb``.", ) config.database = database_config
def test_get_deprecated_key_ignore_warning(caplog): """Verify deprecation warning using get(deprecated='ignore').""" config = Configuration() config.add_option( "option", option_type=str, default="hello", deprecate=dict(version="v1.0", alternative="None! T_T"), ) # Access the deprecated option and trigger a warning. with caplog.at_level(logging.WARNING, logger="orion.core.io.config"): assert config.get("option", deprecated="ignore") == "hello" assert caplog.record_tuples == []
def test_set_real_value(): """Test that a float option can have its value set""" config = Configuration() config.add_option('test', option_type=float) with pytest.raises(ConfigurationError) as exc: config.test config.test = 1 assert config.test == 1.0 config.test = "1" assert config.test == 1.0 with pytest.raises(TypeError) as exc: config.test = "voici_voila" assert "<class 'float'> cannot be set to voici_voila with type <class 'str'>" in str(exc.value)
def test_argument_parser_rename(): """Verify the argument parser built based on config with some options renamed.""" config = Configuration() config.add_option("option", option_type=str) parser = argparse.ArgumentParser() config.add_arguments(parser, rename=dict(option="--new-option")) with pytest.raises(SystemExit) as exc: options = parser.parse_args(["--option", "a"]) assert exc.match("2") options = parser.parse_args(["--new-option", "a"]) assert options.new_option == "a"