Adding high base model tests

these were covered when used by v3 models, but need new tests when moved into a seperate package.
This commit is contained in:
Dave Shanley
2022-09-11 13:10:32 -04:00
parent 165b835f3e
commit 02633ad333
5 changed files with 162 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package base
import (
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestNewContact(t *testing.T) {
var cNode yaml.Node
yml := `name: pizza
url: https://pb33f.io
email: buckaroo@pb33f.io`
_ = yaml.Unmarshal([]byte(yml), &cNode)
// build low
var lowContact lowbase.Contact
_ = lowmodel.BuildModel(&cNode, &lowContact)
// build high
highContact := NewContact(&lowContact)
assert.Equal(t, "pizza", highContact.Name)
assert.Equal(t, "https://pb33f.io", highContact.URL)
assert.Equal(t, "buckaroo@pb33f.io", highContact.Email)
assert.Equal(t, 1, highContact.GoLow().Name.KeyNode.Line)
}

View File

@@ -0,0 +1,35 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package base
import (
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestNewDiscriminator(t *testing.T) {
var cNode yaml.Node
yml := `propertyName: coffee
mapping:
fogCleaner: in the morning`
_ = yaml.Unmarshal([]byte(yml), &cNode)
// build low
var lowDiscriminator lowbase.Discriminator
_ = lowmodel.BuildModel(&cNode, &lowDiscriminator)
// build high
highDiscriminator := NewDiscriminator(&lowDiscriminator)
assert.Equal(t, "coffee", highDiscriminator.PropertyName)
assert.Equal(t, "in the morning", highDiscriminator.Mapping["fogCleaner"])
assert.Equal(t, 3, highDiscriminator.GoLow().FindMappingValue("fogCleaner").ValueNode.Line)
}

View File

@@ -23,7 +23,7 @@ func NewExample(example *low.Example) *Example {
e.low = example
e.Summary = example.Summary.Value
e.Description = example.Description.Value
e.Value = example.Value
e.Value = example.Value.Value
e.ExternalValue = example.ExternalValue.Value
e.Extensions = high.ExtractExtensions(example.Extensions)
return e

View File

@@ -0,0 +1,41 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package base
import (
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestNewExample(t *testing.T) {
var cNode yaml.Node
yml := `summary: an example
description: something more
value: a thing
externalValue: https://pb33f.io
x-hack: code`
_ = yaml.Unmarshal([]byte(yml), &cNode)
// build low
var lowExample lowbase.Example
_ = lowmodel.BuildModel(&cNode, &lowExample)
_ = lowExample.Build(cNode.Content[0], nil)
// build high
highExample := NewExample(&lowExample)
assert.Equal(t, "an example", highExample.Summary)
assert.Equal(t, "something more", highExample.Description)
assert.Equal(t, "https://pb33f.io", highExample.ExternalValue)
assert.Equal(t, "code", highExample.Extensions["x-hack"])
assert.Equal(t, "a thing", highExample.Value)
assert.Equal(t, 4, highExample.GoLow().ExternalValue.ValueNode.Line)
}

View File

@@ -0,0 +1,49 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v2
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestParameter_Build(t *testing.T) {
yml := `$ref: break`
var idxNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&idxNode)
var n Parameter
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.Error(t, err)
}
func TestParameter_Build_Items(t *testing.T) {
yml := `items:
$ref: break`
var idxNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&idxNode)
var n Parameter
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.Error(t, err)
}