nwb-linkml/nwb_linkml/tests/test_adapters/test_adapter.py

113 lines
4.2 KiB
Python
Raw Normal View History

2023-10-09 22:06:53 +00:00
2023-10-09 23:33:02 +00:00
import numpy as np
import pytest
2024-07-02 04:23:31 +00:00
from linkml_runtime.linkml_model import (
ClassDefinition,
2024-07-02 04:44:35 +00:00
SchemaDefinition,
2024-07-02 04:23:31 +00:00
SlotDefinition,
TypeDefinition,
)
2023-10-09 22:06:53 +00:00
from nwb_linkml.adapters import BuildResult
2024-07-02 04:44:35 +00:00
from nwb_schema_language import Attribute, Dataset, Group, Schema
2023-10-09 22:06:53 +00:00
2024-07-02 04:23:31 +00:00
2023-10-09 22:06:53 +00:00
def test_walk(nwb_core_fixture):
"""
Not sure exactly what should be tested here, for now just testing that we get an expected value
"""
everything = nwb_core_fixture.walk(nwb_core_fixture)
# number of items obviously changes based on what version we're talking about
# this is configured as a test matrix on the nwb_core_fixture, currently only testing the latest version
2024-07-02 04:23:31 +00:00
if (
nwb_core_fixture.versions["core"] == "2.6.0-alpha"
and nwb_core_fixture.versions["hdmf-common"] == "1.5.0"
):
assert len(list(everything)) == 9908
2024-07-02 04:23:31 +00:00
@pytest.mark.parametrize(
2024-07-02 04:23:31 +00:00
["walk_class", "known_number"],
[(Dataset, 210), (Group, 144), ((Dataset, Group), 354), (Schema, 19)],
)
def test_walk_types(nwb_core_fixture, walk_class, known_number):
classes = nwb_core_fixture.walk_types(nwb_core_fixture, walk_class)
class_list = list(classes)
assert len(class_list) == known_number
2024-07-02 04:23:31 +00:00
2023-10-09 22:06:53 +00:00
def test_walk_fields(nwb_core_fixture):
2023-10-09 23:33:02 +00:00
# should get same number of dtype fields as there are datasets and attributes + compound dtypes
2024-07-02 04:23:31 +00:00
dtype = list(nwb_core_fixture.walk_fields(nwb_core_fixture, "dtype"))
2023-10-09 23:33:02 +00:00
dtype_havers = list(nwb_core_fixture.walk_types(nwb_core_fixture, (Dataset, Attribute)))
compound_dtypes = [len(d.dtype) for d in dtype_havers if isinstance(d.dtype, list)]
expected_dtypes = np.sum(compound_dtypes) + len(dtype_havers)
assert expected_dtypes == len(dtype)
2023-10-09 22:06:53 +00:00
def test_walk_field_values(nwb_core_fixture):
2024-07-02 04:23:31 +00:00
dtype_models = list(nwb_core_fixture.walk_field_values(nwb_core_fixture, "dtype", value=None))
assert all([hasattr(d, "dtype") for d in dtype_models])
text_models = list(nwb_core_fixture.walk_field_values(nwb_core_fixture, "dtype", value="text"))
assert all([d.dtype == "text" for d in text_models])
2023-10-09 23:33:02 +00:00
# 135 known value from regex search
2024-07-02 04:23:31 +00:00
assert len(text_models) == len([d for d in dtype_models if d.dtype == "text"]) == 134
2023-10-09 22:06:53 +00:00
def test_build_result(linkml_schema_bare):
"""
2023-10-09 22:06:53 +00:00
build results can hold lists of class, slot, and type definitions
"""
schema = linkml_schema_bare
sch = schema.core
2024-07-02 04:23:31 +00:00
cls = sch.classes["MainTopLevel"]
slot1 = cls.attributes["name"]
typ = sch.types["numeric"]
2023-10-09 22:06:53 +00:00
# Build result should hold the results and coerce to list type
2024-07-02 04:23:31 +00:00
res = BuildResult(schemas=sch, classes=cls, slots=slot1, types=typ)
for field in ("schemas", "classes", "slots", "types"):
2023-10-09 22:06:53 +00:00
assert isinstance(getattr(res, field), list)
assert len(getattr(res, field)) == 1
2024-07-02 04:23:31 +00:00
@pytest.mark.parametrize("sch_type", ("schemas", "classes", "slots", "types"))
2023-10-09 22:06:53 +00:00
def test_build_result_add(linkml_schema_bare, sch_type):
"""
Build results can be added together without duplicating
"""
schema = linkml_schema_bare
2024-07-02 04:23:31 +00:00
if sch_type == "schemas":
2023-10-09 22:06:53 +00:00
obj = schema.core
other_obj = SchemaDefinition(name="othername", id="othername", version="1.0.1")
2024-07-02 04:23:31 +00:00
elif sch_type == "classes":
obj = schema.core.classes["MainTopLevel"]
2023-10-09 22:06:53 +00:00
other_obj = ClassDefinition(name="othername")
2024-07-02 04:23:31 +00:00
elif sch_type == "slots":
obj = schema.core.classes["MainTopLevel"].attributes["name"]
2023-10-09 22:06:53 +00:00
other_obj = SlotDefinition(name="othername", range="string")
2024-07-02 04:23:31 +00:00
elif sch_type == "types":
obj = schema.core.types["numeric"]
2023-10-09 22:06:53 +00:00
other_obj = TypeDefinition(name="othername", typeof="float")
else:
raise ValueError(f"Dont know how to test type {sch_type}")
res1 = BuildResult(**{sch_type: [obj]})
res2 = BuildResult(**{sch_type: [obj]})
assert len(getattr(res1, sch_type)) == 1
assert len(getattr(res2, sch_type)) == 1
assert len(getattr(res1 + res2, sch_type)) == 1
assert len(getattr(res2 + res1, sch_type)) == 1
# and then addition works as normal for not same named items
res3 = BuildResult(**{sch_type: [other_obj]})
assert len(getattr(res1 + res3, sch_type)) == 2
assert len(getattr(res2 + res3, sch_type)) == 2
res_combined_2 = res1 + res3
assert getattr(res_combined_2, sch_type)[-1] is other_obj