1
+ from unittest .mock import MagicMock
2
+
3
+ import pytest
4
+
1
5
import openapi_python_client .schema as oai
2
6
from openapi_python_client import GeneratorError
3
7
from openapi_python_client .parser .errors import ParseError
@@ -114,6 +118,19 @@ def test_from_dict_invalid_version(self, mocker):
114
118
115
119
116
120
class TestEndpoint :
121
+ def make_endpoint (self ):
122
+ from openapi_python_client .parser .openapi import Endpoint
123
+
124
+ return Endpoint (
125
+ path = "path" ,
126
+ method = "method" ,
127
+ description = None ,
128
+ name = "name" ,
129
+ requires_security = False ,
130
+ tag = "tag" ,
131
+ relative_imports = {"import_3" },
132
+ )
133
+
117
134
def test_parse_request_form_body (self , mocker ):
118
135
ref = mocker .MagicMock ()
119
136
body = oai .RequestBody .construct (
@@ -195,15 +212,7 @@ def test_add_body_no_data(self, mocker):
195
212
from openapi_python_client .parser .openapi import Endpoint , Schemas
196
213
197
214
parse_request_form_body = mocker .patch .object (Endpoint , "parse_request_form_body" )
198
- endpoint = Endpoint (
199
- path = "path" ,
200
- method = "method" ,
201
- description = None ,
202
- name = "name" ,
203
- requires_security = False ,
204
- tag = "tag" ,
205
- relative_imports = {"import_3" },
206
- )
215
+ endpoint = self .make_endpoint ()
207
216
schemas = Schemas ()
208
217
209
218
Endpoint ._add_body (endpoint = endpoint , data = oai .Operation .construct (), schemas = schemas )
@@ -217,15 +226,7 @@ def test_add_body_bad_data(self, mocker):
217
226
parse_error = ParseError (data = mocker .MagicMock ())
218
227
other_schemas = mocker .MagicMock ()
219
228
mocker .patch .object (Endpoint , "parse_request_json_body" , return_value = (parse_error , other_schemas ))
220
- endpoint = Endpoint (
221
- path = "path" ,
222
- method = "method" ,
223
- description = None ,
224
- name = "name" ,
225
- requires_security = False ,
226
- tag = "tag" ,
227
- relative_imports = {"import_3" },
228
- )
229
+ endpoint = self .make_endpoint ()
229
230
request_body = mocker .MagicMock ()
230
231
schemas = Schemas ()
231
232
@@ -263,15 +264,7 @@ def test_add_body_happy(self, mocker):
263
264
f"{ MODULE_NAME } .import_string_from_reference" , side_effect = ["import_1" , "import_2" ]
264
265
)
265
266
266
- endpoint = Endpoint (
267
- path = "path" ,
268
- method = "method" ,
269
- description = None ,
270
- name = "name" ,
271
- requires_security = False ,
272
- tag = "tag" ,
273
- relative_imports = {"import_3" },
274
- )
267
+ endpoint = self .make_endpoint ()
275
268
initial_schemas = mocker .MagicMock ()
276
269
277
270
(endpoint , response_schemas ) = Endpoint ._add_body (
@@ -303,15 +296,7 @@ def test__add_responses_status_code_error(self, mocker):
303
296
data = {
304
297
"not_a_number" : response_1_data ,
305
298
}
306
- endpoint = Endpoint (
307
- path = "path" ,
308
- method = "method" ,
309
- description = None ,
310
- name = "name" ,
311
- requires_security = False ,
312
- tag = "tag" ,
313
- relative_imports = {"import_3" },
314
- )
299
+ endpoint = self .make_endpoint ()
315
300
parse_error = ParseError (data = mocker .MagicMock ())
316
301
response_from_data = mocker .patch (f"{ MODULE_NAME } .response_from_data" , return_value = (parse_error , schemas ))
317
302
@@ -333,15 +318,7 @@ def test__add_responses_error(self, mocker):
333
318
"200" : response_1_data ,
334
319
"404" : response_2_data ,
335
320
}
336
- endpoint = Endpoint (
337
- path = "path" ,
338
- method = "method" ,
339
- description = None ,
340
- name = "name" ,
341
- requires_security = False ,
342
- tag = "tag" ,
343
- relative_imports = {"import_3" },
344
- )
321
+ endpoint = self .make_endpoint ()
345
322
parse_error = ParseError (data = mocker .MagicMock ())
346
323
response_from_data = mocker .patch (f"{ MODULE_NAME } .response_from_data" , return_value = (parse_error , schemas ))
347
324
@@ -374,15 +351,7 @@ def test__add_responses(self, mocker):
374
351
"200" : response_1_data ,
375
352
"404" : response_2_data ,
376
353
}
377
- endpoint = Endpoint (
378
- path = "path" ,
379
- method = "method" ,
380
- description = None ,
381
- name = "name" ,
382
- requires_security = False ,
383
- tag = "tag" ,
384
- relative_imports = {"import_3" },
385
- )
354
+ endpoint = self .make_endpoint ()
386
355
schemas = mocker .MagicMock ()
387
356
schemas_1 = mocker .MagicMock ()
388
357
schemas_2 = mocker .MagicMock ()
@@ -420,14 +389,7 @@ def test__add_responses(self, mocker):
420
389
def test__add_parameters_handles_no_params (self ):
421
390
from openapi_python_client .parser .openapi import Endpoint , Schemas
422
391
423
- endpoint = Endpoint (
424
- path = "path" ,
425
- method = "method" ,
426
- description = None ,
427
- name = "name" ,
428
- requires_security = False ,
429
- tag = "tag" ,
430
- )
392
+ endpoint = self .make_endpoint ()
431
393
schemas = Schemas ()
432
394
# Just checking there's no exception here
433
395
assert Endpoint ._add_parameters (endpoint = endpoint , data = oai .Operation .construct (), schemas = schemas ) == (
@@ -438,14 +400,7 @@ def test__add_parameters_handles_no_params(self):
438
400
def test__add_parameters_parse_error (self , mocker ):
439
401
from openapi_python_client .parser .openapi import Endpoint
440
402
441
- endpoint = Endpoint (
442
- path = "path" ,
443
- method = "method" ,
444
- description = None ,
445
- name = "name" ,
446
- requires_security = False ,
447
- tag = "tag" ,
448
- )
403
+ endpoint = self .make_endpoint ()
449
404
initial_schemas = mocker .MagicMock ()
450
405
parse_error = ParseError (data = mocker .MagicMock ())
451
406
property_schemas = mocker .MagicMock ()
@@ -463,14 +418,7 @@ def test__add_parameters_parse_error(self, mocker):
463
418
def test__add_parameters_fail_loudly_when_location_not_supported (self , mocker ):
464
419
from openapi_python_client .parser .openapi import Endpoint , Schemas
465
420
466
- endpoint = Endpoint (
467
- path = "path" ,
468
- method = "method" ,
469
- description = None ,
470
- name = "name" ,
471
- requires_security = False ,
472
- tag = "tag" ,
473
- )
421
+ endpoint = self .make_endpoint ()
474
422
parsed_schemas = mocker .MagicMock ()
475
423
mocker .patch (f"{ MODULE_NAME } .property_from_data" , return_value = (mocker .MagicMock (), parsed_schemas ))
476
424
param = oai .Parameter .construct (
@@ -487,15 +435,7 @@ def test__add_parameters_happy(self, mocker):
487
435
from openapi_python_client .parser .openapi import Endpoint
488
436
from openapi_python_client .parser .properties import Property
489
437
490
- endpoint = Endpoint (
491
- path = "path" ,
492
- method = "method" ,
493
- description = None ,
494
- name = "name" ,
495
- requires_security = False ,
496
- tag = "tag" ,
497
- relative_imports = {"import_3" },
498
- )
438
+ endpoint = self .make_endpoint ()
499
439
path_prop = mocker .MagicMock (autospec = Property )
500
440
path_prop_import = mocker .MagicMock ()
501
441
path_prop .get_imports = mocker .MagicMock (return_value = {path_prop_import })
@@ -731,6 +671,19 @@ def test_from_data_no_security(self, mocker):
731
671
endpoint = _add_responses .return_value [0 ], data = data , schemas = _add_responses .return_value [1 ]
732
672
)
733
673
674
+ @pytest .mark .parametrize (
675
+ "response_types, expected" ,
676
+ (([], "None" ), (["Something" ], "Something" ), (["First" , "Second" , "Second" ], "Union[First, Second]" )),
677
+ )
678
+ def test_response_type (self , response_types , expected ):
679
+ endpoint = self .make_endpoint ()
680
+ for response_type in response_types :
681
+ mock_response = MagicMock ()
682
+ mock_response .prop .get_type_string .return_value = response_type
683
+ endpoint .responses .append (mock_response )
684
+
685
+ assert endpoint .response_type () == expected
686
+
734
687
735
688
class TestImportStringFromReference :
736
689
def test_import_string_from_reference_no_prefix (self , mocker ):
0 commit comments