Files
libopenapi/datamodel/low/v2/response_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

116 lines
2.3 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v2
import (
"context"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestResponse_Build_Schema(t *testing.T) {
yml := `schema:
$ref: break`
var idxNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&idxNode)
var n Response
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.Error(t, err)
}
func TestResponse_Build_Examples(t *testing.T) {
yml := `examples:
$ref: break`
var idxNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&idxNode)
var n Response
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.Error(t, err)
}
func TestResponse_Build_Headers(t *testing.T) {
yml := `headers:
$ref: break`
var idxNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&idxNode)
var n Response
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.Error(t, err)
}
func TestResponse_Hash(t *testing.T) {
yml := `description: your thing, sir.
schema:
type: string
headers:
heady:
description: heads up!
examples:
noHerbs:
description: be strong
x-herbs: missing`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n Response
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)
yml2 := `description: your thing, sir.
examples:
noHerbs:
description: be strong
schema:
type: string
x-herbs: missing
headers:
heady:
description: heads up!`
var idxNode2 yaml.Node
_ = yaml.Unmarshal([]byte(yml2), &idxNode2)
idx2 := index.NewSpecIndex(&idxNode2)
var n2 Response
_ = low.BuildModel(idxNode2.Content[0], &n2)
_ = n2.Build(context.Background(), nil, idxNode2.Content[0], idx2)
// hash
assert.Equal(t, n.Hash(), n2.Hash())
assert.Len(t, n.GetExtensions(), 1)
}