-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel_func_test.py
197 lines (178 loc) · 6.05 KB
/
model_func_test.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
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
# -*- coding: utf-8 -*-
"""
Functional tests related to passing models in req/response
"""
import pytest
from jsonschema.exceptions import ValidationError
from mocket.plugins.httpretty import HTTPretty
from aiobravado.compat import json
from tests.functional.conftest import register_get
from tests.functional.conftest import register_spec
from tests.functional.conftest import swagger_client
@pytest.fixture
def sample_model():
return {
"id": 42,
"schools": [
{"name": "School1"},
{"name": "School2"}
]
}
@pytest.fixture
def swagger_dict():
models = {
"School": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"]
},
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"schools": {
"type": "array",
"items": {
"$ref": "#/definitions/School"
}
}
},
"required": ["id"]
}
}
operation = {
"tags": ["api_test"],
"operationId": "testHTTP",
"parameters": [],
"responses": {
"200": {
"description": "blah",
"schema": {
"$ref": "#/definitions/User",
}
}
}
}
operation_post = {
"tags": ["api_test"],
"operationId": "testHTTPPost",
"parameters": [],
"responses": {
"200": {
"description": "Successs"
}
}
}
paths = {
"/test_http": {
'get': operation,
'post': operation_post,
},
}
return {
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Simple"
},
"basePath": "/",
"paths": paths,
"definitions": models
}
@pytest.mark.parametrize(
'spec_type',
(
('json',),
('yaml',),
)
)
@pytest.mark.asyncio
async def test_model_in_response(
swagger_dict, sample_model, spec_type, http_client):
register_spec(swagger_dict, spec_type=spec_type)
register_get("http://localhost/test_http", body=json.dumps(sample_model))
client = await swagger_client(http_client)
result = await client.api_test.testHTTP().result()
User = client.get_model('User')
School = client.get_model('School')
assert isinstance(result, User)
for school in result.schools:
assert isinstance(school, School)
assert User(
id=42,
schools=[
School(name="School1"),
School(name="School2")
]) == result
@pytest.mark.asyncio
async def test_model_missing_required_property_in_response_raises_ValidationError(
swagger_dict, sample_model, http_client):
register_spec(swagger_dict)
sample_model.pop("id")
register_get("http://localhost/test_http", body=json.dumps(sample_model))
client = await swagger_client(http_client)
with pytest.raises(ValidationError) as excinfo:
await client.api_test.testHTTP().result()
assert "'id' is a required property" in str(excinfo.value)
@pytest.mark.asyncio
async def test_additionalProperty_in_model_in_response(swagger_dict, sample_model, http_client):
register_spec(swagger_dict)
sample_model["extra"] = 42
register_get("http://localhost/test_http", body=json.dumps(sample_model))
client = await swagger_client(http_client)
result = await client.api_test.testHTTP().result()
assert result.extra == 42
@pytest.mark.asyncio
async def test_invalid_type_in_response_raises_ValidationError(swagger_dict, sample_model, http_client):
register_spec(swagger_dict)
register_get("http://localhost/test_http", body='"NOT_COMPLEX_TYPE"')
client = await swagger_client(http_client)
with pytest.raises(ValidationError) as excinfo:
await client.api_test.testHTTP().result()
assert "'NOT_COMPLEX_TYPE' is not of type" in str(excinfo.value)
@pytest.mark.asyncio
async def test_error_on_wrong_type_inside_complex_type(swagger_dict, sample_model, http_client):
register_spec(swagger_dict)
sample_model["id"] = "Not Integer"
register_get("http://localhost/test_http", body=json.dumps(sample_model))
client = await swagger_client(http_client)
with pytest.raises(ValidationError) as excinfo:
await client.api_test.testHTTP().result()
assert "'Not Integer' is not of type" in str(excinfo.value)
@pytest.mark.asyncio
async def test_error_on_missing_type_in_model(swagger_dict, sample_model, http_client):
register_spec(swagger_dict)
sample_model["schools"][0] = {} # Omit 'name'
register_get("http://localhost/test_http", body=json.dumps(sample_model))
client = await swagger_client(http_client)
with pytest.raises(ValidationError) as excinfo:
await client.api_test.testHTTP().result()
assert "'name' is a required property" in str(excinfo.value)
@pytest.mark.xfail(reason='Fails with aiohttp 3 due to https://github.com/mindflayer/python-mocket/issues/66')
@pytest.mark.asyncio
async def test_model_in_body_of_request(swagger_dict, sample_model, http_client):
param_spec = {
"in": "body",
"name": "body",
"schema": {
"$ref": "#/definitions/User"
}
}
swagger_dict["paths"]["/test_http"]['post']["parameters"] = [param_spec]
register_spec(swagger_dict)
HTTPretty.register_uri(HTTPretty.POST, "http://localhost/test_http")
client = await swagger_client(http_client)
resource = client.api_test
User = client.get_model('User')
School = client.get_model('School')
user = User(id=42, schools=[School(name='s1')])
await resource.testHTTPPost(body=user).result()
body = json.loads(HTTPretty.last_request.body.decode('utf-8'))
assert {'schools': [{'name': 's1'}], 'id': 42} == body