|
3 | 3 | import geojson |
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from pyodata.policies import PolicyIgnore |
| 6 | +from pyodata.policies import PolicyIgnore, ParserError |
7 | 7 | from pyodata.model.builder import MetadataBuilder |
8 | 8 | from pyodata.exceptions import PyODataModelError, PyODataException, PyODataParserError |
9 | | -from pyodata.model.elements import Types, TypeInfo, NullType |
| 9 | +from pyodata.model.elements import Types, TypeInfo, Schema, NullType |
10 | 10 |
|
11 | 11 | from pyodata.config import Config |
| 12 | +from tests.conftest import metadata |
| 13 | +from pyodata.v4.elements import NavigationTypeProperty, EntitySet, NavigationPropertyBinding |
12 | 14 | from pyodata.v4 import ODataV4, NavigationTypeProperty |
13 | 15 |
|
14 | 16 |
|
@@ -171,6 +173,69 @@ def test_referential_constraint(schema_v4): |
171 | 173 | 'ReferentialConstraint(StructTypeProperty(CategoryID), StructTypeProperty(ID))' |
172 | 174 |
|
173 | 175 |
|
| 176 | +def test_navigation_property_binding(schema_v4: Schema): |
| 177 | + """Test parsing of navigation property bindings on EntitySets""" |
| 178 | + eset: EntitySet = schema_v4.entity_set('People') |
| 179 | + assert str(eset) == 'EntitySet(People)' |
| 180 | + |
| 181 | + nav_prop_biding: NavigationPropertyBinding = eset.navigation_property_bindings[0] |
| 182 | + assert repr(nav_prop_biding) == "NavigationPropertyBinding(NavigationTypeProperty(Friends), EntitySet(People))" |
| 183 | + |
| 184 | + |
| 185 | +def test_invalid_property_binding_on_entity_set(xml_builder_factory): |
| 186 | + """Test parsing of invalid property bindings on EntitySets""" |
| 187 | + schema = """ |
| 188 | + <EntityType Name="Person"> |
| 189 | + <NavigationProperty Name="Friends" Type="Collection(MightySchema.Person)" Partner="Friends" /> |
| 190 | + </EntityType> |
| 191 | + <EntityContainer Name="DefaultContainer"> |
| 192 | + <EntitySet Name="People" EntityType="{}"> |
| 193 | + <NavigationPropertyBinding Path="{}" Target="{}" /> |
| 194 | + </EntitySet> |
| 195 | + </EntityContainer> |
| 196 | + """ |
| 197 | + |
| 198 | + etype, path, target = 'MightySchema.Person', 'Friends', 'People' |
| 199 | + |
| 200 | + xml_builder = xml_builder_factory() |
| 201 | + xml_builder.add_schema('MightySchema', schema.format(etype, 'Mistake', target)) |
| 202 | + xml = xml_builder.serialize() |
| 203 | + |
| 204 | + with pytest.raises(PyODataModelError) as ex_info: |
| 205 | + MetadataBuilder(xml, Config(ODataV4)).build() |
| 206 | + assert ex_info.value.args[0] == 'EntityType(Person) does not contain navigation property Mistake' |
| 207 | + |
| 208 | + try: |
| 209 | + MetadataBuilder(xml, Config(ODataV4, custom_error_policies={ |
| 210 | + ParserError.NAVIGATION_PROPERTY_BIDING: PolicyIgnore() |
| 211 | + })).build() |
| 212 | + except BaseException as ex: |
| 213 | + raise pytest.fail(f'IgnorePolicy was supposed to silence "{ex}" but it did not.') |
| 214 | + |
| 215 | + xml_builder = xml_builder_factory() |
| 216 | + xml_builder.add_schema('MightySchema', schema.format('Mistake', path, target)) |
| 217 | + xml = xml_builder.serialize() |
| 218 | + |
| 219 | + with pytest.raises(KeyError) as ex_info: |
| 220 | + MetadataBuilder(xml, Config(ODataV4)).build() |
| 221 | + assert ex_info.value.args[0] == 'EntityType Mistake does not exist in any Schema Namespace' |
| 222 | + |
| 223 | + try: |
| 224 | + MetadataBuilder(xml, Config(ODataV4, custom_error_policies={ |
| 225 | + ParserError.ENTITY_SET: PolicyIgnore() |
| 226 | + })).build() |
| 227 | + except BaseException as ex: |
| 228 | + raise pytest.fail(f'IgnorePolicy was supposed to silence "{ex}" but it did not.') |
| 229 | + |
| 230 | + xml_builder = xml_builder_factory() |
| 231 | + xml_builder.add_schema('MightySchema', schema.format(etype, path, 'Mistake')) |
| 232 | + xml = xml_builder.serialize() |
| 233 | + |
| 234 | + with pytest.raises(KeyError) as ex_info: |
| 235 | + MetadataBuilder(xml, Config(ODataV4)).build() |
| 236 | + assert ex_info.value.args[0] == 'EntitySet Mistake does not exist in any Schema Namespace' |
| 237 | + |
| 238 | + |
174 | 239 | def test_enum_parsing(schema_v4): |
175 | 240 | """Test correct parsing of enum""" |
176 | 241 |
|
|
0 commit comments