-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_openapi_with_pep563.py
120 lines (91 loc) · 3.77 KB
/
test_openapi_with_pep563.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import annotations
from pydantic import BaseModel, Field
from typing_extensions import Annotated # noqa: TC002
from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.models import (
ParameterInType,
Schema,
)
from aws_lambda_powertools.event_handler.openapi.params import (
Body,
Query,
)
JSON_CONTENT_TYPE = "application/json"
class Todo(BaseModel):
id: int = Field(examples=[1])
title: str = Field(examples=["Example 1"])
priority: float = Field(examples=[0.5])
completed: bool = Field(examples=[True])
def test_openapi_with_pep563_and_input_model():
app = APIGatewayRestResolver()
@app.get("/users", summary="Get Users", operation_id="GetUsers", description="Get paginated users", tags=["Users"])
def handler(
count: Annotated[
int,
Query(gt=0, lt=100, examples=["Example 1"]),
] = 1,
):
print(count)
raise NotImplementedError()
schema = app.get_openapi_schema()
get = schema.paths["/users"].get
assert len(get.parameters) == 1
assert get.summary == "Get Users"
assert get.operationId == "GetUsers"
assert get.description == "Get paginated users"
assert get.tags == ["Users"]
parameter = get.parameters[0]
assert parameter.required is False
assert parameter.name == "count"
assert parameter.in_ == ParameterInType.query
assert parameter.schema_.type == "integer"
assert parameter.schema_.default == 1
assert parameter.schema_.title == "Count"
assert parameter.schema_.exclusiveMinimum == 0
assert parameter.schema_.exclusiveMaximum == 100
assert len(parameter.schema_.examples) == 1
assert parameter.schema_.examples[0] == "Example 1"
def test_openapi_with_pep563_and_output_model():
app = APIGatewayRestResolver()
@app.get("/")
def handler() -> Todo:
return Todo(id=0, title="", priority=0.0, completed=False)
schema = app.get_openapi_schema()
assert "Todo" in schema.components.schemas
todo_schema = schema.components.schemas["Todo"]
assert isinstance(todo_schema, Schema)
assert "id" in todo_schema.properties
id_property = todo_schema.properties["id"]
assert id_property.examples == [1]
assert "title" in todo_schema.properties
title_property = todo_schema.properties["title"]
assert title_property.examples == ["Example 1"]
assert "priority" in todo_schema.properties
priority_property = todo_schema.properties["priority"]
assert priority_property.examples == [0.5]
assert "completed" in todo_schema.properties
completed_property = todo_schema.properties["completed"]
assert completed_property.examples == [True]
def test_openapi_with_pep563_and_annotated_body():
app = APIGatewayRestResolver()
@app.post("/todo")
def create_todo(
todo_create_request: Annotated[Todo, Body(title="New Todo")],
) -> dict:
return {"message": f"Created todo {todo_create_request.title}"}
schema = app.get_openapi_schema()
assert "Todo" in schema.components.schemas
todo_schema = schema.components.schemas["Todo"]
assert isinstance(todo_schema, Schema)
assert "id" in todo_schema.properties
id_property = todo_schema.properties["id"]
assert id_property.examples == [1]
assert "title" in todo_schema.properties
title_property = todo_schema.properties["title"]
assert title_property.examples == ["Example 1"]
assert "priority" in todo_schema.properties
priority_property = todo_schema.properties["priority"]
assert priority_property.examples == [0.5]
assert "completed" in todo_schema.properties
completed_property = todo_schema.properties["completed"]
assert completed_property.examples == [True]