6
6
from jsonasobj2 import JsonObj
7
7
8
8
from linkml_runtime .dumpers import yaml_dumper
9
+ from linkml_runtime .exceptions import AmbiguousNameError , MissingElementError
9
10
from linkml_runtime .linkml_model .meta import Example , SchemaDefinition , ClassDefinition , SlotDefinitionName , SlotDefinition , \
10
11
ClassDefinitionName , Prefix
11
12
from linkml_runtime .loaders .yaml_loader import YAMLLoader
@@ -701,7 +702,7 @@ def test_slot_inheritance():
701
702
702
703
# Test dangling
703
704
view .add_slot (SlotDefinition ('s5' , is_a = 'does-not-exist' ))
704
- with pytest .raises (ValueError ):
705
+ with pytest .raises (MissingElementError ):
705
706
view .slot_ancestors ('s5' )
706
707
707
708
def test_attribute_inheritance ():
@@ -744,9 +745,6 @@ def test_ambiguous_attributes():
744
745
a2x = SlotDefinition ('a2' , range = 'BarEnum' )
745
746
view .add_class (ClassDefinition ('C2' , attributes = {a1x .name : a1x , a2x .name : a2x }))
746
747
747
- assert view .get_slot (a1 .name ).range is None
748
- assert view .get_slot (a2 .name ).range is None
749
- assert view .get_slot (a3 .name ).range is not None
750
748
assert len (view .all_slots (attributes = True )) == 3
751
749
assert len (view .all_slots (attributes = False )) == 0
752
750
assert len (view .all_slots ()) == 3
@@ -757,6 +755,21 @@ def test_ambiguous_attributes():
757
755
assert view .induced_slot (a2x .name , 'C2' ).range == a2x .range
758
756
759
757
758
+ def test_ambiguous_attribute_through_get_slot ():
759
+ schema_path = os .path .join (INPUT_DIR , "get_slot_with_ambiguous_attributes.yaml" )
760
+ sv = SchemaView (schema_path )
761
+
762
+ assert sv .get_slot ("uniqueSlot" ) is not None
763
+ assert sv .get_slot ("randomAttribute" , attributes = False ) is None
764
+ assert sv .induced_slot ("uniqueSlot" , "ImportantSecondClass" ) is not None
765
+
766
+ with pytest .raises (AmbiguousNameError ) as exception :
767
+ sv .get_slot ("randomAttribute" )
768
+ assert str (exception .value ) == ('Attribute "randomAttribute" is already defined in another class, '
769
+ 'these attributes will be ambiguous in RDF generators and you may need to rename '
770
+ 'them or restructure your schema. Furthermore, you can use the induced_slot '
771
+ 'method with the slot name and its containing class as arguments.' )
772
+
760
773
def test_metamodel_in_schemaview ():
761
774
view = package_schemaview ('linkml_runtime.linkml_model.meta' )
762
775
assert 'meta' in view .imports_closure ()
0 commit comments