Added cache set/get for index

It’s required to be able to ensire full coverage to test things that can’t be tested without a huge amount of test rigging.

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-11-01 11:38:16 -04:00
parent 97659f2244
commit 3c27c43ec0
4 changed files with 119 additions and 1 deletions

View File

@@ -7,7 +7,9 @@ import (
"context"
"crypto/sha256"
"fmt"
"golang.org/x/sync/syncmap"
"net/url"
"os"
"strings"
"testing"
@@ -1889,3 +1891,72 @@ func TestLocateRefNode_NoExplode_HTTP(t *testing.T) {
assert.NotNil(t, e)
assert.NotNil(t, c)
}
func TestLocateRefNode_NoExplode_NoSpecPath(t *testing.T) {
no := yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
{
Kind: yaml.ScalarNode,
Value: "$ref",
},
{
Kind: yaml.ScalarNode,
Value: "components/schemas/thing.yaml",
},
},
}
cf := index.CreateClosedAPIIndexConfig()
u, _ := url.Parse("http://smilfghfhfhfhfhes.com/bikes")
cf.BaseURL = u
idx := index.NewSpecIndexWithConfig(&no, cf)
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "no.yaml")
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
assert.Nil(t, n)
assert.NotNil(t, i)
assert.NotNil(t, e)
assert.NotNil(t, c)
}
func TestLocateRefNode_DoARealLookup(t *testing.T) {
no := yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
{
Kind: yaml.ScalarNode,
Value: "$ref",
},
{
Kind: yaml.ScalarNode,
Value: "/root.yaml#/components/schemas/Burger",
},
},
}
b, err := os.ReadFile("../../test_specs/burgershop.openapi.yaml")
if err != nil {
t.Fatal(err)
}
var rootNode yaml.Node
_ = yaml.Unmarshal(b, &rootNode)
cf := index.CreateClosedAPIIndexConfig()
u, _ := url.Parse("http://smilfghfhfhfhfhes.com/bikes")
cf.BaseURL = u
idx := index.NewSpecIndexWithConfig(&rootNode, cf)
// fake cache to a lookup for a file that does not exist will work.
fakeCache := new(syncmap.Map)
fakeCache.Store("/root.yaml#/components/schemas/Burger", &index.Reference{Node: &no})
idx.SetCache(fakeCache)
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "/root.yaml#/components/schemas/Burger")
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
assert.NotNil(t, n)
assert.NotNil(t, i)
assert.Nil(t, e)
assert.NotNil(t, c)
}