Skip to content

Commit 6b3735e

Browse files
committed
Merge pull request #2727 from scottrw93/test-cases
Add test cases for Python Client
2 parents b5e5ea6 + 1674ec3 commit 6b3735e

27 files changed

+1407
-1
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
2020
protected String packageVersion;
2121
protected String apiDocPath = "docs/";
2222
protected String modelDocPath = "docs/";
23+
24+
private String testFolder;
2325

2426
public PythonClientCodegen() {
2527
super();
2628

2729
modelPackage = "models";
2830
apiPackage = "api";
2931
outputFolder = "generated-code" + File.separatorChar + "python";
32+
3033
modelTemplateFiles.put("model.mustache", ".py");
3134
apiTemplateFiles.put("api.mustache", ".py");
35+
36+
modelTestTemplateFiles.put("model_test.mustache", ".py");
37+
apiTestTemplateFiles.put("api_test.mustache", ".py");
38+
3239
embeddedTemplateDir = templateDir = "python";
3340

3441
modelDocTemplateFiles.put("model_doc.mustache", ".md");
3542
apiDocTemplateFiles.put("api_doc.mustache", ".md");
43+
44+
testFolder = "test";
3645

3746
languageSpecificPrimitives.clear();
3847
languageSpecificPrimitives.add("int");
@@ -126,6 +135,7 @@ public void processOpts() {
126135
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
127136
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
128137
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
138+
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
129139
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
130140
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
131141
}
@@ -184,6 +194,16 @@ public String apiFileFolder() {
184194
public String modelFileFolder() {
185195
return outputFolder + File.separatorChar + modelPackage().replace('.', File.separatorChar);
186196
}
197+
198+
@Override
199+
public String apiTestFileFolder() {
200+
return outputFolder + File.separatorChar + testFolder;
201+
}
202+
203+
@Override
204+
public String modelTestFileFolder() {
205+
return outputFolder + File.separatorChar + testFolder;
206+
}
187207

188208
@Override
189209
public String getTypeDeclaration(Property p) {
@@ -310,6 +330,11 @@ public String toModelFilename(String name) {
310330
// PhoneNumber => phone_number
311331
return underscore(dropDots(name));
312332
}
333+
334+
@Override
335+
public String toModelTestFilename(String name) {
336+
return "test_" + toModelFilename(name);
337+
};
313338

314339
@Override
315340
public String toApiFilename(String name) {
@@ -319,6 +344,11 @@ public String toApiFilename(String name) {
319344
// e.g. PhoneNumberApi.rb => phone_number_api.rb
320345
return underscore(name) + "_api";
321346
}
347+
348+
@Override
349+
public String toApiTestFilename(String name) {
350+
return "test_" + toApiFilename(name);
351+
}
322352

323353
@Override
324354
public String toApiName(String name) {

modules/swagger-codegen/src/main/resources/python/__init__test.mustache

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# coding: utf-8
2+
3+
"""
4+
Copyright 2016 SmartBear Software
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
ref: https://github.com/swagger-api/swagger-codegen
19+
"""
20+
21+
from __future__ import absolute_import
22+
23+
import os
24+
import sys
25+
import unittest
26+
27+
import swagger_client
28+
from swagger_client.rest import ApiException
29+
from swagger_client.apis.{{classVarName}} import {{classname}}
30+
31+
32+
class {{#operations}}Test{{classname}}(unittest.TestCase):
33+
""" {{classname}} unit test stubs """
34+
35+
def setUp(self):
36+
self.api = swagger_client.apis.{{classVarName}}.{{classname}}()
37+
38+
def tearDown(self):
39+
pass
40+
41+
{{#operation}}
42+
def test_{{operationId}}(self):
43+
"""
44+
Test case for {{{operationId}}}
45+
46+
{{{summary}}}
47+
"""
48+
pass
49+
50+
{{/operation}}
51+
{{/operations}}
52+
53+
if __name__ == '__main__':
54+
unittest.main()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# coding: utf-8
2+
3+
"""
4+
Copyright 2016 SmartBear Software
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
ref: https://github.com/swagger-api/swagger-codegen
19+
"""
20+
21+
from __future__ import absolute_import
22+
23+
import os
24+
import sys
25+
import unittest
26+
27+
{{#models}}
28+
{{#model}}
29+
import swagger_client
30+
from swagger_client.rest import ApiException
31+
from swagger_client.models.{{classFilename}} import {{classname}}
32+
33+
34+
class Test{{classname}}(unittest.TestCase):
35+
""" {{classname}} unit test stubs """
36+
37+
def setUp(self):
38+
pass
39+
40+
def tearDown(self):
41+
pass
42+
43+
def test{{classname}}(self):
44+
"""
45+
Test {{classname}}
46+
"""
47+
model = swagger_client.models.{{classFilename}}.{{classname}}()
48+
49+
{{/model}}
50+
{{/models}}
51+
52+
if __name__ == '__main__':
53+
unittest.main()

samples/client/petstore/python/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This Python package is automatically generated by the [Swagger Codegen](https://
55

66
- API version: 1.0.0
77
- Package version: 1.0.0
8-
- Build date: 2016-04-27T17:36:32.266+08:00
8+
- Build date: 2016-04-27T22:50:21.115+01:00
99
- Build package: class io.swagger.codegen.languages.PythonClientCodegen
1010

1111
## Requirements.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# swagger_client.FakeApi
2+
3+
All URIs are relative to *http://petstore.swagger.io/v2*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**test_endpoint_parameters**](FakeApi.md#test_endpoint_parameters) | **POST** /fake | Fake endpoint for testing various parameters
8+
9+
10+
# **test_endpoint_parameters**
11+
> test_endpoint_parameters(number, double, string, byte, integer=integer, int32=int32, int64=int64, float=float, binary=binary, date=date, date_time=date_time, password=password)
12+
13+
Fake endpoint for testing various parameters
14+
15+
Fake endpoint for testing various parameters
16+
17+
### Example
18+
```python
19+
import time
20+
import swagger_client
21+
from swagger_client.rest import ApiException
22+
from pprint import pprint
23+
24+
# create an instance of the API class
25+
api_instance = swagger_client.FakeApi()
26+
number = 3.4 # float | None
27+
double = 1.2 # float | None
28+
string = 'string_example' # str | None
29+
byte = 'B' # str | None
30+
integer = 56 # int | None (optional)
31+
int32 = 56 # int | None (optional)
32+
int64 = 789 # int | None (optional)
33+
float = 3.4 # float | None (optional)
34+
binary = 'B' # str | None (optional)
35+
date = '2013-10-20' # date | None (optional)
36+
date_time = '2013-10-20T19:20:30+01:00' # datetime | None (optional)
37+
password = 'password_example' # str | None (optional)
38+
39+
try:
40+
# Fake endpoint for testing various parameters
41+
api_instance.test_endpoint_parameters(number, double, string, byte, integer=integer, int32=int32, int64=int64, float=float, binary=binary, date=date, date_time=date_time, password=password)
42+
except ApiException as e:
43+
print "Exception when calling FakeApi->test_endpoint_parameters: %s\n" % e
44+
```
45+
46+
### Parameters
47+
48+
Name | Type | Description | Notes
49+
------------- | ------------- | ------------- | -------------
50+
**number** | **float**| None |
51+
**double** | **float**| None |
52+
**string** | **str**| None |
53+
**byte** | **str**| None |
54+
**integer** | **int**| None | [optional]
55+
**int32** | **int**| None | [optional]
56+
**int64** | **int**| None | [optional]
57+
**float** | **float**| None | [optional]
58+
**binary** | **str**| None | [optional]
59+
**date** | **date**| None | [optional]
60+
**date_time** | **datetime**| None | [optional]
61+
**password** | **str**| None | [optional]
62+
63+
### Return type
64+
65+
void (empty response body)
66+
67+
### Authorization
68+
69+
No authorization required
70+
71+
### HTTP request headers
72+
73+
- **Content-Type**: Not defined
74+
- **Accept**: application/xml, application/json
75+
76+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
77+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
swagger_client
2+
test
23
tests

0 commit comments

Comments
 (0)