Skip to content

Commit 276844b

Browse files
committed
test: delay import of dandischema.conf in test_conf.py
Without this delay, the `dandischema.conf` and its contained definitions initialized after the execution of the `clear_dandischema_modules_and_set_env_vars` fixture is not accessed in the tests.
1 parent e712e4d commit 276844b

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

dandischema/tests/test_conf.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import logging
2-
from typing import Optional, Union
2+
from typing import Optional
33
from unittest.mock import ANY
44

55
import pytest
66

7-
from dandischema.conf import (
8-
Config,
9-
_instance_config,
10-
get_instance_config,
11-
set_instance_config,
12-
)
13-
147

158
def test_get_instance_config() -> None:
9+
from dandischema.conf import _instance_config, get_instance_config
1610

1711
obtained_config = get_instance_config()
1812

@@ -34,45 +28,51 @@ class TestSetInstanceConfig:
3428
[
3529
(FOO_CONFIG_DICT, {"instance_name": "BAR"}),
3630
(
37-
Config.model_validate(FOO_CONFIG_DICT),
31+
FOO_CONFIG_DICT,
3832
{"instance_name": "Baz", "key": "value"},
3933
),
4034
],
4135
)
42-
def test_invalid_args(self, arg: Union[Config, dict], kwargs: dict) -> None:
36+
def test_invalid_args(self, arg: dict, kwargs: dict) -> None:
4337
"""
4438
Test that `set_instance_config` raises a `ValueError` when called with both
4539
a non-none positional argument and one or more keyword arguments.
4640
"""
41+
from dandischema.conf import Config, set_instance_config
4742

48-
with pytest.raises(ValueError, match="not both"):
49-
set_instance_config(arg, **kwargs)
43+
# Loop over arg in different types/forms
44+
for arg_ in (arg, Config.model_validate(arg)):
45+
with pytest.raises(ValueError, match="not both"):
46+
set_instance_config(arg_, **kwargs)
5047

5148
@pytest.mark.parametrize(
5249
("clear_dandischema_modules_and_set_env_vars", "arg", "kwargs"),
5350
[
5451
({}, FOO_CONFIG_DICT, {}),
55-
({}, Config.model_validate(FOO_CONFIG_DICT), {}),
52+
({}, FOO_CONFIG_DICT, {}),
5653
({}, None, FOO_CONFIG_DICT),
5754
],
5855
indirect=["clear_dandischema_modules_and_set_env_vars"],
5956
)
6057
def test_before_models_import(
6158
self,
6259
clear_dandischema_modules_and_set_env_vars: None,
63-
arg: Optional[Union[Config, dict]],
60+
arg: Optional[dict],
6461
kwargs: dict,
6562
) -> None:
6663
"""
6764
Test setting the instance configuration before importing `dandischema.models`.
6865
"""
6966

7067
# Import entities in `dandischema.conf` after clearing dandischema modules
68+
from dandischema.conf import Config, get_instance_config, set_instance_config
7169

72-
set_instance_config(arg, **kwargs)
73-
assert get_instance_config() == Config.model_validate(
74-
FOO_CONFIG_DICT
75-
), "Configuration values are not set to the expected values"
70+
# Loop over arg in different types/forms
71+
for arg_ in (arg, Config.model_validate(arg)) if arg is not None else (arg,):
72+
set_instance_config(arg_, **kwargs)
73+
assert get_instance_config() == Config.model_validate(
74+
FOO_CONFIG_DICT
75+
), "Configuration values are not set to the expected values"
7676

7777
@pytest.mark.parametrize(
7878
"clear_dandischema_modules_and_set_env_vars",
@@ -88,6 +88,8 @@ def test_after_models_import_same_config(
8888
Test setting the instance configuration after importing `dandischema.models`
8989
with the same configuration.
9090
"""
91+
from dandischema.conf import Config, get_instance_config, set_instance_config
92+
9193
# Make sure the `dandischema.models` module is imported before calling
9294
# `set_instance_config`
9395
import dandischema.models # noqa: F401
@@ -129,6 +131,8 @@ def test_after_models_import_different_config(
129131
Test setting the instance configuration after importing `dandischema.models`
130132
with a different configuration.
131133
"""
134+
from dandischema.conf import Config, get_instance_config, set_instance_config
135+
132136
# Make sure the `dandischema.models` module is imported before calling
133137
# `set_instance_config`
134138
import dandischema.models # noqa: F401

0 commit comments

Comments
 (0)