From faf191bdd01a23daaf84d2b321788429f8c78493 Mon Sep 17 00:00:00 2001 From: quobix Date: Sun, 8 Oct 2023 11:39:43 -0400 Subject: [PATCH] bumped coverage on tests Signed-off-by: quobix --- datamodel/high/base/schema_test.go | 95 +++++++++++++++++++++ datamodel/high/node_builder_test.go | 35 ++++++++ datamodel/low/base/schema_test.go | 124 +++++++++++++++++++++++++++- index/index_model_test.go | 7 ++ 4 files changed, 259 insertions(+), 2 deletions(-) diff --git a/datamodel/high/base/schema_test.go b/datamodel/high/base/schema_test.go index 234adbc..642489f 100644 --- a/datamodel/high/base/schema_test.go +++ b/datamodel/high/base/schema_test.go @@ -5,6 +5,7 @@ package base import ( "fmt" + "github.com/pb33f/libopenapi/datamodel" "strings" "testing" @@ -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))) +} diff --git a/datamodel/high/node_builder_test.go b/datamodel/high/node_builder_test.go index 80589ca..9d28e59 100644 --- a/datamodel/high/node_builder_test.go +++ b/datamodel/high/node_builder_test.go @@ -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"` @@ -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{ diff --git a/datamodel/low/base/schema_test.go b/datamodel/low/base/schema_test.go index ac385c8..014b970 100644 --- a/datamodel/low/base/schema_test.go +++ b/datamodel/low/base/schema_test.go @@ -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 { @@ -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) +} diff --git a/index/index_model_test.go b/index/index_model_test.go index 3bd8e93..60642f7 100644 --- a/index/index_model_test.go +++ b/index/index_model_test.go @@ -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()) +}