Exemple #1
0
def test_load_schema_as_config(restore_singletons: Any) -> None:
    """
    Load structured config as a configuration
    """
    ConfigStore.instance().store(
        group="db",
        name="mysql",
        node=MySQLConfig,
        provider="test_provider",
    )

    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(None))
    cfg = config_loader.load_configuration(config_name="db/mysql",
                                           overrides=[])
    with open_dict(cfg):
        del cfg["hydra"]
    assert cfg == {
        "db": {
            "driver": MISSING,
            "host": MISSING,
            "port": MISSING,
            "user": MISSING,
            "password": MISSING,
        }
    }

    expected = hydra_load_list.copy()
    expected.extend(
        [LoadTrace("db/mysql", "structured://", "test_provider", None)])
    assert config_loader.get_load_history() == expected
def test_override_hydra_config_group_from_config_file() -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(
            "hydra/test_utils/configs"))

    config_loader.load_configuration(
        config_name="overriding_logging_default.yaml",
        overrides=[],
        strict=False,
        run_mode=RunMode.RUN,
    )

    # This load history is too different to easily reuse the standard hydra_load_list
    assert config_loader.get_load_history() == [
        LoadTrace("hydra_config", "structured://", "hydra", None),
        LoadTrace("hydra/hydra_logging/hydra_debug", "pkg://hydra.conf",
                  "hydra", None),
        LoadTrace("hydra/job_logging/disabled", "pkg://hydra.conf", "hydra",
                  None),
        LoadTrace("hydra/sweeper/basic", "structured://", "hydra", None),
        LoadTrace("hydra/output/default", "pkg://hydra.conf", "hydra", None),
        LoadTrace("hydra/help/default", "pkg://hydra.conf", "hydra", None),
        LoadTrace("hydra/hydra_help/default", "pkg://hydra.conf", "hydra",
                  None),
        LoadTrace(
            "overriding_logging_default.yaml",
            "file://hydra/test_utils/configs",
            "main",
            None,
        ),
    ]
Exemple #3
0
    def test_load_config_file_with_schema_validation(self,
                                                     restore_singletons: Any,
                                                     path: str) -> None:

        with ConfigStoreWithProvider("test_provider") as config_store:
            config_store.store(group="db",
                               name="mysql",
                               node=MySQLConfig,
                               package="db")

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))
        cfg = config_loader.load_configuration(config_name="db/mysql",
                                               overrides=[],
                                               strict=False)

        with open_dict(cfg):
            del cfg["hydra"]
        assert cfg == {
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            }
        }

        expected = hydra_load_list.copy()
        expected.append(LoadTrace("db/mysql", path, "main", "test_provider"))
        assert config_loader.get_load_history() == expected
Exemple #4
0
def test_mixed_composition_order() -> None:
    """
    Tests that the order of mixed composition (defaults contains both config group and non config group
    items) is correct
    """
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )
    config_loader.load_configuration(
        config_file="mixed_compose.yaml", overrides=[], strict=False
    )
    assert config_loader.get_load_history() == [
        ("hydra.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_logging/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/job_logging/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/launcher/basic.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/help/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_help/default.yaml", "pkg://hydra.conf", "hydra"),
        ("mixed_compose.yaml", "file://hydra/test_utils/configs", "main"),
        ("some_config.yaml", "file://hydra/test_utils/configs", "main"),
        ("group1/file1.yaml", "file://hydra/test_utils/configs", "main"),
        ("config.yaml", "file://hydra/test_utils/configs", "main"),
    ]
