Skip to content

Commit

Permalink
Adds draft6 tests and python file reader
Browse files Browse the repository at this point in the history
  • Loading branch information
spacether committed Jun 21, 2022
1 parent 042f717 commit 9feb1e4
Show file tree
Hide file tree
Showing 112 changed files with 14,738 additions and 13 deletions.
1 change: 1 addition & 0 deletions CI/circle_parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ elif [ "$NODE_INDEX" = "4" ]; then
#mvn --no-snapshot-updates --quiet verify -Psamples.circleci.node4 -Dorg.slf4j.simpleLogger.defaultLogLevel=error
(cd samples/openapi3/client/petstore/python && make test)
(cd samples/openapi3/client/petstore/python-experimental && make test)
(cd samples/openapi3/client/3_0_3_unit_test/python-experimental && make test)

else
echo "Running node $NODE_INDEX to test 'samples.circleci.others' defined in pom.xml ..."
Expand Down
6 changes: 6 additions & 0 deletions bin/configs/python-experimental_3_0_3_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: python-experimental
outputDir: samples/openapi3/client/3_0_3_unit_test/python-experimental
inputSpec: modules/openapi-generator/src/test/resources/3_0/unit_test_spec/type.yaml
templateDir: modules/openapi-generator/src/main/resources/python-experimental
additionalProperties:
packageName: unit_test_api
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,35 @@ protected void updateModelForAnyType(CodegenModel m, Schema schema) {
setAddProps(schema, m);
}

protected String toTesCaseName(String specTestCaseName) {
return specTestCaseName;
}

/**
* Processes any test cases if they exist in the vendor extensions
* If they exist then cast them to java class instances and assign them back as the value
* in the x-test-examples vendor extension
* @param vendorExtensions
*/
private void processTestCases(HashMap<String, Object> vendorExtensions) {
String testExamplesKey = "x-test-examples";
HashMap<String, SchemaTestCase> testCases = new HashMap<>();
if (vendorExtensions.containsKey(testExamplesKey)) {
LinkedHashMap<String, Object> testExamples = (LinkedHashMap<String, Object>) vendorExtensions.get(testExamplesKey);
for(Map.Entry<String, Object> testExampleEntry: testExamples.entrySet()) {
String exampleName = testExampleEntry.getKey();
LinkedHashMap<String, Object> testExample = (LinkedHashMap<String, Object>) testExampleEntry.getValue();
String nameInSnakeCase = toTesCaseName(exampleName);
SchemaTestCase testCase = new SchemaTestCase(
(String) testExample.getOrDefault("description", ""),
new ObjectWithTypeBooleans(testExample.get("data")),
(boolean) testExample.get("valid")
);
testCases.put(nameInSnakeCase, testCase);
}
vendorExtensions.put(testExamplesKey, testCases);
}
}

