Skip to content

Commit

Permalink
Added more tests to ensure correct coverage.
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <[email protected]>
  • Loading branch information
daveshanley committed Jul 18, 2023
1 parent 0fbcb81 commit d17ae1d
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 43 deletions.
18 changes: 11 additions & 7 deletions what-changed/model/comparison_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func CheckPropertyAdditionOrRemoval[T any](l, r *yaml.Node,
//
// The Change is then added to the slice of []Change[T] instances provided as a pointer.
func CheckForRemoval[T any](l, r *yaml.Node, label string, changes *[]*Change, breaking bool, orig, new T) {
if l != nil && l.Value != "" && (r == nil || r.Value == "") {
if l != nil && l.Value != "" && (r == nil || r.Value == "" && !utils.IsNodeArray(r) && !utils.IsNodeMap(r)) {
CreateChange(changes, PropertyRemoved, label, l, r, breaking, orig, new)
return
}
Expand All @@ -163,7 +163,7 @@ func CheckForRemoval[T any](l, r *yaml.Node, label string, changes *[]*Change, b
func CheckForAddition[T any](l, r *yaml.Node, label string, changes *[]*Change, breaking bool, orig, new T) {
if (l == nil || l.Value == "") && (r != nil && (r.Value != "" || utils.IsNodeArray(r)) || utils.IsNodeMap(r)) {
if r != nil {
if l != nil && len(l.Content) < len(r.Content) {
if l != nil && (len(l.Content) < len(r.Content)) && len(l.Content) <= 0 {
CreateChange(changes, PropertyAdded, label, l, r, breaking, orig, new)
}
if l == nil {
Expand All @@ -188,10 +188,18 @@ func CheckForModification[T any](l, r *yaml.Node, label string, changes *[]*Chan
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
return
}
if l != nil && !utils.IsNodeArray(l) && r != nil && utils.IsNodeArray(r) {
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
return
}
if l != nil && utils.IsNodeMap(l) && r != nil && !utils.IsNodeMap(r) {
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
return
}
if l != nil && !utils.IsNodeMap(l) && r != nil && utils.IsNodeMap(r) {
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
return
}
if l != nil && utils.IsNodeArray(l) && r != nil && utils.IsNodeArray(r) {
if len(l.Content) != len(r.Content) {
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
Expand All @@ -209,11 +217,7 @@ func CheckForModification[T any](l, r *yaml.Node, label string, changes *[]*Chan
return
}
if l != nil && utils.IsNodeMap(l) && r != nil && utils.IsNodeMap(r) {
if len(l.Content) != len(r.Content) {
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
return
}
// there is no way to know how to compare the content of the array, without
// there is no way to know how to compare the content of the map, without
// rendering the yaml.Node to a string and comparing the string.
leftBytes, _ := yaml.Marshal(l)
rightBytes, _ := yaml.Marshal(r)
Expand Down
138 changes: 138 additions & 0 deletions what-changed/model/comparison_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,141 @@ func Test_CheckForModification(t *testing.T) {
})
}
}

func Test_CheckForModification_ArrayMap(t *testing.T) {
tests := []struct {
name string
left any
right any
differ bool
}{
{"Same slice", []string{"cake"}, []string{"cake"}, false},
{"bigger slice right", []string{"cake"}, []string{"cake", "cheese"}, true},
{"bigger slice left", []string{"cake", "burgers"}, []string{"cake"}, true},
{"different slice left", "cake", []string{"cake"}, true},
{"different slice right", []string{"cake"}, "cake", true},
{"different slice value", []string{"cake"}, []string{"burgers"}, true},
{"same map", map[string]string{"pie": "cake"}, map[string]string{"pie": "cake"}, false},
{"different map", map[string]string{"pie": "cake"}, map[string]string{"pizza": "burgers"}, true},
{"different map left", "pie", map[string]string{"pie": "cake"}, true},
{"different map right", map[string]string{"pie": "cake"}, "burgers", true},
{"different map", map[string]string{"pie": "cake"}, map[string]string{"pizza": "time"}, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var lNode, rNode yaml.Node
encA, _ := yaml.Marshal(tt.left)
encB, _ := yaml.Marshal(tt.right)
_ = yaml.Unmarshal(encA, &lNode)
_ = yaml.Unmarshal(encB, &rNode)

changes := []*Change{}
CheckForModification(lNode.Content[0], rNode.Content[0], tt.name, &changes, false, "old", "new")

if tt.differ {
assert.Len(t, changes, 1)
} else {
assert.Empty(t, changes)
}
})
}
}

func Test_CheckMapsRemoval(t *testing.T) {

mapA := make(map[string]string)
mapB := make(map[string]string)

mapA["key"] = "value"
mapB["key"] = "shmalue"

tests := []struct {
name string
left map[string]string
right map[string]string
differ bool
}{
{"Same map", mapA, mapB, false},
{"Different map", mapA, mapB, false},
{"Add map", nil, mapA, false},
{"Remove map", mapA, nil, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var lNode, rNode yaml.Node
encA, _ := yaml.Marshal(tt.left)
encB, _ := yaml.Marshal(tt.right)
_ = yaml.Unmarshal(encA, &lNode)
_ = yaml.Unmarshal(encB, &rNode)

changes := []*Change{}

r := rNode.Content[0]
if len(r.Content) == 0 {
r = nil
}
CheckForRemoval(lNode.Content[0], r, "test", &changes, false, "old", "new")

if tt.differ {
assert.Len(t, changes, 1)
} else {
assert.Empty(t, changes)
}
})
}
}

func Test_CheckMapsAddition(t *testing.T) {

mapA := make(map[string]string)
mapB := make(map[string]string)

mapA["key"] = "value"
mapB["key"] = "shmalue"

tests := []struct {
name string
left map[string]string
right map[string]string
differ bool
empty bool
}{
{"Same map", mapA, mapB, false, false},
{"Different map", mapA, mapB, false, false},
{"Add map", nil, mapA, true, false},
{"Add map value", nil, mapB, true, true},
{"Remove map", mapA, nil, false, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var lNode, rNode yaml.Node
encA, _ := yaml.Marshal(tt.left)
encB, _ := yaml.Marshal(tt.right)
_ = yaml.Unmarshal(encA, &lNode)
_ = yaml.Unmarshal(encB, &rNode)

changes := []*Change{}

r := rNode.Content[0]
l := lNode.Content[0]
if len(r.Content) == 0 {
r = nil
}
if len(l.Content) == 0 {
if !tt.empty {
l = nil
}
}
CheckForAddition(l, r, "test", &changes, false, "old", "new")

if tt.differ {
assert.Len(t, changes, 1)
} else {
assert.Empty(t, changes)
}
})
}
}
36 changes: 0 additions & 36 deletions what-changed/model/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2663,39 +2663,3 @@ components:
assert.Equal(t, 0, changes.TotalBreakingChanges())

}

/*
containerShared:
description: Shared properties by request payload and response
type: object
properties:
close_time:
description: Date and time (in UTC) when the container was closed.
type: string
format: date-time
example: '2020-07-09T00:17:55Z'
container_type:
description: |-
The container type. Valid values are 'default' or 'case'. Containers with the 'default'
type are events in the user interface.
type: string
enum:
- default
- case
example: default
custom_fields:
type:
- array
- 'null'
description: |-
JSON objects contains key/value pairs for custom container fields. There may be
required fields defined in the administration settings. See the Administrator's Guide for
details.
items:
$ref: '#/components/schemas/custom-field'
example:
- name: auditedAt
source: global
dataType: text
requiredToResolve: false
*/

0 comments on commit d17ae1d

Please sign in to comment.