Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_types():
"""Test Types repository"""
# generic
for type_name in ['Edm.Binary', 'Edm.String', 'Edm.Int16', 'Edm.Guid']:
typ = Types.from_name(type_name)
assert typ.kind == Typ.Kinds.Primitive
assert not typ.is_collection
# Collection of primitive types
typ = Types.from_name('Collection(Edm.String)')
assert repr(typ) == 'Collection(Typ(Edm.String))'
assert typ.kind is Typ.Kinds.Primitive
assert typ.is_collection
assert typ.name == 'Edm.String'
def test_traits_collections():
"""Test collection traits"""
typ = Types.from_name('Collection(Edm.Int32)')
assert typ.traits.from_json(['23', '34']) == [23, 34]
typ = Types.from_name('Collection(Edm.String)')
assert typ.traits.from_json(['Bob', 'Alice']) == ['Bob', 'Alice']
def test_traits_collections():
"""Test collection traits"""
typ = Types.from_name('Collection(Edm.Int32)')
assert typ.traits.from_json(['23', '34']) == [23, 34]
typ = Types.from_name('Collection(Edm.String)')
assert typ.traits.from_json(['Bob', 'Alice']) == ['Bob', 'Alice']
assert typ.traits.from_literal('true') is True
assert typ.traits.to_literal(False) == 'false'
assert typ.traits.from_literal('false') is False
assert typ.traits.to_literal(1) == 'true'
assert typ.traits.to_literal(0) == 'false'
assert typ.traits.from_json(True) is True
assert typ.traits.from_json(False) is False
# integers
typ = Types.from_name('Edm.Int16')
assert repr(typ.traits) == 'EdmIntTypTraits'
assert typ.traits.to_literal(23) == '23'
assert typ.traits.from_literal('345') == 345
typ = Types.from_name('Edm.Int32')
assert repr(typ.traits) == 'EdmIntTypTraits'
assert typ.traits.to_literal(23) == '23'
assert typ.traits.from_literal('345') == 345
typ = Types.from_name('Edm.Int64')
assert repr(typ.traits) == 'EdmLongIntTypTraits'
assert typ.traits.to_literal(23) == '23L'
assert typ.traits.from_literal('345L') == 345
assert typ.traits.from_json('345L') == 345
assert typ.traits.from_literal('345') == 345
assert typ.traits.from_json('345') == 345
assert typ.traits.from_literal('0') == 0
assert typ.traits.from_json('0') == 0
assert typ.traits.from_literal('0L') == 0
assert typ.traits.from_json('0L') == 0
def test_traits():
"""Test individual traits"""
# generic
typ = Types.from_name('Edm.Binary')
assert repr(typ.traits) == 'TypTraits'
assert typ.traits.to_literal('bincontent') == 'bincontent'
assert typ.traits.from_literal('some bin content') == 'some bin content'
# string
typ = Types.from_name('Edm.String')
assert repr(typ.traits) == 'EdmStringTypTraits'
assert typ.traits.to_literal('Foo Foo') == "'Foo Foo'"
assert typ.traits.from_literal("'Alice Bob'") == 'Alice Bob'
# bool
typ = Types.from_name('Edm.Boolean')
assert repr(typ.traits) == 'EdmBooleanTypTraits'
assert typ.traits.to_literal(True) == 'true'
assert typ.traits.from_literal('true') is True
assert typ.traits.to_literal(False) == 'false'
# bool
typ = Types.from_name('Edm.Boolean')
assert repr(typ.traits) == 'EdmBooleanTypTraits'
assert typ.traits.to_literal(True) == 'true'
assert typ.traits.from_literal('true') is True
assert typ.traits.to_literal(False) == 'false'
assert typ.traits.from_literal('false') is False
assert typ.traits.to_literal(1) == 'true'
assert typ.traits.to_literal(0) == 'false'
assert typ.traits.from_json(True) is True
assert typ.traits.from_json(False) is False
# integers
typ = Types.from_name('Edm.Int16')
assert repr(typ.traits) == 'EdmIntTypTraits'
assert typ.traits.to_literal(23) == '23'
assert typ.traits.from_literal('345') == 345
typ = Types.from_name('Edm.Int32')
assert repr(typ.traits) == 'EdmIntTypTraits'
assert typ.traits.to_literal(23) == '23'
assert typ.traits.from_literal('345') == 345
typ = Types.from_name('Edm.Int64')
assert repr(typ.traits) == 'EdmLongIntTypTraits'
assert typ.traits.to_literal(23) == '23L'
assert typ.traits.from_literal('345L') == 345
assert typ.traits.from_json('345L') == 345
assert typ.traits.from_literal('345') == 345
assert typ.traits.from_json('345') == 345
assert typ.traits.from_literal('345') == 345
typ = Types.from_name('Edm.Int64')
assert repr(typ.traits) == 'EdmLongIntTypTraits'
assert typ.traits.to_literal(23) == '23L'
assert typ.traits.from_literal('345L') == 345
assert typ.traits.from_json('345L') == 345
assert typ.traits.from_literal('345') == 345
assert typ.traits.from_json('345') == 345
assert typ.traits.from_literal('0') == 0
assert typ.traits.from_json('0') == 0
assert typ.traits.from_literal('0L') == 0
assert typ.traits.from_json('0L') == 0
# GUIDs
typ = Types.from_name('Edm.Guid')
assert repr(typ.traits) == 'EdmPrefixedTypTraits'
assert typ.traits.to_literal('000-0000') == "guid'000-0000'"
assert typ.traits.from_literal("guid'1234-56'") == '1234-56'
with pytest.raises(PyODataModelError) as e_info:
typ.traits.from_literal("'1234-56'")
assert str(e_info.value).startswith("Malformed value '1234-56' for primitive")
def get_type(self, type_info):
# construct search name based on collection information
search_name = type_info.name if not type_info.is_collection else 'Collection({})'.format(type_info.name)
# first look for type in primitive types
try:
return Types.from_name(search_name)
except KeyError:
pass
# then look for type in entity types
try:
return self.entity_type(search_name, type_info.namespace)
except KeyError:
pass
# then look for type in complex types
try:
return self.complex_type(search_name, type_info.namespace)
except KeyError:
pass
# then look for type in enum types
underlying_type = type_node.get('UnderlyingType')
valid_types = {
'Edm.Byte': [0, 2 ** 8 - 1],
'Edm.Int16': [-2 ** 15, 2 ** 15 - 1],
'Edm.Int32': [-2 ** 31, 2 ** 31 - 1],
'Edm.Int64': [-2 ** 63, 2 ** 63 - 1],
'Edm.SByte': [-2 ** 7, 2 ** 7 - 1]
}
if underlying_type not in valid_types:
raise PyODataParserError(
f'Type {underlying_type} is not valid as underlying type for EnumType - must be one of {valid_types}')
mtype = Types.from_name(underlying_type)
etype = EnumType(ename, is_flags, mtype, namespace)
members = type_node.xpath('edm:Member', namespaces=config.namespaces)
next_value = 0
for member in members:
name = member.get('Name')
value = member.get('Value')
if value is not None:
next_value = int(value)
vtype = valid_types[underlying_type]
if not vtype[0] < next_value < vtype[1]:
raise PyODataParserError(f'Value {next_value} is out of range for type {underlying_type}')