Fixes to tests.

This commit is contained in:
Shawn Poulson
2023-09-25 09:47:50 -04:00
parent b6c05f5af0
commit c225546b04
10 changed files with 80 additions and 64 deletions

View File

@@ -56,7 +56,7 @@ func TestNewDocument_Security(t *testing.T) {
initTest()
h := NewDocument(lowDoc)
assert.Len(t, h.Security, 1)
assert.Len(t, h.Security[0].Requirements, 1)
assert.Equal(t, 1, orderedmap.Len(h.Security[0].Requirements))
assert.Len(t, h.Security[0].Requirements.GetOrZero("OAuthScheme"), 2)
}
@@ -97,13 +97,13 @@ func TestNewDocument_Servers(t *testing.T) {
assert.Len(t, h.Servers, 2)
assert.Equal(t, "{scheme}://api.pb33f.io", h.Servers[0].URL)
assert.Equal(t, "this is our main API server, for all fun API things.", h.Servers[0].Description)
assert.Len(t, h.Servers[0].Variables, 1)
assert.Equal(t, 1, orderedmap.Len(h.Servers[0].Variables))
assert.Equal(t, "https", h.Servers[0].Variables.GetOrZero("scheme").Default)
assert.Len(t, h.Servers[0].Variables.GetOrZero("scheme").Enum, 2)
assert.Equal(t, "https://{domain}.{host}.com", h.Servers[1].URL)
assert.Equal(t, "this is our second API server, for all fun API things.", h.Servers[1].Description)
assert.Len(t, h.Servers[1].Variables, 2)
assert.Equal(t, 2, orderedmap.Len(h.Servers[1].Variables))
assert.Equal(t, "api", h.Servers[1].Variables.GetOrZero("domain").Default)
assert.Equal(t, "pb33f.io", h.Servers[1].Variables.GetOrZero("host").Default)
@@ -363,7 +363,7 @@ func testBurgerShop(t *testing.T, h *Document, checkLines bool) {
)
assert.Equal(t, 2, orderedmap.Len(okResp.Links))
assert.Equal(t, "locateBurger", okResp.Links.GetOrZero("LocateBurger").OperationId)
assert.Len(t, burgersOp.Post.Security[0].Requirements, 1)
assert.Equal(t, 1, orderedmap.Len(burgersOp.Post.Security[0].Requirements))
assert.Len(t, burgersOp.Post.Security[0].Requirements.GetOrZero("OAuthScheme"), 2)
assert.Equal(t, "read:burgers", burgersOp.Post.Security[0].Requirements.GetOrZero("OAuthScheme")[0])
assert.Len(t, burgersOp.Post.Servers, 1)

View File

@@ -10,6 +10,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
@@ -42,10 +43,10 @@ links:
r := NewResponse(&n)
assert.Len(t, r.Headers, 1)
assert.Len(t, r.Content, 1)
assert.Equal(t, 1, orderedmap.Len(r.Headers))
assert.Equal(t, 1, orderedmap.Len(r.Content))
assert.Equal(t, "pizza!", r.Extensions["x-pizza-man"])
assert.Len(t, r.Links, 1)
assert.Equal(t, 1, orderedmap.Len(r.Links))
assert.Equal(t, 1, r.GoLow().Description.KeyNode.Line)
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/resolver"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
@@ -171,7 +172,7 @@ func Test_Schema(t *testing.T) {
assert.Equal(t, "something object", sch.Description.Value)
assert.True(t, sch.AdditionalProperties.Value.(bool))
assert.Len(t, sch.Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sch.Properties.Value))
v := sch.FindProperty("somethingB")
assert.Equal(t, "https://pb33f.io", v.Value.Schema().ExternalDocs.Value.URL.Value)
@@ -204,7 +205,7 @@ func Test_Schema(t *testing.T) {
// check polymorphic values allOf
f := sch.AllOf.Value[0].Value.Schema()
assert.Equal(t, "an allof thing", f.Description.Value)
assert.Len(t, f.Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(f.Properties.Value))
v = f.FindProperty("allOfA")
assert.NotNil(t, v)
@@ -221,7 +222,7 @@ func Test_Schema(t *testing.T) {
// check polymorphic values anyOf
assert.Equal(t, "an anyOf thing", sch.AnyOf.Value[0].Value.Schema().Description.Value)
assert.Len(t, sch.AnyOf.Value[0].Value.Schema().Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sch.AnyOf.Value[0].Value.Schema().Properties.Value))
v = sch.AnyOf.Value[0].Value.Schema().FindProperty("anyOfA")
assert.NotNil(t, v)
@@ -235,7 +236,7 @@ func Test_Schema(t *testing.T) {
// check polymorphic values oneOf
assert.Equal(t, "a oneof thing", sch.OneOf.Value[0].Value.Schema().Description.Value)
assert.Len(t, sch.OneOf.Value[0].Value.Schema().Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sch.OneOf.Value[0].Value.Schema().Properties.Value))
v = sch.OneOf.Value[0].Value.Schema().FindProperty("oneOfA")
assert.NotNil(t, v)
@@ -249,7 +250,7 @@ func Test_Schema(t *testing.T) {
// check values NOT
assert.Equal(t, "a not thing", sch.Not.Value.Schema().Description.Value)
assert.Len(t, sch.Not.Value.Schema().Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sch.Not.Value.Schema().Properties.Value))
v = sch.Not.Value.Schema().FindProperty("notA")
assert.NotNil(t, v)
@@ -263,7 +264,7 @@ func Test_Schema(t *testing.T) {
// check values Items
assert.Equal(t, "an items thing", sch.Items.Value.A.Schema().Description.Value)
assert.Len(t, sch.Items.Value.A.Schema().Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sch.Items.Value.A.Schema().Properties.Value))
v = sch.Items.Value.A.Schema().FindProperty("itemsA")
assert.NotNil(t, v)
@@ -277,7 +278,7 @@ func Test_Schema(t *testing.T) {
// check values PrefixItems
assert.Equal(t, "an items thing", sch.PrefixItems.Value[0].Value.Schema().Description.Value)
assert.Len(t, sch.PrefixItems.Value[0].Value.Schema().Properties.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sch.PrefixItems.Value[0].Value.Schema().Properties.Value))
v = sch.PrefixItems.Value[0].Value.Schema().FindProperty("itemsA")
assert.NotNil(t, v)

View File

@@ -4,9 +4,11 @@
package base
import (
"testing"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestSecurityRequirement_Build(t *testing.T) {
@@ -36,7 +38,7 @@ one:
_ = sr.Build(nil, idxNode.Content[0], nil)
_ = sr2.Build(nil, idxNode2.Content[0], nil)
assert.Len(t, sr.Requirements.Value, 2)
assert.Equal(t, 2, orderedmap.Len(sr.Requirements.Value))
assert.Len(t, sr.GetKeys(), 2)
assert.Len(t, sr.FindRequirement("one"), 2)
assert.Equal(t, sr.Hash(), sr2.Hash())

View File

@@ -4,12 +4,14 @@
package v2
import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestOperation_Build_ExternalDocs(t *testing.T) {
@@ -180,7 +182,7 @@ security:
assert.Equal(t, "theMagicCastle", n.GetOperationId().Value)
assert.Len(t, n.GetParameters().Value, 1)
assert.True(t, n.GetDeprecated().Value)
assert.Len(t, n.GetResponses().Value.(*Responses).Codes, 1)
assert.Equal(t, 1, orderedmap.Len(n.GetResponses().Value.(*Responses).Codes))
assert.Len(t, n.GetSecurity().Value, 1)
assert.Len(t, n.GetExtensions(), 1)
}

View File

@@ -4,11 +4,13 @@
package v2
import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestSecurityScheme_Build_Borked(t *testing.T) {
@@ -47,7 +49,7 @@ func TestSecurityScheme_Build_Scopes(t *testing.T) {
err = n.Build(nil, idxNode.Content[0], idx)
assert.NoError(t, err)
assert.Len(t, n.Scopes.Value.Values, 2)
assert.Equal(t, 2, orderedmap.Len(n.Scopes.Value.Values))
}

View File

@@ -5,10 +5,12 @@ package v2
import (
"fmt"
"github.com/pb33f/libopenapi/datamodel"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
)
var doc *Swagger
@@ -55,13 +57,13 @@ func TestCreateDocument(t *testing.T) {
assert.Equal(t, "1.0.6", doc.Info.Value.Version.Value)
assert.Equal(t, "petstore.swagger.io", doc.Host.Value)
assert.Equal(t, "/v2", doc.BasePath.Value)
assert.Len(t, doc.Parameters.Value.Definitions, 1)
assert.Equal(t, 1, orderedmap.Len(doc.Parameters.Value.Definitions))
assert.Len(t, doc.Tags.Value, 3)
assert.Len(t, doc.Schemes.Value, 2)
assert.Len(t, doc.Definitions.Value.Schemas, 6)
assert.Len(t, doc.SecurityDefinitions.Value.Definitions, 3)
assert.Len(t, doc.Paths.Value.PathItems, 15)
assert.Len(t, doc.Responses.Value.Definitions, 2)
assert.Equal(t, 6, orderedmap.Len(doc.Definitions.Value.Schemas))
assert.Equal(t, 3, orderedmap.Len(doc.SecurityDefinitions.Value.Definitions))
assert.Equal(t, 15, orderedmap.Len(doc.Paths.Value.PathItems))
assert.Equal(t, 2, orderedmap.Len(doc.Responses.Value.Definitions))
assert.Equal(t, "http://swagger.io", doc.ExternalDocs.Value.URL.Value)
assert.Equal(t, true, doc.FindExtension("x-pet").Value)
assert.Equal(t, true, doc.FindExtension("X-Pet").Value)
@@ -101,7 +103,7 @@ func TestCreateDocument_SecurityDefinitions(t *testing.T) {
petStoreAuth := doc.SecurityDefinitions.Value.FindSecurityDefinition("petstore_auth")
assert.Equal(t, "oauth2", petStoreAuth.Value.Type.Value)
assert.Equal(t, "implicit", petStoreAuth.Value.Flow.Value)
assert.Len(t, petStoreAuth.Value.Scopes.Value.Values, 2)
assert.Equal(t, 2, orderedmap.Len(petStoreAuth.Value.Scopes.Value.Values))
assert.Equal(t, "read your pets", petStoreAuth.Value.Scopes.Value.FindScope("read:pets").Value)
}
@@ -109,7 +111,7 @@ func TestCreateDocument_Definitions(t *testing.T) {
initTest()
apiResp := doc.Definitions.Value.FindSchema("ApiResponse").Value.Schema()
assert.NotNil(t, apiResp)
assert.Len(t, apiResp.Properties.Value, 3)
assert.Equal(t, 3, orderedmap.Len(apiResp.Properties.Value))
assert.Equal(t, "integer", apiResp.FindProperty("code").Value.Schema().Type.Value.A)
pet := doc.Definitions.Value.FindSchema("Pet").Value.Schema()

View File

@@ -4,11 +4,13 @@
package v3
import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestCallback_Build_Success(t *testing.T) {
@@ -36,7 +38,7 @@ func TestCallback_Build_Success(t *testing.T) {
err = n.Build(nil, rootNode.Content[0], nil)
assert.NoError(t, err)
assert.Len(t, n.Expression.Value, 1)
assert.Equal(t, 1, orderedmap.Len(n.Expression.Value))
}
@@ -102,7 +104,7 @@ func TestCallback_Build_Using_InlineRef(t *testing.T) {
err = n.Build(nil, rootNode.Content[0], idx)
assert.NoError(t, err)
assert.Len(t, n.Expression.Value, 1)
assert.Equal(t, 1, orderedmap.Len(n.Expression.Value))
exp := n.FindExpression("{$request.query.queryUrl}")
assert.NotNil(t, exp.Value)

View File

@@ -225,7 +225,7 @@ func TestCreateDocument_Info(t *testing.T) {
func TestCreateDocument_WebHooks(t *testing.T) {
initTest()
assert.Len(t, doc.Webhooks.Value, 1)
assert.Equal(t, 1, orderedmap.Len(doc.Webhooks.Value))
for pair := orderedmap.First(doc.Webhooks.Value); pair != nil; pair = pair.Next() {
// a nice deep model should be available for us.
assert.Equal(t, "Information about a new burger",
@@ -255,7 +255,7 @@ func TestCreateDocument_Servers(t *testing.T) {
// server 1
assert.Equal(t, "{scheme}://api.pb33f.io", server1.URL.Value)
assert.NotEmpty(t, server1.Description.Value)
assert.Len(t, server1.Variables.Value, 1)
assert.Equal(t, 1, orderedmap.Len(server1.Variables.Value))
assert.Len(t, server1.FindVariable("scheme").Value.Enum, 2)
assert.Equal(t, server1.FindVariable("scheme").Value.Default.Value, "https")
assert.NotEmpty(t, server1.FindVariable("scheme").Value.Description.Value)
@@ -263,7 +263,7 @@ func TestCreateDocument_Servers(t *testing.T) {
// server 2
assert.Equal(t, "https://{domain}.{host}.com", server2.URL.Value)
assert.NotEmpty(t, server2.Description.Value)
assert.Len(t, server2.Variables.Value, 2)
assert.Equal(t, 2, orderedmap.Len(server2.Variables.Value))
assert.Equal(t, "api", server2.FindVariable("domain").Value.Default.Value)
assert.NotEmpty(t, server2.FindVariable("domain").Value.Description.Value)
assert.NotEmpty(t, server2.FindVariable("host").Value.Description.Value)
@@ -319,7 +319,7 @@ func TestCreateDocument_Tags(t *testing.T) {
func TestCreateDocument_Paths(t *testing.T) {
initTest()
assert.Len(t, doc.Paths.Value.PathItems, 5)
assert.Equal(t, 5, orderedmap.Len(doc.Paths.Value.PathItems))
burgerId := doc.Paths.Value.FindPath("/burgers/{burgerId}")
assert.NotNil(t, burgerId)
assert.Len(t, burgerId.Value.Get.Value.Parameters.Value, 2)
@@ -335,7 +335,7 @@ func TestCreateDocument_Paths(t *testing.T) {
encoding := pContent.Value.FindPropertyEncoding("burgerTheme")
assert.NotNil(t, encoding.Value)
assert.Len(t, encoding.Value.Headers.Value, 1)
assert.Equal(t, 1, orderedmap.Len(encoding.Value.Headers.Value))
header := encoding.Value.FindHeader("someHeader")
assert.NotNil(t, header.Value)
@@ -357,8 +357,8 @@ func TestCreateDocument_Paths(t *testing.T) {
content := requestBody.FindContent("application/json").Value
assert.NotNil(t, content)
assert.Len(t, content.Schema.Value.Schema().Properties.Value, 4)
assert.Len(t, content.GetAllExamples(), 2)
assert.Equal(t, 4, orderedmap.Len(content.Schema.Value.Schema().Properties.Value))
assert.Equal(t, 2, orderedmap.Len(content.GetAllExamples()))
ex := content.FindExample("pbjBurger")
assert.NotNil(t, ex.Value)
@@ -388,14 +388,14 @@ func TestCreateDocument_Paths(t *testing.T) {
// check responses
responses := burgersPost.Responses.Value
assert.NotNil(t, responses)
assert.Len(t, responses.Codes, 3)
assert.Equal(t, 3, orderedmap.Len(responses.Codes))
okCode := responses.FindResponseByCode("200")
assert.NotNil(t, okCode.Value)
assert.Equal(t, "A tasty burger for you to eat.", okCode.Value.Description.Value)
// check headers are populated
assert.Len(t, okCode.Value.Headers.Value, 1)
assert.Equal(t, 1, orderedmap.Len(okCode.Value.Headers.Value))
okheader := okCode.Value.FindHeader("UseOil")
assert.NotNil(t, okheader.Value)
assert.Equal(t, "this is a header example for UseOil", okheader.Value.Description.Value)
@@ -421,7 +421,7 @@ func TestCreateDocument_Paths(t *testing.T) {
// check links
links := okCode.Value.Links
assert.NotNil(t, links.Value)
assert.Len(t, links.Value, 2)
assert.Equal(t, 2, orderedmap.Len(links.Value))
assert.Equal(t, "locateBurger", okCode.Value.FindLink("LocateBurger").Value.OperationId.Value)
locateBurger := okCode.Value.FindLink("LocateBurger").Value

View File

@@ -6,9 +6,11 @@ package renderer
import (
"encoding/json"
"fmt"
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
"gopkg.in/yaml.v3"
"reflect"
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
)
const Example = "Example"
@@ -25,7 +27,7 @@ const (
// MockGenerator is used to generate mocks for high-level mockable structs or *base.Schema pointers.
// The mock generator will attempt to generate a mock from a struct using the following fields:
// - Example: any type, this is the default example to use if no examples are present.
// - Examples: map[string]*base.Example, this is a map of examples keyed by name.
// - Examples: orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
// - Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
//
// The mock generator will attempt to generate a mock from a *base.Schema pointer.
@@ -59,7 +61,7 @@ func (mg *MockGenerator) SetPretty() {
// GenerateMock generates a mock for a given high-level mockable struct. The mockable struct must contain the following fields:
// Example: any type, this is the default example to use if no examples are present.
// Examples: map[string]*base.Example, this is a map of examples keyed by name.
// Examples: orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
// Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
// The name parameter is optional, if provided, the mock generator will attempt to find an example with the given name.
// If no name is provided, the first example will be used.
@@ -98,18 +100,20 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
examplesValue := examples.Interface()
if examplesValue != nil && !examples.IsNil() {
// cast examples to map[string]interface{}
examplesMap := examplesValue.(map[string]*highbase.Example)
// cast examples to orderedmap.Map[string, any]
examplesMap := examplesValue.(orderedmap.Map[string, *highbase.Example])
// if the name is not empty, try and find the example by name
for k, exp := range examplesMap {
for pair := orderedmap.First(examplesMap); pair != nil; pair = pair.Next() {
k, exp := pair.Key(), pair.Value()
if k == name {
return mg.renderMock(exp.Value), nil
}
}
// if the name is empty, just return the first example
for _, exp := range examplesMap {
for pair := orderedmap.First(examplesMap); pair != nil; pair = pair.Next() {
exp := pair.Value()
return mg.renderMock(exp.Value), nil
}
}