From 96c0f7781224a2137d29ecf9182dcd342266e361 Mon Sep 17 00:00:00 2001 From: Omry Yadan Date: Thu, 4 Mar 2021 13:10:56 -0800 Subject: [PATCH] refactor compose tests --- tests/test_compose.py | 119 +++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/tests/test_compose.py b/tests/test_compose.py index d05368b5033..fab3b3b2181 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -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) @@ -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", @@ -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") @@ -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"), [ @@ -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: @@ -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"), [ @@ -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], @@ -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],