@@ -1680,6 +1680,13 @@ def sv_range_riid_gen(request: pytest.FixtureRequest) -> tuple[SchemaView, tuple
16801680 for key , value in ranges_replaced_by_defaults ["none_range" ].items ()
16811681}
16821682
1683+ induced_range_strict_errors = {
1684+ "any_of_and_exactly_one_of_range" : "ClassWithRanges slot any_of_and_exactly_one_of_range has range specified in both `exactly_one_of` and `any_of`" ,
1685+ "invalid_any_range_no_linkml_any" : "ClassWithRanges slot invalid_any_range_no_linkml_any has range specified in `exactly_one_of` or `any_of` but the slot range is not linkml:Any" ,
1686+ "invalid_any_range_enum" : "ClassWithRanges slot invalid_any_range_enum has range specified in `exactly_one_of` or `any_of` but the slot range is not linkml:Any" ,
1687+ "invalid_any_range_class" : "ClassWithRanges slot invalid_any_range_class has range specified in `exactly_one_of` or `any_of` but the slot range is not linkml:Any" ,
1688+ }
1689+
16831690
16841691def test_generated_range_schema (sv_range_riid_gen : tuple [SchemaView , tuple [str , str | None , str | None ]]) -> None :
16851692 """Tests for generation of range schemas.
@@ -1696,8 +1703,17 @@ def test_generated_range_schema(sv_range_riid_gen: tuple[SchemaView, tuple[str,
16961703 assert isinstance (sv_range , SchemaView )
16971704
16981705
1699- @pytest .mark .parametrize ("range_function" , ["slot_range" , "slot_range_as_union" , "slot_applicable_range_elements" ])
17001706@pytest .mark .parametrize ("slot_name" , ranges_no_defaults .keys ())
1707+ @pytest .mark .parametrize (
1708+ "range_function" ,
1709+ [
1710+ "slot_range" ,
1711+ "slot_range_as_union" ,
1712+ "induced_slot_range" ,
1713+ "induced_range_strict" ,
1714+ "slot_applicable_range_elements" ,
1715+ ],
1716+ )
17011717def test_slot_range (
17021718 range_function : str ,
17031719 slot_name : str ,
@@ -1713,25 +1729,37 @@ def test_slot_range(
17131729 :type sv_range_riid_gen: tuple[SchemaView, tuple[str, str | None, str | None]]
17141730 """
17151731 (sv_range , range_tuple ) = sv_range_riid_gen
1716-
1717- slots_by_name = {s .name : s for s in sv_range .class_induced_slots ("ClassWithRanges" )}
1732+ slot_object = sv_range .induced_slot (slot_name , "ClassWithRanges" )
17181733 expected = ranges_no_defaults [slot_name ]
1734+
17191735 if slot_name in ranges_replaced_by_defaults :
17201736 expected = ranges_replaced_by_defaults [slot_name ][range_tuple ]
1737+
17211738 if range_function == "slot_range" :
1722- assert slots_by_name [ slot_name ] .range == expected [0 ]
1739+ assert slot_object .range == expected [0 ]
17231740 elif range_function == "slot_range_as_union" :
1724- assert set (sv_range .slot_range_as_union (slots_by_name [ slot_name ] )) == expected [1 ]
1741+ assert set (sv_range .slot_range_as_union (slot_object )) == expected [1 ]
17251742 elif range_function == "induced_slot_range" :
1726- assert sv_range .induced_slot_range (slots_by_name [slot_name ]) == expected [2 ]
1743+ assert sv_range .induced_slot_range (slot_object ) == expected [2 ]
1744+ elif range_function == "induced_range_strict" :
1745+ # err_msg will be None if there is no error in the slot range specification
1746+ err_msg = induced_range_strict_errors .get (slot_name )
1747+ if not err_msg and expected [2 ] == set ():
1748+ err_msg = f"ClassWithRanges slot { slot_name } has no range specified"
1749+
1750+ if err_msg :
1751+ with pytest .raises (ValueError , match = err_msg ):
1752+ sv_range .induced_slot_range (slot_object , strict = True )
1753+ else :
1754+ assert sv_range .induced_slot_range (slot_object , strict = True ) == expected [2 ]
17271755 elif range_function == "slot_applicable_range_elements" :
17281756 if slot_name in ranges_replaced_by_defaults and len (expected ) < 4 :
17291757 expected = ranges_no_defaults [slot_name ]
17301758 if isinstance (expected [3 ], set ):
1731- assert set (sv_range .slot_applicable_range_elements (slots_by_name [ slot_name ] )) == expected [3 ]
1759+ assert set (sv_range .slot_applicable_range_elements (slot_object )) == expected [3 ]
17321760 else :
17331761 with pytest .raises (expected [3 ], match = "Unrecognized range: None" ):
1734- sv_range .slot_applicable_range_elements (slots_by_name [ slot_name ] )
1762+ sv_range .slot_applicable_range_elements (slot_object )
17351763 else :
17361764 pytest .fail (f"Unexpected range_function value: { range_function } " )
17371765
0 commit comments