65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
"""Shared test setup.
|
|
|
|
Several modules under test import first-party packages (`graphCreation`,
|
|
`fhirImport`, `schema_config_generation`, `mdm2neo4j`) and heavy third-party
|
|
ones (`biocypher`, `dotenv`) at import time. ``_ensure`` uses the *real*
|
|
module when it can be imported and falls back to a lightweight stub only when
|
|
it can't. That way the same suite runs in a full checkout (everything present)
|
|
and in a minimal environment (only the module under test available). As you
|
|
add real packages, the stubs simply stop being used -- no edits needed here.
|
|
"""
|
|
|
|
import importlib
|
|
import sys
|
|
import types
|
|
from pathlib import Path
|
|
|
|
# Make the repo root importable. Adjust if your tests live elsewhere.
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
|
|
def _stub(name, **attrs):
|
|
module = types.ModuleType(name)
|
|
for key, value in attrs.items():
|
|
setattr(module, key, value)
|
|
sys.modules[name] = module
|
|
return module
|
|
|
|
|
|
def _noop(*_args, **_kwargs):
|
|
return None
|
|
|
|
|
|
def _ensure(name, **stub_attrs):
|
|
"""Import the real module if available; otherwise register a stub."""
|
|
try:
|
|
return importlib.import_module(name)
|
|
except Exception:
|
|
return _stub(name, **stub_attrs)
|
|
|
|
|
|
# External deps that are irrelevant to the pure logic under test.
|
|
_ensure("dotenv", load_dotenv=_noop)
|
|
_ensure("biocypher", BioCypher=object)
|
|
_ensure("schema_config_generation", write_automated_schema=_noop)
|
|
_ensure("fhirImport", get_patient_everything=_noop, get_bundle=_noop)
|
|
|
|
# First-party graph helpers. create_graph is real and tested directly; the
|
|
# reference/convolution modules are stubbed only if they're not on disk yet.
|
|
_ensure("graphCreation")
|
|
_ensure("graphCreation.create_graph", add_json_to_networkx=_noop)
|
|
_ensure("graphCreation.process_references", process_references=_noop)
|
|
_ensure("graphCreation.property_convolution", property_convolution=_noop)
|
|
|
|
# mdm2neo4j XML-processor chain (imported by multiple_adapters).
|
|
_ensure("mdm2neo4j")
|
|
_ensure("mdm2neo4j.src")
|
|
_ensure("mdm2neo4j.src.xml_processor")
|
|
_ensure(
|
|
"mdm2neo4j.src.xml_processor.xml_processor",
|
|
parse_xml_generate_nodes=_noop,
|
|
parse_xml_generate_edges=_noop,
|
|
)
|