|
1 | 1 | """Tests for OData Model module""" |
2 | 2 | # pylint: disable=line-too-long,too-many-locals,too-many-statements,invalid-name, too-many-lines, no-name-in-module, expression-not-assigned, pointless-statement |
3 | | - |
| 3 | +import os |
4 | 4 | from datetime import datetime, timezone |
5 | 5 | from unittest.mock import patch |
6 | 6 | import pytest |
@@ -1206,3 +1206,97 @@ def test_enum_value_out_of_range(xml_builder_factory): |
1206 | 1206 | MetadataBuilder(xml).build() |
1207 | 1207 | except PyODataParserError as ex: |
1208 | 1208 | assert str(ex) == f'Value -130 is out of range for type Edm.Byte' |
| 1209 | + |
| 1210 | + |
| 1211 | +@patch('logging.Logger.warning') |
| 1212 | +def test_missing_property_referenced_in_annotation(mock_warning, xml_builder_factory): |
| 1213 | + """Test that correct behavior when non existing property is referenced in annotation""" |
| 1214 | + |
| 1215 | + local_data_property = 'DataType' |
| 1216 | + value_list_property = 'Type' |
| 1217 | + |
| 1218 | + schema = """ |
| 1219 | + <EntityType Name="MasterEntity" sap:content-version="1"> |
| 1220 | + <Property Name="Data" Type="Edm.String" sap:text="DataName"/> |
| 1221 | + <Property Name="DataName" Type="Edm.String" /> |
| 1222 | + <Property Name="DataType" Type="Edm.String"/> |
| 1223 | + </EntityType> |
| 1224 | + <EntityType Name="DataEntity" sap:content-version="1" sap:value-list="true" sap:label="Data entities"> |
| 1225 | + <Property Name="Type" Type="Edm.String" Nullable="false"/> |
| 1226 | + </EntityType> |
| 1227 | +
|
| 1228 | + <EntityContainer Name="EXAMPLE_SRV" > |
| 1229 | + <EntitySet Name="DataValueHelp" EntityType="EXAMPLE_SRV.DataEntity" /> |
| 1230 | + </EntityContainer> |
| 1231 | +
|
| 1232 | + <Annotations xmlns="http://docs.oasis-open.org/odata/ns/edm" Target="EXAMPLE_SRV.MasterEntity/Data"> |
| 1233 | + <Annotation Term="com.sap.vocabularies.Common.v1.ValueList"> |
| 1234 | + <Record> |
| 1235 | + <PropertyValue Property="CollectionPath" String="DataValueHelp"/> |
| 1236 | + <PropertyValue Property="Parameters"> |
| 1237 | + <Collection> |
| 1238 | + <Record Type="com.sap.vocabularies.Common.v1.ValueListParameterDisplayOnly"> |
| 1239 | + <PropertyValue Property="LocalDataProperty" PropertyPath="{}"/> |
| 1240 | + <PropertyValue Property="ValueListProperty" String="{}"/> |
| 1241 | + </Record> |
| 1242 | + </Collection> |
| 1243 | + </PropertyValue> |
| 1244 | + </Record> |
| 1245 | + </Annotation> |
| 1246 | + </Annotations> |
| 1247 | + """ |
| 1248 | + |
| 1249 | + # Test case 1. -> LocalDataProperty is faulty and ValueListProperty is valid |
| 1250 | + xml_builder = xml_builder_factory() |
| 1251 | + xml_builder.add_schema('EXAMPLE_SRV', schema.format('---', value_list_property)) |
| 1252 | + xml = xml_builder.serialize() |
| 1253 | + |
| 1254 | + with pytest.raises(RuntimeError) as typ_ex_info: |
| 1255 | + MetadataBuilder(xml).build() |
| 1256 | + |
| 1257 | + assert typ_ex_info.value.args[0] == 'ValueHelperParameter(Type) of ValueHelper(MasterEntity/Data) points to ' \ |
| 1258 | + 'an non existing LocalDataProperty --- of EntityType(MasterEntity)' |
| 1259 | + |
| 1260 | + MetadataBuilder(xml, Config( |
| 1261 | + default_error_policy=PolicyWarning() |
| 1262 | + )).build() |
| 1263 | + |
| 1264 | + assert_logging_policy(mock_warning, |
| 1265 | + 'RuntimeError', |
| 1266 | + 'ValueHelperParameter(Type) of ValueHelper(MasterEntity/Data) points to ' |
| 1267 | + 'an non existing LocalDataProperty --- of EntityType(MasterEntity)' |
| 1268 | + ) |
| 1269 | + |
| 1270 | + # Test case 2. -> LocalDataProperty is valid and ValueListProperty is faulty |
| 1271 | + xml_builder = xml_builder_factory() |
| 1272 | + xml_builder.add_schema('EXAMPLE_SRV', schema.format(local_data_property, '---')) |
| 1273 | + xml = xml_builder.serialize() |
| 1274 | + |
| 1275 | + with pytest.raises(RuntimeError) as typ_ex_info: |
| 1276 | + MetadataBuilder(xml).build() |
| 1277 | + |
| 1278 | + assert typ_ex_info.value.args[0] == 'ValueHelperParameter(---) of ValueHelper(MasterEntity/Data) points to an non ' \ |
| 1279 | + 'existing ValueListProperty --- of EntityType(DataEntity)' |
| 1280 | + |
| 1281 | + MetadataBuilder(xml, Config( |
| 1282 | + default_error_policy=PolicyWarning() |
| 1283 | + )).build() |
| 1284 | + |
| 1285 | + assert_logging_policy(mock_warning, |
| 1286 | + 'RuntimeError', |
| 1287 | + 'ValueHelperParameter(---) of ValueHelper(MasterEntity/Data) points to an non ' |
| 1288 | + 'existing ValueListProperty --- of EntityType(DataEntity)' |
| 1289 | + ) |
| 1290 | + |
| 1291 | + # Test case 3. -> LocalDataProperty is valid and ValueListProperty is also valid |
| 1292 | + xml_builder = xml_builder_factory() |
| 1293 | + xml_builder.add_schema('EXAMPLE_SRV', schema.format(local_data_property, value_list_property)) |
| 1294 | + xml = xml_builder.serialize() |
| 1295 | + |
| 1296 | + mock_warning.reset_mock() |
| 1297 | + |
| 1298 | + MetadataBuilder(xml, Config( |
| 1299 | + default_error_policy=PolicyWarning() |
| 1300 | + )).build() |
| 1301 | + |
| 1302 | + assert mock_warning.called is False |
0 commit comments