fix: add test coverage

This commit is contained in:
marclave
2023-05-22 10:36:22 -07:00
committed by Dave Shanley
parent bdc6038722
commit 2777dcc4ad
10 changed files with 589 additions and 236 deletions

View File

@@ -5,12 +5,13 @@ package base
import ( import (
"fmt" "fmt"
"strings"
"testing"
lowmodel "github.com/pb33f/libopenapi/datamodel/low" lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base" lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
func TestNewTag(t *testing.T) { func TestNewTag(t *testing.T) {
@@ -46,8 +47,18 @@ x-hack: code`
} }
func ExampleNewTag() { func TestTag_RenderInline(t *testing.T) {
tag := &Tag{
Name: "cake",
}
tri, _ := tag.RenderInline()
assert.Equal(t, "name: cake", strings.TrimSpace(string(tri)))
}
func ExampleNewTag() {
// create an example schema object // create an example schema object
// this can be either JSON or YAML. // this can be either JSON or YAML.
yml := ` yml := `

View File

@@ -4,17 +4,108 @@
package v3 package v3
import ( import (
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
) )
func TestMediaType_MarshalYAMLInline(t *testing.T) {
// load the petstore spec
data, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = v3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("broken something")
}
// create a new document and extract a media type object from it.
d := NewDocument(lowDoc)
mt := d.Paths.PathItems["/pet"].Put.RequestBody.Content["application/json"]
// render out the media type
yml, _ := mt.Render()
// the rendered output should be a ref to the media type.
op := `schema:
$ref: '#/components/schemas/Pet'`
assert.Equal(t, op, strings.TrimSpace(string(yml)))
// modify the media type to have an example
mt.Example = "testing a nice mutation"
op = `schema:
required:
- name
- photoUrls
type: object
properties:
id:
type: integer
format: int64
example: 10
name:
type: string
example: doggie
category:
type: object
properties:
id:
type: integer
format: int64
example: 1
name:
type: string
example: Dogs
xml:
name: category
photoUrls:
type: array
xml:
wrapped: true
items:
type: string
xml:
name: photoUrl
tags:
type: array
xml:
wrapped: true
items:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: tag
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: pet
example: testing a nice mutation`
yml, _ = mt.RenderInline()
assert.Equal(t, op, strings.TrimSpace(string(yml)))
}
func TestMediaType_MarshalYAML(t *testing.T) { func TestMediaType_MarshalYAML(t *testing.T) {
// load the petstore spec // load the petstore spec
data, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json") data, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")
@@ -77,4 +168,3 @@ func TestMediaType_Examples(t *testing.T) {
rend, _ := r.Render() rend, _ := r.Render()
assert.Len(t, rend, 290) assert.Len(t, rend, 290)
} }

View File

@@ -4,10 +4,11 @@
package v3 package v3
import ( import (
"github.com/pb33f/libopenapi/datamodel/high/base"
"strings" "strings"
"testing" "testing"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
@@ -90,3 +91,41 @@ requestBody:
assert.Equal(t, desired, strings.TrimSpace(string(rend))) assert.Equal(t, desired, strings.TrimSpace(string(rend)))
} }
func TestOperation_MarshalYAMLInline(t *testing.T) {
op := &Operation{
Tags: []string{"test"},
Summary: "nice",
Description: "rice",
ExternalDocs: &base.ExternalDoc{
Description: "spice",
},
OperationId: "slice",
Parameters: []*Parameter{
&Parameter{
Name: "mice",
},
},
RequestBody: &RequestBody{
Description: "dice",
},
}
rend, _ := op.RenderInline()
desired := `tags:
- test
summary: nice
description: rice
externalDocs:
description: spice
operationId: slice
parameters:
- name: mice
requestBody:
description: dice`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}

View File

@@ -4,10 +4,11 @@
package v3 package v3
import ( import (
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
"strings" "strings"
"testing" "testing"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
) )
func TestParameter_MarshalYAML(t *testing.T) { func TestParameter_MarshalYAML(t *testing.T) {
@@ -44,6 +45,40 @@ x-burgers: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend))) assert.Equal(t, desired, strings.TrimSpace(string(rend)))
} }
func TestParameter_MarshalYAMLInline(t *testing.T) {
explode := true
param := Parameter{
Name: "chicken",
In: "nuggets",
Description: "beefy",
Deprecated: true,
Style: "simple",
Explode: &explode,
AllowReserved: true,
Example: "example",
Examples: map[string]*base.Example{"example": {Value: "example"}},
Extensions: map[string]interface{}{"x-burgers": "why not?"},
}
rend, _ := param.RenderInline()
desired := `name: chicken
in: nuggets
description: beefy
deprecated: true
style: simple
explode: true
allowReserved: true
example: example
examples:
example:
value: example
x-burgers: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestParameter_IsExploded(t *testing.T) { func TestParameter_IsExploded(t *testing.T) {
explode := true explode := true

View File

@@ -4,13 +4,14 @@
package v3 package v3
import ( import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
// this test exists because the sample contract doesn't contain a // this test exists because the sample contract doesn't contain a
@@ -108,3 +109,44 @@ parameters:
assert.Equal(t, desired, strings.TrimSpace(string(rend))) assert.Equal(t, desired, strings.TrimSpace(string(rend)))
} }
func TestPathItem_MarshalYAMLInline(t *testing.T) {
pi := &PathItem{
Description: "a path item",
Summary: "It's a test, don't worry about it, Jim",
Servers: []*Server{
{
Description: "a server",
},
},
Parameters: []*Parameter{
{
Name: "I am a query parameter",
In: "query",
},
},
Get: &Operation{
Description: "a get operation",
},
Post: &Operation{
Description: "a post operation",
},
}
rend, _ := pi.RenderInline()
desired := `description: a path item
summary: It's a test, don't worry about it, Jim
get:
description: a get operation
post:
description: a post operation
servers:
- description: a server
parameters:
- name: I am a query parameter
in: query`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}

View File

@@ -4,13 +4,14 @@
package v3 package v3
import ( import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3low "github.com/pb33f/libopenapi/datamodel/low/v3" v3low "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
func TestPaths_MarshalYAML(t *testing.T) { func TestPaths_MarshalYAML(t *testing.T) {
@@ -64,3 +65,55 @@ func TestPaths_MarshalYAML(t *testing.T) {
assert.Equal(t, yml, strings.TrimSpace(string(rend))) assert.Equal(t, yml, strings.TrimSpace(string(rend)))
} }
func TestPaths_MarshalYAMLInline(t *testing.T) {
yml := `/foo/bar/bizzle:
get:
description: get a bizzle
/jim/jam/jizzle:
post:
description: post a jizzle
/beer:
get:
description: get a beer now.`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3low.Paths
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.NoError(t, err)
high := NewPaths(&n)
assert.NotNil(t, high)
rend, _ := high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
// mutate
deprecated := true
high.PathItems["/beer"].Get.Deprecated = &deprecated
yml = `/foo/bar/bizzle:
get:
description: get a bizzle
/jim/jam/jizzle:
post:
description: post a jizzle
/beer:
get:
description: get a beer now.
deprecated: true`
rend, _ = high.RenderInline()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}

View File

@@ -4,9 +4,10 @@
package v3 package v3
import ( import (
"github.com/stretchr/testify/assert"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestRequestBody_MarshalYAML(t *testing.T) { func TestRequestBody_MarshalYAML(t *testing.T) {
@@ -28,6 +29,25 @@ x-high-gravity: why not?`
} }
func TestRequestBody_MarshalYAMLInline(t *testing.T) {
rb := true
req := &RequestBody{
Description: "beer",
Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rend, _ := req.RenderInline()
desired := `description: beer
required: true
x-high-gravity: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestRequestBody_MarshalNoRequired(t *testing.T) { func TestRequestBody_MarshalNoRequired(t *testing.T) {
rb := false rb := false
req := &RequestBody{ req := &RequestBody{
@@ -61,4 +81,3 @@ x-high-gravity: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend))) assert.Equal(t, desired, strings.TrimSpace(string(rend)))
} }

View File

@@ -4,13 +4,14 @@
package v3 package v3
import ( import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
// this test exists because the sample contract doesn't contain a // this test exists because the sample contract doesn't contain a
@@ -76,3 +77,31 @@ links:
assert.Equal(t, yml, strings.TrimSpace(string(rend))) assert.Equal(t, yml, strings.TrimSpace(string(rend)))
} }
func TestResponse_MarshalYAMLInline(t *testing.T) {
yml := `description: this is a response
headers:
someHeader:
description: a header
content:
something/thing:
example: cake
links:
someLink:
description: a link!`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3.Response
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewResponse(&n)
rend, _ := r.RenderInline()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}

View File

@@ -4,13 +4,14 @@
package v3 package v3
import ( import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
// this test exists because the sample contract doesn't contain a // this test exists because the sample contract doesn't contain a
@@ -67,3 +68,33 @@ func TestResponses_MarshalYAML(t *testing.T) {
assert.Equal(t, yml, strings.TrimSpace(string(rend))) assert.Equal(t, yml, strings.TrimSpace(string(rend)))
} }
func TestResponses_MarshalYAMLInline(t *testing.T) {
yml := `"201":
description: this is a response
content:
something/thing:
example: cake
"404":
description: this is a 404
content:
something/thing:
example: why do you need an example?
"200":
description: OK! not bad.`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3.Responses
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewResponses(&n)
rend, _ := r.RenderInline()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}

4
go.sum
View File

@@ -58,6 +58,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk= github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ= github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -78,6 +80,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=