Skip to content

Commit

Permalink
Add missing tests on specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Oct 13, 2018
1 parent 7201668 commit 84181d2
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
20 changes: 20 additions & 0 deletions dataset/multi_file_spec/pet_definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
52 changes: 52 additions & 0 deletions dataset/multi_file_spec/petstore_minimal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team"
},
"license": {
"name": "MIT"
}
},
"host": "petstore.swagger.io",
"basePath": "/api",
"schemes": [
"http"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/pets": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "A list of pets.",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
}
}
}
}
},
"definitions": {
"Pet": {
"$ref": "./pet_definition.json"
}
}
}
52 changes: 52 additions & 0 deletions dataset/petstore_invalid_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team"
},
"license": {
"name": "MIT"
}
},
"host": "petstore.swagger.io",
"basePath": "/api",
"schemes": [
"http"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/pets": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "A list of pets.",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
}
}
}
}
},
"definitions": {
"Pet": {
"$ref": "./invalid-file-ref.json"
}
}
}
35 changes: 35 additions & 0 deletions specs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oaichecker

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,13 +15,47 @@ func Test_NewSpecsFromFile_with_load_error(t *testing.T) {
assert.EqualError(t, err, "open some-unknown-path: no such file or directory")
}

func Test_NewSpecsFromRaw(t *testing.T) {
rawSpecs, err := ioutil.ReadFile("./dataset/petstore_minimal.json")
require.NoError(t, err)

specs, err := NewSpecsFromRaw(rawSpecs)

assert.NotNil(t, specs)
assert.NoError(t, err)
}

func Test_NewSpecsFromRaw_with_unmarshalable_content(t *testing.T) {
specs, err := NewSpecsFromRaw([]byte("no a valid spec"))

assert.Nil(t, specs)
assert.EqualError(t, err, "analyzed: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `no a va...` into map[interface {}]interface {}")
}

func Test_NewSpecsFromFile_with_multi_file_spec_and_invalid_ref_should_fail(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore_invalid_ref.json")

assert.Nil(t, specs)
assert.Contains(t, err.Error(), "no such file or directory")
}

func Test_NewSpecsFromFile_with_multi_file_spec(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/multi_file_spec/petstore_minimal.json")

assert.NotNil(t, specs)
assert.NoError(t, err)
}

func Test_NewSpecsFromRaw_with_multi_file_spec_and_invalid_working_dir_should_fail(t *testing.T) {
rawSpecs, err := ioutil.ReadFile("./dataset/multi_file_spec/petstore_minimal.json")
require.NoError(t, err)

specs, err := NewSpecsFromRaw(rawSpecs)

assert.Nil(t, specs)
assert.Contains(t, err.Error(), "no such file or directory")
}

func Test_Specs_Validate_with_invalid_specs(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore_invalid.json")
require.NoError(t, err)
Expand Down

0 comments on commit 84181d2

Please sign in to comment.