-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_builder.py
More file actions
235 lines (199 loc) · 8.15 KB
/
Copy pathtest_builder.py
File metadata and controls
235 lines (199 loc) · 8.15 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from ellar.common import Controller, get, put, serialize_object
from ellar.core import AppFactory
from ellar.openapi.builder import OpenAPIDocumentBuilder
@Controller
class CatController:
@get("/create")
async def create_cat(self):
return dict(message="created")
@put("/{cat_id:int}")
async def update_cat(self, cat_id: int):
return dict(message="created", cat_id=cat_id)
def convert_server(url, description=None, **variables):
return dict(url=url, description=description, variables=variables)
def test_builder_defaults():
builder = OpenAPIDocumentBuilder()
assert builder._build["info"]["title"] == "Ellar API Docs"
assert builder._build["info"]["version"] == "1.0.0"
assert builder._build["tags"] == []
assert builder._build["openapi"] == "3.0.2"
def test_set_openapi_version_works():
builder = OpenAPIDocumentBuilder()
builder.set_openapi_version("2.0.0")
assert builder._build["openapi"] == "2.0.0"
def test_builder_set_title_works():
builder = OpenAPIDocumentBuilder()
builder.set_title("Some new title")
assert builder._build["info"]["title"] == "Some new title"
def test_builder_set_version_works():
builder = OpenAPIDocumentBuilder()
builder.set_version("2.0.0")
assert builder._build["info"]["version"] == "2.0.0"
def test_builder_set_description_works():
description = "Whatever description available"
builder = OpenAPIDocumentBuilder()
builder.set_description(description)
assert builder._build["info"]["description"] == description
def test_builder_set_term_of_service_works():
terms_of_service = "What terms of service available"
builder = OpenAPIDocumentBuilder().set_term_of_service(terms_of_service)
assert builder._build["info"]["termsOfService"] == terms_of_service
def test_builder_set_contact_works():
details = dict(
name="Eadwin", url="https://github.com/eadwinCode", email="eadwin@gmail.com"
)
builder = OpenAPIDocumentBuilder().set_contact(**details)
assert builder._build["info"]["contact"] == details
def test_builder_set_license_works():
details = dict(name="Yahoo", url="https://yahoo.com")
builder = OpenAPIDocumentBuilder().set_license(**details)
assert builder._build["info"]["license"] == details
def test_builder_set_external_doc_works():
details = dict(
description="More detailed documentation can be foound here",
url="https://external-doc.com",
)
builder = OpenAPIDocumentBuilder().set_external_doc(**details)
assert builder._build["externalDocs"] == details
def test_builder_add_server_works():
server1 = dict(
url="{server}/v1",
description="Servers",
server=dict(default="https://staging.server.com"),
)
server2 = dict(
url="https://{environment}.example.com/v2",
environment=dict(default="api", enum=["api", "api.dev", "api.staging"]),
)
builder = OpenAPIDocumentBuilder().add_server(**server1).add_server(**server2)
servers = builder._build["servers"]
assert len(servers) == 2
assert servers[0] == convert_server(**server1)
assert servers[1] == convert_server(**server2)
def test_builder_add_tags_works():
tag1 = dict(name="tag1", description="some tag1 description")
tag2 = dict(
name="tag2",
description="some tag2 description",
external_doc_url="https://tag2.com",
external_doc_description="some tag2 link description",
)
builder = OpenAPIDocumentBuilder().add_tags(**tag1).add_tags(**tag2)
tags = builder._build["tags"]
tag2_result = dict(
name="tag2",
description="some tag2 description",
externalDocs=dict(
url="https://tag2.com", description="some tag2 link description"
),
)
assert len(tags) == 2
assert tags[0] == tag1
assert tags[1] == tag2_result
def test_builder_build_document_adds_error_schemas():
app = AppFactory.create_app()
builder = OpenAPIDocumentBuilder()
schema = builder.build_document(app)
scheme_dict = serialize_object(schema.dict(exclude_none=True))
assert "HTTPValidationError" in scheme_dict["components"]["schemas"]
assert "ValidationError" in scheme_dict["components"]["schemas"]
def test_builder_build_document_has_correct_schema():
app = AppFactory.create_app(controllers=(CatController,))
builder = OpenAPIDocumentBuilder()
schema = builder.build_document(app)
scheme_dict = serialize_object(schema.dict(exclude_none=True))
assert "HTTPValidationError" in scheme_dict["components"]["schemas"]
assert "ValidationError" in scheme_dict["components"]["schemas"]
assert scheme_dict == {
"openapi": "3.0.2",
"info": {"title": "Ellar API Docs", "version": "1.0.0"},
"paths": {
"/cat/create": {
"get": {
"tags": ["cat"],
"operationId": "create_cat_create_get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema_": {
"title": "Response Model",
"type": "object",
}
}
},
}
},
}
},
"/cat/{cat_id}": {
"put": {
"tags": ["cat"],
"operationId": "update_cat__cat_id__put",
"parameters": [
{
"required": True,
"schema_": {"title": "Cat Id", "type": "integer"},
"name": "cat_id",
"in_": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema_": {
"title": "Response Model",
"type": "object",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema_": {
"ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"required": ["detail"],
"type": "object",
"properties": {
"detail": {
"title": "Details",
"type": "array",
"items": {"ref": "#/components/schemas/ValidationError"},
}
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
"tags": [{"name": "cat"}],
}