2525)
2626from linkml_runtime .loaders .yaml_loader import YAMLLoader
2727from linkml_runtime .utils .introspection import package_schemaview
28+ from linkml_runtime .utils .schema_builder import SchemaBuilder
2829from linkml_runtime .utils .schemaops import roll_down , roll_up
2930from linkml_runtime .utils .schemaview import (
3031 CLASSES ,
@@ -727,6 +728,56 @@ def test_metamodel_in_schemaview() -> None:
727728 assert exp_slot_uri is not None
728729
729730
731+ def test_eq_true_false () -> None :
732+ """Test that __eq__ returns True or False appropriately."""
733+ schema = SchemaDefinition (id = "test-schema" , name = "TestSchema" )
734+ view = SchemaView (schema )
735+
736+ # Create a new SchemaView with the same schema
737+ view_copy = SchemaView (schema )
738+ assert view .schema .id == view_copy .schema .id
739+ assert view .modifications == view_copy .modifications
740+ # the new schema will have a unique UUID
741+ assert view .uuid != view_copy .uuid
742+ # the two schemas will therefore not be equal
743+ assert view != view_copy
744+
745+ # copy over the uuid and modifications from the original schema
746+ view_copy .uuid = view .uuid
747+ view_copy .modifications = view .modifications
748+
749+ # the schemas are now equal. Hurrah!
750+ assert view == view_copy
751+
752+ # alter the modification count
753+ view_copy .modifications += 1
754+ assert view != view_copy
755+
756+ # Create a new SchemaView with a different schema
757+ diff_schema = SchemaDefinition (id = "different-schema" , name = "DifferentSchema" )
758+ diff_view = SchemaView (diff_schema )
759+ assert view != diff_view
760+
761+ # copy over the UUID and modifications from the original schema
762+ diff_view .uuid = view .uuid
763+ diff_view .modifications = view .modifications
764+
765+ # schemas have different IDs so will still be different
766+ assert diff_view != view
767+
768+
769+ def test_eq_not_implemented () -> None :
770+ """Test that __eq__ returns NotImplemented for non-SchemaView objects."""
771+ schema = SchemaDefinition (id = "test-schema" , name = "TestSchema" )
772+ view = SchemaView (schema )
773+
774+ # Compare with a string
775+ assert view .__eq__ ("not-a-schemaview" ) is NotImplemented
776+
777+ # Compare with a different object
778+ assert view .__eq__ (object ()) is NotImplemented
779+
780+
730781def test_in_schema (schema_view_with_imports : SchemaView ) -> None :
731782 """Test the in_schema function for determining the source schema of a class or slot."""
732783 view = schema_view_with_imports
@@ -736,6 +787,8 @@ def test_in_schema(schema_view_with_imports: SchemaView) -> None:
736787 assert view .in_schema (SlotDefinitionName ("name" )) == "core"
737788 assert view .in_schema (SlotDefinitionName (ACTIVITY )) == "core"
738789 assert view .in_schema (SlotDefinitionName ("string" )) == "types"
790+ with pytest .raises (ValueError , match = "Element fake_element not in any schema" ):
791+ view .in_schema ("fake_element" )
739792
740793
741794# Prefixes, curi_maps, and imports to add to a schema
@@ -1254,6 +1307,12 @@ def test_all_classes_ordered_by(sv_ordering_tests: SchemaView, ordered_by: str)
12541307 assert list (sv_ordering_tests .all_classes (ordered_by = ordered_by ).keys ()) == ORDERING_TESTS [ordered_by ]
12551308
12561309
1310+ def test_all_classes_ordered_by_error (sv_ordering_tests : SchemaView ) -> None :
1311+ """Test the ordered_by method throws an error when appropriate."""
1312+ with pytest .raises (ValueError , match = "ordered_by must be in OrderedBy or None, got whatever" ):
1313+ sv_ordering_tests .all_classes (ordered_by = "whatever" )
1314+
1315+
12571316def test_all_classes_class_induced_slots (schema_view_with_imports : SchemaView ) -> None :
12581317 """Test all_classes and class_induced_slots."""
12591318 view = schema_view_with_imports
@@ -3061,3 +3120,59 @@ def check_recursive_id_slots(class_name: str) -> list[str]:
30613120
30623121 else :
30633122 detect_cycles (lambda x : check_recursive_id_slots (x ), target )
3123+
3124+
3125+ @pytest .mark .parametrize (
3126+ ("entity_type" , "entity_name" , "type_for_methods" , "get_all_method" ),
3127+ [
3128+ (ClassDefinition , "ToDeleteClass" , "class" , "all_classes" ),
3129+ (SlotDefinition , "ToDeleteSlot" , "slot" , "all_slots" ),
3130+ (EnumDefinition , "ToDeleteEnum" , "enum" , "all_enums" ),
3131+ (TypeDefinition , "ToDeleteType" , "type" , "all_types" ),
3132+ (SubsetDefinition , "ToDeleteSubset" , "subset" , "all_subsets" ),
3133+ ],
3134+ )
3135+ def test_add_delete_get_entity (
3136+ entity_type : ClassDefinition | SlotDefinition | EnumDefinition | TypeDefinition | SubsetDefinition ,
3137+ entity_name : str ,
3138+ type_for_methods : str ,
3139+ get_all_method : str ,
3140+ ) -> None :
3141+ """Test that entities can be added and deleted from a schema."""
3142+ # method for adding an entity, e.g. view.add_class(...)
3143+ add_method = f"add_{ type_for_methods } "
3144+ # method for deleting an entity, e.g. view.delete_subset(...)
3145+ delete_method = f"delete_{ type_for_methods } "
3146+ # method for getting a specific entity, e.g. view.get_enum(...)
3147+ get_method = f"get_{ type_for_methods } "
3148+
3149+ # Build the schema
3150+ builder = SchemaBuilder (name = "test_schema" , id = "test_schema" )
3151+ schema = builder .schema
3152+ view = SchemaView (schema )
3153+
3154+ assert view .modifications == 0
3155+ # create the entity definition object, e.g. ClassDefinition
3156+ entity = entity_type (name = entity_name )
3157+ # add the entity to the schemaview, e.g. view.add_class(...)
3158+ getattr (view , add_method )(entity )
3159+
3160+ # check that the entity is returned when running the get all command, e.g. view.all_classes(...)
3161+ assert {entity_name } == set (getattr (view , get_all_method )())
3162+ assert view .modifications == 1
3163+
3164+ # retrieve the individual class, e.g. view.get_class(...)
3165+ just_added_entity = getattr (view , get_method )(entity_name )
3166+ assert isinstance (just_added_entity , entity_type )
3167+
3168+ # delete the entity from the schema , e.g. view.delete_class(...)
3169+ getattr (view , delete_method )(entity_name )
3170+ assert getattr (view , get_all_method )() == {}
3171+ assert view .modifications == 2
3172+
3173+ # try to retrieve the entity again using get_{type_for_methods}, e.g. view.get_class(...)
3174+ assert getattr (view , get_method )(entity_name , strict = False ) is None
3175+
3176+ # expect that retrieving the entity will return an error if strict mode is on
3177+ with pytest .raises (ValueError , match = f"No such { type_for_methods } " ):
3178+ getattr (view , get_method )(entity_name , strict = True )
0 commit comments