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_faulty_association_set(xml_builder_factory): """ Test NullAssociation being correctly assigned to invalid associations""" xml_builder = xml_builder_factory() xml_builder.add_schema('EXAMPLE_SRV', """ <EntityContainer Name="EXAMPLE_SRV" m:IsDefaultEntityContainer="true"> <AssociationSet Name="toDataEntitySet" Association="EXAMPLE_SRV.toDataEntity"> <End EntitySet="MasterEntities" Role="FromRole_toDataEntity"/> <End EntitySet="DataValueHelp" Role="ToRole_toDataEntity"/> </AssociationSet> </EntityContainer> """) metadata = MetadataBuilder( xml_builder.serialize(), config=Config( default_error_policy=PolicyWarning() )) schema = metadata.build() assert isinstance(schema.association_set('toDataEntitySet'), NullAssociation) with pytest.raises(PyODataModelError) as typ_ex_info: schema.association_set('toDataEntitySet').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_unsupported_schema_n(mock_from_etree, xml_builder_factory): """Test correct handling of non-whitelisted Schema namespaces""" xml_builder = xml_builder_factory() edm = 'wedonotsupportthisnamespace.com' xml_builder.namespaces['edm'] = edm xml_builder.add_schema('', '') xml = xml_builder.serialize() MetadataBuilder( xml, config=Config( xml_namespaces={'edm': edm} ) ).build() assert Schema.from_etree is mock_from_etree mock_from_etree.assert_called_once() try: MetadataBuilder(xml).build() except PyODataParserError as ex: assert str(ex) == f'Unsupported Schema namespace - {edm}' mock_from_etree.assert_called_once()
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
import pyodata from pyodata.v2.model import PolicyFatal, PolicyWarning, ParserError, Config custom_config = Config(default_error_policy=PolicyWarning(), custom_error_policies={ ParserError.ANNOTATION: PolicyWarning(), ParserError.ASSOCIATION: PolicyWarning() }) FILE_NAME = 'metadata.xml' with open(FILE_NAME, 'rb') as mtd_file: local_metadata = mtd_file.read() sample = pyodata.Client('NOT_VALID', None, metadata=local_metadata, config=custom_config) print('Metadata validation status: ', sample.schema.is_valid)
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)