def test_non_nullable_none_value(empty_swagger_spec, property_type):
    content_spec = nullable_spec_factory(required=True,
                                         nullable=False,
                                         property_type=property_type)
    value = {'x': None}
    with pytest.raises(SwaggerMappingError) as excinfo:
        unmarshal_object(empty_swagger_spec, content_spec, value)
    assert 'is a required value' in str(excinfo.value)
def test_non_nullable_none_value(empty_swagger_spec, property_type):
    content_spec = nullable_spec_factory(required=True,
                                         nullable=False,
                                         property_type=property_type)
    value = {'x': None}
    with pytest.raises(SwaggerMappingError) as excinfo:
        unmarshal_object(empty_swagger_spec, content_spec, value)
    assert 'is a required value' in str(excinfo.value)
def test_recursive_ref_with_depth_1(recursive_swagger_spec):
    result = unmarshal_object(
        recursive_swagger_spec,
        {'$ref': '#/definitions/Node'},
        {'name': 'foo'},
    )
    assert result == {'name': 'foo', 'date': None, 'child': None}
def test_recursive_ref_with_depth_n(recursive_swagger_spec):
    result = unmarshal_object(
        recursive_swagger_spec,
        {'$ref': '#/definitions/Node'},
        {
            'name': 'foo',
            'date': '2019-05-01',
            'child': {
                'name': 'bar',
                'date': '2019-05-02',
                'child': {
                    'name': 'baz',
                },
            },
        },
    )
    assert result == {
        'name': 'foo',
        'date': datetime.date(2019, 5, 1),
        'child': {
            'name': 'bar',
            'date': datetime.date(2019, 5, 2),
            'child': {
                'name': 'baz',
                'date': None,
                'child': None,
            },
        },
    }
def test_non_required_none_value(empty_swagger_spec, property_type):
    content_spec = nullable_spec_factory(required=False,
                                         nullable=False,
                                         property_type=property_type)
    value = {'x': None}
    result = unmarshal_object(empty_swagger_spec, content_spec, value)
    assert result == {'x': None}
def test_nullable_no_value(empty_swagger_spec, nullable, property_type):
    content_spec = nullable_spec_factory(required=False,
                                         nullable=nullable,
                                         property_type=property_type)
    value = {}
    result = unmarshal_object(empty_swagger_spec, content_spec, value)
    assert result == {'x': None}  # Missing parameters are re-introduced
def test_nullable_with_value(empty_swagger_spec, nullable, required,
                             property_type, value):
    content_spec = nullable_spec_factory(required, nullable, property_type)
    obj = {'x': value}
    expected = copy.deepcopy(obj)
    result = unmarshal_object(empty_swagger_spec, content_spec, obj)
    assert expected == result
def test_nullable_with_value(empty_swagger_spec, nullable, required,
                             property_type, value):
    content_spec = nullable_spec_factory(required, nullable, property_type)
    obj = {'x': value}
    expected = copy.deepcopy(obj)
    result = unmarshal_object(empty_swagger_spec, content_spec, obj)
    assert expected == result
def test_non_required_none_value(empty_swagger_spec, property_type):
    content_spec = nullable_spec_factory(required=False,
                                         nullable=False,
                                         property_type=property_type)
    value = {'x': None}
    result = unmarshal_object(empty_swagger_spec, content_spec, value)
    assert result == {'x': None}
Exemplo n.º 10
0
def test_nullable_no_value(empty_swagger_spec, nullable, property_type):
    content_spec = nullable_spec_factory(required=False,
                                         nullable=nullable,
                                         property_type=property_type)
    value = {}
    result = unmarshal_object(empty_swagger_spec, content_spec, value)
    assert result == {'x': None}  # Missing parameters are re-introduced
Exemplo n.º 11
0
def test_unmarshal_object_polymorphic_specs(polymorphic_spec):
    list_of_pets_dict = {
        'number_of_pets':
        2,
        'list': [
            {
                'name': 'a dog name',
                'type': 'Dog',
                'birth_date': '2017-03-09',
            },
            {
                'name': 'a cat name',
                'type': 'Cat',
                'color': 'white',
            },
        ]
    }
    polymorphic_spec.config['use_models'] = False
    pet_list = unmarshal_object(
        swagger_spec=polymorphic_spec,
        object_spec=polymorphic_spec.spec_dict['definitions']['PetList'],
        object_value=list_of_pets_dict,
    )

    assert list_of_pets_dict == pet_list
