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.
|
|
|
|
"""
|
|
|
|
|
2023-09-07 05:48:47 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict
|
2023-08-24 02:56:09 +00:00
|
|
|
|
2023-08-22 04:43:02 +00:00
|
|
|
import pytest
|
|
|
|
from linkml_runtime.dumpers import yaml_dumper
|
2023-09-07 05:48:47 +00:00
|
|
|
from linkml_runtime.linkml_model import SchemaDefinition
|
|
|
|
from linkml_runtime.loaders.yaml_loader import YAMLLoader
|
2023-08-22 04:43:02 +00:00
|
|
|
|
2024-07-02 04:44:35 +00:00
|
|
|
from nwb_linkml.generators.pydantic import NWBPydanticGenerator
|
2023-08-22 04:43:02 +00:00
|
|
|
from nwb_linkml.lang_elements import NwbLangSchema
|
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
|
2023-08-22 04:43:02 +00:00
|
|
|
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")
|
2023-08-22 04:43:02 +00:00
|
|
|
yaml_dumper.dump(NwbLangSchema, output_file)
|
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
|
2023-08-24 02:56:09 +00:00
|
|
|
def test_generate_core(nwb_core_fixture, tmp_output_dir):
|
|
|
|
schemas = nwb_core_fixture.build().schemas
|
2023-08-29 05:16:58 +00:00
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
(tmp_output_dir / "schema").mkdir(exist_ok=True)
|
2023-08-29 05:16:58 +00:00
|
|
|
|
2023-08-24 02:56:09 +00:00
|
|
|
for schema in schemas:
|
2024-07-02 04:23:31 +00:00
|
|
|
output_file = tmp_output_dir / "schema" / (schema.name + ".yaml")
|
2023-08-24 02:56:09 +00:00
|
|
|
yaml_dumper.dump(schema, output_file)
|
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
|
2023-09-07 05:48:47 +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"):
|
2023-09-07 05:48:47 +00:00
|
|
|
sch = yaml_loader.load(str(schema_path), target_class=SchemaDefinition)
|
|
|
|
preloaded_schema[sch.name] = sch
|
|
|
|
return preloaded_schema
|
2023-08-25 07:22:47 +00:00
|
|
|
|
|
|
|
|
2024-07-09 07:27:22 +00:00
|
|
|
@pytest.mark.xfail()
|
2024-07-02 04:23:31 +00:00
|
|
|
@pytest.mark.depends(on=["test_generate_core"])
|
2023-09-07 05:48:47 +00:00
|
|
|
def test_generate_pydantic(tmp_output_dir):
|
2023-08-25 07:22:47 +00:00
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
(tmp_output_dir / "models").mkdir(exist_ok=True)
|
2023-08-29 05:16:58 +00:00
|
|
|
|
2023-09-07 05:48:47 +00:00
|
|
|
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"):
|
2023-09-07 05:48:47 +00:00
|
|
|
if not schema_path.exists():
|
2023-09-01 03:56:21 +00:00
|
|
|
continue
|
2023-08-29 05:16:58 +00:00
|
|
|
# python friendly name
|
2024-07-02 04:23:31 +00:00
|
|
|
python_name = schema_path.stem.replace(".", "_").replace("-", "_")
|
2023-08-25 07:22:47 +00:00
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
pydantic_file = (schema_path.parent.parent / "models" / python_name).with_suffix(".py")
|
2023-08-29 05:16:58 +00:00
|
|
|
|
|
|
|
generator = NWBPydanticGenerator(
|
2023-09-07 05:48:47 +00:00
|
|
|
str(schema_path),
|
2024-07-02 04:23:31 +00:00
|
|
|
pydantic_version="2",
|
2023-08-25 07:22:47 +00:00
|
|
|
emit_metadata=True,
|
|
|
|
gen_classvars=True,
|
2023-09-07 05:48:47 +00:00
|
|
|
gen_slots=True,
|
2024-07-02 04:23:31 +00:00
|
|
|
schema_map=preloaded_schema,
|
2023-08-25 07:22:47 +00:00
|
|
|
)
|
|
|
|
gen_pydantic = generator.serialize()
|
2023-08-29 05:16:58 +00:00
|
|
|
|
2024-07-02 04:23:31 +00:00
|
|
|
with open(pydantic_file, "w") as pfile:
|
2023-08-25 07:22:47 +00:00
|
|
|
pfile.write(gen_pydantic)
|
2023-09-04 21:56:36 +00:00
|
|
|
|
|
|
|
# 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")
|