-
Notifications
You must be signed in to change notification settings - Fork 6k
issue #2569: added model tests for Go #2577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientOptionsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package io.swagger.codegen.Go; | ||
|
|
||
| import io.swagger.codegen.AbstractOptionsTest; | ||
| import io.swagger.codegen.CodegenConfig; | ||
| import io.swagger.codegen.languages.GoClientCodegen; | ||
| import io.swagger.codegen.options.GoClientOptionsProvider; | ||
|
|
||
| import mockit.Expectations; | ||
| import mockit.Tested; | ||
|
|
||
| public class GoClientOptionsTest extends AbstractOptionsTest { | ||
|
|
||
| @Tested | ||
| private GoClientCodegen clientCodegen; | ||
|
|
||
| public GoClientOptionsTest() { | ||
| super(new GoClientOptionsProvider()); | ||
| } | ||
|
|
||
| @Override | ||
| protected CodegenConfig getCodegenConfig() { | ||
| return clientCodegen; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @Override | ||
| protected void setExpectations() { | ||
| new Expectations(clientCodegen) {{ | ||
| clientCodegen.setPackageVersion(GoClientOptionsProvider.PACKAGE_VERSION_VALUE); | ||
| times = 1; | ||
| clientCodegen.setPackageName(GoClientOptionsProvider.PACKAGE_NAME_VALUE); | ||
| times = 1; | ||
| }}; | ||
| } | ||
| } |
271 changes: 271 additions & 0 deletions
271
modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoModelTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| package io.swagger.codegen.Go; | ||
|
|
||
| import io.swagger.codegen.CodegenModel; | ||
| import io.swagger.codegen.CodegenProperty; | ||
| import io.swagger.codegen.DefaultCodegen; | ||
| import io.swagger.codegen.languages.GoClientCodegen; | ||
| import io.swagger.models.ArrayModel; | ||
| import io.swagger.models.Model; | ||
| import io.swagger.models.ModelImpl; | ||
| import io.swagger.models.properties.ArrayProperty; | ||
| import io.swagger.models.properties.DateTimeProperty; | ||
| import io.swagger.models.properties.LongProperty; | ||
| import io.swagger.models.properties.MapProperty; | ||
| import io.swagger.models.properties.RefProperty; | ||
| import io.swagger.models.properties.StringProperty; | ||
|
|
||
| import com.google.common.collect.Sets; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.DataProvider; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| @SuppressWarnings("static-method") | ||
| public class GoModelTest { | ||
|
|
||
| @Test(description = "convert a simple Go model") | ||
| public void simpleModelTest() { | ||
| final Model model = new ModelImpl() | ||
| .description("a sample model") | ||
| .property("id", new LongProperty()) | ||
| .property("name", new StringProperty()) | ||
| .property("createdAt", new DateTimeProperty()) | ||
| .required("id") | ||
| .required("name"); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a sample model"); | ||
| Assert.assertEquals(cm.vars.size(), 3); | ||
| // {{imports}} is not used in template | ||
| //Assert.assertEquals(cm.imports.size(), 1); | ||
|
|
||
| final CodegenProperty property1 = cm.vars.get(0); | ||
| Assert.assertEquals(property1.baseName, "id"); | ||
| Assert.assertEquals(property1.datatype, "int64"); | ||
| Assert.assertEquals(property1.name, "Id"); | ||
| Assert.assertEquals(property1.defaultValue, "null"); | ||
| Assert.assertEquals(property1.baseType, "int64"); | ||
| Assert.assertTrue(property1.hasMore); | ||
| Assert.assertTrue(property1.required); | ||
| Assert.assertTrue(property1.isPrimitiveType); | ||
| Assert.assertTrue(property1.isNotContainer); | ||
|
|
||
| final CodegenProperty property2 = cm.vars.get(1); | ||
| Assert.assertEquals(property2.baseName, "name"); | ||
| Assert.assertEquals(property2.datatype, "string"); | ||
| Assert.assertEquals(property2.name, "Name"); | ||
| Assert.assertEquals(property2.defaultValue, "null"); | ||
| Assert.assertEquals(property2.baseType, "string"); | ||
| Assert.assertTrue(property2.hasMore); | ||
| Assert.assertTrue(property2.required); | ||
| Assert.assertTrue(property2.isPrimitiveType); | ||
| Assert.assertTrue(property2.isNotContainer); | ||
|
|
||
| final CodegenProperty property3 = cm.vars.get(2); | ||
| Assert.assertEquals(property3.baseName, "createdAt"); | ||
| Assert.assertEquals(property3.complexType, "time.Time"); | ||
| Assert.assertEquals(property3.datatype, "time.Time"); | ||
| Assert.assertEquals(property3.name, "CreatedAt"); | ||
| Assert.assertEquals(property3.defaultValue, "null"); | ||
| Assert.assertEquals(property3.baseType, "time.Time"); | ||
| Assert.assertNull(property3.hasMore); | ||
| Assert.assertNull(property3.required); | ||
| Assert.assertTrue(property3.isNotContainer); | ||
| } | ||
|
|
||
| @Test(description = "convert a model with list property") | ||
| public void listPropertyTest() { | ||
| final Model model = new ModelImpl() | ||
| .description("a sample model") | ||
| .property("id", new LongProperty()) | ||
| .property("urls", new ArrayProperty() | ||
| .items(new StringProperty())) | ||
| .required("id"); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a sample model"); | ||
| Assert.assertEquals(cm.vars.size(), 2); | ||
|
|
||
| final CodegenProperty property1 = cm.vars.get(0); | ||
| Assert.assertEquals(property1.baseName, "id"); | ||
| Assert.assertEquals(property1.datatype, "int64"); | ||
| Assert.assertEquals(property1.name, "Id"); | ||
| Assert.assertEquals(property1.defaultValue, "null"); | ||
| Assert.assertEquals(property1.baseType, "int64"); | ||
| Assert.assertTrue(property1.hasMore); | ||
| Assert.assertTrue(property1.required); | ||
| Assert.assertTrue(property1.isPrimitiveType); | ||
| Assert.assertTrue(property1.isNotContainer); | ||
|
|
||
| final CodegenProperty property2 = cm.vars.get(1); | ||
| Assert.assertEquals(property2.baseName, "urls"); | ||
| Assert.assertEquals(property2.datatype, "[]string"); | ||
| Assert.assertEquals(property2.name, "Urls"); | ||
| Assert.assertEquals(property2.baseType, "array"); | ||
| Assert.assertNull(property2.hasMore); | ||
| Assert.assertEquals(property2.containerType, "array"); | ||
| Assert.assertNull(property2.required); | ||
| Assert.assertTrue(property2.isPrimitiveType); | ||
| Assert.assertTrue(property2.isContainer); | ||
| } | ||
|
|
||
| @Test(description = "convert a model with a map property") | ||
| public void mapPropertyTest() { | ||
| final Model model = new ModelImpl() | ||
| .description("a sample model") | ||
| .property("translations", new MapProperty() | ||
| .additionalProperties(new StringProperty())) | ||
| .required("id"); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a sample model"); | ||
| Assert.assertEquals(cm.vars.size(), 1); | ||
|
|
||
| final CodegenProperty property1 = cm.vars.get(0); | ||
| Assert.assertEquals(property1.baseName, "translations"); | ||
| Assert.assertEquals(property1.datatype, "map[string]string"); | ||
| Assert.assertEquals(property1.name, "Translations"); | ||
| Assert.assertEquals(property1.baseType, "map"); | ||
| Assert.assertEquals(property1.containerType, "map"); | ||
| Assert.assertNull(property1.required); | ||
| Assert.assertTrue(property1.isContainer); | ||
| Assert.assertTrue(property1.isPrimitiveType); | ||
| } | ||
|
|
||
| @Test(description = "convert a model with complex property") | ||
| public void complexPropertyTest() { | ||
| final Model model = new ModelImpl() | ||
| .description("a sample model") | ||
| .property("children", new RefProperty("#/definitions/Children")); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a sample model"); | ||
| Assert.assertEquals(cm.vars.size(), 1); | ||
|
|
||
| final CodegenProperty property1 = cm.vars.get(0); | ||
| Assert.assertEquals(property1.baseName, "children"); | ||
| Assert.assertEquals(property1.datatype, "Children"); | ||
| Assert.assertEquals(property1.name, "Children"); | ||
| Assert.assertEquals(property1.baseType, "Children"); | ||
| Assert.assertNull(property1.required); | ||
| Assert.assertTrue(property1.isNotContainer); | ||
| } | ||
|
|
||
| @Test(description = "convert a model with complex list property") | ||
| public void complexListProperty() { | ||
| final Model model = new ModelImpl() | ||
| .description("a sample model") | ||
| .property("children", new ArrayProperty() | ||
| .items(new RefProperty("#/definitions/Children"))); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a sample model"); | ||
| Assert.assertEquals(cm.vars.size(), 1); | ||
|
|
||
| final CodegenProperty property1 = cm.vars.get(0); | ||
| Assert.assertEquals(property1.baseName, "children"); | ||
| Assert.assertEquals(property1.datatype, "[]Children"); | ||
| Assert.assertEquals(property1.name, "Children"); | ||
| Assert.assertEquals(property1.baseType, "array"); | ||
| Assert.assertEquals(property1.containerType, "array"); | ||
| Assert.assertNull(property1.required); | ||
| Assert.assertTrue(property1.isContainer); | ||
| } | ||
|
|
||
| @Test(description = "convert a model with complex map property") | ||
| public void complexMapProperty() { | ||
| final Model model = new ModelImpl() | ||
| .description("a sample model") | ||
| .property("children", new MapProperty() | ||
| .additionalProperties(new RefProperty("#/definitions/Children"))); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a sample model"); | ||
| Assert.assertEquals(cm.vars.size(), 1); | ||
| // {{imports}} is not used in template | ||
| //Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); | ||
|
|
||
| final CodegenProperty property1 = cm.vars.get(0); | ||
| Assert.assertEquals(property1.baseName, "children"); | ||
| Assert.assertEquals(property1.complexType, "Children"); | ||
| Assert.assertEquals(property1.datatype, "map[string]Children"); | ||
| Assert.assertEquals(property1.name, "Children"); | ||
| Assert.assertEquals(property1.baseType, "map"); | ||
| Assert.assertEquals(property1.containerType, "map"); | ||
| Assert.assertNull(property1.required); | ||
| Assert.assertTrue(property1.isContainer); | ||
| Assert.assertNull(property1.isNotContainer); | ||
| } | ||
|
|
||
| @Test(description = "convert an array model") | ||
| public void arrayModelTest() { | ||
| final Model model = new ArrayModel() | ||
| .description("an array model") | ||
| .items(new RefProperty("#/definitions/Children")); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "an array model"); | ||
| Assert.assertEquals(cm.vars.size(), 0); | ||
| // skip import test as import is not used by Go codegen | ||
| } | ||
|
|
||
| @Test(description = "convert an map model") | ||
| public void mapModelTest() { | ||
| final Model model = new ModelImpl() | ||
| .description("a map model") | ||
| .additionalProperties(new RefProperty("#/definitions/Children")); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel("sample", model); | ||
|
|
||
| Assert.assertEquals(cm.name, "sample"); | ||
| Assert.assertEquals(cm.classname, "Sample"); | ||
| Assert.assertEquals(cm.description, "a map model"); | ||
| Assert.assertEquals(cm.vars.size(), 0); | ||
| // {{imports}} is not used in template | ||
| //Assert.assertEquals(cm.imports.size(), 2); | ||
| //Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); | ||
| } | ||
|
|
||
| @DataProvider(name = "modelNames") | ||
| public static Object[][] primeNumbers() { | ||
| return new Object[][] { | ||
| {"sample", "Sample"}, | ||
| {"sample_name", "SampleName"}, | ||
| {"sample__name", "SampleName"}, | ||
| {"/sample", "Sample"}, | ||
| {"\\sample", "Sample"}, | ||
| {"sample.name", "SampleName"}, | ||
| {"_sample", "Sample"}, | ||
| }; | ||
| } | ||
|
|
||
| @Test(dataProvider = "modelNames", description = "avoid inner class") | ||
| public void modelNameTest(String name, String expectedName) { | ||
| final Model model = new ModelImpl(); | ||
| final DefaultCodegen codegen = new GoClientCodegen(); | ||
| final CodegenModel cm = codegen.fromModel(name, model); | ||
|
|
||
| Assert.assertEquals(cm.name, name); | ||
| Assert.assertEquals(cm.classname, expectedName); | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
...les/swagger-codegen/src/test/java/io/swagger/codegen/options/GoClientOptionsProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package io.swagger.codegen.options; | ||
|
|
||
| import io.swagger.codegen.CodegenConstants; | ||
| import io.swagger.codegen.languages.GoClientCodegen; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class GoClientOptionsProvider implements OptionsProvider { | ||
|
|
||
| public static final String PACKAGE_VERSION_VALUE = "1.0.0"; | ||
| public static final String PACKAGE_NAME_VALUE = "Go"; | ||
|
|
||
| @Override | ||
| public String getLanguage() { | ||
| return "go"; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> createOptions() { | ||
| ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>(); | ||
| return builder | ||
| .put(CodegenConstants.PACKAGE_VERSION, PACKAGE_VERSION_VALUE) | ||
| .put(CodegenConstants.PACKAGE_NAME, PACKAGE_NAME_VALUE) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isServer() { | ||
| return false; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike PHP, we can test {{imports}} as well, which is used in the Go mustache template.