Skip to content

Commit

Permalink
bumped coverage on tests
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Oct 8, 2023
1 parent 2970bf1 commit 131646e
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 2 deletions.
95 changes: 95 additions & 0 deletions datamodel/high/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package base

import (
"fmt"
"github.com/pb33f/libopenapi/datamodel"
"strings"
"testing"

Expand Down Expand Up @@ -1191,3 +1192,97 @@ properties:
assert.Equal(t, true, compiled.Properties["additionalPropertiesBool"].Schema().AdditionalProperties.B)
assert.Equal(t, []string{"string"}, compiled.Properties["additionalPropertiesAnyOf"].Schema().AdditionalProperties.A.Schema().AnyOf[0].Schema().Type)
}

func TestSchema_RenderProxyWithConfig_3(t *testing.T) {
testSpec := `exclusiveMinimum: true`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)
err := sp.Build(nil, compNode.Content[0], nil)
assert.NoError(t, err)

config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.0,
}
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

// now render it out, it should be identical.
schemaBytes, _ := compiled.Render()
assert.Equal(t, testSpec, strings.TrimSpace(string(schemaBytes)))
}

func TestSchema_RenderProxyWithConfig_Corrected_31(t *testing.T) {
testSpec := `exclusiveMinimum: true`
testSpecCorrect := `exclusiveMinimum: 0`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)
config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.1,
}
idx := index.NewSpecIndexWithConfig(compNode.Content[0], config)

err := sp.Build(nil, compNode.Content[0], idx)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

// now render it out, it should be identical.
schemaBytes, _ := compiled.Render()
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))

schemaBytes, _ = compiled.RenderInline()
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))

}

func TestSchema_RenderProxyWithConfig_Corrected_3(t *testing.T) {
testSpec := `exclusiveMinimum: 0`
testSpecCorrect := `exclusiveMinimum: false`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)
config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.0,
}
idx := index.NewSpecIndexWithConfig(compNode.Content[0], config)

err := sp.Build(nil, compNode.Content[0], idx)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

// now render it out, it should be identical.
schemaBytes, _ := compiled.Render()
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))

schemaBytes, _ = compiled.RenderInline()
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
}
35 changes: 35 additions & 0 deletions datamodel/high/node_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type test1 struct {
Thugg *bool `yaml:"thugg,renderZero"`
Thurr *int64 `yaml:"thurr,omitempty"`
Thral *float64 `yaml:"thral,omitempty"`
Throo *float64 `yaml:"throo,renderZero,omitempty"`
Tharg []string `yaml:"tharg,omitempty"`
Type []string `yaml:"type,omitempty"`
Throg []*key `yaml:"throg,omitempty"`
Expand Down Expand Up @@ -922,6 +923,40 @@ func TestNewNodeBuilder_TestRenderZero(t *testing.T) {
assert.Equal(t, desired, strings.TrimSpace(string(data)))
}

func TestNewNodeBuilder_TestRenderZero_Float(t *testing.T) {

f := 0.0
t1 := test1{
Throo: &f,
}

nb := NewNodeBuilder(&t1, &t1)
node := nb.Render()

data, _ := yaml.Marshal(node)

desired := `throo: 0`

assert.Equal(t, desired, strings.TrimSpace(string(data)))
}

func TestNewNodeBuilder_TestRenderZero_Float_NotZero(t *testing.T) {

f := 0.12
t1 := test1{
Throo: &f,
}

nb := NewNodeBuilder(&t1, &t1)
node := nb.Render()

data, _ := yaml.Marshal(node)

desired := `throo: 0.12`

assert.Equal(t, desired, strings.TrimSpace(string(data)))
}

func TestNewNodeBuilder_TestRenderServerVariableSimulation(t *testing.T) {

t1 := test1{
Expand Down
124 changes: 122 additions & 2 deletions datamodel/low/base/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package base

import (
"testing"

"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/resolver"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)

func test_get_schema_blob() string {
Expand Down Expand Up @@ -1636,3 +1636,123 @@ func TestSchema_UnevaluatedPropertiesAsBool_Undefined(t *testing.T) {

assert.Nil(t, res.Value.Schema().UnevaluatedProperties.Value)
}

func TestSchema_ExclusiveMinimum_3_with_Config(t *testing.T) {
yml := `openapi: 3.0.3
components:
schemas:
Something:
type: integer
minimum: 3
exclusiveMinimum: true`

var iNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &iNode)
assert.NoError(t, mErr)

config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.0,
}

idx := index.NewSpecIndexWithConfig(&iNode, config)

yml = `$ref: '#/components/schemas/Something'`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)

res, _ := ExtractSchema(idxNode.Content[0], idx)

assert.True(t, res.Value.Schema().ExclusiveMinimum.Value.A)
}

func TestSchema_ExclusiveMinimum_31_with_Config(t *testing.T) {
yml := `openapi: 3.1
components:
schemas:
Something:
type: integer
minimum: 3
exclusiveMinimum: 3`

var iNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &iNode)
assert.NoError(t, mErr)

config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.1,
}

idx := index.NewSpecIndexWithConfig(&iNode, config)

yml = `$ref: '#/components/schemas/Something'`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)

res, _ := ExtractSchema(idxNode.Content[0], idx)

assert.Equal(t, 3.0, res.Value.Schema().ExclusiveMinimum.Value.B)
}

func TestSchema_ExclusiveMaximum_3_with_Config(t *testing.T) {
yml := `openapi: 3.0.3
components:
schemas:
Something:
type: integer
maximum: 3
exclusiveMaximum: true`

var iNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &iNode)
assert.NoError(t, mErr)

config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.0,
}

idx := index.NewSpecIndexWithConfig(&iNode, config)

yml = `$ref: '#/components/schemas/Something'`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)

res, _ := ExtractSchema(idxNode.Content[0], idx)

assert.True(t, res.Value.Schema().ExclusiveMaximum.Value.A)
}

func TestSchema_ExclusiveMaximum_31_with_Config(t *testing.T) {
yml := `openapi: 3.1
components:
schemas:
Something:
type: integer
maximum: 3
exclusiveMaximum: 3`

var iNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &iNode)
assert.NoError(t, mErr)

config := index.CreateOpenAPIIndexConfig()
config.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3.1,
}

idx := index.NewSpecIndexWithConfig(&iNode, config)

yml = `$ref: '#/components/schemas/Something'`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)

res, _ := ExtractSchema(idxNode.Content[0], idx)

assert.Equal(t, 3.0, res.Value.Schema().ExclusiveMaximum.Value.B)
}
7 changes: 7 additions & 0 deletions index/index_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ func TestSpecIndex_Children(t *testing.T) {
assert.Equal(t, 1, len(idx4.GetChildren()))
assert.Equal(t, 0, len(idx5.GetChildren()))
}

func TestSpecIndex_GetConfig(t *testing.T) {
idx1 := new(SpecIndex)
c := SpecIndexConfig{}
idx1.config = &c
assert.Equal(t, &c, idx1.GetConfig())
}

0 comments on commit 131646e

Please sign in to comment.