Exemplo n.º 12
0
def test_recursive_ref_with_depth_n(recursive_swagger_spec):
    value = {
        'name': 'foo',
        'child': {
            'name': 'bar',
            'child': {
                'name': 'baz'
            }
        }
    }
    result = unmarshal_object(
        recursive_swagger_spec,
        {'$ref': '#/definitions/Node'},
        value)

    expected = {
        'name': 'foo',
        'child': {
            'name': 'bar',
            'child': {
                'name': 'baz',
                'child': None
            }
        }
    }
    assert result == expected
Exemplo n.º 13
0
def test_with_properties(empty_swagger_spec, address_spec, address):
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': 'Avenue'
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 14
0
def test_properties(empty_swagger_spec, address_spec, address):
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': 'Avenue'
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
def test_self_property_with_model(minimal_swagger_dict):
    link_spec = {
        'type': 'object',
        'required': ['_links'],
        'properties': {
            '_links': {
                '$ref': '#/definitions/Self',
            },
        },
    }

    self_spec = {
        'type': 'object',
        'required': ['self'],
        'properties': {
            'self': {
                'type': 'object',
                'required': ['href'],
                'properties': {
                    'href': {
                        'type': 'string',
                    },
                },
            },
        },
    }

    minimal_swagger_dict['definitions']['Links'] = link_spec
    minimal_swagger_dict['definitions']['Self'] = self_spec

    self_link_response = {
        'get': {
            'responses': {
                '200': {
                    'description': 'A self link.',
                    'schema': {
                        '$ref': '#/definitions/Links',
                    }
                }
            }
        }
    }
    minimal_swagger_dict['paths']['/foo'] = self_link_response

    self_link_swagger_spec = Spec.from_dict(minimal_swagger_dict)

    href = "http://example.com"
    self_link_dict = {
        "_links": {
            "self": {
                "href": href,
            },
        },
    }

    self_link = unmarshal_object(self_link_swagger_spec, link_spec,
                                 self_link_dict)
    assert self_link["_links"].self["href"] == href
Exemplo n.º 16
0
def test_missing_with_default(empty_swagger_spec, address_spec, address):
    del address['street_type']
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': 'Street',
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 17
0
def test_with_properties(empty_swagger_spec, address_spec, address, street_type, expected_street_type):
    address['street_type'] = street_type
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': expected_street_type,
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 18
0
def test_self_property_with_model(minimal_swagger_dict):
    link_spec = {
        'type': 'object',
        'required': ['_links'],
        'properties': {
            '_links': {
                '$ref': '#/definitions/Self',
            },
        },
    }

    self_spec = {
        'type': 'object',
        'required': ['self'],
        'properties': {
            'self': {
                'type': 'object',
                'required': ['href'],
                'properties': {
                    'href': {
                        'type': 'string',
                    },
                },
            },
        },
    }

    minimal_swagger_dict['definitions']['Links'] = link_spec
    minimal_swagger_dict['definitions']['Self'] = self_spec

    self_link_response = {
        'get': {
            'responses': {
                '200': {
                    'description': 'A self link.',
                    'schema': {
                        '$ref': '#/definitions/Links',
                    }
                }
            }
        }
    }
    minimal_swagger_dict['paths']['/foo'] = self_link_response

    self_link_swagger_spec = Spec.from_dict(minimal_swagger_dict)

    href = "http://example.com"
    self_link_dict = {
        "_links": {
            "self": {
                "href": href,
            },
        },
    }

    self_link = unmarshal_object(self_link_swagger_spec, link_spec, self_link_dict)
    assert self_link["_links"].self["href"] == href
