|
25 | 25 | ) |
26 | 26 | from linkml_runtime.loaders.yaml_loader import YAMLLoader |
27 | 27 | from linkml_runtime.utils.introspection import package_schemaview |
| 28 | +from linkml_runtime.utils.schema_builder import SchemaBuilder |
28 | 29 | from linkml_runtime.utils.schemaops import roll_down, roll_up |
29 | 30 | from linkml_runtime.utils.schemaview import ( |
30 | 31 | CLASSES, |
@@ -2919,3 +2920,59 @@ def check_recursive_id_slots(class_name: str) -> list[str]: |
2919 | 2920 |
|
2920 | 2921 | else: |
2921 | 2922 | detect_cycles(lambda x: check_recursive_id_slots(x), target) |
| 2923 | + |
| 2924 | + |
| 2925 | +@pytest.mark.parametrize( |
| 2926 | + ("entity_type", "entity_name", "type_for_methods", "get_all_method"), |
| 2927 | + [ |
| 2928 | + (ClassDefinition, "ToDeleteClass", "class", "all_classes"), |
| 2929 | + (SlotDefinition, "ToDeleteSlot", "slot", "all_slots"), |
| 2930 | + (EnumDefinition, "ToDeleteEnum", "enum", "all_enums"), |
| 2931 | + (TypeDefinition, "ToDeleteType", "type", "all_types"), |
| 2932 | + (SubsetDefinition, "ToDeleteSubset", "subset", "all_subsets"), |
| 2933 | + ], |
| 2934 | +) |
| 2935 | +def test_add_delete_get_entity( |
| 2936 | + entity_type: ClassDefinition | SlotDefinition | EnumDefinition | TypeDefinition | SubsetDefinition, |
| 2937 | + entity_name: str, |
| 2938 | + type_for_methods: str, |
| 2939 | + get_all_method: str, |
| 2940 | +) -> None: |
| 2941 | + """Test that entities can be added and deleted from a schema.""" |
| 2942 | + # method for adding an entity, e.g. view.add_class(...) |
| 2943 | + add_method = f"add_{type_for_methods}" |
| 2944 | + # method for deleting an entity, e.g. view.delete_subset(...) |
| 2945 | + delete_method = f"delete_{type_for_methods}" |
| 2946 | + # method for getting a specific entity, e.g. view.get_enum(...) |
| 2947 | + get_method = f"get_{type_for_methods}" |
| 2948 | + |
| 2949 | + # Build the schema |
| 2950 | + builder = SchemaBuilder(name="test_schema", id="test_schema") |
| 2951 | + schema = builder.schema |
| 2952 | + view = SchemaView(schema) |
| 2953 | + |
| 2954 | + assert view.modifications == 0 |
| 2955 | + # create the entity definition object, e.g. ClassDefinition |
| 2956 | + entity = entity_type(name=entity_name) |
| 2957 | + # add the entity to the schemaview, e.g. view.add_class(...) |
| 2958 | + getattr(view, add_method)(entity) |
| 2959 | + |
| 2960 | + # check that the entity is returned when running the get all command, e.g. view.all_classes(...) |
| 2961 | + assert {entity_name} == set(getattr(view, get_all_method)()) |
| 2962 | + assert view.modifications == 1 |
| 2963 | + |
| 2964 | + # retrieve the individual class, e.g. view.get_class(...) |
| 2965 | + just_added_entity = getattr(view, get_method)(entity_name) |
| 2966 | + assert isinstance(just_added_entity, entity_type) |
| 2967 | + |
| 2968 | + # delete the entity from the schema , e.g. view.delete_class(...) |
| 2969 | + getattr(view, delete_method)(entity_name) |
| 2970 | + assert getattr(view, get_all_method)() == {} |
| 2971 | + assert view.modifications == 2 |
| 2972 | + |
| 2973 | + # try to retrieve the entity again using get_{type_for_methods}, e.g. view.get_class(...) |
| 2974 | + assert getattr(view, get_method)(entity_name, strict=False) is None |
| 2975 | + |
| 2976 | + # expect that retrieving the entity will return an error if strict mode is on |
| 2977 | + with pytest.raises(ValueError, match=f"No such {type_for_methods}"): |
| 2978 | + getattr(view, get_method)(entity_name, strict=True) |
0 commit comments