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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
# -*- coding: utf-8 -*-
# @Author : llc
# @Time : 2022/12/5 10:27
from typing import Optional
import pytest
from pydantic import BaseModel, Field
from flask_openapi3 import OpenAPI, APIView
app = OpenAPI(__name__)
app.config["TESTING"] = True
api_view = APIView(url_prefix="/api/v1")
class Query(BaseModel):
q: str
class BookQuery(BaseModel):
age: Optional[int] = Field(None, description="Age")
class BookBody(BaseModel):
age: Optional[int] = Field(..., ge=2, le=4, description="Age")
author: str = Field(None, min_length=2, max_length=4, description="Author")
@app.get("/open/api")
async def get_openapi(query: Query):
print(query)
return "GET, OpenAPI!"
@app.post("/open/api")
async def post_openapi(body: Query):
print(body)
return "POST, OpenAPI!"
@api_view.route("/book")
class BookListAPIView:
@api_view.doc(summary="get book list")
async def get(self, query: BookQuery):
return query.model_dump_json()
@api_view.doc(summary="create book")
async def post(self, body: BookBody):
"""description for a created book"""
return body.model_dump_json()
app.register_api_view(api_view)
@pytest.fixture
def client():
client = app.test_client()
return client
def test_openapi(client):
resp = client.get("/openapi/openapi.json")
assert resp.status_code == 200
assert resp.json == app.api_doc
def test_get_openapi(client):
resp = client.get("/open/api?q=1")
assert resp.status_code == 200
assert resp.text == "GET, OpenAPI!"
def test_post_openapi(client):
resp = client.post("/open/api", json={"q": "string"})
assert resp.status_code == 200
assert resp.text == "POST, OpenAPI!"
def test_get_list(client):
resp = client.get("/api/v1/book")
assert resp.status_code == 200
def test_post(client):
resp = client.post("/api/v1/book", json={"age": 2})
assert resp.status_code == 200
|