Files
libopenapi/datamodel/low/base/external_doc_test.go
quobix 6e9db7f838 A massive test update to bring everything inlne with the new Buildable signature.
All tests in index and datamodel now pass. The rolodex fixes all the things.

Signed-off-by: quobix <dave@quobix.com>
2023-10-23 18:18:44 -04:00

83 lines
2.0 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
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"
)
func TestExternalDoc_FindExtension(t *testing.T) {
yml := `x-fish: cake`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n ExternalDoc
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)
assert.Equal(t, "cake", n.FindExtension("x-fish").Value)
}
func TestExternalDoc_Build(t *testing.T) {
yml := `url: https://pb33f.io
description: the ranch
x-b33f: princess`
var idxNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&idxNode)
var n ExternalDoc
err := low.BuildModel(idxNode.Content[0], &n)
assert.NoError(t, err)
err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)
assert.Equal(t, "https://pb33f.io", n.URL.Value)
assert.Equal(t, "the ranch", n.Description.Value)
ext := n.FindExtension("x-b33f")
assert.NotNil(t, ext)
assert.Equal(t, "princess", ext.Value)
}
func TestExternalDoc_Hash(t *testing.T) {
left := `url: https://pb33f.io
description: the ranch
x-b33f: princess`
right := `url: https://pb33f.io
x-b33f: princess
description: the ranch`
var lNode, rNode yaml.Node
_ = yaml.Unmarshal([]byte(left), &lNode)
_ = yaml.Unmarshal([]byte(right), &rNode)
// create low level objects
var lDoc ExternalDoc
var rDoc ExternalDoc
_ = low.BuildModel(lNode.Content[0], &lDoc)
_ = low.BuildModel(rNode.Content[0], &rDoc)
_ = lDoc.Build(context.Background(), nil, lNode.Content[0], nil)
_ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil)
assert.Equal(t, lDoc.Hash(), rDoc.Hash())
assert.Len(t, lDoc.GetExtensions(), 1)
}