Skip to content

Commit

Permalink
Merge pull request #5579 from fabriziopandini/add-mergepatch-tests
Browse files Browse the repository at this point in the history
🌱 add tests for merge patch private func
  • Loading branch information
k8s-ci-robot authored Nov 3, 2021
2 parents 33b0f87 + 8b0bcb1 commit b1cff70
Showing 1 changed file with 187 additions and 0 deletions.
187 changes: 187 additions & 0 deletions controllers/topology/internal/mergepatch/mergepatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,190 @@ func TestNewHelper(t *testing.T) {
})
}
}

func Test_filterPatchMap(t *testing.T) {
tests := []struct {
name string
patchMap map[string]interface{}
paths []contract.Path
want map[string]interface{}
}{
{
name: "Allow values",
patchMap: map[string]interface{}{
"foo": "123",
"bar": 123,
"baz": map[string]interface{}{
"foo": "123",
"bar": 123,
},
},
paths: []contract.Path{
[]string{"foo"},
[]string{"baz", "foo"},
},
want: map[string]interface{}{
"foo": "123",
"baz": map[string]interface{}{
"foo": "123",
},
},
},
{
name: "Allow maps",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"foo": "123",
"bar": 123,
},
"bar": map[string]interface{}{
"foo": "123",
"bar": 123,
},
"baz": map[string]interface{}{
"foo": map[string]interface{}{
"foo": "123",
"bar": 123,
},
"bar": map[string]interface{}{
"foo": "123",
"bar": 123,
},
},
},
paths: []contract.Path{
[]string{"foo"},
[]string{"baz", "foo"},
},
want: map[string]interface{}{
"foo": map[string]interface{}{
"foo": "123",
"bar": 123,
},
"baz": map[string]interface{}{
"foo": map[string]interface{}{
"foo": "123",
"bar": 123,
},
},
},
},
{
name: "Cleanup empty maps",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "123",
"baz": map[string]interface{}{
"bar": "123",
},
},
},
paths: []contract.Path{},
want: map[string]interface{}{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

filterPatchMap(tt.patchMap, tt.paths)

g.Expect(tt.patchMap).To(Equal(tt.want))
})
}
}

func Test_removePath(t *testing.T) {
tests := []struct {
name string
patchMap map[string]interface{}
path contract.Path
want map[string]interface{}
}{
{
name: "Remove value",
patchMap: map[string]interface{}{
"foo": "123",
},
path: contract.Path([]string{"foo"}),
want: map[string]interface{}{},
},
{
name: "Remove map",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "123",
},
},
path: contract.Path([]string{"foo"}),
want: map[string]interface{}{},
},
{
name: "Remove nested value",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "123",
"baz": "123",
},
},
path: contract.Path([]string{"foo", "bar"}),
want: map[string]interface{}{
"foo": map[string]interface{}{
"baz": "123",
},
},
},
{
name: "Remove nested map",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": "123",
},
"baz": "123",
},
},
path: contract.Path([]string{"foo", "bar"}),
want: map[string]interface{}{
"foo": map[string]interface{}{
"baz": "123",
},
},
},
{
name: "Ignore partial match",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "123",
},
},
path: contract.Path([]string{"foo", "bar", "baz"}),
want: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "123",
},
},
},
{
name: "Cleanup empty maps",
patchMap: map[string]interface{}{
"foo": map[string]interface{}{
"baz": map[string]interface{}{
"bar": "123",
},
},
},
path: contract.Path([]string{"foo", "baz", "bar"}),
want: map[string]interface{}{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

removePath(tt.patchMap, tt.path)

g.Expect(tt.patchMap).To(Equal(tt.want))
})
}
}

0 comments on commit b1cff70

Please sign in to comment.