Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ToRole="FollowerRole"/>
""")
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_client_custom_configuration(mock_warning, metadata):
"""Check client creation for custom configuration"""
responses.add(
responses.GET,
"{0}/$metadata".format(SERVICE_URL),
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',
"""
# Test case 1. -> LocalDataProperty is faulty and ValueListProperty is valid
xml_builder = xml_builder_factory()
xml_builder.add_schema('EXAMPLE_SRV', schema.format('---', value_list_property))
xml = xml_builder.serialize()
with pytest.raises(RuntimeError) as typ_ex_info:
MetadataBuilder(xml).build()
assert typ_ex_info.value.args[0] == 'ValueHelperParameter(Type) of ValueHelper(MasterEntity/Data) points to ' \
'an non existing LocalDataProperty --- of EntityType(MasterEntity)'
MetadataBuilder(xml, Config(
default_error_policy=PolicyWarning()
)).build()
assert_logging_policy(mock_warning,
'RuntimeError',
'ValueHelperParameter(Type) of ValueHelper(MasterEntity/Data) points to '
'an non existing LocalDataProperty --- of EntityType(MasterEntity)'
)
# Test case 2. -> LocalDataProperty is valid and ValueListProperty is faulty
xml_builder = xml_builder_factory()
xml_builder.add_schema('EXAMPLE_SRV', schema.format(local_data_property, '---'))
xml = xml_builder.serialize()
with pytest.raises(RuntimeError) as typ_ex_info:
MetadataBuilder(xml).build()
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', """
""")
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_edmx_n(mock_from_etree, xml_builder_factory):
"""Test correct handling of non-whitelisted Edmx namespaces"""
xml_builder = xml_builder_factory()
edmx = 'wedonotsupportthisnamespace.com'
xml_builder.namespaces['edmx'] = edmx
xml_builder.add_schema('', '')
xml = xml_builder.serialize()
MetadataBuilder(
xml,
config=Config(
xml_namespaces={'edmx': edmx}
)
).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 Edmx namespace - {edmx}'
mock_from_etree.assert_called_once()
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()
if odata_version == Client.ODATA_VERSION_2:
# sanitize url
url = url.rstrip('/') + '/'
if metadata is None:
metadata = _fetch_metadata(connection, url, logger)
else:
logger.info('Using static metadata')
if config is not None and namespaces is not None:
raise PyODataException('You cannot pass namespaces and config at the same time')
if config is None:
config = pyodata.v2.model.Config()
if namespaces is not None:
warnings.warn("Passing namespaces directly is deprecated. Use class Config instead", DeprecationWarning)
config.namespaces = namespaces
# create model instance from received metadata
logger.info('Creating OData Schema (version: %d)', odata_version)
schema = pyodata.v2.model.MetadataBuilder(metadata, config=config).build()
# create service instance based on model we have
logger.info('Creating OData Service (version: %d)', odata_version)
service = pyodata.v2.service.Service(url, schema, connection)
return service
raise PyODataException('No implementation for selected odata version {}'.format(odata_version))
def schema_from_xml(metadata_xml, namespaces=None):
"""Parses XML data and returns Schema representing OData Metadata"""
meta = MetadataBuilder(
metadata_xml,
config=Config(
xml_namespaces=namespaces,
))
return meta.build()
def __init__(self, xml, config=None):
self._xml = xml
if config is None:
config = Config()
self._config = config