Adding more tests to bump coverage

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-11-22 14:10:18 -05:00
parent 102f7fc93f
commit 5d14ef226b
4 changed files with 84 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ package base
import (
"context"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
@@ -96,3 +97,67 @@ x-common-definitions:
assert.Equal(t, "The type of life cycle", sch.Schema().Description.Value)
}
func TestSchemaProxy_GetSchemaReferenceLocation(t *testing.T) {
yml := `type: object
properties:
name:
type: string
description: thing`
var idxNodeA yaml.Node
e := yaml.Unmarshal([]byte(yml), &idxNodeA)
assert.NoError(t, e)
yml = `
type: object
properties:
name:
type: string
description: thang`
var schA SchemaProxy
var schB SchemaProxy
var schC SchemaProxy
var idxNodeB yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNodeB)
c := index.CreateOpenAPIIndexConfig()
rolo := index.NewRolodex(c)
rolo.SetRootNode(&idxNodeA)
_ = rolo.IndexTheRolodex()
err := schA.Build(context.Background(), nil, idxNodeA.Content[0], rolo.GetRootIndex())
assert.NoError(t, err)
err = schB.Build(context.Background(), nil, idxNodeB.Content[0].Content[3].Content[1], rolo.GetRootIndex())
assert.NoError(t, err)
rolo.GetRootIndex().SetAbsolutePath("/rooty/rootster")
origin := schA.GetSchemaReferenceLocation()
assert.NotNil(t, origin)
assert.Equal(t, "/rooty/rootster", origin.AbsoluteLocation)
// mess things up so it cannot be found
schA.vn = schB.vn
origin = schA.GetSchemaReferenceLocation()
assert.Nil(t, origin)
// create a new index
idx := index.NewSpecIndexWithConfig(&idxNodeB, c)
idx.SetAbsolutePath("/boaty/mcboatface")
// add the index to the rolodex
rolo.AddIndex(idx)
// can now find the origin
origin = schA.GetSchemaReferenceLocation()
assert.NotNil(t, origin)
assert.Equal(t, "/boaty/mcboatface", origin.AbsoluteLocation)
// do it again, but with no index
err = schC.Build(context.Background(), nil, idxNodeA.Content[0], nil)
origin = schC.GetSchemaReferenceLocation()
assert.Nil(t, origin)
}

View File

@@ -296,6 +296,16 @@ func (index *SpecIndex) GetCache() *syncmap.Map {
return index.cache
}
// SetAbsolutePath sets the absolute path to the spec file for the index. Will be absolute, either as a http link or a file.
func (index *SpecIndex) SetAbsolutePath(absolutePath string) {
index.specAbsolutePath = absolutePath
}
// GetAbsolutePath returns the absolute path to the spec file for the index. Will be absolute, either as a http link or a file.
func (index *SpecIndex) GetAbsolutePath() string {
return index.specAbsolutePath
}
// ExternalLookupFunction is for lookup functions that take a JSONSchema reference and tries to find that node in the
// URI based document. Decides if the reference is local, remote or in a file.
type ExternalLookupFunction func(id string) (foundNode *yaml.Node, rootNode *yaml.Node, lookupError error)

View File

@@ -70,13 +70,16 @@ func (index *SpecIndex) MapNodes(rootNode *yaml.Node) {
close(index.nodeMapCompleted)
}
// FindNodeOrigin searches this index for a matching node. If the node is found, a NodeOrigin
// is returned, otherwise nil is returned.
func (index *SpecIndex) FindNodeOrigin(node *yaml.Node) *NodeOrigin {
// local search, then throw up to rolodex for a full search
if node != nil {
if index.nodeMap[node.Line] != nil {
if index.nodeMap[node.Line][node.Column] != nil {
foundNode := index.nodeMap[node.Line][node.Column]
if foundNode.Kind == yaml.DocumentNode {
foundNode = foundNode.Content[0]
}
match := true
if foundNode.Value != node.Value || foundNode.Kind != node.Kind || foundNode.Tag != node.Tag {
match = false

View File

@@ -519,6 +519,10 @@ func TestSpecIndex_PetstoreV3(t *testing.T) {
assert.Equal(t, 19, index.GetAllSummariesCount())
assert.Len(t, index.GetAllDescriptions(), 90)
assert.Len(t, index.GetAllSummaries(), 19)
index.SetAbsolutePath("/rooty/rootster")
assert.Equal(t, "/rooty/rootster", index.GetSpecAbsolutePath())
}
var mappedRefs = 15