Skip to content

Commit 7e53203

Browse files
committed
Adding tests for SchemaView's modification functions: adding or deleting elements in a schema.
1 parent 3c42fc7 commit 7e53203

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/test_utils/test_schemaview.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from linkml_runtime.loaders.yaml_loader import YAMLLoader
2727
from linkml_runtime.utils.introspection import package_schemaview
28+
from linkml_runtime.utils.schema_builder import SchemaBuilder
2829
from linkml_runtime.utils.schemaops import roll_down, roll_up
2930
from linkml_runtime.utils.schemaview import (
3031
CLASSES,
@@ -2861,3 +2862,59 @@ def check_recursive_id_slots(class_name: str) -> list[str]:
28612862

28622863
else:
28632864
detect_cycles(lambda x: check_recursive_id_slots(x), target)
2865+
2866+
2867+
@pytest.mark.parametrize(
2868+
("entity_type", "entity_name", "type_for_methods", "get_all_method"),
2869+
[
2870+
(ClassDefinition, "ToDeleteClass", "class", "all_classes"),
2871+
(SlotDefinition, "ToDeleteSlot", "slot", "all_slots"),
2872+
(EnumDefinition, "ToDeleteEnum", "enum", "all_enums"),
2873+
(TypeDefinition, "ToDeleteType", "type", "all_types"),
2874+
(SubsetDefinition, "ToDeleteSubset", "subset", "all_subsets"),
2875+
],
2876+
)
2877+
def test_add_delete_get_entity(
2878+
entity_type: ClassDefinition | SlotDefinition | EnumDefinition | TypeDefinition | SubsetDefinition,
2879+
entity_name: str,
2880+
type_for_methods: str,
2881+
get_all_method: str,
2882+
) -> None:
2883+
"""Test that entities can be added and deleted from a schema."""
2884+
# method for adding an entity, e.g. view.add_class(...)
2885+
add_method = f"add_{type_for_methods}"
2886+
# method for deleting an entity, e.g. view.delete_subset(...)
2887+
delete_method = f"delete_{type_for_methods}"
2888+
# method for getting a specific entity, e.g. view.get_enum(...)
2889+
get_method = f"get_{type_for_methods}"
2890+
2891+
# Build the schema
2892+
builder = SchemaBuilder(name="test_schema", id="test_schema")
2893+
schema = builder.schema
2894+
view = SchemaView(schema)
2895+
2896+
assert view.modifications == 0
2897+
# create the entity definition object, e.g. ClassDefinition
2898+
entity = entity_type(name=entity_name)
2899+
# add the entity to the schemaview, e.g. view.add_class(...)
2900+
getattr(view, add_method)(entity)
2901+
2902+
# check that the entity is returned when running the get all command, e.g. view.all_classes(...)
2903+
assert {entity_name} == set(getattr(view, get_all_method)())
2904+
assert view.modifications == 1
2905+
2906+
# retrieve the individual class, e.g. view.get_class(...)
2907+
just_added_entity = getattr(view, get_method)(entity_name)
2908+
assert isinstance(just_added_entity, entity_type)
2909+
2910+
# delete the entity from the schema , e.g. view.delete_class(...)
2911+
getattr(view, delete_method)(entity_name)
2912+
assert getattr(view, get_all_method)() == {}
2913+
assert view.modifications == 2
2914+
2915+
# try to retrieve the entity again using get_{type_for_methods}, e.g. view.get_class(...)
2916+
assert getattr(view, get_method)(entity_name, strict=False) is None
2917+
2918+
# expect that retrieving the entity will return an error if strict mode is on
2919+
with pytest.raises(ValueError, match=f"No such {type_for_methods}"):
2920+
getattr(view, get_method)(entity_name, strict=True)

0 commit comments

Comments
 (0)