def test_mixed_composition_order() -> None:
    """
    Tests that the order of mixed composition (defaults contains both config group and non config group
    items) is correct
    """
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(
            "hydra/test_utils/configs"))
    config_loader.load_configuration(
        config_name="mixed_compose.yaml",
        overrides=[],
        strict=False,
        run_mode=RunMode.RUN,
    )

    expected = hydra_load_list.copy()
    expected.extend([
        LoadTrace("mixed_compose.yaml", "file://hydra/test_utils/configs",
                  "main", None),
        LoadTrace("some_config", "file://hydra/test_utils/configs", "main",
                  None),
        LoadTrace("group1/file1", "file://hydra/test_utils/configs", "main",
                  None),
        LoadTrace("config", "file://hydra/test_utils/configs", "main", None),
    ])

    assert config_loader.get_load_history() == expected
    def test_load_config_file_with_schema_validation(
            self, hydra_restore_singletons: Any, path: str) -> None:

        with ConfigStoreWithProvider("this_test") as cs:
            cs.store(name="config", node=TopLevelConfig)
            cs.store(group="db", name="mysql", node=MySQLConfig, package="db")

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))
        cfg = config_loader.load_configuration(
            config_name="config",
            overrides=["+db=mysql"],
            strict=False,
            run_mode=RunMode.RUN,
        )

        with open_dict(cfg):
            del cfg["hydra"]
        assert cfg == {
            "normal_yaml_config": True,
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            },
        }

        expected = hydra_load_list.copy()
        expected.append(LoadTrace("config", path, "main", "this_test"))
        expected.append(LoadTrace("db/mysql", path, "main", "this_test"))
        assert config_loader.get_load_history() == expected
def test_load_schema_as_config(hydra_restore_singletons: Any) -> None:
    """
    Load structured config as a configuration
    """
    ConfigStore.instance().store(name="config",
                                 node=TopLevelConfig,
                                 provider="this_test")

    ConfigStore.instance().store(
        name="db/mysql",
        node=MySQLConfig,
        provider="this_test",
    )

    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(None))
    cfg = config_loader.load_configuration(config_name="config",
                                           overrides=[],
                                           run_mode=RunMode.RUN)
    with open_dict(cfg):
        del cfg["hydra"]
    assert cfg == {
        "normal_yaml_config": "???",
        "db": {
            "driver": MISSING,
            "host": MISSING,
            "port": MISSING,
            "user": MISSING,
            "password": MISSING,
        },
    }

    expected = hydra_load_list.copy()
    expected.extend([LoadTrace("config", "structured://", "this_test", None)])
    assert config_loader.get_load_history() == expected
Exemple #8
0
    def test_load_history_with_basic_launcher(self, path: str) -> None:
        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))
        config_loader.load_configuration(
            config_name="custom_default_launcher.yaml",
            overrides=["hydra/launcher=basic"],
            strict=False,
        )

        expected = hydra_load_list.copy()
        expected.append(("custom_default_launcher.yaml", path, "main", None))
        assert config_loader.get_load_history() == expected
Exemple #9
0
    def test_load_history(self, path: str) -> None:
        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))
        config_loader.load_configuration(
            config_name="missing-optional-default.yaml",
            overrides=[],
            strict=False)
        expected = hydra_load_list.copy()
        expected.append(("missing-optional-default.yaml", path, "main", None))
        expected.append(("foo/missing", None, None, None))

        assert config_loader.get_load_history() == expected
Exemple #10
0
def test_default_removal(config_file: str, overrides: List[str]) -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )
    config_loader.load_configuration(
        config_name=config_file, overrides=overrides, strict=False
    )

    expected = list(
        filter(lambda x: x[0] != "hydra/launcher/basic", hydra_load_list.copy())
    )
    expected.extend([(config_file, "file://hydra/test_utils/configs", "main", None)])
    assert config_loader.get_load_history() == expected
    def test_load_config_with_schema(self, hydra_restore_singletons: Any,
                                     path: str) -> None:

        ConfigStore.instance().store(name="config",
                                     node=TopLevelConfig,
                                     provider="this_test")
        ConfigStore.instance().store(
            group="db",
            name="mysql",
            node=MySQLConfig,
            provider="this_test",
        )

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))

        cfg = config_loader.load_configuration(config_name="config",
                                               overrides=["+db=mysql"],
                                               run_mode=RunMode.RUN)
        with open_dict(cfg):
            del cfg["hydra"]
        assert cfg == {
            "normal_yaml_config": True,
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            },
        }

        expected = hydra_load_list.copy()
        expected.append(LoadTrace("config", path, "main", "this_test"))
        expected.append(LoadTrace("db/mysql", path, "main", "this_test"))
        assert config_loader.get_load_history() == expected

        # verify illegal modification is rejected at runtime
        with pytest.raises(ValidationError):
            cfg.db.port = "fail"

        # verify illegal override is rejected during load
        with pytest.raises(HydraException):
            config_loader.load_configuration(config_name="db/mysql",
                                             overrides=["db.port=fail"],
                                             run_mode=RunMode.RUN)
