Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
77 lines (63 loc) · 2.8 KB
/
Copy pathconftest.py
File metadata and controls
77 lines (63 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Root conftest for R2X workspace.
Handles getter registry cleanup to avoid collisions between packages.
r2x_core uses a global GETTER_REGISTRY dict. Multiple workspace packages
register getters with identical names (e.g., 'is_slack_bus', 'get_availability').
We clear the registry and reload the correct getter module when tests
transition between packages.
This is a workaround for a design limitation in r2x_core. The proper fix
would be to namespace getters by plugin/package in r2x_core itself.
"""
import importlib
import sys
from r2x_core.getters import GETTER_REGISTRY
# ---------------------------------------------------------------------------
# Fixture plugins
# ---------------------------------------------------------------------------
# Pytest 8.4+ requires pytest_plugins to be declared only in the root conftest.
pytest_plugins = [
"fixtures.configs",
"fixtures.context",
"fixtures.five_bus_systems",
"fixtures.getters_utils",
"fixtures.rules",
"fixtures.systems",
"fixtures.time_series",
]
# ---------------------------------------------------------------------------
# Getter registry management
# ---------------------------------------------------------------------------
# Map from test directory to the getter module(s) that should be active.
_PACKAGE_GETTER_MODULES = {
"r2x-sienna-to-plexos": ["r2x_sienna_to_plexos.getters"],
"r2x-plexos-to-sienna": ["r2x_plexos_to_sienna.getters"],
"r2x-reeds-to-plexos": ["r2x_reeds_to_plexos.getters"],
"r2x-reeds-to-sienna": ["r2x_reeds_to_sienna.getters"],
}
# Track which package was last loaded so we only reload on transitions.
_last_package: str | None = None
def pytest_collectstart(collector):
"""Clear getter registry before each collector starts."""
GETTER_REGISTRY.clear()
def pytest_runtest_setup(item):
"""Ensure the correct getter module is loaded for each test.
Translation tests call apply_rules_to_context which looks up getters
by name from GETTER_REGISTRY. If multiple packages have been imported,
the wrong getters may be active. We detect the owning package from
the test path and reload the correct getters module.
"""
global _last_package
fspath = str(item.fspath)
for pkg_name, module_names in _PACKAGE_GETTER_MODULES.items():
if pkg_name in fspath:
if pkg_name != _last_package:
GETTER_REGISTRY.clear()
for mod_name in module_names:
try:
if mod_name in sys.modules:
importlib.reload(sys.modules[mod_name])
else:
importlib.import_module(mod_name)
except (ImportError, ModuleNotFoundError):
pass
_last_package = pkg_name
break