|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import logging |
| 6 | +from contextlib import nullcontext |
6 | 7 | from copy import deepcopy |
7 | 8 | from pathlib import Path |
8 | 9 | from typing import Any |
@@ -2126,6 +2127,84 @@ def test_slot_range( |
2126 | 2127 | """End of range-related tests. Phew!""" |
2127 | 2128 |
|
2128 | 2129 |
|
| 2130 | +@pytest.fixture(scope="module") |
| 2131 | +def enum_string_type_schema() -> str: |
| 2132 | + """Fixture for testing Enum root types.""" |
| 2133 | + return """ |
| 2134 | +id: https://example.com/induced_slot_range_root_types_enums |
| 2135 | +name: enum_test |
| 2136 | +
|
| 2137 | +prefixes: |
| 2138 | + xsd: http://www.w3.org/2001/XMLSchema# |
| 2139 | + linkml: https://www.w3.org/linkml |
| 2140 | +
|
| 2141 | +slots: |
| 2142 | + slot_range_enum: |
| 2143 | + range: EnumRange |
| 2144 | +
|
| 2145 | + slot_range_multi_enum: |
| 2146 | + range: Any |
| 2147 | + exactly_one_of: |
| 2148 | + - range: EnumRange |
| 2149 | + - range: AnotherEnumRange |
| 2150 | + - range: EnumTheThirdRange |
| 2151 | +
|
| 2152 | +classes: |
| 2153 | + Any: |
| 2154 | + class_uri: linkml:Any |
| 2155 | +
|
| 2156 | + TestClass: |
| 2157 | + slots: |
| 2158 | + - slot_range_enum |
| 2159 | + - slot_range_multi_enum |
| 2160 | +
|
| 2161 | +enums: |
| 2162 | + EnumRange: |
| 2163 | + AnotherEnumRange: |
| 2164 | + EnumTheThirdRange: |
| 2165 | +
|
| 2166 | +""" |
| 2167 | + |
| 2168 | + |
| 2169 | +@pytest.mark.parametrize( |
| 2170 | + ("expected", "text_for_schema"), |
| 2171 | + [ |
| 2172 | + (nullcontext("string"), "\ntypes:\n string:\n base: str\n"), |
| 2173 | + # retrieve the string using the type URI |
| 2174 | + (nullcontext("stringiformes"), "\ntypes:\n stringiformes:\n uri: xsd:string\n base: str\n"), |
| 2175 | + # retrieve the string from the imported linkml:types "string" |
| 2176 | + (nullcontext("string"), "\nimports:\n - linkml:types\n"), |
| 2177 | + # raise an error if there is no "string" type in the schema |
| 2178 | + ( |
| 2179 | + pytest.raises( |
| 2180 | + ValueError, |
| 2181 | + match="Cannot find a suitable 'string' type: no types with name 'string' or uri 'xsd:string'.", |
| 2182 | + ), |
| 2183 | + "\ntypes:\n\n", |
| 2184 | + ), |
| 2185 | + # raise an error if there are several potential "string" types |
| 2186 | + ( |
| 2187 | + pytest.raises( |
| 2188 | + ValueError, |
| 2189 | + match="Cannot find a suitable 'string' type: no types with name 'string' and more than one type with uri 'xsd:string'.", |
| 2190 | + ), |
| 2191 | + "types:\n curie:\n uri: xsd:string\n" |
| 2192 | + "\n characters:\n uri: xsd:string\n" |
| 2193 | + "\n char_seq:\n uri: xsd:string\n", |
| 2194 | + ), |
| 2195 | + ], |
| 2196 | +) |
| 2197 | +def test_induced_get_string_type(enum_string_type_schema: str, expected: Any, text_for_schema: str) -> None: |
| 2198 | + """Ensure that an appropriate string type exists in the schema. |
| 2199 | +
|
| 2200 | + Ensures that the appropriate error is thrown if there is no clear string type in a schema. |
| 2201 | + """ |
| 2202 | + sv = SchemaView(enum_string_type_schema + text_for_schema) |
| 2203 | + |
| 2204 | + with expected as e: |
| 2205 | + assert sv._get_string_type() == sv.get_type(e) |
| 2206 | + |
| 2207 | + |
2129 | 2208 | def test_permissible_value_relationships(schema_view_no_imports: SchemaView) -> None: |
2130 | 2209 | """Test relationships between permissible values. |
2131 | 2210 |
|
|
0 commit comments