/**
* Convert OAS Model object to Codegen Model object.
Expand All @@ -2706,6 +2735,9 @@ protected void updateModelForAnyType(CodegenModel m, Schema schema) {
*/
@Override
public CodegenModel fromModel(String name, Schema schema) {
HashMap<String, Object> vendorExtensions = (HashMap<String, Object>) schema.getExtensions();
processTestCases(vendorExtensions);

Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
if (typeAliases == null) {
// Only do this once during first call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.openapitools.codegen;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

public class ObjectWithTypeBooleans {
public boolean isUnboundedInteger;
public boolean isNumber;
public boolean isString;
public boolean isMap;
public boolean isArray;
public boolean isBoolean;
public boolean isNull;
public Object value;

public ObjectWithTypeBooleans(Object value) {
Object usedValue = null;
if (value instanceof Integer){
this.isUnboundedInteger = true;
this.value = value;
} else if (value instanceof Double || value instanceof Float){
this.isNumber = true;
this.value = value;
} else if (value instanceof String) {
this.isString = true;
this.value = value;
} else if (value instanceof LinkedHashMap) {
LinkedHashMap<String, Object> castValue = (LinkedHashMap<String, Object>) value;
LinkedHashMap<String, ObjectWithTypeBooleans> castMap = new LinkedHashMap<>();
for (Map.Entry entry: castValue.entrySet()) {
String entryKey = (String) entry.getKey();
ObjectWithTypeBooleans entryValue = new ObjectWithTypeBooleans(entry.getValue());
castMap.put(entryKey, entryValue);
}
this.value = castMap;
this.isMap = true;
} else if (value instanceof ArrayList) {
ArrayList<ObjectWithTypeBooleans> castList = new ArrayList<>();
for (Object item: (ArrayList<Object>) value) {
castList.add(new ObjectWithTypeBooleans(item));
}
this.value = castList;
this.isArray = true;
} else if (value instanceof Boolean) {
this.isBoolean = true;
this.value = value;
} else if (value == null) {
this.isNull = true;
this.value = value;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.openapitools.codegen;

public class SchemaTestCase {
public String description;
public ObjectWithTypeBooleans data;
// true means the test case should pass, false means it should fail
public boolean valid;

public SchemaTestCase(String description, ObjectWithTypeBooleans data, boolean valid) {
this.description = description;
this.data = data;
this.valid = valid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.curiousoddman.rgxgen.RgxGen;
import com.github.curiousoddman.rgxgen.config.RgxGenOption;
import com.github.curiousoddman.rgxgen.config.RgxGenProperties;
import com.google.common.base.CaseFormat;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
Expand Down Expand Up @@ -1152,6 +1153,10 @@ protected void addParentContainer(CodegenModel model, String name, Schema schema
model.dataType = getTypeString(schema, "", "", referencedModelNames);
}

protected String toTesCaseName(String specTestCaseName) {
return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, specTestCaseName);
}

/**
* Convert OAS Model object to Codegen Model object
* We have a custom version of this method so we can:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{#or isNumber isUnboundedInteger}}
{{value}}
{{/or}}
{{#if isBoolean}}
{{#if value}}
True
{{else}}
False
{{/if}}
{{/if}}
{{#if isNull}}
None
{{/if}}
{{#if isString}}
"{{value}}"
{{/if}}
{{#if isArray}}
[
{{#each value}}
{{> model_templates/payload_renderer }}
{{/each}}
]
{{/if}}
{{#if isMap}}
{
{{#each value}}
"{{@key}}":
{{#with this}}
{{> model_templates/payload_renderer }},
{{/with}}
{{/each}}
}
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ from {{packageName}}.{{modelPackage}}.{{classFilename}} import {{classname}}
class Test{{classname}}(unittest.TestCase):
"""{{classname}} unit test stubs"""

def setUp(self):
pass

def tearDown(self):
pass

def test_{{classname}}(self):
"""Test {{classname}}"""
# FIXME: construct object with mandatory attributes with example values
# model = {{classname}}() # noqa: E501
pass
{{#each vendorExtensions}}
{{#if @key eq "x-test-examples" }}
{{#each this }}
def test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}(self):
# {{description}}
{{#if valid}}
{{classname}}(
{{#with data}}
{{>model_templates/payload_renderer}}
{{/with}}
)
{{else}}
with self.assertRaises(({{packageName}}.ApiValueError, {{packageName}}.ApiTypeError)):
{{classname}}(
{{#with data}}
{{>model_templates/payload_renderer}}
{{/with}}
)
{{/if}}

{{/each}}
{{/if}}
{{/each}}
{{/with}}
{{/each}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1865,8 +1865,9 @@ class IntBase(NumberBase):
@classmethod
def _validate_format(cls, arg: typing.Optional[decimal.Decimal], validation_metadata: ValidationMetadata):
if isinstance(arg, decimal.Decimal):
exponent = arg.as_tuple().exponent
if exponent != 0:

denominator = arg.as_integer_ratio()[-1]
if denominator != 1:
raise ApiValueError(
"Invalid value '{}' for type integer at {}".format(arg, validation_metadata.path_to_item)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
[
{
"description": "additionalItems as schema",
"schema": {
"items": [{}],
"additionalItems": {"type": "integer"}
},
"tests": [
{
"description": "additional items match schema",
"data": [ null, 2, 3, 4 ],
"valid": true
},
{
"description": "additional items do not match schema",
"data": [ null, 2, 3, "foo" ],
"valid": false
}
]
},
{
"description": "when items is schema, additionalItems does nothing",
"schema": {
"items": {},
"additionalItems": false
},
"tests": [
{
"description": "all items match schema",
"data": [ 1, 2, 3, 4, 5 ],
"valid": true
}
]
},
{
"description": "array of items with no additionalItems permitted",
"schema": {
"items": [{}, {}, {}],
"additionalItems": false
},
"tests": [
{
"description": "empty array",
"data": [ ],
"valid": true
},
{
"description": "fewer number of items present (1)",
"data": [ 1 ],
"valid": true
},
{
"description": "fewer number of items present (2)",
"data": [ 1, 2 ],
"valid": true
},
{
"description": "equal number of items present",
"data": [ 1, 2, 3 ],
"valid": true
},
{
"description": "additional items are not permitted",
"data": [ 1, 2, 3, 4 ],
"valid": false
}
]
},
{
"description": "additionalItems as false without items",
"schema": {"additionalItems": false},
"tests": [
{
"description":
"items defaults to empty schema so everything is valid",
"data": [ 1, 2, 3, 4, 5 ],
"valid": true
},
{
"description": "ignores non-arrays",
"data": {"foo" : "bar"},
"valid": true
}
]
},
{
"description": "additionalItems are allowed by default",
"schema": {"items": [{"type": "integer"}]},
"tests": [
{
"description": "only the first item is validated",
"data": [1, "foo", false],
"valid": true
}
]
},
{
"description": "additionalItems should not look in applicators, valid case",
"schema": {
"allOf": [
{ "items": [ { "type": "integer" } ] }
],
"additionalItems": { "type": "boolean" }
},
"tests": [
{
"description": "items defined in allOf are not examined",
"data": [ 1, null ],
"valid": true
}
]
},
{
"description": "additionalItems should not look in applicators, invalid case",
"schema": {
"allOf": [
{ "items": [ { "type": "integer" }, { "type": "string" } ] }
],
"items": [ {"type": "integer" } ],
"additionalItems": { "type": "boolean" }
},
"tests": [
{
"description": "items defined in allOf are not examined",
"data": [ 1, "hello" ],
"valid": false
}
]
},
{
"description": "items validation adjusts the starting index for additionalItems",
"schema": {
"items": [ { "type": "string" } ],
"additionalItems": { "type": "integer" }
},
"tests": [
{
"description": "valid items",
"data": [ "x", 2, 3 ],
"valid": true
},
{
"description": "wrong type of second item",
"data": [ "x", "y" ],
"valid": false
}
]
}
]
Loading

0 comments on commit 9feb1e4

Please sign in to comment.