Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !9674

Fix to allow tests to be generated under pythonSrcRoot (This change also adds AssertJ)

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/tray2100/pythonSrcRootTestFix into master 4 years ago
  • Overview 0
  • Commits 1
  • Pipelines 0
  • Changes 6

Created by: tray2100

(#9673 (closed)) 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. I've updated the tests in this PR to use AssertJ.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc: @taxpon @frol @mbohlool @cbornet @kenjones-cisco @tomplus @Jyhess @arun-nalla @spacether

Compare
  • master (base)

and
  • latest version
    63b459de
    1 commit, 2 years ago

6 files
+ 70
- 15

    Preferences

    File browser
    Compare changes
docs/ge‎nerators‎
python-a‎iohttp.md‎ +1 -0
python-blu‎eplanet.md‎ +1 -0
python-‎flask.md‎ +1 -0
modules/open‎api-generator‎
s‎rc‎
main/java/org/…/‎codegen/languages‎
AbstractPythonConnex‎ionServerCodegen.java‎ +16 -2
test/java/org/…‎/codegen/python‎
AbstractPythonConnexio‎nServerCodegenTest.java‎ +45 -12
pom‎.xml‎ +6 -1
docs/generators/python-aiohttp.md
+ 1
- 0
  • View file @ 63b459de

  • Edit in single-file editor

  • Open in Web IDE


@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
|useNose|use the nose test framework| |false|
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|
docs/generators/python-blueplanet.md
+ 1
- 0
  • View file @ 63b459de

  • Edit in single-file editor

  • Open in Web IDE


@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
|useNose|use the nose test framework| |false|
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|
docs/generators/python-flask.md
+ 1
- 0
  • View file @ 63b459de

  • Edit in single-file editor

  • Open in Web IDE


@@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
|useNose|use the nose test framework| |false|
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonConnexionServerCodegen.java
+ 16
- 2
  • View file @ 63b459de

  • Edit in single-file editor

  • Open in Web IDE


@@ -53,6 +53,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
public static final String USE_NOSE = "useNose";
public static final String PYTHON_SRC_ROOT = "pythonSrcRoot";
public static final String USE_PYTHON_SRC_ROOT_IN_IMPORTS = "usePythonSrcRootInImports";
public static final String MOVE_TESTS_UNDER_PYTHON_SRC_ROOT = "testsUsePythonSrcRoot";
static final String MEDIA_TYPE = "mediaType";
protected int serverPort = 8080;
@@ -64,6 +65,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
protected boolean useNose = Boolean.FALSE;
protected String pythonSrcRoot;
protected boolean usePythonSrcRootInImports = Boolean.FALSE;
protected boolean moveTestsUnderPythonSrcRoot = Boolean.FALSE;
public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
super();
@@ -136,6 +138,8 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
defaultValue(""));
cliOptions.add(new CliOption(USE_PYTHON_SRC_ROOT_IN_IMPORTS, "include pythonSrcRoot in import namespaces.").
defaultValue("false"));
cliOptions.add(new CliOption(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "generates test under the pythonSrcRoot folder.")
.defaultValue("false"));
}
protected void addSupportingFiles() {
@@ -183,11 +187,17 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
if (additionalProperties.containsKey(USE_PYTHON_SRC_ROOT_IN_IMPORTS)) {
setUsePythonSrcRootInImports((String) additionalProperties.get(USE_PYTHON_SRC_ROOT_IN_IMPORTS));
}
if (additionalProperties.containsKey(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT)) {
setMoveTestsUnderPythonSrcRoot((String) additionalProperties.get(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT));
}
if (additionalProperties.containsKey(PYTHON_SRC_ROOT)) {
String pythonSrcRoot = (String) additionalProperties.get(PYTHON_SRC_ROOT);
if (moveTestsUnderPythonSrcRoot) {
testPackage = pythonSrcRoot + "." + testPackage;
}
if (usePythonSrcRootInImports) {
// if we prepend the package name if the pythonSrcRoot we get the desired effect.
// but we also need to set pythonSrcRoot itself to "" to ensure all the paths are
// if we prepend the package name with the pythonSrcRoot we get the desired effect.
// But, we also need to set pythonSrcRoot itself to "" to ensure all the paths are
// what we expect.
setPackageName(pythonSrcRoot + "." + packageName);
pythonSrcRoot = "";
@@ -196,6 +206,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
} else {
setPythonSrcRoot("");
}
supportingFiles.add(new SupportingFile("__main__.mustache", packagePath(), "__main__.py"));
supportingFiles.add(new SupportingFile("util.mustache", packagePath(), "util.py"));
supportingFiles.add(new SupportingFile("typing_utils.mustache", packagePath(), "typing_utils.py"));
@@ -238,6 +249,9 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
this.usePythonSrcRootInImports = Boolean.parseBoolean(val);
}
public void setMoveTestsUnderPythonSrcRoot(String val) {
this.moveTestsUnderPythonSrcRoot = Boolean.parseBoolean(val);
}
public String pythonSrcOutputFolder() {
return outputFolder + File.separator + pythonSrcRoot;
modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java
+ 45
- 12
  • View file @ 63b459de

  • Edit in single-file editor

  • Open in Web IDE


package org.openapitools.codegen.python;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.MOVE_TESTS_UNDER_PYTHON_SRC_ROOT;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.PYTHON_SRC_ROOT;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.USE_PYTHON_SRC_ROOT_IN_IMPORTS;
@@ -9,11 +11,9 @@ import java.util.Map;
import java.util.Objects;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -29,11 +29,12 @@ public class AbstractPythonConnexionServerCodegenTest {
codegen.additionalProperties().putAll(additionalProperties);
codegen.processOpts();
String pythonSrcRoot = Objects.toString(codegen.additionalProperties().get(PYTHON_SRC_ROOT), null);
Assert.assertEquals(pythonSrcRoot, expectedValues.pythonSrcRoot);
Assert.assertEquals(codegen.apiPackage(), expectedValues.expectedApiPackage);
Assert.assertEquals(codegen.modelFileFolder(), expectedValues.expectedModelFileFolder);
Assert.assertEquals(codegen.apiFileFolder(), expectedValues.expectedApiFileFolder);
Assert.assertEquals(codegen.toModelImport(modelName), expectedValues.expectedImport);
assertThat(pythonSrcRoot).isEqualTo(expectedValues.pythonSrcRoot);
assertThat(codegen.apiPackage()).isEqualTo(expectedValues.expectedApiPackage);
assertThat(codegen.modelFileFolder()).isEqualTo(expectedValues.expectedModelFileFolder);
assertThat(codegen.apiFileFolder()).isEqualTo(expectedValues.expectedApiFileFolder);
assertThat(codegen.apiTestFileFolder()).isEqualTo(expectedValues.expectedApiTestFileFolder);
assertThat(codegen.toModelImport(modelName)).isEqualTo(expectedValues.expectedImport);
}
@DataProvider
@@ -47,6 +48,7 @@ public class AbstractPythonConnexionServerCodegenTest {
"openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
null)
},
new Object[]{
@@ -57,6 +59,7 @@ public class AbstractPythonConnexionServerCodegenTest {
"openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
"test_root")
},
new Object[]{
@@ -67,6 +70,19 @@ public class AbstractPythonConnexionServerCodegenTest {
"test_root.openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
null)
},
new Object[]{
"Python src in import and tests under python src root",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root", USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "true"),
"TestModel",
new ExpectedValues("from test_root.openapi_server.models.test_model import TestModel",
"test_root.openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test"),
null)
},
new Object[]{
@@ -79,15 +95,26 @@ public class AbstractPythonConnexionServerCodegenTest {
"test_root.test_package.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
null)
},
new Object[]{
"Python src in import with specified package and tests under python src root",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root",
USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
CodegenConstants.PACKAGE_NAME, "test_package",
MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "true"),
"TestModel",
new ExpectedValues("from test_root.test_package.models.test_model import TestModel",
"test_root.test_package.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test"),
null)
}
};
}
private static String platformAgnosticPath(String... nodes) {
return StringUtils.join(nodes, File.separatorChar);
}
private static class MockAbstractPythonConnexionServerCodegen extends AbstractPythonConnexionServerCodegen {
public MockAbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
super(templateDirectory, fixBodyNameValue);
@@ -99,16 +126,22 @@ public class AbstractPythonConnexionServerCodegenTest {
public final String expectedApiPackage;
public final String expectedModelFileFolder;
public final String expectedApiFileFolder;
public final String expectedApiTestFileFolder;
public final String pythonSrcRoot;
public ExpectedValues(String expectedImport, String expectedApiPackage, String expectedModelFileFolder,
String expectedApiFileFolder, String pythonSrcRoot) {
String expectedApiFileFolder, String expectedApiTestFileFolder, String pythonSrcRoot) {
this.expectedImport = expectedImport;
this.expectedApiPackage = expectedApiPackage;
this.expectedModelFileFolder = expectedModelFileFolder;
this.expectedApiFileFolder = expectedApiFileFolder;
this.expectedApiTestFileFolder = expectedApiTestFileFolder;
this.pythonSrcRoot = pythonSrcRoot != null ? pythonSrcRoot + File.separatorChar : null;
}
}
private static String platformAgnosticPath(String... nodes) {
return StringUtils.join(nodes, File.separatorChar);
}
}
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
2
Enhancement: Feature Feature: Documentation
2
Enhancement: Feature Feature: Documentation
    Assign labels
  • Manage project labels

Milestone
5.0.0
5.0.0 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: OpenAPITools/openapi-generator!8127
Source branch: github/fork/tray2100/pythonSrcRootTestFix

Menu

Explore Projects Groups Snippets