File: test_response.py

package info (click to toggle)
flask-openapi3 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,976 kB
  • sloc: python: 4,754; sh: 17; makefile: 15; javascript: 5
file content (62 lines) | stat: -rw-r--r-- 1,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2023/7/9 11:23
from http import HTTPStatus

import pytest
from pydantic import BaseModel, Field

from flask_openapi3 import OpenAPI, APIBlueprint

app = OpenAPI(__name__)
app.config["TESTING"] = True
api = APIBlueprint("/api", __name__, url_prefix="/api")


class BookResponse(BaseModel):
    code: int = Field(0, description="Status Code")
    message: str = Field("ok", description="Exception Information")


class BookPath(BaseModel):
    bid: int = Field(..., description="book id")


@pytest.fixture
def client():
    client = app.test_client()

    return client


@app.get(
    "/book/<int:bid>",
    responses={
        HTTPStatus.OK: BookResponse,
        "201": BookResponse,
        202: {"content": {"text/html": {"schema": {"type": "string"}}}},
        204: None,
        "422": {"description": "validation error"}
    }
)
def get_book(path: BookPath):
    print(path)  # pragma: no cover


@api.get("/book", responses={
    HTTPStatus.OK: BookResponse,
    "201": BookResponse, 204: None
})
def get_api_book():
    return {"code": 0, "message": "ok"}  # pragma: no cover


app.register_api(api)


def test_openapi(client):
    resp = client.get("/openapi/openapi.json")
    _json = resp.json
    assert resp.status_code == 200
    assert _json["paths"]["/book/{bid}"]["get"]["responses"].keys() - ["200", "201", "202", "204"] == {"422"}
    assert _json["paths"]["/api/book"]["get"]["responses"].keys() - ["200", "201", "202", "204"] == set()