Debian Codegraph @

tests/test_openapi_params.py

raw symbols SCIP
1 from ninja import NinjaAPI
2
3 api = NinjaAPI()
4
5
6 @api.get("/operation1", operation_id="my_id")
7 def operation_1(request):
8 """
9 This will be in description
10 """
11 return {"docstrings": True}
12
13
14 @api.get("/operation2", description="description from argument", deprecated=True)
15 def operation2(request):
16 return {"description": True, "deprecated": True}
17
18
19 @api.get("/operation3", summary="Summary from argument", description="description arg")
20 def operation3(request):
21 "This one also has docstring description"
22 return {"summary": True, "description": "multiple"}
23
24
25 @api.get("/operation4", tags=["tag1", "tag2"])
26 def operation4(request):
27 return {"tags": True}
28
29
30 @api.get(
31 "/operation5",
32 openapi_extra={
33 "requestBody": {
34 "content": {
35 "application/json": {
36 "schema": {
37 "required": ["email"],
38 "type": "object",
39 "properties": {
40 "name": {"type": "string"},
41 "phone": {"type": "number"},
42 "email": {"type": "string"},
43 },
44 }
45 }
46 },
47 "required": True,
48 },
49 },
50 )
51 def operation5(request):
52 return {"openapi_extra": True}
53
54
55 @api.get("/not-included", include_in_schema=False)
56 def not_included(request):
57 return True
58
59
60 schema = api.get_openapi_schema()
61
62
63 def test_schema():
64 op1 = schema["paths"]["/api/operation1"]["get"]
65 op2 = schema["paths"]["/api/operation2"]["get"]
66 op3 = schema["paths"]["/api/operation3"]["get"]
67 op4 = schema["paths"]["/api/operation4"]["get"]
68 op5 = schema["paths"]["/api/operation5"]["get"]
69
70 assert op1["operationId"] == "my_id"
71 assert op2["operationId"] == "test_openapi_params_operation2"
72 assert op3["operationId"] == "test_openapi_params_operation3"
73 assert op4["operationId"] == "test_openapi_params_operation4"
74 assert op5["operationId"] == "test_openapi_params_operation5"
75
76 assert op1["summary"] == "Operation 1"
77 assert op2["summary"] == "Operation2"
78 assert op3["summary"] == "Summary from argument"
79 assert op4["summary"] == "Operation4"
80 assert op5["summary"] == "Operation5"
81
82 assert op1["description"] == "This will be in description"
83
84 assert op2["description"] == "description from argument"
85 assert op2["deprecated"] is True
86
87 assert op3["description"] == "description arg"
88
89 assert op4["tags"] == ["tag1", "tag2"]
90
91 assert op5["requestBody"] == {
92 "content": {
93 "application/json": {
94 "schema": {
95 "required": ["email"],
96 "type": "object",
97 "properties": {
98 "name": {"type": "string"},
99 "phone": {"type": "number"},
100 "email": {"type": "string"},
101 },
102 }
103 }
104 },
105 "required": True,
106 }
107
108
109 def test_not_included():
110 assert list(schema["paths"].keys()) == [
111 "/api/operation1",
112 "/api/operation2",
113 "/api/operation3",
114 "/api/operation4",
115 "/api/operation5",
116 ]
117 # checking that "/not-included" is not there