Skip to content

Commit

Permalink
refactor compose tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Mar 5, 2021
1 parent 5647661 commit 7e7832f
Showing 1 changed file with 64 additions and 55 deletions.
119 changes: 64 additions & 55 deletions tests/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
chdir_hydra_root()


@fixture
def initialize_hydra(config_path: Optional[str]) -> Any:
try:
init = initialize(config_path=config_path)
init.__enter__()
yield
finally:
init.__exit__(*sys.exc_info())


@fixture
def initialize_hydra_no_path() -> Any:
try:
init = initialize()
init.__enter__()
yield
finally:
init.__exit__(*sys.exc_info())


def test_initialize(hydra_restore_singletons: Any) -> None:
assert not GlobalHydra().is_initialized()
initialize(config_path=None)
Expand All @@ -46,7 +66,7 @@ def test_initialize_with_config_path(hydra_restore_singletons: Any) -> None:
assert idx != -1


@mark.usefixtures("hydra_restore_singletons")
@mark.usefixtures("initialize_hydra")
@mark.parametrize("config_path", ["../hydra/test_utils/configs"])
@mark.parametrize(
"config_file, overrides, expected",
Expand All @@ -59,20 +79,21 @@ def test_initialize_with_config_path(hydra_restore_singletons: Any) -> None:
)
class TestCompose:
def test_compose_config(
self, config_path: str, config_file: str, overrides: List[str], expected: Any
self,
config_file: str,
overrides: List[str],
expected: Any,
) -> None:
with initialize(config_path=config_path):
cfg = compose(config_file, overrides)
assert cfg == expected
cfg = compose(config_file, overrides)
assert cfg == expected

def test_strict_failure_global_strict(
self, config_path: str, config_file: str, overrides: List[str], expected: Any
self, config_file: str, overrides: List[str], expected: Any
) -> None:
with initialize(config_path=config_path):
# default strict True, call is unspecified
overrides.append("fooooooooo=bar")
with raises(HydraException):
compose(config_file, overrides)
# default strict True, call is unspecified
overrides.append("fooooooooo=bar")
with raises(HydraException):
compose(config_file, overrides)


@mark.usefixtures("hydra_restore_singletons")
Expand Down Expand Up @@ -297,6 +318,7 @@ def test_initialization_root_module(monkeypatch: Any) -> None:
subprocess.check_call([sys.executable, "-m", "main"])


@mark.usefixtures("initialize_hydra_no_path")
@mark.parametrize(
("overrides", "expected"),
[
Expand All @@ -314,57 +336,55 @@ class Config:

ConfigStore.instance().store(name="config", node=Config)

with initialize():
cfg = compose(config_name="config", overrides=overrides)
assert cfg == expected
cfg = compose(config_name="config", overrides=overrides)
assert cfg == expected


@mark.usefixtures("hydra_restore_singletons")
@mark.usefixtures("initialize_hydra_no_path")
class TestAdd:
def test_add(self) -> None:
ConfigStore.instance().store(name="config", node={"key": 0})
with initialize():
with raises(
ConfigCompositionException,
match="Could not append to config. An item is already at 'key'",
):
compose(config_name="config", overrides=["+key=value"])
with raises(
ConfigCompositionException,
match="Could not append to config. An item is already at 'key'",
):
compose(config_name="config", overrides=["+key=value"])

cfg = compose(config_name="config", overrides=["key=1"])
assert cfg == {"key": 1}
cfg = compose(config_name="config", overrides=["key=1"])
assert cfg == {"key": 1}

def test_force_add(self) -> None:
ConfigStore.instance().store(name="config", node={"key": 0})
with initialize():
cfg = compose(config_name="config", overrides=["++key=1"])
assert cfg == {"key": 1}
cfg = compose(config_name="config", overrides=["++key=1"])
assert cfg == {"key": 1}

cfg = compose(config_name="config", overrides=["++key2=1"])
assert cfg == {"key": 0, "key2": 1}
cfg = compose(config_name="config", overrides=["++key2=1"])
assert cfg == {"key": 0, "key2": 1}

def test_add_config_group(self) -> None:
ConfigStore.instance().store(group="group", name="a0", node={"key": 0})
ConfigStore.instance().store(group="group", name="a1", node={"key": 1})
with initialize():
# overriding non existing group throws
with raises(ConfigCompositionException):
compose(overrides=["group=a0"])

# appending a new group
cfg = compose(overrides=["+group=a0"])
assert cfg == {"group": {"key": 0}}

# force adding is not supported for config groups.
with raises(
ConfigCompositionException,
match=re.escape(
"force-add of config groups is not supported: '++group=a1'"
),
):
compose(overrides=["++group=a1"])
# overriding non existing group throws
with raises(ConfigCompositionException):
compose(overrides=["group=a0"])

# appending a new group
cfg = compose(overrides=["+group=a0"])
assert cfg == {"group": {"key": 0}}

# force adding is not supported for config groups.
with raises(
ConfigCompositionException,
match=re.escape(
"force-add of config groups is not supported: '++group=a1'"
),
):
compose(overrides=["++group=a1"])


@mark.usefixtures("hydra_restore_singletons")
@mark.usefixtures("initialize_hydra_no_path")
class TestConfigSearchPathOverride:
@fixture
def init_configs(self) -> Any:
Expand All @@ -387,15 +407,6 @@ def init_configs(self) -> Any:
)
yield

@fixture
def initialize_hydra(self) -> Any:
try:
init = initialize()
init.__enter__()
yield
finally:
init.__exit__(*sys.exc_info())

@mark.parametrize(
("config_name", "overrides", "expected"),
[
Expand All @@ -419,7 +430,6 @@ def initialize_hydra(self) -> Any:
)
def test_searchpath_in_primary_config(
self,
initialize_hydra: Any,
init_configs: Any,
config_name: str,
overrides: List[str],
Expand Down Expand Up @@ -491,7 +501,6 @@ def test_searchpath_in_primary_config(
)
def test_searchpath_config_errors(
self,
initialize_hydra: Any,
init_configs: Any,
config_name: str,
overrides: List[str],
Expand Down

0 comments on commit 7e7832f

Please sign in to comment.