Exemple #12
0
def test_default_removal(config_file: str, overrides: List[str]) -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )
    config_loader.load_configuration(
        config_file=config_file, overrides=overrides, strict=False
    )
    assert config_loader.get_load_history() == [
        ("hydra.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_logging/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/job_logging/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/help/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_help/default.yaml", "pkg://hydra.conf", "hydra"),
        (config_file, "file://hydra/test_utils/configs", "main"),
    ]
Exemple #13
0
def test_override_hydra_config_group_from_config_file() -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )

    config_loader.load_configuration(
        config_file="overriding_logging_default.yaml", overrides=[], strict=False
    )
    assert config_loader.get_load_history() == [
        ("hydra.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_logging/hydra_debug.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/job_logging/disabled.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/help/default.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_help/default.yaml", "pkg://hydra.conf", "hydra"),
        ("overriding_logging_default.yaml", "file://hydra/test_utils/configs", "main"),
    ]
Exemple #14
0
 def test_load_history(self, path: str) -> None:
     config_loader = ConfigLoaderImpl(
         config_search_path=create_config_search_path(path)
     )
     config_loader.load_configuration(
         config_file="missing-optional-default.yaml", overrides=[], strict=False
     )
     assert config_loader.get_load_history() == [
         ("hydra.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/hydra_logging/default.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/job_logging/default.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/launcher/basic.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/help/default.yaml", "pkg://hydra.conf", "hydra"),
         ("hydra/hydra_help/default.yaml", "pkg://hydra.conf", "hydra"),
         ("missing-optional-default.yaml", path, "main"),
         ("foo/missing.yaml", None, None),
     ]
Exemple #15
0
    def test_load_config_with_schema(
            self,
            restore_singletons: Any,
            path: str  # noqa: F811
    ) -> None:

        ConfigStore.instance().store(
            group="db",
            name="mysql",
            node=MySQLConfig,
            path="db",
            provider="test_provider",
        )

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))

        cfg = config_loader.load_configuration(config_name="db/mysql",
                                               overrides=[])
        del cfg["hydra"]
        assert cfg == {
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            }
        }

        expected = hydra_load_list.copy()
        expected.append(("db/mysql", path, "main", "test_provider"))
        assert config_loader.get_load_history() == expected

        # verify illegal modification is rejected at runtime
        with pytest.raises(ValidationError):
            cfg.db.port = "fail"

        # verify illegal override is rejected during load
        with pytest.raises(ValidationError):
            config_loader.load_configuration(config_name="db/mysql",
                                             overrides=["db.port=fail"])
Exemple #16
0
def test_non_config_group_default() -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(
            "hydra/test_utils/configs"))
    config_loader.load_configuration(
        config_name="non_config_group_default.yaml",
        overrides=[],
        strict=False)

    expected = hydra_load_list.copy()
    expected.extend([
        (
            "non_config_group_default.yaml",
            "file://hydra/test_utils/configs",
            "main",
            None,
        ),
        ("some_config", "file://hydra/test_utils/configs", "main", None),
    ])
    assert config_loader.get_load_history() == expected
Exemple #17
0
    def test_load_history_with_basic_launcher(self, path: str) -> None:
        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path)
        )
        config_loader.load_configuration(
            config_file="custom_default_launcher.yaml",
            overrides=["hydra/launcher=basic"],
            strict=False,
        )

        assert config_loader.get_load_history() == [
            ("hydra.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/hydra_logging/default.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/job_logging/default.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/launcher/basic.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/help/default.yaml", "pkg://hydra.conf", "hydra"),
            ("hydra/hydra_help/default.yaml", "pkg://hydra.conf", "hydra"),
            ("custom_default_launcher.yaml", path, "main"),
        ]