Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
)
metadata = MetadataBuilder(xml_builder.serialize())
try:
metadata.build()
assert 'Expected' == 'RuntimeError'
except RuntimeError as ex:
assert str(ex) == 'Target Type Dict of ValueHelper(Dict/Value) does not exist'
metadata.config.set_custom_error_policy({
ParserError.ANNOTATION: PolicyWarning()
})
metadata.build()
assert_logging_policy(mock_warning,
'RuntimeError',
'Target Type Dict of ValueHelper(Dict/Value) does not exist'
)
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)
decl.add_enum_type(etype)
for complex_type in schema_node.xpath('edm:ComplexType', namespaces=config.namespaces):
try:
ctype = ComplexType.from_etree(complex_type, config)
except (KeyError, AttributeError) as ex:
config.err_policy(ParserError.COMPLEX_TYPE).resolve(ex)
ctype = NullType(complex_type.get('Name'))
decl.add_complex_type(ctype)
for entity_type in schema_node.xpath('edm:EntityType', namespaces=config.namespaces):
try:
etype = EntityType.from_etree(entity_type, config)
except (KeyError, AttributeError) as ex:
config.err_policy(ParserError.ENTITY_TYPE).resolve(ex)
etype = NullType(entity_type.get('Name'))
decl.add_entity_type(etype)
# resolve types of properties
for stype in itertools.chain(schema.entity_types, schema.complex_types):
if isinstance(stype, NullType):
continue
if stype.kind == Typ.Kinds.Complex:
# skip collections (no need to assign any types since type of collection
# items is resolved separately
if stype.is_collection:
continue
for prop in stype.proprties():
# Parse Schema nodes by parts to get over the problem of not-yet known
# entity types referenced by entity sets, function imports and
# annotations.
# First, process EnumType, EntityType and ComplexType nodes. They have almost no dependencies on other elements.
for schema_node in schema_nodes:
namespace = schema_node.get('Namespace')
decl = Schema.Declaration(namespace)
schema._decls[namespace] = decl
for enum_type in schema_node.xpath('edm:EnumType', namespaces=config.namespaces):
try:
etype = EnumType.from_etree(enum_type, namespace, config)
except (PyODataParserError, AttributeError) as ex:
config.err_policy(ParserError.ENUM_TYPE).resolve(ex)
etype = NullType(enum_type.get('Name'))
decl.add_enum_type(etype)
for complex_type in schema_node.xpath('edm:ComplexType', namespaces=config.namespaces):
try:
ctype = ComplexType.from_etree(complex_type, config)
except (KeyError, AttributeError) as ex:
config.err_policy(ParserError.COMPLEX_TYPE).resolve(ex)
ctype = NullType(complex_type.get('Name'))
decl.add_complex_type(ctype)
for entity_type in schema_node.xpath('edm:EntityType', namespaces=config.namespaces):
try:
etype = EntityType.from_etree(entity_type, config)
schema._decls[namespace] = decl
for enum_type in schema_node.xpath('edm:EnumType', namespaces=config.namespaces):
try:
etype = EnumType.from_etree(enum_type, namespace, config)
except (PyODataParserError, AttributeError) as ex:
config.err_policy(ParserError.ENUM_TYPE).resolve(ex)
etype = NullType(enum_type.get('Name'))
decl.add_enum_type(etype)
for complex_type in schema_node.xpath('edm:ComplexType', namespaces=config.namespaces):
try:
ctype = ComplexType.from_etree(complex_type, config)
except (KeyError, AttributeError) as ex:
config.err_policy(ParserError.COMPLEX_TYPE).resolve(ex)
ctype = NullType(complex_type.get('Name'))
decl.add_complex_type(ctype)
for entity_type in schema_node.xpath('edm:EntityType', namespaces=config.namespaces):
try:
etype = EntityType.from_etree(entity_type, config)
except (KeyError, AttributeError) as ex:
config.err_policy(ParserError.ENTITY_TYPE).resolve(ex)
etype = NullType(entity_type.get('Name'))
decl.add_entity_type(etype)
# resolve types of properties
for stype in itertools.chain(schema.entity_types, schema.complex_types):
if isinstance(stype, NullType):
# resolve types of properties
for stype in itertools.chain(schema.entity_types, schema.complex_types):
if isinstance(stype, NullType):
continue
if stype.kind == Typ.Kinds.Complex:
# skip collections (no need to assign any types since type of collection
# items is resolved separately
if stype.is_collection:
continue
for prop in stype.proprties():
try:
prop.typ = schema.get_type(prop.type_info)
except PyODataModelError as ex:
config.err_policy(ParserError.PROPERTY).resolve(ex)
prop.typ = NullType(prop.type_info.name)
# pylint: disable=too-many-nested-blocks
# Then, process Associations nodes because they refer EntityTypes and
# they are referenced by AssociationSets.
for schema_node in schema_nodes:
namespace = schema_node.get('Namespace')
decl = schema._decls[namespace]
for association in schema_node.xpath('edm:Association', namespaces=config.namespaces):
assoc = Association.from_etree(association, config)
try:
for end_role in assoc.end_roles:
try:
# search and assign entity type (it must exist)
if end_role.entity_type_info.namespace is None:
# resolve navigation properties
for stype in schema.entity_types:
# skip null type
if isinstance(stype, NullType):
continue
# skip collections
if stype.is_collection:
continue
for nav_prop in stype.nav_proprties:
try:
assoc = schema.association(nav_prop.association_info.name, nav_prop.association_info.namespace)
nav_prop.association = assoc
except KeyError as ex:
config.err_policy(ParserError.ASSOCIATION).resolve(ex)
nav_prop.association = NullAssociation(nav_prop.association_info.name)
# Then, process EntitySet, FunctionImport and AssociationSet nodes.
for schema_node in schema_nodes:
namespace = schema_node.get('Namespace')
decl = schema._decls[namespace]
for entity_set in schema_node.xpath('edm:EntityContainer/edm:EntitySet', namespaces=config.namespaces):
eset = EntitySet.from_etree(entity_set)
eset.entity_type = schema.entity_type(eset.entity_type_info[1], namespace=eset.entity_type_info[0])
decl.entity_sets[eset.name] = eset
for function_import in schema_node.xpath('edm:EntityContainer/edm:FunctionImport', namespaces=config.namespaces):
efn = FunctionImport.from_etree(function_import, config)
# complete type information for return type and parameters