|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# flake8: noqa |
| 4 | + |
| 5 | +""" |
| 6 | +Run the tests. |
| 7 | +$ pip install -U pytest |
| 8 | +$ cd OpenAPIetstore-python |
| 9 | +$ pytest |
| 10 | +""" |
| 11 | + |
| 12 | +import unittest |
| 13 | +from decimal import Decimal |
| 14 | +from enum import Enum |
| 15 | + |
| 16 | +from dateutil.parser import parse |
| 17 | + |
| 18 | +import petstore_api |
| 19 | +import petstore_api.configuration |
| 20 | + |
| 21 | +HOST = 'http://localhost/v2' |
| 22 | + |
| 23 | + |
| 24 | +class ApiClientTests(unittest.TestCase): |
| 25 | + |
| 26 | + def setUp(self): |
| 27 | + self.api_client = petstore_api.ApiClient() |
| 28 | + |
| 29 | + def test_configuration(self): |
| 30 | + config = petstore_api.Configuration() |
| 31 | + |
| 32 | + config.api_key['api_key'] = '123456' |
| 33 | + config.api_key_prefix['api_key'] = 'PREFIX' |
| 34 | + config.username = 'test_username' |
| 35 | + config.password = 'test_password' |
| 36 | + |
| 37 | + header_params = {'test1': 'value1'} |
| 38 | + query_params = {'test2': 'value2'} |
| 39 | + auth_settings = ['api_key', 'unknown'] |
| 40 | + |
| 41 | + client = petstore_api.ApiClient(config) |
| 42 | + |
| 43 | + # test prefix |
| 44 | + self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key']) |
| 45 | + |
| 46 | + # update parameters based on auth setting |
| 47 | + client.update_params_for_auth(header_params, query_params, |
| 48 | + auth_settings, |
| 49 | + None, None, None) |
| 50 | + |
| 51 | + # test api key auth |
| 52 | + self.assertEqual(header_params['test1'], 'value1') |
| 53 | + self.assertEqual(header_params['api_key'], 'PREFIX 123456') |
| 54 | + self.assertEqual(query_params['test2'], 'value2') |
| 55 | + |
| 56 | + # test basic auth |
| 57 | + self.assertEqual('test_username', client.configuration.username) |
| 58 | + self.assertEqual('test_password', client.configuration.password) |
| 59 | + |
| 60 | + def test_ignore_operation_servers(self): |
| 61 | + config = petstore_api.Configuration(host=HOST) |
| 62 | + client = petstore_api.ApiClient(config) |
| 63 | + user_api_instance = petstore_api.api.user_api.UserApi(client) |
| 64 | + |
| 65 | + config_ignore = petstore_api.Configuration(host=HOST, ignore_operation_servers=True) |
| 66 | + client_ignore = petstore_api.ApiClient(config_ignore) |
| 67 | + user_api_instance_ignore = petstore_api.api.user_api.UserApi(client_ignore) |
| 68 | + |
| 69 | + params_to_serialize = { |
| 70 | + 'user': petstore_api.User(id=1, username='test'), |
| 71 | + '_request_auth': None, |
| 72 | + '_content_type': 'application/json', |
| 73 | + '_headers': None, |
| 74 | + '_host_index': 0 |
| 75 | + } |
| 76 | + |
| 77 | + # operation servers should be used |
| 78 | + _, url, *_ = user_api_instance._create_user_serialize(**params_to_serialize) |
| 79 | + self.assertEqual(client.configuration.host, HOST) |
| 80 | + self.assertEqual(url, 'http://localhost/v2/user') |
| 81 | + |
| 82 | + # operation servers should be ignored |
| 83 | + _, url_ignore, *_ = user_api_instance_ignore._create_user_serialize(**params_to_serialize) |
| 84 | + self.assertEqual(client.configuration.host, HOST) |
| 85 | + self.assertEqual(url_ignore, HOST + '/user') |
| 86 | + |
| 87 | + def test_select_header_accept(self): |
| 88 | + accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] |
| 89 | + accept = self.api_client.select_header_accept(accepts) |
| 90 | + self.assertEqual(accept, 'APPLICATION/JSON') |
| 91 | + |
| 92 | + accepts = ['application/json', 'application/xml'] |
| 93 | + accept = self.api_client.select_header_accept(accepts) |
| 94 | + self.assertEqual(accept, 'application/json') |
| 95 | + |
| 96 | + accepts = ['application/xml', 'application/json'] |
| 97 | + accept = self.api_client.select_header_accept(accepts) |
| 98 | + self.assertEqual(accept, 'application/json') |
| 99 | + |
| 100 | + accepts = ['application/xml', 'application/json-patch+json'] |
| 101 | + accept = self.api_client.select_header_accept(accepts) |
| 102 | + self.assertEqual(accept, 'application/json-patch+json') |
| 103 | + |
| 104 | + accepts = ['application/xml', 'application/json; charset=utf-8'] |
| 105 | + accept = self.api_client.select_header_accept(accepts) |
| 106 | + self.assertEqual(accept, 'application/json; charset=utf-8') |
| 107 | + |
| 108 | + accepts = ['application/xml', 'application/json;format=flowed'] |
| 109 | + accept = self.api_client.select_header_accept(accepts) |
| 110 | + self.assertEqual(accept, 'application/json;format=flowed') |
| 111 | + |
| 112 | + accepts = ['text/plain', 'application/xml'] |
| 113 | + accept = self.api_client.select_header_accept(accepts) |
| 114 | + self.assertEqual(accept, 'text/plain') |
| 115 | + |
| 116 | + accepts = [] |
| 117 | + accept = self.api_client.select_header_accept(accepts) |
| 118 | + self.assertEqual(accept, None) |
| 119 | + |
| 120 | + def test_select_header_content_type(self): |
| 121 | + content_types = ['APPLICATION/JSON', 'APPLICATION/XML'] |
| 122 | + content_type = self.api_client.select_header_content_type(content_types) |
| 123 | + self.assertEqual(content_type, 'APPLICATION/JSON') |
| 124 | + |
| 125 | + content_types = ['application/json', 'application/xml'] |
| 126 | + content_type = self.api_client.select_header_content_type(content_types) |
| 127 | + self.assertEqual(content_type, 'application/json') |
| 128 | + |
| 129 | + content_types = ['application/xml', 'application/json'] |
| 130 | + content_type = self.api_client.select_header_content_type(content_types) |
| 131 | + self.assertEqual(content_type, 'application/json') |
| 132 | + |
| 133 | + content_types = ['application/xml', 'application/json-patch+json'] |
| 134 | + content_type = self.api_client.select_header_content_type(content_types) |
| 135 | + self.assertEqual(content_type, 'application/json-patch+json') |
| 136 | + |
| 137 | + content_types = ['application/xml', 'application/json; charset=utf-8'] |
| 138 | + content_type = self.api_client.select_header_content_type(content_types) |
| 139 | + self.assertEqual(content_type, 'application/json; charset=utf-8') |
| 140 | + |
| 141 | + content_types = ['application/xml', 'application/json;format=flowed'] |
| 142 | + content_type = self.api_client.select_header_content_type(content_types) |
| 143 | + self.assertEqual(content_type, 'application/json;format=flowed') |
| 144 | + |
| 145 | + content_types = ['text/plain', 'application/xml'] |
| 146 | + content_type = self.api_client.select_header_content_type(content_types) |
| 147 | + self.assertEqual(content_type, 'text/plain') |
| 148 | + |
| 149 | + # no content type, default to None |
| 150 | + content_types = [] |
| 151 | + content_type = self.api_client.select_header_content_type(content_types) |
| 152 | + self.assertEqual(content_type, None) |
| 153 | + |
| 154 | + def test_sanitize_for_serialization_none(self): |
| 155 | + data = None |
| 156 | + result = self.api_client.sanitize_for_serialization(None) |
| 157 | + self.assertEqual(result, data) |
| 158 | + |
| 159 | + def test_sanitize_for_serialization_str(self): |
| 160 | + data = "test string" |
| 161 | + result = self.api_client.sanitize_for_serialization(data) |
| 162 | + self.assertEqual(result, data) |
| 163 | + |
| 164 | + def test_sanitize_for_serialization_int(self): |
| 165 | + data = 1 |
| 166 | + result = self.api_client.sanitize_for_serialization(data) |
| 167 | + self.assertEqual(result, data) |
| 168 | + |
| 169 | + def test_sanitize_for_serialization_bool(self): |
| 170 | + data = True |
| 171 | + result = self.api_client.sanitize_for_serialization(data) |
| 172 | + self.assertEqual(result, data) |
| 173 | + |
| 174 | + def test_sanitize_for_serialization_date(self): |
| 175 | + data = parse("1997-07-16").date() # date |
| 176 | + result = self.api_client.sanitize_for_serialization(data) |
| 177 | + self.assertEqual(result, "1997-07-16") |
| 178 | + |
| 179 | + def test_sanitize_for_serialization_datetime(self): |
| 180 | + data = parse("1997-07-16T19:20:30.45+01:00") # datetime |
| 181 | + result = self.api_client.sanitize_for_serialization(data) |
| 182 | + self.assertEqual(result, "1997-07-16T19:20:30.450000+01:00") |
| 183 | + |
| 184 | + def test_sanitize_for_serialization_decimal(self): |
| 185 | + data = Decimal("1.0") |
| 186 | + result = self.api_client.sanitize_for_serialization(data) |
| 187 | + self.assertEqual(result, "1.0") |
| 188 | + |
| 189 | + def test_sanitize_for_serialization_list_enum(self): |
| 190 | + class EnumSerialization(int, Enum): |
| 191 | + NUMBER_0 = 0 |
| 192 | + NUMBER_1 = 1 |
| 193 | + |
| 194 | + data = [EnumSerialization.NUMBER_1] |
| 195 | + result = self.api_client.sanitize_for_serialization(data) |
| 196 | + self.assertEqual(result, [1]) |
| 197 | + self.assertNotIsInstance(result[0], EnumSerialization) |
| 198 | + |
| 199 | + def test_sanitize_for_serialization_list(self): |
| 200 | + data = [1] |
| 201 | + result = self.api_client.sanitize_for_serialization(data) |
| 202 | + self.assertEqual(result, data) |
| 203 | + |
| 204 | + def test_sanitize_for_serialization_dict(self): |
| 205 | + data = {"test key": "test value"} |
| 206 | + result = self.api_client.sanitize_for_serialization(data) |
| 207 | + self.assertEqual(result, data) |
| 208 | + |
| 209 | + def test_sanitize_for_serialization_model(self): |
| 210 | + pet_dict = {"id": 1, "name": "monkey", |
| 211 | + "category": {"id": 1, "name": "test category"}, |
| 212 | + "tags": [{"id": 1, "name": "test tag1"}, |
| 213 | + {"id": 2, "name": "test tag2"}], |
| 214 | + "status": "available", |
| 215 | + "photoUrls": ["http://foo.bar.com/3", |
| 216 | + "http://foo.bar.com/4"]} |
| 217 | + pet = petstore_api.Pet(name="monkey", photoUrls=["http://foo.bar.com/3", "http://foo.bar.com/4"]) |
| 218 | + pet.id = 1 |
| 219 | + cate = petstore_api.Category(name="test category") |
| 220 | + cate.id = 1 |
| 221 | + pet.category = cate |
| 222 | + tag1 = petstore_api.Tag() |
| 223 | + tag1.id = 1 |
| 224 | + tag1.name = "test tag1" |
| 225 | + tag2 = petstore_api.Tag() |
| 226 | + tag2.id = 2 |
| 227 | + tag2.name = "test tag2" |
| 228 | + pet.tags = [tag1, tag2] |
| 229 | + pet.status = "available" |
| 230 | + |
| 231 | + data = pet |
| 232 | + result = self.api_client.sanitize_for_serialization(data) |
| 233 | + self.assertEqual(result, pet_dict) |
| 234 | + |
| 235 | + # list of models |
| 236 | + list_of_pet_dict = [pet_dict] |
| 237 | + result = self.api_client.sanitize_for_serialization([pet]) |
| 238 | + self.assertEqual(result, list_of_pet_dict) |
| 239 | + |
| 240 | + def test_parameters_to_url_query_simple_values(self): |
| 241 | + data = 'value={"category": "example", "category2": "example2"}' |
| 242 | + dictionary = { |
| 243 | + "category": "example", |
| 244 | + "category2": "example2" |
| 245 | + } |
| 246 | + result = self.api_client.parameters_to_url_query([('value', dictionary)], {}) |
| 247 | + self.assertEqual(result, |
| 248 | + "value=%7B%22category%22%3A%20%22example%22%2C%20%22category2%22%3A%20%22example2%22%7D") |
| 249 | + |
| 250 | + def test_parameters_to_url_query_complex_values(self): |
| 251 | + data = 'value={"number": 1, "string": "str", "bool": true, "dict": {"number": 1, "string": "str", "bool": true}}' |
| 252 | + dictionary = { |
| 253 | + "number": 1, |
| 254 | + "string": "str", |
| 255 | + "bool": True, |
| 256 | + "dict": { |
| 257 | + "number": 1, |
| 258 | + "string": "str", |
| 259 | + "bool": True |
| 260 | + } |
| 261 | + } |
| 262 | + result = self.api_client.parameters_to_url_query([('value', dictionary)], {}) |
| 263 | + self.assertEqual(result, |
| 264 | + 'value=%7B%22number%22%3A%201%2C%20%22string%22%3A%20%22str%22%2C%20%22bool%22%3A%20true%2C%20%22dict%22%3A%20%7B%22number%22%3A%201%2C%20%22string%22%3A%20%22str%22%2C%20%22bool%22%3A%20true%7D%7D') |
| 265 | + |
| 266 | + def test_parameters_to_url_query_dict_values(self): |
| 267 | + data = 'value={"strValues": ["one", "two", "three"], "dictValues": [{"name": "value1", "age": 14}, {"name": "value2", "age": 12}]}' |
| 268 | + dictionary = { |
| 269 | + "strValues": [ |
| 270 | + "one", |
| 271 | + "two", |
| 272 | + "three" |
| 273 | + ], |
| 274 | + "dictValues": [ |
| 275 | + { |
| 276 | + "name": "value1", |
| 277 | + "age": 14 |
| 278 | + }, |
| 279 | + { |
| 280 | + "name": "value2", |
| 281 | + "age": 12 |
| 282 | + }, |
| 283 | + ] |
| 284 | + } |
| 285 | + result = self.api_client.parameters_to_url_query([('value', dictionary)], {}) |
| 286 | + self.assertEqual(result, |
| 287 | + 'value=%7B%22strValues%22%3A%20%5B%22one%22%2C%20%22two%22%2C%20%22three%22%5D%2C%20%22dictValues%22%3A%20%5B%7B%22name%22%3A%20%22value1%22%2C%20%22age%22%3A%2014%7D%2C%20%7B%22name%22%3A%20%22value2%22%2C%20%22age%22%3A%2012%7D%5D%7D') |
| 288 | + |
| 289 | + def test_parameters_to_url_query_boolean_value(self): |
| 290 | + result = self.api_client.parameters_to_url_query([('boolean', True)], {}) |
| 291 | + self.assertEqual(result, "boolean=true") |
| 292 | + |
| 293 | + def test_parameters_to_url_query_list_value(self): |
| 294 | + params = self.api_client.parameters_to_url_query(params=[('list', [1, 2, 3])], |
| 295 | + collection_formats={'list': 'multi'}) |
| 296 | + self.assertEqual(params, "list=1&list=2&list=3") |
| 297 | + |
| 298 | + def test_parameters_to_url_query_list_value_encoded(self): |
| 299 | + params = self.api_client.parameters_to_url_query(params=[('list', [" !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", "2023-01-01T00:00:00+01:00"])], |
| 300 | + collection_formats={'list': 'multi'}) |
| 301 | + self.assertEqual(params, "list=%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-./%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~&list=2023-01-01T00%3A00%3A00%2B01%3A00") |
0 commit comments