chore: add more test coverage

This commit is contained in:
Tristan Cartledge
2023-12-14 14:46:20 +00:00
parent eec2dfa29d
commit 0119853344
3 changed files with 57 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
lowV3 "github.com/pb33f/libopenapi/datamodel/low/v3"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
@@ -159,3 +160,40 @@ parameters:
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestPathItem_GetOperations_NoLow(t *testing.T) {
pi := &PathItem{
Delete: &Operation{},
Post: &Operation{},
Get: &Operation{},
}
ops := pi.GetOperations()
expectedOrderOfOps := []string{"get", "post", "delete"}
actualOrder := []string{}
for pair := orderedmap.First(ops); pair != nil; pair = pair.Next() {
actualOrder = append(actualOrder, pair.Key())
}
assert.Equal(t, expectedOrderOfOps, actualOrder)
}
func TestPathItem_GetOperations_LowWithUnsetOperations(t *testing.T) {
pi := &PathItem{
Delete: &Operation{},
Post: &Operation{},
Get: &Operation{},
low: &lowV3.PathItem{},
}
ops := pi.GetOperations()
expectedOrderOfOps := []string{"get", "post", "delete"}
actualOrder := []string{}
for pair := orderedmap.First(ops); pair != nil; pair = pair.Next() {
actualOrder = append(actualOrder, pair.Key())
}
assert.Equal(t, expectedOrderOfOps, actualOrder)
}

View File

@@ -55,10 +55,7 @@ func BuildModel(node *yaml.Node, model interface{}) error {
switch kind {
case reflect.Struct, reflect.Slice, reflect.Map, reflect.Pointer:
vn = utils.NodeAlias(vn)
err := SetField(&field, vn, kn)
if err != nil {
return err
}
SetField(&field, vn, kn)
default:
return fmt.Errorf("unable to parse unsupported type: %v", kind)
}
@@ -71,9 +68,9 @@ func BuildModel(node *yaml.Node, model interface{}) error {
// SetField accepts a field reflection value, a yaml.Node valueNode and a yaml.Node keyNode. Using reflection, the
// function will attempt to set the value of the field based on the key and value nodes. This method is only useful
// for low-level models, it has no value to high-level ones.
func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) error {
func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) {
if valueNode == nil {
return nil
return
}
switch field.Type() {
@@ -451,7 +448,7 @@ func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) er
// we want to ignore everything else, each model handles its own complex types.
break
}
return nil
return
}
// BuildModelAsync is a convenience function for calling BuildModel from a goroutine, requires a sync.WaitGroup

View File

@@ -5,6 +5,7 @@ import (
"testing"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
@@ -407,3 +408,17 @@ func TestBuildModelAsync(t *testing.T) {
wg.Wait()
assert.Equal(t, 3, orderedmap.Len(ins.Thing.Value))
}
func TestSetField_NilValueNode(t *testing.T) {
assert.NotPanics(t, func() {
SetField(nil, nil, nil)
})
}
func TestBuildModelAsync_HandlesError(t *testing.T) {
errs := []error{}
wg := sync.WaitGroup{}
wg.Add(1)
BuildModelAsync(utils.CreateStringNode("cake"), "cake", &wg, &errs)
assert.NotEmpty(t, errs)
}