Exemplo n.º 19
0
def test_missing_with_default(empty_swagger_spec, address_spec, address):
    del address['street_type']
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': 'Street',
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 20
0
def test_mising_properties_set_to_None(empty_swagger_spec, address_spec,
                                       address):
    del address['street_name']
    expected_address = {
        'number': 1600,
        'street_name': None,
        'street_type': 'Avenue'
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 21
0
def test_with_properties(empty_swagger_spec, address_spec, address,
                         street_type, expected_street_type):
    address['street_type'] = street_type
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': expected_street_type,
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 22
0
def test_mising_properties_set_to_None(
        empty_swagger_spec, address_spec, address):
    del address['street_name']
    expected_address = {
        'number': 1600,
        'street_name': None,
        'street_type': 'Avenue'
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 23
0
def test_array(empty_swagger_spec, address_spec):
    tags_spec = {'type': 'array', 'items': {'type': 'string'}}
    address_spec['properties']['tags'] = tags_spec
    address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'tags': ['home', 'best place on earth', 'cul de sac'],
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
Exemplo n.º 24
0
def test_model(minimal_swagger_dict, address_spec):
    location_spec = {
        'type': 'object',
        'properties': {
            'longitude': {
                'type': 'number'
            },
            'latitude': {
                'type': 'number'
            },
        }
    }
    minimal_swagger_dict['definitions']['Location'] = location_spec

    # The Location model type won't be built on schema ingestion unless
    # something actually references it. Create a throwaway response for this
    # purpose.
    location_response = {
        'get': {
            'responses': {
                '200': {
                    'description': 'A location',
                    'schema': {
                        '$ref': '#/definitions/Location',
                    }
                }
            }
        }
    }
    minimal_swagger_dict['paths']['/foo'] = location_response

    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    address_spec['properties']['location'] = \
        swagger_spec.spec_dict['definitions']['Location']
    Location = swagger_spec.definitions['Location']

    address_dict = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    expected_address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': Location(longitude=100.1, latitude=99.9),
    }

    address = unmarshal_object(swagger_spec, address_spec, address_dict)
    assert expected_address == address
def test_model(minimal_swagger_dict, address_spec):
    location_spec = {
        'type': 'object',
        'properties': {
            'longitude': {
                'type': 'number'
            },
            'latitude': {
                'type': 'number'
            },
        }
    }
    minimal_swagger_dict['definitions']['Location'] = location_spec

    # The Location model type won't be built on schema ingestion unless
    # something actually references it. Create a throwaway response for this
    # purpose.
    location_response = {
        'get': {
            'responses': {
                '200': {
                    'description': 'A location',
                    'schema': {
                        '$ref': '#/definitions/Location',
                    }
                }
            }
        }
    }
    minimal_swagger_dict['paths']['/foo'] = location_response

    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    address_spec['properties']['location'] = \
        swagger_spec.spec_dict['definitions']['Location']
    Location = swagger_spec.definitions['Location']

    address_dict = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    expected_address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': Location(longitude=100.1, latitude=99.9),
    }

    address = unmarshal_object(swagger_spec, address_spec, address_dict)
    assert expected_address == address
