def test_faulty_association(xml_builder_factory): """ Test NullAssociation being correctly assigned to invalid associations""" xml_builder = xml_builder_factory() xml_builder.add_schema('EXAMPLE_SRV', """ <EntityType Name="MasterEntity"> <Property Name="Key" Type="Edm.String" /> <NavigationProperty Name="Followers" Relationship="EXAMPLE_SRV.Followers" FromRole="MasterRole" ToRole="FollowerRole"/> </EntityType> <EntityType Name="FollowerEntity"> <Property Name="Key" Type="Edm.String" /> </EntityType> <Association Name="Followers"> <End Type="FaultyNamespace.MasterEntity" Multiplicity="1" Role="MasterRole"/> <End Type="FaultyNamespace.FollowerEntity" Multiplicity="*" Role="FollowerRole"/> </Association> """) metadata = MetadataBuilder( xml_builder.serialize(), config=Config( default_error_policy=PolicyIgnore() )) schema = metadata.build() assert isinstance(schema.associations[0], NullAssociation) with pytest.raises(PyODataModelError) as typ_ex_info: schema.associations[0].Any assert typ_ex_info.value.args[0] == 'Cannot access this association. An error occurred during parsing ' \ 'association metadata due to that annotation has been omitted.'
def test_config_set_default_error_policy(): """ Test configurability of policies """ config = Config( custom_error_policies={ParserError.ANNOTATION: PolicyWarning()}) assert isinstance(config.err_policy(ParserError.ENTITY_TYPE), PolicyFatal) assert isinstance(config.err_policy(ParserError.ANNOTATION), PolicyWarning) config.set_default_error_policy(PolicyIgnore()) assert isinstance(config.err_policy(ParserError.ENTITY_TYPE), PolicyIgnore) assert isinstance(config.err_policy(ParserError.ANNOTATION), PolicyIgnore)
def test_null_type(xml_builder_factory): """ Test NullType being correctly assigned to invalid types""" xml_builder = xml_builder_factory() xml_builder.add_schema( 'TEST.NAMESPACE', """ <EntityType Name="MasterProperty"> <Property Name="Key" Type="Edm.UnknownType" /> </EntityType> <EnumType Name="MasterEnum" UnderlyingType="Edm.String" /> <ComplexType Name="MasterComplex"> <Property Name="Width" Type="Edm.Double" /> <Property Name="Width" Type="Edm.Double" /> </ComplexType> <EntityType Name="MasterEntity"> <NavigationProperty Name="ID" /> </EntityType> """) metadata = MetadataBuilder( xml_builder.serialize(), config=Config(default_error_policy=PolicyIgnore())) schema = metadata.build() type_info = TypeInfo(namespace=None, name='MasterProperty', is_collection=False) assert isinstance(schema.get_type(type_info).proprty('Key').typ, NullType) type_info = TypeInfo(namespace=None, name='MasterEnum', is_collection=False) assert isinstance(schema.get_type(type_info), NullType) type_info = TypeInfo(namespace=None, name='MasterComplex', is_collection=False) assert isinstance(schema.get_type(type_info), NullType) type_info = TypeInfo(namespace=None, name='MasterEntity', is_collection=False) assert isinstance(schema.get_type(type_info), NullType) with pytest.raises(PyODataModelError) as typ_ex_info: schema.get_type(type_info).Any assert typ_ex_info.value.args[0] == f'Cannot access this type. An error occurred during parsing type ' \ f'stated in xml({schema.get_type(type_info).name}) was not found, therefore it has been ' \ f'replaced with NullType.'
def test_client_custom_configuration(mock_warning, metadata): """Check client creation for custom configuration""" responses.add( responses.GET, f"{SERVICE_URL}/$metadata", content_type='application/xml', body=metadata, status=200) namespaces = { 'edmx': "customEdmxUrl.com", 'edm': 'customEdmUrl.com' } custom_config = Config( xml_namespaces=namespaces, default_error_policy=PolicyFatal(), custom_error_policies={ ParserError.ANNOTATION: PolicyWarning(), ParserError.ASSOCIATION: PolicyIgnore() }) with pytest.raises(PyODataException) as e_info: client = pyodata.Client(SERVICE_URL, requests, config=custom_config, namespaces=namespaces) assert str(e_info.value) == 'You cannot pass namespaces and config at the same time' client = pyodata.Client(SERVICE_URL, requests, namespaces=namespaces) mock_warning.assert_called_with( 'Passing namespaces directly is deprecated. Use class Config instead', DeprecationWarning ) assert isinstance(client, pyodata.v2.service.Service) assert client.schema.config.namespaces == namespaces client = pyodata.Client(SERVICE_URL, requests, config=custom_config) assert isinstance(client, pyodata.v2.service.Service) assert client.schema.config == custom_config
def test_annot_v_l_trgt_inv_prop(mock_warning, mock_resolve, xml_builder_factory): """Test correct handling of annotations whose target property does not exist""" xml_builder = xml_builder_factory() xml_builder.add_schema( 'MISSING_TP', """ <EntityType Name="Dict" sap:content-version="1"> <Key><PropertyRef Name="Key"/></Key> <Property Name="Key" Type="Edm.String" Nullable="false" sap:unicode="false" sap:label="Key" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:value-list="standard"/> <Property Name="Value" Type="Edm.String" Nullable="false" sap:unicode="false" sap:label="Key" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:value-list="standard"/> </EntityType> <EntityType Name="Database" sap:content-version="1"> <Key><PropertyRef Name="Data"/></Key> <Property Name="Data" Type="Edm.String" Nullable="false" sap:unicode="false" sap:label="Key" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:value-list="standard"/> </EntityType> <EntityContainer Name="EXAMPLE_SRV" m:IsDefaultEntityContainer="true" sap:supported-formats="atom json xlsx"> <EntitySet Name="DataValueHelp" EntityType="MISSING_TP.Database" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:searchable="true" sap:content-version="1"/> </EntityContainer> <Annotations xmlns="http://docs.oasis-open.org/odata/ns/edm" Target="MISSING_TP.Dict/NoExisting"> <Annotation Term="com.sap.vocabularies.Common.v1.ValueList"> <Record> <PropertyValue Property="Label" String="Data"/> <PropertyValue Property="CollectionPath" String="DataValueHelp"/> <PropertyValue Property="SearchSupported" Bool="true"/> <PropertyValue Property="Parameters"> <Collection> <Record Type="com.sap.vocabularies.Common.v1.ValueListParameterOut"> <PropertyValue Property="LocalDataProperty" PropertyPath="Value"/> <PropertyValue Property="ValueListProperty" String="Data"/> </Record> </Collection> </PropertyValue> </Record> </Annotation> </Annotations> """ ) metadata = MetadataBuilder(xml_builder.serialize()) with pytest.raises(RuntimeError) as typ_ex_info: metadata.build() assert typ_ex_info.value.args[0] == 'Target Property NoExisting of EntityType(Dict) as defined in ' \ 'ValueHelper(Dict/NoExisting) does not exist' metadata.config.set_custom_error_policy({ ParserError.ANNOTATION: PolicyIgnore() }) metadata.build() assert PolicyIgnore.resolve is mock_resolve mock_resolve.asser_called_once() metadata.config.set_custom_error_policy({ ParserError.ANNOTATION: PolicyWarning() }) metadata.build() assert_logging_policy(mock_warning, 'RuntimeError', 'Target Property NoExisting of EntityType(Dict) as defined in ValueHelper(Dict/NoExisting)' ' does not exist' )
import pyodata from requests import Request, Session from pyodata.v2.model import PolicyFatal, PolicyWarning, PolicyIgnore, ParserError, Config SERVICE_URL = 'http://psl-e.one-erp.telekom.de/sap/opu/odata/SAP/ZPSL_GWSAMPLE_BASIC_SRV/' session = Session() # 400 session.auth = ('44544331', 'Fhm9Z2478p!EW') # 200 # session.auth = ('44544331', 'xmXxPj6GXZHa!') session.params = {'sap-client': 400, 'sap-language': 'EN'} namespaces = { 'atom': 'http://www.w3.org/2005/Atom', 'app': 'http://www.w3.org/2007/app' } custom_config = Config(xml_namespaces=namespaces, default_error_policy=PolicyFatal(), custom_error_policies={ParserError.ANNOTATION: PolicyWarning(), ParserError.ASSOCIATION: PolicyIgnore()}) services = pyodata.Client(url=SERVICE_URL, connection=session, config=custom_config) bp_request = services.entity_sets.BusinessPartnerSet.get_entities() for item in bp_request.select('BusinessPartnerID,CompanyName').execute(): # print(item.BusinessPartnerID, item.CompanyName) print(item.EmailAddress)