mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
chore: add more test coverage
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel/high/base"
|
||||
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
@@ -100,7 +101,6 @@ func TestNewMockGeneratorWithDictionary(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMockGenerator_GenerateJSONMock_NoObject(t *testing.T) {
|
||||
|
||||
mg := NewMockGenerator(JSON)
|
||||
|
||||
var isNil any
|
||||
@@ -285,3 +285,41 @@ func TestMockGenerator_GenerateJSONMock_Object_RawSchema(t *testing.T) {
|
||||
assert.GreaterOrEqual(t, m["herbs"].(int), 350)
|
||||
assert.LessOrEqual(t, m["herbs"].(int), 400)
|
||||
}
|
||||
|
||||
func TestMockGenerator_GenerateMock_YamlNode(t *testing.T) {
|
||||
mg := NewMockGenerator(YAML)
|
||||
|
||||
type mockable struct {
|
||||
Example *yaml.Node
|
||||
Examples *orderedmap.Map[string, *base.Example]
|
||||
}
|
||||
|
||||
mock, err := mg.GenerateMock(&mockable{
|
||||
Example: utils.CreateStringNode("hello"),
|
||||
}, "")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello", strings.TrimSpace(string(mock)))
|
||||
}
|
||||
|
||||
func TestMockGenerator_GenerateMock_YamlNode_Nil(t *testing.T) {
|
||||
mg := NewMockGenerator(YAML)
|
||||
|
||||
var example *yaml.Node
|
||||
|
||||
type mockable struct {
|
||||
Example any
|
||||
Examples *orderedmap.Map[string, *base.Example]
|
||||
}
|
||||
|
||||
examples := orderedmap.New[string, *base.Example]()
|
||||
examples.Set("exampleOne", &base.Example{
|
||||
Value: utils.CreateStringNode("hello"),
|
||||
})
|
||||
|
||||
mock, err := mg.GenerateMock(&mockable{
|
||||
Example: example,
|
||||
Examples: examples,
|
||||
}, "")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello", strings.TrimSpace(string(mock)))
|
||||
}
|
||||
|
||||
@@ -247,6 +247,7 @@ func CheckMapForChanges[T any, R any](expLeft, expRight *orderedmap.Map[low.KeyR
|
||||
return CheckMapForChangesWithComp(expLeft, expRight, changes, label, compareFunc, true)
|
||||
}
|
||||
|
||||
// CheckMapForAdditionRemoval checks a left and right low level map for any additions or subtractions, but not modifications
|
||||
func CheckMapForAdditionRemoval[T any](expLeft, expRight *orderedmap.Map[low.KeyReference[string], low.ValueReference[T]],
|
||||
changes *[]*Change, label string,
|
||||
) any {
|
||||
@@ -254,20 +255,13 @@ func CheckMapForAdditionRemoval[T any](expLeft, expRight *orderedmap.Map[low.Key
|
||||
doNothing := func(l, r T) any {
|
||||
return nil
|
||||
}
|
||||
// Adding purely to make sure code is called for coverage.
|
||||
var l, r T
|
||||
doNothing(l, r)
|
||||
// end of coverage code.
|
||||
return CheckMapForChangesWithComp(expLeft, expRight, changes, label, doNothing, false)
|
||||
}
|
||||
|
||||
//// CheckMapForAdditionRemoval checks a left and right low level map for any additions or subtractions, but not modifications
|
||||
//func CheckMapForAdditionRemoval[T any, R any](expLeft, expRight map[low.KeyReference[string]]low.ValueReference[T],
|
||||
// changes *[]*Change, label string) map[string]R {
|
||||
//
|
||||
// // do nothing
|
||||
// doNothing := func(l, r T) R {
|
||||
// return nil
|
||||
// }
|
||||
// return CheckMapForChangesWithComp(expLeft, expRight, changes, label, doNothing, false)
|
||||
//}
|
||||
|
||||
// CheckMapForChangesWithComp checks a left and right low level map for any additions, subtractions or modifications to
|
||||
// values. The compareFunc argument should reference the correct comparison function for the generic type. The compare
|
||||
// bit determines if the comparison should be run or not.
|
||||
|
||||
@@ -6,6 +6,9 @@ package model
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/orderedmap"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -90,7 +93,6 @@ func Test_CheckForModification_ArrayMap(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_CheckMapsRemoval(t *testing.T) {
|
||||
|
||||
mapA := make(map[string]string)
|
||||
mapB := make(map[string]string)
|
||||
|
||||
@@ -135,7 +137,6 @@ func Test_CheckMapsRemoval(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_CheckMapsAddition(t *testing.T) {
|
||||
|
||||
mapA := make(map[string]string)
|
||||
mapB := make(map[string]string)
|
||||
|
||||
@@ -186,3 +187,48 @@ func Test_CheckMapsAddition(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenLowLevelOrderedMap(t *testing.T) {
|
||||
m := orderedmap.New[low.KeyReference[string], low.ValueReference[string]]()
|
||||
m.Set(low.KeyReference[string]{
|
||||
Value: "key",
|
||||
}, low.ValueReference[string]{
|
||||
Value: "value",
|
||||
})
|
||||
|
||||
o := FlattenLowLevelOrderedMap[string](m)
|
||||
assert.Equal(t, map[string]*low.ValueReference[string]{
|
||||
"key": {
|
||||
Value: "value",
|
||||
},
|
||||
}, o)
|
||||
}
|
||||
|
||||
func TestCheckMapForAdditionRemoval(t *testing.T) {
|
||||
var changes []*Change
|
||||
|
||||
l := orderedmap.New[low.KeyReference[string], low.ValueReference[string]]()
|
||||
l.Set(low.KeyReference[string]{
|
||||
Value: "key",
|
||||
}, low.ValueReference[string]{
|
||||
Value: "value",
|
||||
ValueNode: utils.CreateStringNode("value"),
|
||||
})
|
||||
l.Set(low.KeyReference[string]{
|
||||
Value: "key2",
|
||||
}, low.ValueReference[string]{
|
||||
Value: "value2",
|
||||
ValueNode: utils.CreateStringNode("value2"),
|
||||
})
|
||||
|
||||
r := orderedmap.New[low.KeyReference[string], low.ValueReference[string]]()
|
||||
r.Set(low.KeyReference[string]{
|
||||
Value: "key",
|
||||
}, low.ValueReference[string]{
|
||||
Value: "value",
|
||||
ValueNode: utils.CreateStringNode("value"),
|
||||
})
|
||||
|
||||
CheckMapForAdditionRemoval(l, r, &changes, "label")
|
||||
assert.Len(t, changes, 1)
|
||||
}
|
||||
|
||||
@@ -110,6 +110,10 @@ func (d *DocumentChanges) GetAllChanges() []*Change {
|
||||
|
||||
// TotalBreakingChanges returns a total count of all breaking changes made in the Document
|
||||
func (d *DocumentChanges) TotalBreakingChanges() int {
|
||||
if d == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
c := d.PropertyChanges.TotalBreakingChanges()
|
||||
if d.InfoChanges != nil {
|
||||
c += d.InfoChanges.TotalBreakingChanges()
|
||||
|
||||
@@ -794,7 +794,6 @@ jsonSchemaDialect: https://pb33f.io/schema`
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_BaseProperties_Modified(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
x-diet: tough
|
||||
jsonSchemaDialect: https://pb33f.io/schema`
|
||||
@@ -823,7 +822,6 @@ jsonSchemaDialect: https://pb33f.io/schema/changed`
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_AddComponents(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1`
|
||||
|
||||
right := `openapi: 3.1
|
||||
@@ -853,7 +851,6 @@ components:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_Removed(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1`
|
||||
|
||||
right := `openapi: 3.1
|
||||
@@ -883,7 +880,6 @@ components:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_ModifyPaths(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
paths:
|
||||
/brown/cow:
|
||||
@@ -922,7 +918,6 @@ paths:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_Identical_Security(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
security:
|
||||
- cakes:
|
||||
@@ -958,7 +953,6 @@ security:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_ModifyComponents(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
components:
|
||||
schemas:
|
||||
@@ -995,7 +989,6 @@ components:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_ModifyServers(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
servers:
|
||||
- url: https://pb33f.io
|
||||
@@ -1027,7 +1020,6 @@ servers:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_ModifyExamples(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
components:
|
||||
examples:
|
||||
@@ -1062,7 +1054,6 @@ components:
|
||||
}
|
||||
|
||||
func TestCompareDocuments_OpenAPI_ModifyWebhooks(t *testing.T) {
|
||||
|
||||
left := `openapi: 3.1
|
||||
webhooks:
|
||||
bHook:
|
||||
@@ -1183,3 +1174,10 @@ paths:
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
|
||||
func TestDocumentChanges_Nil(t *testing.T) {
|
||||
var dc *DocumentChanges
|
||||
assert.Equal(t, 0, dc.TotalChanges())
|
||||
assert.Equal(t, 0, dc.TotalBreakingChanges())
|
||||
assert.Nil(t, dc.GetAllChanges())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user