|
| 1 | +"""Tests of OData Model: class VariableDeclaration""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import datetime |
| 5 | +from pyodata.v2.model import EdmStructTypeSerializer, Types, StructType, StructTypeProperty |
| 6 | +from pyodata.exceptions import PyODataException |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def complex_type_property_declarations(): |
| 11 | + return { |
| 12 | + 'TestString': (Types.parse_type_name('Edm.String'), "'FooBar'", "'FooBar'", 'FooBar'), |
| 13 | + 'TestBoolean': (Types.parse_type_name('Edm.Boolean'), False, 'false', False), |
| 14 | + 'TestInt64': (Types.parse_type_name('Edm.Int64'), '123L', '123L', 123), |
| 15 | + 'TestDateTime': (Types.parse_type_name('Edm.DateTime'), "/Date(2147483647000)/", "datetime'2038-01-19T3:14:7'", |
| 16 | + datetime.datetime(2038, 1, 19, hour=3, minute=14, second=7, tzinfo=datetime.timezone.utc)) |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | +def define_complex_type(complex_type_property_declarations, nullable = True): |
| 21 | + complex_typ = StructType('TestComplexType', 'Label Complex Type', False) |
| 22 | + |
| 23 | + for name, prop_decl in complex_type_property_declarations.items(): |
| 24 | + prop = StructTypeProperty(name, prop_decl[0], nullable, None, None, None, |
| 25 | + None, None, None, None, None, None, None, None, None, None, None, None) |
| 26 | + |
| 27 | + prop.typ = Types.from_name(prop.type_info.name) |
| 28 | + complex_typ._properties[prop.name] = prop |
| 29 | + prop.struct_type = complex_typ |
| 30 | + |
| 31 | + return complex_typ |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def complex_type_with_nullable_props(complex_type_property_declarations, nullable = True): |
| 36 | + return define_complex_type(complex_type_property_declarations, nullable=True) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def complex_type_without_nullable_props(complex_type_property_declarations, nullable = True): |
| 41 | + return define_complex_type(complex_type_property_declarations, nullable=False) |
| 42 | + |
| 43 | + |
| 44 | +def test_nullable_from_json_null_properties(complex_type_with_nullable_props, complex_type_property_declarations): |
| 45 | + entity_json = { prop_name: None for prop_name in complex_type_property_declarations.keys() } |
| 46 | + |
| 47 | + entity_odata = complex_type_with_nullable_props.traits.from_json(entity_json) |
| 48 | + |
| 49 | + assert entity_json.keys() == entity_odata.keys() |
| 50 | + |
| 51 | + for name, value in entity_odata.items(): |
| 52 | + assert value is None, f'Property: {name}' |
| 53 | + |
| 54 | + |
| 55 | +def test_non_nullable_from_json_null_properties(complex_type_without_nullable_props, complex_type_property_declarations): |
| 56 | + for prop_name in complex_type_property_declarations.keys(): |
| 57 | + entity_json = { prop_name : None } |
| 58 | + with pytest.raises(PyODataException): |
| 59 | + entity_odata = complex_type_without_nullable_props.traits.from_json(entity_json) |
| 60 | + |
| 61 | + |
| 62 | +def test_non_nullable_from_json(complex_type_without_nullable_props, complex_type_property_declarations): |
| 63 | + entity_json = { prop_name : prop_decl[1] for prop_name, prop_decl in complex_type_property_declarations.items() } |
| 64 | + |
| 65 | + entity_odata =complex_type_without_nullable_props.traits.from_json(entity_json) |
| 66 | + |
| 67 | + assert entity_json.keys() == entity_odata.keys() |
| 68 | + |
| 69 | + for name, value in entity_odata.items(): |
| 70 | + assert value == complex_type_property_declarations[name][3], f'Value of {name}' |
| 71 | + |
| 72 | + |
| 73 | +def test_nullable_from_literal_null_properties(complex_type_with_nullable_props, complex_type_property_declarations): |
| 74 | + entity_literal = { prop_name: None for prop_name in complex_type_property_declarations.keys() } |
| 75 | + |
| 76 | + entity_odata = complex_type_with_nullable_props.traits.from_literal(entity_literal) |
| 77 | + |
| 78 | + assert entity_literal.keys() == entity_odata.keys() |
| 79 | + |
| 80 | + for name, value in entity_odata.items(): |
| 81 | + assert value is None, f'Property: {name}' |
| 82 | + |
| 83 | + |
| 84 | +def test_non_nullable_from_literal_null_properties(complex_type_without_nullable_props, complex_type_property_declarations): |
| 85 | + for prop_name in complex_type_property_declarations.keys(): |
| 86 | + entity_literal = { prop_name : None } |
| 87 | + with pytest.raises(PyODataException): |
| 88 | + entity_odata = complex_type_without_nullable_props.traits.from_literal(entity_literal) |
| 89 | + |
| 90 | + |
| 91 | +def test_non_nullable_from_literal(complex_type_without_nullable_props, complex_type_property_declarations): |
| 92 | + entity_literal = { prop_name : prop_decl[2] for prop_name, prop_decl in complex_type_property_declarations.items() } |
| 93 | + |
| 94 | + entity_odata =complex_type_without_nullable_props.traits.from_literal(entity_literal) |
| 95 | + |
| 96 | + assert entity_literal.keys() == entity_odata.keys() |
| 97 | + |
| 98 | + for name, value in entity_odata.items(): |
| 99 | + assert value == complex_type_property_declarations[name][3], f'Value of {name}' |
0 commit comments