Increased coverage for model back to 100%

This commit is contained in:
Dave Shanley
2022-12-08 18:16:48 -05:00
parent b3f0a0b1ae
commit 3fd088b217
8 changed files with 417 additions and 36 deletions

View File

@@ -26,12 +26,12 @@ type DynamicValue[A any, B any] struct {
} }
// IsA will return true if the 'A' or left value is set. (OpenAPI 3) // IsA will return true if the 'A' or left value is set. (OpenAPI 3)
func (s DynamicValue[A, B]) IsA() bool { func (s *DynamicValue[A, B]) IsA() bool {
return s.N == 0 return s.N == 0
} }
// IsB will return true if the 'B' or right value is set (OpenAPI 3.1) // IsB will return true if the 'B' or right value is set (OpenAPI 3.1)
func (s DynamicValue[A, B]) IsB() bool { func (s *DynamicValue[A, B]) IsB() bool {
return s.N == 1 return s.N == 1
} }

View File

@@ -14,6 +14,12 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
func TestDynamicValue_IsA(t *testing.T) {
dv := &DynamicValue[int, bool]{N: 0, A: 23}
assert.True(t, dv.IsA())
assert.False(t, dv.IsB())
}
func TestNewSchemaProxy(t *testing.T) { func TestNewSchemaProxy(t *testing.T) {
// check proxy // check proxy
@@ -541,6 +547,16 @@ exclusiveMaximum: 5
assert.EqualValues(t, value, highSchema.ExclusiveMaximum.B) assert.EqualValues(t, value, highSchema.ExclusiveMaximum.B)
} }
func TestSchema_Items_Boolean(t *testing.T) {
yml := `
type: number
items: true
`
highSchema := getHighSchema(t, yml)
assert.True(t, highSchema.Items.B)
}
func TestSchemaExamples(t *testing.T) { func TestSchemaExamples(t *testing.T) {
yml := ` yml := `
type: number type: number

View File

@@ -684,17 +684,7 @@ func (s *Schema) Build(root *yaml.Node, idx *index.SpecIndex) error {
} }
} }
/*
If low.NodeReference[*SchemaProxy]
Else low.NodeReference[*SchemaProxy]
Then low.NodeReference[*SchemaProxy]
PropertyNames low.NodeReference[*SchemaProxy]
UnevaluatedItems low.NodeReference[*SchemaProxy]
UnevaluatedProperties low.NodeReference[*SchemaProxy]
*/
var allOf, anyOf, oneOf, prefixItems []low.ValueReference[*SchemaProxy] var allOf, anyOf, oneOf, prefixItems []low.ValueReference[*SchemaProxy]
var items, not, contains, sif, selse, sthen, propertyNames, unevalItems, unevalProperties low.ValueReference[*SchemaProxy] var items, not, contains, sif, selse, sthen, propertyNames, unevalItems, unevalProperties low.ValueReference[*SchemaProxy]
_, allOfLabel, allOfValue := utils.FindKeyNodeFullTop(AllOfLabel, root.Content) _, allOfLabel, allOfValue := utils.FindKeyNodeFullTop(AllOfLabel, root.Content)

View File

