Skip to content

Commit

Permalink
Fix unit tests for yaml merge functions (#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinFairise2 authored Oct 18, 2024
1 parent fd73f9b commit 07cbe64
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 1 addition & 2 deletions common/utils/fixtures/tags_a.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
tags:
- a
tags: [a]
2 changes: 1 addition & 1 deletion common/utils/fixtures/tags_ab.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tags:
- a
- b
- b
2 changes: 1 addition & 1 deletion common/utils/fixtures/tags_b.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
tags:
- b
- b
24 changes: 19 additions & 5 deletions common/utils/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func TestMergeYAMLWithSlices(t *testing.T) {
"slices value, merge slices": {oldValues: tagsA, newValues: tagsB, expectedResult: tagsAB, expectError: false},
"slices value inverted, merge slices": {oldValues: tagsB, newValues: tagsA, expectedResult: tagsAB, expectError: false},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, err := MergeYAMLWithSlices(tc.oldValues, tc.newValues)
Expand All @@ -80,10 +79,25 @@ func TestMergeYAMLWithSlices(t *testing.T) {

var gotYAML map[string]interface{}
var expectedYAML map[string]interface{}

gotMap := yaml.Unmarshal([]byte(got), &gotYAML)
expectedMap := yaml.Unmarshal([]byte(tc.expectedResult), &expectedYAML)
assert.Equal(t, gotMap, expectedMap, "expected %v, got %v", expectedMap, gotMap)
err = yaml.Unmarshal([]byte(got), &gotYAML)
require.NoError(t, err, "unexpected error: %v", err)
err = yaml.Unmarshal([]byte(tc.expectedResult), &expectedYAML)
require.NoError(t, err, "unexpected error: %v", err)
assertMapsEqual(t, expectedYAML, gotYAML)
})
}
}

func assertMapsEqual(t *testing.T, expected, actual map[string]interface{}) {
assert.Equal(t, len(expected), len(actual), "expected %v, got %v", expected, actual)
for k, v := range expected {
switch v.(type) {
case map[string]interface{}:
assertMapsEqual(t, v.(map[string]interface{}), actual[k].(map[string]interface{}))
case []interface{}:
assert.ElementsMatch(t, v.([]interface{}), actual[k].([]interface{}), "expected %v, got %v", v, actual[k])
default:
assert.Equal(t, v, actual[k], "expected %v, got %v", v, actual[k])
}
}
}

0 comments on commit 07cbe64

Please sign in to comment.