Skip to content

Commit 63b459d

Browse files
committed
(fix #9673) This change introduces a flag to have tests generated under the pythonSrcRoot directory if it's specified.
This change also introduces AssertJ as a new dependency.
1 parent 4c8279e commit 63b459d

6 files changed

Lines changed: 70 additions & 15 deletions

File tree

docs/generators/python-aiohttp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2222
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
2323
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
2424
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
25+
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
2526
|useNose|use the nose test framework| |false|
2627
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|
2728

docs/generators/python-blueplanet.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2222
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
2323
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
2424
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
25+
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
2526
|useNose|use the nose test framework| |false|
2627
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|
2728

docs/generators/python-flask.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2222
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
2323
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
2424
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
25+
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
2526
|useNose|use the nose test framework| |false|
2627
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|
2728

modules/openapi-generator/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,12 @@
418418
<artifactId>caffeine</artifactId>
419419
<version>2.8.1</version>
420420
</dependency>
421-
421+
<dependency>
422+
<groupId>org.assertj</groupId>
423+
<artifactId>assertj-core</artifactId>
424+
<version>3.19.0</version>
425+
<scope>test</scope>
426+
</dependency>
422427
</dependencies>
423428
<repositories>
424429
<repository>

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonConnexionServerCodegen.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
5353
public static final String USE_NOSE = "useNose";
5454
public static final String PYTHON_SRC_ROOT = "pythonSrcRoot";
5555
public static final String USE_PYTHON_SRC_ROOT_IN_IMPORTS = "usePythonSrcRootInImports";
56+
public static final String MOVE_TESTS_UNDER_PYTHON_SRC_ROOT = "testsUsePythonSrcRoot";
5657
static final String MEDIA_TYPE = "mediaType";
5758

5859
protected int serverPort = 8080;
@@ -64,6 +65,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
6465
protected boolean useNose = Boolean.FALSE;
6566
protected String pythonSrcRoot;
6667
protected boolean usePythonSrcRootInImports = Boolean.FALSE;
68+
protected boolean moveTestsUnderPythonSrcRoot = Boolean.FALSE;
6769

6870
public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
6971
super();
@@ -136,6 +138,8 @@ public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fi
136138
defaultValue(""));
137139
cliOptions.add(new CliOption(USE_PYTHON_SRC_ROOT_IN_IMPORTS, "include pythonSrcRoot in import namespaces.").
138140
defaultValue("false"));
141+
cliOptions.add(new CliOption(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "generates test under the pythonSrcRoot folder.")
142+
.defaultValue("false"));
139143
}
140144