Exemplo n.º 26
0
def test_pass_through_additionalProperties_with_no_spec(
        empty_swagger_spec, address_spec, address):
    address_spec['additionalProperties'] = True
    address['city'] = 'Swaggerville'
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': 'Avenue',
        'city': 'Swaggerville',
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
Exemplo n.º 27
0
def test_pass_through_additionalProperties_with_no_spec(
        empty_swagger_spec, address_spec, address):
    address_spec['additionalProperties'] = True
    address['city'] = 'Swaggerville'
    expected_address = {
        'number': 1600,
        'street_name': u'Ümlaut',
        'street_type': 'Avenue',
        'city': 'Swaggerville',
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert expected_address == result
def test_with_nested_object(empty_swagger_spec, address_spec, location_spec):
    address_spec['properties']['location'] = location_spec
    address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
Exemplo n.º 29
0
def test_with_nested_object(empty_swagger_spec, address_spec, location_spec):
    address_spec['properties']['location'] = location_spec
    address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
Exemplo n.º 30
0
def test_with_ref(minimal_swagger_dict, address_spec, location_spec):
    minimal_swagger_dict['definitions']['Location'] = location_spec
    address_spec['properties']['location'] = {'$ref': '#/definitions/Location'}
    address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    minimal_swagger_spec = Spec(minimal_swagger_dict)
    result = unmarshal_object(minimal_swagger_spec, address_spec, address)
    assert result == address
def test_with_ref(minimal_swagger_dict, address_spec, location_spec):
    minimal_swagger_dict['definitions']['Location'] = location_spec
    address_spec['properties']['location'] = {'$ref': '#/definitions/Location'}
    address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    minimal_swagger_spec = Spec(minimal_swagger_dict)
    result = unmarshal_object(minimal_swagger_spec, address_spec, address)
    assert result == address
def test_recursive_ref_with_depth_n(recursive_swagger_spec):
    value = {'name': 'foo', 'child': {'name': 'bar', 'child': {'name': 'baz'}}}
    result = unmarshal_object(recursive_swagger_spec,
                              {'$ref': '#/definitions/Node'}, value)

    expected = {
        'name': 'foo',
        'child': {
            'name': 'bar',
            'child': {
                'name': 'baz',
                'child': None
            }
        }
    }
    assert result == expected
def test_unmarshal_object_with_additional_properties(minimal_swagger_dict,
                                                     additionalProperties,
                                                     value, expected):
    MyModel_spec = {
        'properties': {
            'property': {
                'type': 'string',
                'format': 'date-time',
            },
        },
        'type': 'object',
        'x-model': 'MyModel',
    }
    if additionalProperties is not None:
        MyModel_spec['additionalProperties'] = additionalProperties
    minimal_swagger_dict['definitions']['MyModel'] = MyModel_spec
    spec = Spec.from_dict(minimal_swagger_dict)
    assert unmarshal_object(spec, MyModel_spec, value) == expected
def test_default_with_format(empty_swagger_spec):
    result = unmarshal_object(
        empty_swagger_spec,
        {
            'type': 'object',
            'properties': {
                'item': {
                    'type': 'string',
                    'format': 'date',
                },
            },
            'default': {
                'item': '2019-05-22'
            },
        },
        None,
    )
    assert {'item': datetime.date(2019, 5, 22)} == result
def test_with_model_composition(business_address_swagger_spec, address_spec,
                                business_address_spec):
    business_address_dict = {
        'company': 'n/a',
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue'
    }
    expected_business_address = {
        'company': 'n/a',
        'number': 1600,
        'name': None,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue'
    }

    business_address = unmarshal_object(business_address_swagger_spec,
                                        business_address_spec,
                                        business_address_dict)
    assert expected_business_address == business_address
Exemplo n.º 36
0
def test_with_model_composition(business_address_swagger_spec, address_spec, business_address_spec):
    business_address_dict = {
        'company': 'n/a',
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue'
    }
    expected_business_address = {
        'company': 'n/a',
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
    }

    if business_address_swagger_spec.config['include_missing_properties']:
        expected_business_address.update(floor=None, name=None)

    business_address = unmarshal_object(business_address_swagger_spec, business_address_spec,
                                        business_address_dict)
    assert expected_business_address == business_address
Exemplo n.º 37
0
def test_with_array(empty_swagger_spec, address_spec):
    tags_spec = {
        'type': 'array',
        'items': {
            'type': 'string'
        }
    }
    address_spec['properties']['tags'] = tags_spec
    address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'tags': [
            'home',
            'best place on earth',
            'cul de sac'
        ],
    }
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
def test_missing_property_with_default_value(empty_swagger_spec,
                                             include_missing_properties,
                                             expected_value):
    empty_swagger_spec.config[
        'include_missing_properties'] = include_missing_properties
    result = unmarshal_object(
        empty_swagger_spec,
        {
            'type': 'object',
            'properties': {
                'item': {
                    'type': 'string',
                    'format': 'date',
                    'default': '2019-05-22',
                },
            },
        },
        {},
    )

    assert expected_value == result
Exemplo n.º 39
0
def test_with_model_composition(business_address_swagger_spec, address_spec,
                                business_address_spec):
    business_address_dict = {
        'company': 'n/a',
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue'
    }
    expected_business_address = {
        'company': 'n/a',
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
    }

    if business_address_swagger_spec.config['include_missing_properties']:
        expected_business_address.update(floor=None, name=None)

    business_address = unmarshal_object(business_address_swagger_spec,
                                        business_address_spec,
                                        business_address_dict)
    assert expected_business_address == business_address
