Skip to content

Commit

Permalink
convert json files to yaml and run the same tests against them
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Nov 30, 2014
1 parent dd84244 commit 1d6e4d6
Show file tree
Hide file tree
Showing 70 changed files with 649 additions and 16 deletions.
6 changes: 6 additions & 0 deletions convert-json-fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

json2yaml fixtures/json -s -r
cp -a fixtures/json/* fixtures/yaml
find fixtures/yaml -name '*.json' -exec rm -rf {} \;
find fixtures/json -name '*.yaml' -exec rm -rf {} \;
79 changes: 63 additions & 16 deletions fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import (
"strings"
"testing"

"gopkg.in/yaml.v2"

. "github.com/smartystreets/goconvey/convey"
)

func roundTripTest(t *testing.T, fixtureType, fileName string, schema interface{}) {
var extensions = []string{"json", "yaml"}

func roundTripTest(t *testing.T, fixtureType, extension, fileName string, schema interface{}) {
if extension == "yaml" {
roundTripTestYAML(t, fixtureType, fileName, schema)
} else {
roundTripTestJSON(t, fixtureType, fileName, schema)
}

}

func roundTripTestJSON(t *testing.T, fixtureType, fileName string, schema interface{}) {
specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
Convey("verifying "+fixtureType+" fixture "+specName, t, func() {
Convey("verifying "+fixtureType+" JSON fixture "+specName, t, func() {
b, err := ioutil.ReadFile(fileName)
So(err, ShouldBeNil)
Println()
Expand Down Expand Up @@ -42,15 +55,49 @@ func roundTripTest(t *testing.T, fixtureType, fileName string, schema interface{
})
}

func roundTripTestYAML(t *testing.T, fixtureType, fileName string, schema interface{}) {
specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
Convey("verifying "+fixtureType+" YAML fixture "+specName, t, func() {
b, err := ioutil.ReadFile(fileName)
So(err, ShouldBeNil)
Println()
//Println("Reading file", fileName, "returned", string(b))
var expected map[string]interface{}
err = yaml.Unmarshal(b, &expected)
So(err, ShouldBeNil)

err = yaml.Unmarshal(b, schema)
So(err, ShouldBeNil)

//Println()
//Println("unmarshalling from file resulted in: %#v", schema)
cb, err := yaml.Marshal(schema)
So(err, ShouldBeNil)
//Println()
//Println("Marshalling to yaml returned", string(cb))

var actual map[string]interface{}
err = yaml.Unmarshal(cb, &actual)
So(err, ShouldBeNil)
//Println()
//spew.Dump(expected)
//spew.Dump(actual)
//fmt.Printf("comparing %s\n\t%#v\nto\n\t%#+v\n", fileName, expected, actual)
So(actual, ShouldBeEquivalentTo, expected)
})
}

func TestPropertyFixtures(t *testing.T) {
path := filepath.Join("fixtures", "json", "models", "properties")
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatal(err)
}
for _, extension := range extensions {
path := filepath.Join("fixtures", extension, "models", "properties")
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatal(err)
}

for _, f := range files {
roundTripTest(t, "property", filepath.Join(path, f.Name()), &Schema{})
for _, f := range files {
roundTripTest(t, "property", extension, filepath.Join(path, f.Name()), &Schema{})
}
}
}

Expand All @@ -68,12 +115,12 @@ FILES:
}
for _, spec := range specs {
if strings.HasPrefix(f.Name(), spec) {
roundTripTest(t, "model", filepath.Join(path, f.Name()), &Spec{})
roundTripTest(t, "model", "json", filepath.Join(path, f.Name()), &Spec{})
continue FILES
}
}
//fmt.Println("trying", f.Name())
roundTripTest(t, "model", filepath.Join(path, f.Name()), &Schema{})
roundTripTest(t, "model", "json", filepath.Join(path, f.Name()), &Schema{})
}
}

Expand All @@ -85,7 +132,7 @@ func TestParameterFixtures(t *testing.T) {
}

for _, f := range files {
roundTripTest(t, "parameter", filepath.Join(path, f.Name()), &Parameter{})
roundTripTest(t, "parameter", "json", filepath.Join(path, f.Name()), &Parameter{})
}
}

Expand All @@ -97,7 +144,7 @@ func TestOperationFixtures(t *testing.T) {
}

for _, f := range files {
roundTripTest(t, "operation", filepath.Join(path, f.Name()), &Operation{})
roundTripTest(t, "operation", "json", filepath.Join(path, f.Name()), &Operation{})
}
}

Expand All @@ -110,9 +157,9 @@ func TestResponseFixtures(t *testing.T) {

for _, f := range files {
if !strings.HasPrefix(f.Name(), "multiple") {
roundTripTest(t, "response", filepath.Join(path, f.Name()), &Response{})
roundTripTest(t, "response", "json", filepath.Join(path, f.Name()), &Response{})
} else {
roundTripTest(t, "responses", filepath.Join(path, f.Name()), &Responses{})
roundTripTest(t, "responses", "json", filepath.Join(path, f.Name()), &Responses{})
}
}
}
Expand Down Expand Up @@ -142,7 +189,7 @@ FILES:
}
for _, pi := range pathItems {
if strings.HasPrefix(f.Name(), pi) {
roundTripTest(t, "path items", filepath.Join(path, f.Name()), &PathItem{})
roundTripTest(t, "path items", "json", filepath.Join(path, f.Name()), &PathItem{})
continue FILES
}
}
Expand Down
5 changes: 5 additions & 0 deletions fixtures/yaml/models/modelWithArrayRef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
required:
- id
properties:
id: {type: integer, format: int64}
children: {type: array, items: {$ref: Person}}
5 changes: 5 additions & 0 deletions fixtures/yaml/models/modelWithComposition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
definitions:
Cat: {description: 'A representation of a cat', allOf: [{$ref: '#/models/Pet'}, {properties: {huntingSkill: {type: string, description: 'The measured skill for hunting', default: lazy, enum: [clueless, lazy, adventerous, aggressive]}}, required: [huntingSkill]}]}
Dog: {description: 'A representation of a dog', allOf: [{$ref: '#/models/Pet'}, {properties: {packSize: {type: integer, format: int32, description: 'the size of the pack the dog is from', default: 0}}, required: [name, packSize]}]}
Fish: {description: 'A representation of a fish', allOf: [{$ref: '#/models/Pet'}, {properties: {fins: {type: integer, format: int32, description: 'count of fins'}}, required: [fins]}]}
Pet: {discriminator: petType, properties: {name: {type: string}, petType: {type: string}}, required: [name, petType]}
4 changes: 4 additions & 0 deletions fixtures/yaml/models/modelWithDateTimeMap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
description: 'true'
additionalProperties:
type: string
format: date-time
3 changes: 3 additions & 0 deletions fixtures/yaml/models/modelWithExamples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
definitions:
Pet: {properties: {name: {type: string}}, required: [name]}
Dog: {properties: {id: {type: integer, format: int64}, name: {type: string}}, required: [name]}
4 changes: 4 additions & 0 deletions fixtures/yaml/models/modelWithInt32Map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
description: 'This is a Map[String, Integer]'
additionalProperties:
type: integer
format: int32
4 changes: 4 additions & 0 deletions fixtures/yaml/models/modelWithInt64Map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
description: 'true'
additionalProperties:
type: integer
format: int64
14 changes: 14 additions & 0 deletions fixtures/yaml/models/modelWithMultipleProperties.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description: 'true'
properties:
booleanValue: {type: boolean}
byteValue: {type: string, format: byte}
dateTimeValue: {type: string, format: date-time}
int32Value: {type: integer, format: int32}
int64Value: {type: integer, format: int64}
stringValue: {type: string}
booleanArrayValue: {type: array, items: {type: boolean}}
byteArrayValue: {type: array, items: {type: string, format: byte}}
dateTimeArrayValue: {type: array, items: {type: string, format: date-time}}
int32ArrayValue: {type: array, items: {type: integer, format: int32}}
int64ArrayValue: {type: array, items: {type: integer, format: int64}}
stringArrayValue: {type: array, items: {type: string}}
5 changes: 5 additions & 0 deletions fixtures/yaml/models/modelWithPrimitiveArray.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
required:
- id
properties:
id: {type: integer, format: int64}
childrensAges: {type: array, items: {type: integer, format: int32}}
3 changes: 3 additions & 0 deletions fixtures/yaml/models/modelWithStringProperty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: 'true'
properties:
name: {type: string}
6 changes: 6 additions & 0 deletions fixtures/yaml/models/modelWithXmlAttributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: 'this model serves xml and json structures'
xml:
name: XMLModel
properties:
id: {type: integer, format: int64, xml: {attribute: true, namespace: ns1, prefix: urn1}}
items: {type: array, items: {type: string}, xml: {wrapped: true}}
2 changes: 2 additions & 0 deletions fixtures/yaml/models/models.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
definitions:
Pet: {properties: {name: {type: string}}, required: [name]}
3 changes: 3 additions & 0 deletions fixtures/yaml/models/multipleModels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
definitions:
Pet: {properties: {name: {type: string}}, required: [name]}
Dog: {properties: {id: {type: integer, format: int64}, name: {type: string}}, required: [name]}
3 changes: 3 additions & 0 deletions fixtures/yaml/models/properties/propertyWithBooleanArray.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: array
items:
type: boolean
4 changes: 4 additions & 0 deletions fixtures/yaml/models/properties/propertyWithByteArray.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: array
items:
type: string
format: byte
3 changes: 3 additions & 0 deletions fixtures/yaml/models/properties/propertyWithComplexArray.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: array
items:
$ref: ComplexType
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: array
items:
type: string
format: date-time
4 changes: 4 additions & 0 deletions fixtures/yaml/models/properties/propertyWithInt32Array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: array
items:
type: integer
format: int32
4 changes: 4 additions & 0 deletions fixtures/yaml/models/properties/propertyWithInt64Array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: array
items:
type: integer
format: int64
3 changes: 3 additions & 0 deletions fixtures/yaml/models/properties/propertyWithRef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ref: Foo
description: 'a boolean'
readOnly: true
3 changes: 3 additions & 0 deletions fixtures/yaml/models/properties/propertyWithStringArray.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: array
items:
type: string
3 changes: 3 additions & 0 deletions fixtures/yaml/models/properties/simpleBooleanProperty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: boolean
description: 'a boolean'
readOnly: true
2 changes: 2 additions & 0 deletions fixtures/yaml/models/properties/simpleByteProperty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: string
format: byte
2 changes: 2 additions & 0 deletions fixtures/yaml/models/properties/simpleDateTimeProperty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: string
format: date-time
2 changes: 2 additions & 0 deletions fixtures/yaml/models/properties/simpleInt32Property.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: integer
format: int32
2 changes: 2 additions & 0 deletions fixtures/yaml/models/properties/simpleInt64Property.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: integer
format: int64
1 change: 1 addition & 0 deletions fixtures/yaml/models/properties/simpleStringProperty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: string
23 changes: 23 additions & 0 deletions fixtures/yaml/resources/cascadingSchemes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
swagger: '2.0'
info:
version: 1.0.9-abcd
title: 'Swagger Sample API'
description: 'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification'
termsOfService: 'http://helloreverb.com/terms/'
contact: {name: 'wordnik api team', url: 'http://developer.wordnik.com'}
license: {name: 'Creative Commons 4.0 International', url: 'http://creativecommons.org/licenses/by/4.0/'}
host: my.api.com
basePath: /v1
schemes:
- http
- https
consumes:
- application/json
produces:
- application/json
- application/xml
paths:
'/pets/{petId}': {get: {description: 'Returns a pet based on ID', summary: 'Find pet by ID', operationId: getPetsById, produces: [application/json, text/html], parameters: [{name: petId, in: path, description: 'ID of pet that needs to be fetched', required: true, type: array, items: {type: string}, collectionFormat: csv}], responses: {'200': {description: 'pet response', schema: {$ref: Pet}}, default: {description: 'error payload', schema: {$ref: ErrorModel}}}, schemes: [https]}}
definitions:
Pet: {required: [name], properties: {name: {type: string}, tag: {type: string}}}
ErrorModel: {required: [code, message], properties: {code: {type: integer, format: int32}, message: {type: string}}}
23 changes: 23 additions & 0 deletions fixtures/yaml/resources/commonParameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
swagger: '2.0'
info:
version: 1.0.9-abcd
title: 'Swagger Sample API'
description: 'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification'
termsOfService: 'http://helloreverb.com/terms/'
contact: {name: 'wordnik api team', url: 'http://developer.wordnik.com'}
license: {name: 'Creative Commons 4.0 International', url: 'http://creativecommons.org/licenses/by/4.0/'}
host: my.api.com
basePath: /v1
schemes:
- http
- https
consumes:
- application/json
produces:
- application/json
- application/xml
paths:
'/pets/{id}': {parameters: [{name: id, in: path, description: 'ID of pet to use', required: true, type: array, items: {type: string}, collectionFormat: csv}], get: {description: 'Returns pets based on ID', summary: 'Find pets by ID', operationId: getPetsById, produces: [application/json, text/html], responses: {'200': {description: 'pet response', schema: {type: array, items: {$ref: Pet}}}, default: {description: 'error payload', schema: {$ref: ErrorModel}}}}}
definitions:
Pet: {required: [name], properties: {name: {type: string}, tag: {type: string}}}
ErrorModel: {required: [code, message], properties: {code: {type: integer, format: int32}, message: {type: string}}}
32 changes: 32 additions & 0 deletions fixtures/yaml/resources/multipleMimeTypes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
swagger: '2.0'
info:
version: 1.0.9-abcd
title: 'Swagger Sample API'
description: 'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification'
termsOfService: 'http://helloreverb.com/terms/'
contact: {name: 'wordnik api team', url: 'http://developer.wordnik.com'}
license: {name: 'Creative Commons 4.0 International', url: 'http://creativecommons.org/licenses/by/4.0/'}
host: my.api.com
basePath: /v1
schemes:
- http
- https
consumes:
- 'text/plain; charset=utf-8'
- application/json
- application/vnd.github+json
- application/vnd.github.v3+json
- application/vnd.github.v3.raw+json
- application/vnd.github.v3.text+json
- application/vnd.github.v3.html+json
- application/vnd.github.v3.full+json
- application/vnd.github.v3.diff
- application/vnd.github.v3.patch
produces:
- application/json
- application/xml
paths:
'/pets/{id}': {parameters: [{name: id, in: path, description: 'ID of pet to use', required: true, type: array, items: {type: string}, collectionFormat: csv}], get: {description: 'Returns pets based on ID', summary: 'Find pets by ID', operationId: getPetsById, produces: [application/json, text/html], responses: {'200': {description: 'pet response', schema: {type: array, items: {$ref: Pet}}}, default: {description: 'error payload', schema: {$ref: ErrorModel}}}}}
definitions:
Pet: {required: [name], properties: {name: {type: string}, tag: {type: string}}}
ErrorModel: {required: [code, message], properties: {code: {type: integer, format: int32}, message: {type: string}}}
14 changes: 14 additions & 0 deletions fixtures/yaml/resources/operations/operationWithTags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description: 'Returns a pet based on ID'
summary: 'Find pet by ID'
operationId: getPetsById
tags:
- foo
- bar
produces:
- application/json
- text/html
parameters:
- {name: petId, in: path, description: 'ID of pet that needs to be fetched', required: true, type: integer, format: int64}
responses:
'200': {description: 'a pet to be returned', schema: {$ref: Pet}}
default: {description: 'Unexpected error', schema: {$ref: ErrorModel}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
description: 'Returns a pet based on ID'
summary: 'Find pet by ID'
operationId: getPetsById
produces:
- application/json
- text/html
parameters:
- {name: petId, in: path, description: 'ID of pet that needs to be fetched', required: true, type: integer, format: int64}
- {name: includeDetails, in: query, description: 'include details in response', required: true, type: boolean}
responses:
'200': {description: 'a pet to be returned', schema: {$ref: Pet}}
default: {description: 'Unexpected error', schema: {$ref: ErrorModel}}
12 changes: 12 additions & 0 deletions fixtures/yaml/resources/operations/stringPathParamResource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
description: 'Returns a pet based on ID'
summary: 'Find pet by ID'
operationId: getPetsById
produces:
- application/json
- text/html
parameters:
- {name: petId, in: path, description: 'ID of pet that needs to be fetched', required: true, type: integer, format: int64}
responses:
'200': {description: fun, schema: {$ref: Pet}}
'400': {description: 'Invalid ID supplied <= this is purely for documentation', schema: {$ref: ErrorModel}}
default: {description: 'Unexpected error', schema: {$ref: ErrorModel}}
Loading

0 comments on commit 1d6e4d6

Please sign in to comment.