-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
test_api_client.py
221 lines (177 loc) · 8.17 KB
/
test_api_client.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# coding: utf-8
# flake8: noqa
"""
Run the tests.
$ pip install nose (optional)
$ cd OpenAPIetstore-python
$ nosetests -v
"""
import os
import time
import atexit
import weakref
import unittest
from dateutil.parser import parse
import petstore_api
import petstore_api.configuration
HOST = 'http://petstore.swagger.io/v2'
class ApiClientTests(unittest.TestCase):
def setUp(self):
self.api_client = petstore_api.ApiClient()
def test_configuration(self):
config = petstore_api.Configuration()
config.host = 'http://localhost/'
config.api_key['api_key'] = '123456'
config.api_key_prefix['api_key'] = 'PREFIX'
config.username = 'test_username'
config.password = 'test_password'
header_params = {'test1': 'value1'}
query_params = {'test2': 'value2'}
auth_settings = ['api_key', 'unknown']
client = petstore_api.ApiClient(config)
# test prefix
self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key'])
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings, resource_path=None, method=None, body=None)
# test api key auth
self.assertEqual(header_params['test1'], 'value1')
self.assertEqual(header_params['api_key'], 'PREFIX 123456')
self.assertEqual(query_params['test2'], 'value2')
# test basic auth
self.assertEqual('test_username', client.configuration.username)
self.assertEqual('test_password', client.configuration.password)
# test api key without prefix
config.api_key['api_key'] = '123456'
config.api_key_prefix['api_key'] = None
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings, resource_path=None, method=None, body=None)
self.assertEqual(header_params['api_key'], '123456')
# test api key with empty prefix
config.api_key['api_key'] = '123456'
config.api_key_prefix['api_key'] = ''
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings, resource_path=None, method=None, body=None)
self.assertEqual(header_params['api_key'], '123456')
# test api key with prefix specified in the api_key, useful when the prefix
# must include '=' sign followed by the API key secret without space.
config.api_key['api_key'] = 'PREFIX=123456'
config.api_key_prefix['api_key'] = None
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings, resource_path=None, method=None, body=None)
self.assertEqual(header_params['api_key'], 'PREFIX=123456')
def test_select_header_accept(self):
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, 'application/json')
accepts = ['application/json', 'application/xml']
accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, 'application/json')
accepts = ['application/xml', 'application/json']
accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, 'application/json')
accepts = ['text/plain', 'application/xml']
accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, 'text/plain, application/xml')
accepts = []
accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, None)
def test_select_header_content_type(self):
content_types = ['APPLICATION/JSON', 'APPLICATION/XML']
content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json')
content_types = ['application/json', 'application/xml']
content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json')
content_types = ['application/xml', 'application/json']
content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json')
content_types = ['text/plain', 'application/xml']
content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'text/plain')
content_types = []
content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, None)
def test_sanitize_for_serialization(self):
# None
data = None
result = self.api_client.sanitize_for_serialization(None)
self.assertEqual(result, data)
# str
data = "test string"
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# int
data = 1
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# bool
data = True
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# date
data = parse("1997-07-16").date() # date
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, "1997-07-16")
# datetime
data = parse("1997-07-16T19:20:30.45+01:00") # datetime
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, "1997-07-16T19:20:30.450000+01:00")
# list
data = [1]
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# dict
data = {"test key": "test value"}
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# model
pet_dict = {"id": 1, "name": "monkey",
"category": {"id": 1, "name": "test category"},
"tags": [{"id": 1, "fullName": "test tag1"},
{"id": 2, "fullName": "test tag2"}],
"status": "available",
"photoUrls": ["http://foo.bar.com/3",
"http://foo.bar.com/4"]}
from petstore_api.model.pet import Pet
from petstore_api.model.category import Category
from petstore_api.model.tag import Tag
from petstore_api.model.string_boolean_map import StringBooleanMap
pet = Pet(name=pet_dict["name"], photo_urls=pet_dict["photoUrls"])
pet.id = pet_dict["id"]
cate = Category()
cate.id = pet_dict["category"]["id"]
cate.name = pet_dict["category"]["name"]
pet.category = cate
tag1 = Tag()
tag1.id = pet_dict["tags"][0]["id"]
tag1.full_name = pet_dict["tags"][0]["fullName"]
tag2 = Tag()
tag2.id = pet_dict["tags"][1]["id"]
tag2.full_name = pet_dict["tags"][1]["fullName"]
pet.tags = [tag1, tag2]
pet.status = pet_dict["status"]
data = pet
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, pet_dict)
# list of models
list_of_pet_dict = [pet_dict]
data = [pet]
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, list_of_pet_dict)
# model with additional proerties
model_dict = {'some_key': True}
model = StringBooleanMap(**model_dict)
result = self.api_client.sanitize_for_serialization(model)
self.assertEqual(result, model_dict)
def test_context_manager_closes_threadpool(self):
with petstore_api.ApiClient() as client:
self.assertIsNotNone(client.pool)
pool_ref = weakref.ref(client._pool)
self.assertIsNotNone(pool_ref())
self.assertIsNone(pool_ref())
def test_atexit_closes_threadpool(self):
client = petstore_api.ApiClient()
self.assertIsNotNone(client.pool)
self.assertIsNotNone(client._pool)
atexit._run_exitfuncs()
self.assertIsNone(client._pool)