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