Skip to content

Commit

Permalink
Increased coverage for model back to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
daveshanley committed Dec 9, 2022
1 parent b3f0a0b commit 3fd088b
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 36 deletions.
4 changes: 2 additions & 2 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
func (s DynamicValue[A, B]) IsA() bool {
func (s *DynamicValue[A, B]) IsA() bool {
return s.N == 0
}

// 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
}

Expand Down
16 changes: 16 additions & 0 deletions datamodel/high/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
"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) {

// check proxy
Expand Down Expand Up @@ -541,6 +547,16 @@ exclusiveMaximum: 5
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) {
yml := `
type: number
Expand Down
10 changes: 0 additions & 10 deletions datamodel/low/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 items, not, contains, sif, selse, sthen, propertyNames, unevalItems, unevalProperties low.ValueReference[*SchemaProxy]

_, allOfLabel, allOfValue := utils.FindKeyNodeFullTop(AllOfLabel, root.Content)
Expand Down
63 changes: 63 additions & 0 deletions datamodel/low/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ exclusiveMinimum: 12
exclusiveMaximum: 13
contentEncoding: fish64
contentMediaType: fish/paste
items: true
examples:
- testing`

Expand All @@ -387,6 +388,8 @@ examples:
assert.Equal(t, "testing", sch.Examples.Value[0].Value)
assert.Equal(t, "fish64", sch.ContentEncoding.Value)
assert.Equal(t, "fish/paste", sch.ContentMediaType.Value)
assert.True(t, sch.Items.Value.IsB())
assert.True(t, sch.Items.Value.B)

}

Expand Down Expand Up @@ -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) {

yml := `components:
Expand Down Expand Up @@ -1465,13 +1522,19 @@ func TestSchema_Hash_NotEqual(t *testing.T) {

left := `schema:
title: an OK message - but different
items: true
minContains: 3
maxContains: 22
properties:
propA:
title: a proxy property
type: string`

right := `schema:
title: an OK message
items: false
minContains: 2
maxContains: 10
properties:
propA:
title: a proxy property
Expand Down
5 changes: 1 addition & 4 deletions what-changed/model/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func addCommonParameterProperties(left, right low.SharedParameters, changes *[]*
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 {
return CompareParameters(l, r)
}
Expand Down Expand Up @@ -306,9 +306,6 @@ func CompareParameters(l, r any) *ParameterChanges {

pc.PropertyChanges = NewPropertyChanges(changes)
pc.ExtensionChanges = CompareExtensions(lext, rext)
if pc.TotalChanges() <= 0 {
return nil
}
return pc
}

Expand Down
2 changes: 1 addition & 1 deletion what-changed/model/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCompareParameters_V3(t *testing.T) {
_ = rDoc.Build(rNode.Content[0], nil)

// compare.
extChanges := CompareParameters(&lDoc, &rDoc)
extChanges := CompareParametersV3(&lDoc, &rDoc)
assert.Equal(t, 1, extChanges.TotalChanges())
}

Expand Down
41 changes: 22 additions & 19 deletions what-changed/model/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// inception, eat your heart out.
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.
go extractSchemaChanges(lSchema.OneOf.Value, rSchema.OneOf.Value, v3.OneOfLabel,
Expand All @@ -280,13 +287,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
go extractSchemaChanges(lSchema.AnyOf.Value, rSchema.AnyOf.Value, v3.AnyOfLabel,
&sc.AnyOfChanges, &changes, doneChan)

//go extractSchemaChanges(lSchema.Items.Value, rSchema.Items.Value, v3.ItemsLabel,
// &sc.ItemsChanges, &changes, doneChan)
//
//go extractSchemaChanges(lSchema.Not.Value, rSchema.Not.Value, v3.NotLabel,
// &sc.NotChanges, &changes, doneChan)

totalChecks := totalProperties + 3
totalChecks := totalProperties + depsTotal + patternsTotal + 3
completedChecks := 0
for completedChecks < totalChecks {
select {
Expand Down Expand Up @@ -324,12 +325,11 @@ func checkSchemaXML(lSchema *base.Schema, rSchema *base.Schema, changes *[]*Chan
}
}

func checkPropertiesPropertyOfASchema(
lSchema *base.Schema,
rSchema *base.Schema,
func checkMappedSchemaOfASchema(
lSchema,
rSchema map[low.KeyReference[string]]low.ValueReference[*base.SchemaProxy],
changes *[]*Change,
sc *SchemaChanges,
doneChan chan bool) int {
doneChan chan bool) (map[string]*SchemaChanges, int) {

propChanges := make(map[string]*SchemaChanges)

Expand All @@ -340,19 +340,24 @@ func checkPropertiesPropertyOfASchema(
rEntities := make(map[string]*base.SchemaProxy)
rKeyNodes := make(map[string]*yaml.Node)

for w := range lSchema.Properties.Value {
for w := range lSchema {
lProps = append(lProps, w.Value)
lEntities[w.Value] = lSchema.Properties.Value[w].Value
lEntities[w.Value] = lSchema[w].Value
lKeyNodes[w.Value] = w.KeyNode
}
for w := range rSchema.Properties.Value {
for w := range rSchema {
rProps = append(rProps, w.Value)
rEntities[w.Value] = rSchema.Properties.Value[w].Value
rEntities[w.Value] = rSchema[w].Value
rKeyNodes[w.Value] = w.KeyNode
}
sort.Strings(lProps)
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
checkProperty := func(key string, lp, rp *base.SchemaProxy, propChanges map[string]*SchemaChanges, done chan bool) {
if lp != nil && rp != nil {
Expand Down Expand Up @@ -421,8 +426,6 @@ func checkPropertiesPropertyOfASchema(
}
}
}

sc.SchemaPropertyChanges = propChanges
return totalProperties
}

Expand Down
Loading

0 comments on commit 3fd088b

Please sign in to comment.