Exemplo n.º 40
0
def test_model(minimal_swagger_dict, address_spec):
    location_spec = {
        'type': 'object',
        'properties': {
            'longitude': {
                'type': 'number'
            },
            'latitude': {
                'type': 'number'
            },
        }
    }
    minimal_swagger_dict['definitions']['Location'] = location_spec
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    address_spec['properties']['location'] = \
        swagger_spec.spec_dict['definitions']['Location']
    Location = swagger_spec.definitions['Location']

    address_dict = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': {
            'longitude': 100.1,
            'latitude': 99.9,
        },
    }
    expected_address = {
        'number': 1600,
        'street_name': 'Pennsylvania',
        'street_type': 'Avenue',
        'location': Location(longitude=100.1, latitude=99.9),
    }

    address = unmarshal_object(swagger_spec, address_spec, address_dict)
    assert expected_address == address
Exemplo n.º 41
0
def test_unmarshal_object_polymorphic_specs(polymorphic_spec):
    list_of_pets_dict = {
        'number_of_pets': 2,
        'list': [
            {
                'name': 'a dog name',
                'type': 'Dog',
                'birth_date': '2017-03-09',
            },
            {
                'name': 'a cat name',
                'type': 'Cat',
                'color': 'white',
            },
        ]
    }
    polymorphic_spec.config['use_models'] = False
    pet_list = unmarshal_object(
        swagger_spec=polymorphic_spec,
        object_spec=polymorphic_spec.spec_dict['definitions']['PetList'],
        object_value=list_of_pets_dict,
    )

    assert list_of_pets_dict == pet_list
def test_nullable_none_value(empty_swagger_spec, required):
    content_spec = nullable_spec_factory(required, True)
    value = {'x': None}

    result = unmarshal_object(empty_swagger_spec, content_spec, value)
    assert result == {'x': None}
Exemplo n.º 43
0
def test_recursive_ref_with_depth_1(recursive_swagger_spec):
    result = unmarshal_object(
        recursive_swagger_spec,
        {'$ref': '#/definitions/Node'},
        {'name': 'foo'})
    assert result == {'name': 'foo', 'child': None}
Exemplo n.º 44
0
def test_pass_through_null_property_with_no_spec(empty_swagger_spec,
                                                 address_spec, address):
    address['no_spec_field'] = None
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
Exemplo n.º 45
0
def test_object_not_dict_like_raises_error(empty_swagger_spec, address_spec):
    i_am_not_dict_like = 34
    with pytest.raises(SwaggerMappingError) as excinfo:
        unmarshal_object(empty_swagger_spec, address_spec, i_am_not_dict_like)
    assert 'Expected dict' in str(excinfo.value)
def test_pass_through_property_with_no_spec(empty_swagger_spec, address_spec,
                                            address):
    del address_spec['properties']['street_name']['type']
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
def test_nullable_with_value(empty_swagger_spec, nullable, required):
    content_spec = nullable_spec_factory(required, nullable)
    value = {'x': 'y'}

    result = unmarshal_object(empty_swagger_spec, content_spec, value)
    assert result == value
Exemplo n.º 48
0
def test_pass_through_property_with_no_spec(
        empty_swagger_spec, address_spec, address):
    del address_spec['properties']['street_name']['type']
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
Exemplo n.º 49
0
def test_pass_through_null_property_with_no_spec(empty_swagger_spec, address_spec, address):
    address['no_spec_field'] = None
    result = unmarshal_object(empty_swagger_spec, address_spec, address)
    assert result == address
Exemplo n.º 50
0
def test_object_not_dict_like_raises_error(empty_swagger_spec, address_spec):
    i_am_not_dict_like = 34
    with pytest.raises(SwaggerMappingError) as excinfo:
        unmarshal_object(empty_swagger_spec, address_spec, i_am_not_dict_like)
    assert 'Expected dict' in str(excinfo.value)