Пример #1
0
def complex_type_property_declarations():
    return {
        'TestString': (Types.parse_type_name('Edm.String'), "'FooBar'", "'FooBar'", 'FooBar'),
        'TestBoolean': (Types.parse_type_name('Edm.Boolean'), False, 'false', False),
        'TestInt64': (Types.parse_type_name('Edm.Int64'), '123L', '123L', 123),
        'TestDateTime': (Types.parse_type_name('Edm.DateTime'), "/Date(2147483647000)/", "datetime'2038-01-19T3:14:7'",
                         datetime.datetime(2038, 1, 19, hour=3, minute=14, second=7, tzinfo=datetime.timezone.utc))
    }
Пример #2
0
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']
Пример #3
0
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():
    """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'
    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

    # 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")
Пример #5
0
def define_complex_type(complex_type_property_declarations, nullable = True):
    complex_typ = StructType('TestComplexType', 'Label Complex Type', False)

    for name, prop_decl in complex_type_property_declarations.items():
        prop = StructTypeProperty(name, prop_decl[0], nullable, None, None, None,
            None, None, None, None, None, None, None, None, None, None, None, None)

        prop.typ = Types.from_name(prop.type_info.name)
        complex_typ._properties[prop.name] = prop
        prop.struct_type = complex_typ

    return complex_typ
Пример #6
0
def test_type_parsing():
    """Test parsing of type names"""

    type_info = Types.parse_type_name('Edm.Boolean')
    assert type_info[0] is None
    assert type_info[1] == 'Edm.Boolean'
    assert not type_info[2]

    type_info = Types.parse_type_name('SomeType')
    assert type_info[0] is None
    assert type_info[1] == 'SomeType'
    assert not type_info[2]

    type_info = Types.parse_type_name('SomeNamespace.SomeType')
    assert type_info[0] == 'SomeNamespace'
    assert type_info[1] == 'SomeType'
    assert not type_info[2]

    # collections
    type_info = Types.parse_type_name('Collection(Edm.String)')
    assert type_info[0] is None
    assert type_info[1] == 'Edm.String'
    assert type_info[2]

    type_info = Types.parse_type_name('Collection(SomeType)')
    assert type_info[0] is None
    assert type_info[1] == 'SomeType'
    assert type_info[2]

    type_info = Types.parse_type_name('Collection(SomeNamespace.SomeType)')
    assert type_info[0] == 'SomeNamespace'
    assert type_info[1] == 'SomeType'
    assert type_info[2]
Пример #7
0
def type_date_time_offset():
    return Types.from_name('Edm.DateTimeOffset')
Пример #8
0
def type_date_time():
    return Types.from_name('Edm.DateTime')
Пример #9
0
def test_traits_datetime():
    """Test Edm.DateTime traits"""

    typ = Types.from_name('Edm.DateTime')
    assert repr(typ.traits) == 'EdmDateTimeTypTraits'

    # 1. direction Python -> OData

    testdate = datetime(2005, 1, 28, 18, 30, 44, 123456)
    assert typ.traits.to_literal(
        testdate) == "datetime'2005-01-28T18:30:44.123456'"

    # without miliseconds part
    testdate = datetime(2005, 1, 28, 18, 30, 44, 0)
    assert typ.traits.to_literal(testdate) == "datetime'2005-01-28T18:30:44'"

    # serialization of invalid value
    with pytest.raises(PyODataModelError) as e_info:
        typ.traits.to_literal('xyz')
    assert str(e_info.value).startswith('Cannot convert value of type')

    # 2. direction Literal -> python

    # parsing full representation
    testdate = typ.traits.from_literal("datetime'1976-11-23T03:33:06.654321'")
    assert testdate.year == 1976
    assert testdate.month == 11
    assert testdate.day == 23
    assert testdate.hour == 3
    assert testdate.minute == 33
    assert testdate.second == 6
    assert testdate.microsecond == 654321

    # parsing without miliseconds
    testdate = typ.traits.from_literal("datetime'1976-11-23T03:33:06'")
    assert testdate.year == 1976
    assert testdate.second == 6
    assert testdate.microsecond == 0

    # parsing without seconds and miliseconds
    testdate = typ.traits.from_literal("datetime'1976-11-23T03:33'")
    assert testdate.year == 1976
    assert testdate.minute == 33
    assert testdate.second == 0
    assert testdate.microsecond == 0

    # parsing invalid value
    with pytest.raises(PyODataModelError) as e_info:
        typ.traits.from_literal('xyz')
    assert str(e_info.value).startswith('Malformed value xyz for primitive')

    with pytest.raises(PyODataModelError) as e_info:
        typ.traits.from_literal("datetime'xyz'")
    assert str(
        e_info.value).startswith('Cannot decode datetime from value xyz')

    # 3. direction OData -> python

    # parsing full representation
    testdate = typ.traits.from_json("/Date(217567986010)/")
    assert testdate.year == 1976
    assert testdate.month == 11
    assert testdate.day == 23
    assert testdate.hour == 3
    assert testdate.minute == 33
    assert testdate.second == 6
    assert testdate.microsecond == 10000

    # parsing without miliseconds
    testdate = typ.traits.from_json("/Date(217567986000)/")
    assert testdate.year == 1976
    assert testdate.second == 6
    assert testdate.microsecond == 0

    # parsing without seconds and miliseconds
    testdate = typ.traits.from_json("/Date(217567980000)/")
    assert testdate.year == 1976
    assert testdate.minute == 33
    assert testdate.second == 0
    assert testdate.microsecond == 0

    # parsing invalid value
    with pytest.raises(PyODataModelError) as e_info:
        typ.traits.from_json("xyz")
    assert str(e_info.value).startswith('Malformed value xyz for primitive')

    with pytest.raises(PyODataModelError) as e_info:
        typ.traits.from_json("/Date(xyz)/")
    assert str(
        e_info.value).startswith('Cannot decode datetime from value xyz')
Пример #10
0
def variable_of_string_nullable():
    variable = VariableDeclaration('TestVariable',
                                   Types.parse_type_name('Edm.String'), True,
                                   None, None, None)
    variable.typ = Types.from_name(variable.type_info.name)
    return variable