nwb-linkml/nwb_linkml/tests/test_generate.py

94 lines
2.9 KiB
Python
Raw Normal View History

2023-10-06 04:22:00 +00:00
"""
Placeholder end-to-end tests for generating linkml translations and pydantic models.
Should be replaced with more specific unit and integration tests, but in place for now
to ensure that the basics of the whole thing operate -- not doing any actual data validation
here.
"""
from pathlib import Path
from typing import Dict
import pytest
from linkml_runtime.dumpers import yaml_dumper
from linkml_runtime.linkml_model import SchemaDefinition
from linkml_runtime.loaders.yaml_loader import YAMLLoader
2024-07-02 04:44:35 +00:00
from nwb_linkml.generators.pydantic import NWBPydanticGenerator
from nwb_linkml.lang_elements import NwbLangSchema
from nwb_linkml.providers import LinkMLProvider, PydanticProvider
2024-07-25 05:49:35 +00:00
@pytest.mark.dev
def test_generate_nwblang(tmp_output_dir):
2024-07-02 04:23:31 +00:00
output_file = (tmp_output_dir / NwbLangSchema.name).with_suffix(".yml")
yaml_dumper.dump(NwbLangSchema, output_file)
2024-07-25 05:49:35 +00:00
@pytest.mark.dev
def test_generate_core(nwb_core_fixture, tmp_output_dir):
schemas = nwb_core_fixture.build().schemas
2024-07-02 04:23:31 +00:00
(tmp_output_dir / "schema").mkdir(exist_ok=True)
for schema in schemas:
2024-07-02 04:23:31 +00:00
output_file = tmp_output_dir / "schema" / (schema.name + ".yaml")
yaml_dumper.dump(schema, output_file)
2024-07-02 04:23:31 +00:00
def load_schema_files(path: Path) -> Dict[str, SchemaDefinition]:
yaml_loader = YAMLLoader()
sch: SchemaDefinition
preloaded_schema = {}
2024-07-02 04:23:31 +00:00
for schema_path in (path / "schema").glob("*.yaml"):
sch = yaml_loader.load(str(schema_path), target_class=SchemaDefinition)
preloaded_schema[sch.name] = sch
return preloaded_schema
2024-07-25 05:49:35 +00:00
@pytest.mark.dev
2024-07-02 04:23:31 +00:00
@pytest.mark.depends(on=["test_generate_core"])
def test_generate_pydantic(tmp_output_dir):
2024-07-02 04:23:31 +00:00
(tmp_output_dir / "models").mkdir(exist_ok=True)
preloaded_schema = load_schema_files(tmp_output_dir)
2024-07-02 04:23:31 +00:00
for schema_path in (tmp_output_dir / "schema").glob("*.yaml"):
if not schema_path.exists():
continue
# python friendly name
2024-07-02 04:23:31 +00:00
python_name = schema_path.stem.replace(".", "_").replace("-", "_")
2024-07-02 04:23:31 +00:00
pydantic_file = (schema_path.parent.parent / "models" / python_name).with_suffix(".py")
generator = NWBPydanticGenerator(
str(schema_path),
2024-07-02 04:23:31 +00:00
schema_map=preloaded_schema,
)
gen_pydantic = generator.serialize()
2024-07-02 04:23:31 +00:00
with open(pydantic_file, "w") as pfile:
pfile.write(gen_pydantic)
# make __init__.py
2024-07-02 04:23:31 +00:00
with open(tmp_output_dir / "models" / "__init__.py", "w") as initfile:
initfile.write("# Autogenerated module indicator")
2024-07-25 05:49:35 +00:00
@pytest.mark.linkml
@pytest.mark.provider
@pytest.mark.dev
def test_generate_linkml_provider(tmp_output_dir, nwb_core_fixture):
provider = LinkMLProvider(path=tmp_output_dir, verbose=False)
result = provider.build(nwb_core_fixture)
@pytest.mark.pydantic
@pytest.mark.provider
@pytest.mark.dev
def test_generate_pydantic_provider(tmp_output_dir):
provider = PydanticProvider(path=tmp_output_dir, verbose=False)
2024-07-25 05:49:35 +00:00
result = provider.build("core")