@@ -361,6 +361,7 @@ exclusiveMinimum: 12
exclusiveMaximum: 13 exclusiveMaximum: 13
contentEncoding: fish64 contentEncoding: fish64
contentMediaType: fish/paste contentMediaType: fish/paste
items: true
examples: examples:
- testing` - testing`
@@ -387,6 +388,8 @@ examples:
assert.Equal(t, "testing", sch.Examples.Value[0].Value) assert.Equal(t, "testing", sch.Examples.Value[0].Value)
assert.Equal(t, "fish64", sch.ContentEncoding.Value) assert.Equal(t, "fish64", sch.ContentEncoding.Value)
assert.Equal(t, "fish/paste", sch.ContentMediaType.Value) assert.Equal(t, "fish/paste", sch.ContentMediaType.Value)
assert.True(t, sch.Items.Value.IsB())
assert.True(t, sch.Items.Value.B)
} }
@@ -563,6 +566,60 @@ properties:
} }
func TestSchema_Build_DependentSchemas_Fail(t *testing.T) {
yml := `components:
schemas:
Something:
description: this is something
type: string`
var iNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &iNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&iNode)
yml = `type: object
dependentSchemas:
aValue:
$ref: '#/bork'`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
var n Schema
err := n.Build(idxNode.Content[0], idx)
assert.Error(t, err)
}
func TestSchema_Build_PatternProperties_Fail(t *testing.T) {
yml := `components:
schemas:
Something:
description: this is something
type: string`
var iNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &iNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&iNode)
yml = `type: object
patternProperties:
aValue:
$ref: '#/bork'`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
var n Schema
err := n.Build(idxNode.Content[0], idx)
assert.Error(t, err)
}
func Test_Schema_Polymorphism_Array_Ref(t *testing.T) { func Test_Schema_Polymorphism_Array_Ref(t *testing.T) {
yml := `components: yml := `components:
@@ -1465,6 +1522,9 @@ func TestSchema_Hash_NotEqual(t *testing.T) {
left := `schema: left := `schema:
title: an OK message - but different title: an OK message - but different
items: true
minContains: 3
maxContains: 22
properties: properties:
propA: propA:
title: a proxy property title: a proxy property
@@ -1472,6 +1532,9 @@ func TestSchema_Hash_NotEqual(t *testing.T) {
right := `schema: right := `schema:
title: an OK message title: an OK message
items: false
minContains: 2
maxContains: 10
properties: properties:
propA: propA:
title: a proxy property title: a proxy property

View File

@@ -192,7 +192,7 @@ func addCommonParameterProperties(left, right low.SharedParameters, changes *[]*
return props return props
} }
// CompareParametersV3 is amn OpenAPI type safe proxy for CompareParameters // CompareParametersV3 is an OpenAPI type safe proxy for CompareParameters
func CompareParametersV3(l, r *v3.Parameter) *ParameterChanges { func CompareParametersV3(l, r *v3.Parameter) *ParameterChanges {
return CompareParameters(l, r) return CompareParameters(l, r)
} }
@@ -306,9 +306,6 @@ func CompareParameters(l, r any) *ParameterChanges {
pc.PropertyChanges = NewPropertyChanges(changes) pc.PropertyChanges = NewPropertyChanges(changes)
pc.ExtensionChanges = CompareExtensions(lext, rext) pc.ExtensionChanges = CompareExtensions(lext, rext)
if pc.TotalChanges() <= 0 {
return nil
}
return pc return pc
} }

View File

@@ -52,7 +52,7 @@ func TestCompareParameters_V3(t *testing.T) {
_ = rDoc.Build(rNode.Content[0], nil) _ = rDoc.Build(rNode.Content[0], nil)
// compare. // compare.
extChanges := CompareParameters(&lDoc, &rDoc) extChanges := CompareParametersV3(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges()) assert.Equal(t, 1, extChanges.TotalChanges())
} }

View File

@@ -268,7 +268,14 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
// now for the confusing part, there is also a schema's 'properties' property to parse. // now for the confusing part, there is also a schema's 'properties' property to parse.
// inception, eat your heart out. // inception, eat your heart out.
doneChan := make(chan bool) doneChan := make(chan bool)
totalProperties := checkPropertiesPropertyOfASchema(lSchema, rSchema, &changes, sc, doneChan) props, totalProperties := checkMappedSchemaOfASchema(lSchema.Properties.Value, rSchema.Properties.Value, &changes, doneChan)
sc.SchemaPropertyChanges = props
deps, depsTotal := checkMappedSchemaOfASchema(lSchema.DependentSchemas.Value, rSchema.DependentSchemas.Value, &changes, doneChan)
sc.DependentSchemasChanges = deps
patterns, patternsTotal := checkMappedSchemaOfASchema(lSchema.PatternProperties.Value, rSchema.PatternProperties.Value, &changes, doneChan)
sc.PatternPropertiesChanges = patterns
// check polymorphic and multi-values async for speed. // check polymorphic and multi-values async for speed.
go extractSchemaChanges(lSchema.OneOf.Value, rSchema.OneOf.Value, v3.OneOfLabel, go extractSchemaChanges(lSchema.OneOf.Value, rSchema.OneOf.Value, v3.OneOfLabel,
@@ -280,13 +287,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
go extractSchemaChanges(lSchema.AnyOf.Value, rSchema.AnyOf.Value, v3.AnyOfLabel, go extractSchemaChanges(lSchema.AnyOf.Value, rSchema.AnyOf.Value, v3.AnyOfLabel,
&sc.AnyOfChanges, &changes, doneChan) &sc.AnyOfChanges, &changes, doneChan)
//go extractSchemaChanges(lSchema.Items.Value, rSchema.Items.Value, v3.ItemsLabel, totalChecks := totalProperties + depsTotal + patternsTotal + 3
// &sc.ItemsChanges, &changes, doneChan)
//
//go extractSchemaChanges(lSchema.Not.Value, rSchema.Not.Value, v3.NotLabel,
// &sc.NotChanges, &changes, doneChan)
totalChecks := totalProperties + 3
completedChecks := 0 completedChecks := 0
for completedChecks < totalChecks { for completedChecks < totalChecks {
select { select {
@@ -324,12 +325,11 @@ func checkSchemaXML(lSchema *base.Schema, rSchema *base.Schema, changes *[]*Chan
} }
} }
func checkPropertiesPropertyOfASchema( func checkMappedSchemaOfASchema(
lSchema *base.Schema, lSchema,
rSchema *base.Schema, rSchema map[low.KeyReference[string]]low.ValueReference[*base.SchemaProxy],
changes *[]*Change, changes *[]*Change,
sc *SchemaChanges, doneChan chan bool) (map[string]*SchemaChanges, int) {
doneChan chan bool) int {
propChanges := make(map[string]*SchemaChanges) propChanges := make(map[string]*SchemaChanges)
@@ -340,19 +340,24 @@ func checkPropertiesPropertyOfASchema(
rEntities := make(map[string]*base.SchemaProxy) rEntities := make(map[string]*base.SchemaProxy)
rKeyNodes := make(map[string]*yaml.Node) rKeyNodes := make(map[string]*yaml.Node)
for w := range lSchema.Properties.Value { for w := range lSchema {
lProps = append(lProps, w.Value) lProps = append(lProps, w.Value)
lEntities[w.Value] = lSchema.Properties.Value[w].Value lEntities[w.Value] = lSchema[w].Value
lKeyNodes[w.Value] = w.KeyNode lKeyNodes[w.Value] = w.KeyNode
} }
for w := range rSchema.Properties.Value { for w := range rSchema {
rProps = append(rProps, w.Value) rProps = append(rProps, w.Value)
rEntities[w.Value] = rSchema.Properties.Value[w].Value rEntities[w.Value] = rSchema[w].Value
rKeyNodes[w.Value] = w.KeyNode rKeyNodes[w.Value] = w.KeyNode
} }
sort.Strings(lProps) sort.Strings(lProps)
sort.Strings(rProps) sort.Strings(rProps)
totalProperties := buildProperty(lProps, rProps, lEntities, rEntities, propChanges, doneChan, changes, rKeyNodes, lKeyNodes)
return propChanges, totalProperties
}
func buildProperty(lProps, rProps []string, lEntities, rEntities map[string]*base.SchemaProxy,
propChanges map[string]*SchemaChanges, doneChan chan bool, changes *[]*Change, rKeyNodes, lKeyNodes map[string]*yaml.Node) int {
var propLock sync.Mutex var propLock sync.Mutex
checkProperty := func(key string, lp, rp *base.SchemaProxy, propChanges map[string]*SchemaChanges, done chan bool) { checkProperty := func(key string, lp, rp *base.SchemaProxy, propChanges map[string]*SchemaChanges, done chan bool) {
if lp != nil && rp != nil { if lp != nil && rp != nil {
@@ -421,8 +426,6 @@ func checkPropertiesPropertyOfASchema(
} }
} }
} }
sc.SchemaPropertyChanges = propChanges
return totalProperties return totalProperties
} }

View File

@@ -544,6 +544,318 @@ components:
assert.Equal(t, v3.PropertiesLabel, changes.Changes[0].Property) assert.Equal(t, v3.PropertiesLabel, changes.Changes[0].Property)
} }
func TestCompareSchemas_If(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
if:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
if:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, 1, changes.IfChanges.PropertyChanges.TotalChanges())
}
func TestCompareSchemas_If_Added(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
type: string
if:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, v3.IfLabel, changes.Changes[0].Property)
}
func TestCompareSchemas_If_Removed(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
type: string
if:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(rSchemaProxy, lSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, v3.IfLabel, changes.Changes[0].Property)
}
func TestCompareSchemas_Else(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
else:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
else:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, 1, changes.ElseChanges.PropertyChanges.TotalChanges())
}
func TestCompareSchemas_Else_Added(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
type: string
else:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, v3.ElseLabel, changes.Changes[0].Property)
}
func TestCompareSchemas_Else_Removed(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
type: string
else:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(rSchemaProxy, lSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, v3.ElseLabel, changes.Changes[0].Property)
}
func TestCompareSchemas_Then(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
then:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
then:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, 1, changes.ThenChanges.PropertyChanges.TotalChanges())
}
func TestCompareSchemas_Then_Added(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
type: string
then:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, v3.ThenLabel, changes.Changes[0].Property)
}
func TestCompareSchemas_Then_Removed(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
type: string`
right := `openapi: 3.1
components:
schemas:
OK:
type: string
then:
type: int`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(rSchemaProxy, lSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, v3.ThenLabel, changes.Changes[0].Property)
}
func TestCompareSchemas_DependentSchemas(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
dependentSchemas:
schemaOne:
type: int`
right := `openapi: 3.1
components:
schemas:
OK:
dependentSchemas:
schemaOne:
type: string`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, 1, changes.DependentSchemasChanges["schemaOne"].PropertyChanges.TotalChanges())
}
func TestCompareSchemas_PatternProperties(t *testing.T) {
left := `openapi: 3.1
components:
schemas:
OK:
patternProperties:
schemaOne:
type: int`
right := `openapi: 3.1
components:
schemas:
OK:
patternProperties:
schemaOne:
type: string`
leftDoc, rightDoc := test_BuildDoc(left, right)
// extract left reference schema and non reference schema.
lSchemaProxy := leftDoc.Components.Value.FindSchema("OK").Value
rSchemaProxy := rightDoc.Components.Value.FindSchema("OK").Value
changes := CompareSchemas(lSchemaProxy, rSchemaProxy)
assert.NotNil(t, changes)
assert.Equal(t, 1, changes.TotalChanges())
assert.Equal(t, 1, changes.TotalBreakingChanges())
assert.Equal(t, 1, changes.PatternPropertiesChanges["schemaOne"].PropertyChanges.TotalChanges())
}
func TestCompareSchemas_PropertyNames(t *testing.T) { func TestCompareSchemas_PropertyNames(t *testing.T) {
left := `openapi: 3.1 left := `openapi: 3.1
components: components: