|
1 | | -from bec_lib.device import DeviceBase |
2 | | -from bec_lib.utils.import_utils import isinstance_based_on_class_name |
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +from types import SimpleNamespace |
3 | 5 |
|
| 6 | +from bec_lib.utils import import_utils |
4 | 7 |
|
5 | | -def test_isinstance_based_on_class_name(): |
6 | | - obj = DeviceBase(name="test_obj") |
7 | 8 |
|
8 | | - assert isinstance_based_on_class_name(obj, "bec_lib.device.DeviceBase") |
9 | | - assert not isinstance_based_on_class_name(obj, "bec_lib.device.Status") |
| 9 | +def _clean_pythonpath() -> str: |
| 10 | + return os.pathsep.join(str(path) for path in sys.path if path) |
| 11 | + |
| 12 | + |
| 13 | +def test_lazy_import_from_accepts_string_input(): |
| 14 | + json_decoder = import_utils.lazy_import_from("json", "JSONDecoder") |
| 15 | + assert json_decoder.__name__ == "JSONDecoder" |
| 16 | + |
| 17 | + |
| 18 | +def test_lazy_import_from_single_tuple_returns_single_proxy(): |
| 19 | + json_decoder = import_utils.lazy_import_from("json", ("JSONDecoder",)) |
| 20 | + assert json_decoder.__name__ == "JSONDecoder" |
| 21 | + |
| 22 | + |
| 23 | +def test_lazy_import_from_multiple_names_returns_tuple(): |
| 24 | + proxies = import_utils.lazy_import_from("json", ("JSONDecoder", "JSONEncoder")) |
| 25 | + assert isinstance(proxies, tuple) |
| 26 | + assert [proxy.__name__ for proxy in proxies] == ["JSONDecoder", "JSONEncoder"] |
| 27 | + |
| 28 | + |
| 29 | +def test_lazy_import_from_materializes_once(monkeypatch): |
| 30 | + calls = [] |
| 31 | + |
| 32 | + def fake_import(module_name): |
| 33 | + calls.append(module_name) |
| 34 | + return SimpleNamespace(DemoClass=type("DemoClass", (), {})) |
| 35 | + |
| 36 | + monkeypatch.setattr(import_utils, "import_module", fake_import) |
| 37 | + |
| 38 | + demo_class = import_utils.lazy_import_from("demo.module", "DemoClass") |
| 39 | + assert demo_class.__name__ == "DemoClass" |
| 40 | + assert demo_class.__name__ == "DemoClass" |
| 41 | + assert calls == ["demo.module"] |
| 42 | + |
| 43 | + |
| 44 | +def test_lazy_import_does_not_import_module_until_use(tmp_path, monkeypatch): |
| 45 | + module_name = "lazy_target_module" |
| 46 | + module_path = tmp_path / "lazy_target_module.py" |
| 47 | + module_path.write_text("VALUE = 123\n", encoding="utf-8") |
| 48 | + monkeypatch.syspath_prepend(str(tmp_path)) |
| 49 | + sys.modules.pop(module_name, None) |
| 50 | + |
| 51 | + mod = import_utils.lazy_import(module_name) |
| 52 | + |
| 53 | + assert module_name not in sys.modules |
| 54 | + assert mod.VALUE == 123 |
| 55 | + assert module_name in sys.modules |
| 56 | + |
| 57 | + |
| 58 | +def test_lazy_import_from_does_not_import_module_until_use(tmp_path, monkeypatch): |
| 59 | + module_name = "lazy_from_target_module" |
| 60 | + module_path = tmp_path / "lazy_from_target_module.py" |
| 61 | + module_path.write_text( |
| 62 | + "class DemoClass:\n" |
| 63 | + " VALUE = 456\n", |
| 64 | + encoding="utf-8", |
| 65 | + ) |
| 66 | + monkeypatch.syspath_prepend(str(tmp_path)) |
| 67 | + sys.modules.pop(module_name, None) |
| 68 | + |
| 69 | + demo_cls = import_utils.lazy_import_from(module_name, "DemoClass") |
| 70 | + |
| 71 | + assert module_name not in sys.modules |
| 72 | + assert demo_cls.VALUE == 456 |
| 73 | + assert module_name in sys.modules |
| 74 | + |
| 75 | + |
| 76 | +def test_importing_import_utils_does_not_import_scan_utils(): |
| 77 | + # This needs a clean interpreter because sys.modules is shared by the test process. |
| 78 | + env = os.environ | {"PYTHONPATH": _clean_pythonpath()} |
| 79 | + proc = subprocess.run( |
| 80 | + [ |
| 81 | + sys.executable, |
| 82 | + "-c", |
| 83 | + "from bec_lib.utils.import_utils import lazy_import_from; import sys; " |
| 84 | + "print('bec_lib.utils.scan_utils' in sys.modules)", |
| 85 | + ], |
| 86 | + check=True, |
| 87 | + capture_output=True, |
| 88 | + text=True, |
| 89 | + env=env, |
| 90 | + ) |
| 91 | + assert proc.stdout.strip() == "False" |
0 commit comments