141145
protected void addSupportingFiles() {
@@ -183,11 +187,17 @@ public void processOpts() {
183187
if (additionalProperties.containsKey(USE_PYTHON_SRC_ROOT_IN_IMPORTS)) {
184188
setUsePythonSrcRootInImports((String) additionalProperties.get(USE_PYTHON_SRC_ROOT_IN_IMPORTS));
185189
}
190+
if (additionalProperties.containsKey(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT)) {
191+
setMoveTestsUnderPythonSrcRoot((String) additionalProperties.get(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT));
192+
}
186193
if (additionalProperties.containsKey(PYTHON_SRC_ROOT)) {
187194
String pythonSrcRoot = (String) additionalProperties.get(PYTHON_SRC_ROOT);
195+
if (moveTestsUnderPythonSrcRoot) {
196+
testPackage = pythonSrcRoot + "." + testPackage;
197+
}
188198
if (usePythonSrcRootInImports) {
189-
// if we prepend the package name if the pythonSrcRoot we get the desired effect.
190-
// but we also need to set pythonSrcRoot itself to "" to ensure all the paths are
199+
// if we prepend the package name with the pythonSrcRoot we get the desired effect.
200+
// But, we also need to set pythonSrcRoot itself to "" to ensure all the paths are
191201
// what we expect.
192202
setPackageName(pythonSrcRoot + "." + packageName);
193203
pythonSrcRoot = "";
@@ -196,6 +206,7 @@ public void processOpts() {
196206
} else {
197207
setPythonSrcRoot("");
198208
}
209+
199210
supportingFiles.add(new SupportingFile("__main__.mustache", packagePath(), "__main__.py"));
200211
supportingFiles.add(new SupportingFile("util.mustache", packagePath(), "util.py"));
201212
supportingFiles.add(new SupportingFile("typing_utils.mustache", packagePath(), "typing_utils.py"));
@@ -238,6 +249,9 @@ public void setUsePythonSrcRootInImports(String val) {
238249
this.usePythonSrcRootInImports = Boolean.parseBoolean(val);
239250
}
240251

252+
public void setMoveTestsUnderPythonSrcRoot(String val) {
253+
this.moveTestsUnderPythonSrcRoot = Boolean.parseBoolean(val);
254+
}
241255

242256
public String pythonSrcOutputFolder() {
243257
return outputFolder + File.separator + pythonSrcRoot;

modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.openapitools.codegen.python;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.MOVE_TESTS_UNDER_PYTHON_SRC_ROOT;
35
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.PYTHON_SRC_ROOT;
46
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.USE_PYTHON_SRC_ROOT_IN_IMPORTS;
57

@@ -9,11 +11,9 @@
911
import java.util.Objects;
1012

1113
import com.google.common.collect.ImmutableMap;
12-
import org.apache.commons.lang3.ObjectUtils;
1314
import org.apache.commons.lang3.StringUtils;
1415
import org.openapitools.codegen.CodegenConstants;
1516
import org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen;
16-
import org.testng.Assert;
1717
import org.testng.annotations.DataProvider;
1818
import org.testng.annotations.Test;
1919

@@ -29,11 +29,12 @@ public void test(String description, Map<String, Object> additionalProperties, S
2929
codegen.additionalProperties().putAll(additionalProperties);
3030
codegen.processOpts();
3131
String pythonSrcRoot = Objects.toString(codegen.additionalProperties().get(PYTHON_SRC_ROOT), null);
32-
Assert.assertEquals(pythonSrcRoot, expectedValues.pythonSrcRoot);
33-
Assert.assertEquals(codegen.apiPackage(), expectedValues.expectedApiPackage);
34-
Assert.assertEquals(codegen.modelFileFolder(), expectedValues.expectedModelFileFolder);
35-
Assert.assertEquals(codegen.apiFileFolder(), expectedValues.expectedApiFileFolder);
36-
Assert.assertEquals(codegen.toModelImport(modelName), expectedValues.expectedImport);
32+
assertThat(pythonSrcRoot).isEqualTo(expectedValues.pythonSrcRoot);
33+
assertThat(codegen.apiPackage()).isEqualTo(expectedValues.expectedApiPackage);
34+
assertThat(codegen.modelFileFolder()).isEqualTo(expectedValues.expectedModelFileFolder);
35+
assertThat(codegen.apiFileFolder()).isEqualTo(expectedValues.expectedApiFileFolder);
36+
assertThat(codegen.apiTestFileFolder()).isEqualTo(expectedValues.expectedApiTestFileFolder);
37+
assertThat(codegen.toModelImport(modelName)).isEqualTo(expectedValues.expectedImport);
3738
}
3839

3940
@DataProvider
@@ -47,6 +48,7 @@ public Object[][] data() {
4748
"openapi_server.controllers",
4849
platformAgnosticPath("generated-code", "connexion", "openapi_server", "models"),
4950
platformAgnosticPath("generated-code", "connexion", "openapi_server", "controllers"),
51+
platformAgnosticPath("generated-code", "connexion", "test"),
5052
null)
5153
},
5254
new Object[]{
@@ -57,6 +59,7 @@ public Object[][] data() {
5759
"openapi_server.controllers",
5860
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
5961
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
62+
platformAgnosticPath("generated-code", "connexion", "test"),
6063
"test_root")
6164
},
6265
new Object[]{
@@ -67,6 +70,19 @@ public Object[][] data() {
6770
"test_root.openapi_server.controllers",
6871
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
6972
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
73+
platformAgnosticPath("generated-code", "connexion", "test"),
74+
null)
75+
},
76+
new Object[]{
77+
"Python src in import and tests under python src root",
78+
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root", USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
79+
MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "true"),
80+
"TestModel",
81+
new ExpectedValues("from test_root.openapi_server.models.test_model import TestModel",
82+
"test_root.openapi_server.controllers",
83+
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
84+
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
85+
platformAgnosticPath("generated-code", "connexion", "test_root", "test"),
7086
null)
7187
},
7288
new Object[]{
@@ -79,15 +95,26 @@ public Object[][] data() {
7995
"test_root.test_package.controllers",
8096
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "models"),
8197
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "controllers"),
98+
platformAgnosticPath("generated-code", "connexion", "test"),
99+
null)
100+
},
101+
new Object[]{
102+
"Python src in import with specified package and tests under python src root",
103+
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root",
104+
USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
105+
CodegenConstants.PACKAGE_NAME, "test_package",
106+
MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "true"),
107+
"TestModel",
108+
new ExpectedValues("from test_root.test_package.models.test_model import TestModel",
109+
"test_root.test_package.controllers",
110+
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "models"),
111+
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "controllers"),
112+
platformAgnosticPath("generated-code", "connexion", "test_root", "test"),
82113
null)
83114
}
84115
};
85116
}
86117

87-
private static String platformAgnosticPath(String... nodes) {
88-
return StringUtils.join(nodes, File.separatorChar);
89-
}
90-
91118
private static class MockAbstractPythonConnexionServerCodegen extends AbstractPythonConnexionServerCodegen {
92119
public MockAbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
93120
super(templateDirectory, fixBodyNameValue);
@@ -99,16 +126,22 @@ private static class ExpectedValues {
99126
public final String expectedApiPackage;
100127
public final String expectedModelFileFolder;
101128
public final String expectedApiFileFolder;
129+
public final String expectedApiTestFileFolder;
102130
public final String pythonSrcRoot;
103131

104132
public ExpectedValues(String expectedImport, String expectedApiPackage, String expectedModelFileFolder,
105-
String expectedApiFileFolder, String pythonSrcRoot) {
133+
String expectedApiFileFolder, String expectedApiTestFileFolder, String pythonSrcRoot) {
106134
this.expectedImport = expectedImport;
107135
this.expectedApiPackage = expectedApiPackage;
108136
this.expectedModelFileFolder = expectedModelFileFolder;
109137
this.expectedApiFileFolder = expectedApiFileFolder;
138+
this.expectedApiTestFileFolder = expectedApiTestFileFolder;
110139
this.pythonSrcRoot = pythonSrcRoot != null ? pythonSrcRoot + File.separatorChar : null;
111140
}
112141
}
113142

143+
private static String platformAgnosticPath(String... nodes) {
144+
return StringUtils.join(nodes, File.separatorChar);
145+
}
146+
114147
}

0 commit comments

Comments
 (0)