def test_set_non_existing_option(): """Test that setting a non existing option crash""" config = Configuration() with pytest.raises(TypeError) as exc: config.test = 1 assert "Can only set test as a Configuration, not <class 'int'>" in str( exc.value)
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 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_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_loading_empty_config(yaml_path): """Test loading for empty config fails like setting non existing attributes.""" config = Configuration() with pytest.raises(ConfigurationError) as exc: config.load_yaml(yaml_path) assert "Configuration does not have an attribute 'test'." in str(exc.value)
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_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(): """Test that setting a subconfig works""" config = Configuration() config.test = Configuration() assert isinstance(config.test, Configuration) with pytest.raises(ConfigurationError): config.test.voici_voila
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 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_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_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 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_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_value_of_subconfig_directly(): """Test that we can access subconfig and set value directly""" config = Configuration() config.sub = Configuration() config.sub.add_option("test", option_type=str) with pytest.raises(ConfigurationError): config.test config.sub.test = "1" assert config.sub.test == "1" config.sub.test = 1 assert config.sub.test == "1"
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_invalid_subconfig_value_like_dict(): """Test that deep keys cannot be set if subconfig does not exist""" config = Configuration() with pytest.raises(BaseException) as exc: config['sub.test'] = "1" assert 'Configuration does not have an attribute \'sub\'.' in str( exc.value)
def test_deprecate_option_no_alternative(caplog): """Verify option deprecation when there is no alternative.""" config = Configuration() config.add_option('option', option_type=str, default='hello', deprecate=dict(version='v1.0')) # 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.') ]
def test_get_deprecated_key(caplog): """Verify deprecation warning using get().""" 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') == '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.' )]
def test_access_to_config(): """Test that access to _config returns properly _config. This is because getattr() could grasp any key including `_config` and makes it impossible to access the later """ assert Configuration()._config == {}
def test_fetch_non_existing_option(): """Test that access to a non existing key raises ConfigurationError""" config = Configuration() with pytest.raises(ConfigurationError) as exc: config.voici_voila assert 'Configuration does not have an attribute \'voici_voila\'.' in str(exc.value)
def define_worker_config(config): """Create and define the fields of the worker configuration.""" worker_config = Configuration() worker_config.add_option('heartbeat', option_type=int, default=120) worker_config.add_option('max_broken', option_type=int, default=3) worker_config.add_option('max_idle_time', option_type=int, default=60) config.worker = worker_config
def test_argument_parser_dict_list_tuple(): """Verify the argument parser does not contain options of type dict/list/tuple in config.""" config = Configuration() config.add_option('st', option_type=str) config.add_option('di', option_type=dict) config.add_option('li', option_type=list) config.add_option('tu', option_type=tuple) parser = argparse.ArgumentParser() config.add_arguments(parser) options = parser.parse_args([]) assert vars(options) == {'st': None} with pytest.raises(SystemExit) as exc: options = parser.parse_args(['--di', 'ct']) assert exc.match('2')
def test_deprecate_option_print_with_different_name(caplog): """Verify deprecation warning with different name (for nested options).""" config = Configuration() config.add_option('option', option_type=str, default='hello', deprecate=dict(version='v1.0', alternative='None! T_T', name='nested.option')) # 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 `nested.option` will be removed in v1.0. Use `None! T_T` instead.' )]
def test_argument_parser_dict_list_tuple(): """Verify the argument parser does not contain options of type dict/list/tuple in config.""" config = Configuration() config.add_option("st", option_type=str) config.add_option("di", option_type=dict) config.add_option("li", option_type=list) config.add_option("tu", option_type=tuple) parser = argparse.ArgumentParser() config.add_arguments(parser) options = parser.parse_args([]) assert vars(options) == {"st": None} with pytest.raises(SystemExit) as exc: options = parser.parse_args(["--di", "ct"]) assert exc.match("2")
def test_get_deprecated_key(caplog): """Verify deprecation warning using get().""" 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") == "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.", )]
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_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)