コード例 #1
0
def test_schema_ref_01():
    data = {
        'definitions': {
            'Product': {
                'properties': {
                    'price': {
                        'type': 'number'
                    },
                    'name': {
                        'type': 'string'
                    },
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {
                        'type': 'string'
                    },
                    'age': {
                        'type': 'number'
                    },
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    print("!!!!!!!!!!!!!!!!!!", data['definitions']['definitions'])
    assert len(data['definitions']['definitions']) == 2
コード例 #2
0
def test_schema_base_02():
    data = {
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['schemas']) == 1
コード例 #3
0
def test_schema_base_02():
    data = {
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['definitions']['definitions']) == 1
コード例 #4
0
def test_schema_base_03():
    data = {
        'parameters': {
            "limitParam": {
                "name": "limit",
                "in": "query",
                "description": "max records to return",
                "required": True,
                "type": "integer",
                "format": "int32"
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['schemas']) == 0
コード例 #5
0
def test_schema_base_03():
    data = {
        'parameters': {
            "limitParam": {
                "name": "limit",
                "in": "query",
                "description": "max records to return",
                "required": True,
                "type": "integer",
                "format": "int32"
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['definitions']['definitions']) == 0
コード例 #6
0
def test_schema_ref_01():
    data = {
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['schemas']) == 2
    assert list(data['schemas'].keys())[0] == 'DefinitionsUser'
コード例 #7
0
def test_schema_ref_01():
    data = {
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    print("!!!!!!!!!!!!!!!!!!", data['definitions']['definitions'])
    assert len(data['definitions']['definitions']) == 2
コード例 #8
0
def test_schema_ref_01():
    data = {
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['schemas']) == 2
    assert list(data['schemas'].keys())[0] == 'DefinitionsUser'
コード例 #9
0
def test_validators():
    data = {
        'paths': {
            '/users': {
                'parameters': [
                    {'name': 'page', 'in': 'query', 'type': 'number', 'required': True},
                    {'name': 'limit', 'in': 'query', 'type': 'number'}
                ],
                'post': {
                    'parameters': [
                        {'name': 'user', 'in': 'body', 'schema': {'$ref': '#/definitions/User'}}
                    ]
                },
                'put': {
                    'parameters': [
                        {'name': 'user', 'in': 'body', 'schema': {
                            'properties': {
                                'id': {
                                    'type': 'number'
                                }
                            }
                        }}
                    ]
                }
            }
        },
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    schemas = data['schemas']
    validators = data['validators']

    # body parameters
    assert ('/users', 'POST') in validators
    v1 = validators[('/users', 'POST')]['body']
    assert v1 == schemas['DefinitionsUser']

    # query parameters
    v2 = validators[('/users', 'POST')]['query']
    assert v2 == dict(
        required=['page'],
        properties=dict(
            page=dict(type='number'),
            limit=dict(type='number')))

    # no others parameters
    assert 'path' not in validators[('/users', 'POST')]

    # definitions
    assert 'DefinitionsUser' in schemas
    assert 'DefinitionsProduct' in schemas

    assert len(schemas) == 2
コード例 #10
0
def test_schema_base_01():
    data = {}
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['schemas']) == 0
コード例 #11
0
def test_filters():
    data = {
        'paths': {
            '/users': {
                'parameters': [
                    {'name': 'page', 'in': 'query', 'type': 'number', 'required': True},
                    {'name': 'limit', 'in': 'query', 'type': 'number'}
                ],
                'post': {
                    'parameters': [
                        {'name': 'user', 'in': 'body', 'schema': {'$ref': '#/definitions/User'}}
                    ],
                    'responses': {
                        '201': {
                            'schema': {'$ref': '#/definitions/User'},
                            'headers': {'X-COUNT': {'type': 'number'}},
                            'examples': {'application/json': {'id': 1, 'name': 'Bob', 'age': 12}}
                        },
                        '422': {
                            'schema': {
                                'properties': {
                                    'code': {'type': 'string'},
                                    'message': {'type': 'string'}
                                }
                            }
                        }
                    }
                }
            }
        },
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    schemas = data['schemas']
    filters = data['filters']

    assert 201 in filters[('/users', 'POST')]
    assert 422 in filters[('/users', 'POST')]

    r1 = filters[('/users', 'POST')][201]
    r2 = filters[('/users', 'POST')][422]

    assert r1['schema'] == schemas['DefinitionsUser']
    assert r2['schema']['properties']['code'] == {'type': 'string'}
コード例 #12
0
def test_validators():
    data = {
        'paths': {
            '/users': {
                'parameters': [
                    {'name': 'page', 'in': 'query', 'type': 'number', 'required': True},
                    {'name': 'limit', 'in': 'query', 'type': 'number'}
                ],
                'post': {
                    'parameters': [
                        {'name': 'user', 'in': 'body', 'schema': {'$ref': '#/definitions/User'}}
                    ]
                },
                'put': {
                    'parameters': [
                        {'name': 'user', 'in': 'body', 'schema': {
                            'properties': {
                                'id': {
                                    'type': 'number'
                                }
                            }
                        }}
                    ]
                }
            }
        },
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    schemas = data['definitions']['definitions']
    validators = data['validators']

    # body parameters
    assert ('/users', 'POST') in validators
    v1 = validators[('/users', 'POST')]['body']
    assert(v1 == RefNode(schemas['User'], '#/definitions/User'))

    # query parameters
    v2 = validators[('/users', 'POST')]['query']
    assert v2 == dict(
        required=['page'],
        properties=dict(
            page=dict(type='number'),
            limit=dict(type='number')))

    # no others parameters
    assert 'path' not in validators[('/users', 'POST')]

    # definitions
    assert 'User' in schemas
    assert 'Product' in schemas

    assert len(schemas) == 2
コード例 #13
0
def test_schema_base_01():
    data = {}
    swagger = Swagger(data)
    data = build_data(swagger)
    assert len(data['definitions']['definitions']) == 0
コード例 #14
0
def test_filters():
    data = {
        'paths': {
            '/users': {
                'parameters': [
                    {'name': 'page', 'in': 'query', 'type': 'number', 'required': True},
                    {'name': 'limit', 'in': 'query', 'type': 'number'}
                ],
                'post': {
                    'parameters': [
                        {'name': 'user', 'in': 'body', 'schema': {'$ref': '#/definitions/User'}}
                    ],
                    'responses': {
                        '201': {
                            'schema': {'$ref': '#/definitions/User'},
                            'headers': {'X-COUNT': {'type': 'number'}},
                            'examples': {'application/json': {'id': 1, 'name': 'Bob', 'age': 12}}
                        },
                        '422': {
                            'schema': {
                                'properties': {
                                    'code': {'type': 'string'},
                                    'message': {'type': 'string'}
                                }
                            }
                        }
                    }
                }
            }
        },
        'definitions': {
            'Product': {
                'properties': {
                    'price': {'type': 'number'},
                    'name': {'type': 'string'},
                    'seller': {
                        '$ref': '#/definitions/User'
                    }
                }
            },
            'User': {
                'properties': {
                    'name': {'type': 'string'},
                    'age': {'type': 'number'},
                }
            }
        }
    }
    swagger = Swagger(data)
    data = build_data(swagger)
    definitions = data['definitions']['definitions']
    filters = data['filters']

    assert 201 in filters[('/users', 'POST')]
    assert 422 in filters[('/users', 'POST')]

    r1 = filters[('/users', 'POST')][201]
    r2 = filters[('/users', 'POST')][422]

    assert r1['schema'] == RefNode(definitions['User'], '#/definitions/User')
    assert r2['schema']['properties']['code'] == {'type': 'string'}