From 516a3e08d20b0d508865e3b77180d180624f29c8 Mon Sep 17 00:00:00 2001 From: Joeyyy09 Date: Sat, 18 May 2024 15:49:03 +0530 Subject: [PATCH] Add tests for TestStrategicMergePatch function in the patch package Signed-off-by: Joeyyy09 Add testcase where StrategicMerge passes and Merge doesnt. Signed-off-by: Joeyyy09 --- pkg/utils/patch/patch_test.go | 98 +++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/pkg/utils/patch/patch_test.go b/pkg/utils/patch/patch_test.go index c6a794192..bf521abee 100644 --- a/pkg/utils/patch/patch_test.go +++ b/pkg/utils/patch/patch_test.go @@ -94,6 +94,44 @@ func TestStrategicMerge(t *testing.T) { }, }, }, + { + name: "test3", + args: args[testStruct]{ + original: testStruct{ + A: "a", + C: []testStructItem{ + { + A: "a", + }, + }, + }, + patch: testStruct{ + B: 1, + C: []testStructItem{ + { + A: "b", + B: 1, + }, + { + A: "a", + }, + }, + }, + }, + wantResult: testStruct{ + A: "a", + B: 1, + C: []testStructItem{ + { + A: "b", + B: 1, + }, + { + A: "a", + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -108,3 +146,63 @@ func TestStrategicMerge(t *testing.T) { }) } } + +func TestStrategicMergePatch(t *testing.T) { + type args[T any] struct { + original T + patchData []byte + } + type testCase[T any] struct { + name string + args args[T] + wantResult T + wantErr bool + } + tests := []testCase[testStruct]{ + { + name: "test1", + args: args[testStruct]{ + original: testStruct{ + A: "a", + }, + patchData: []byte(`{"B":1}`), + }, + wantResult: testStruct{ + A: "a", + B: 1, + }, + }, + { + name: "test2", + args: args[testStruct]{ + original: testStruct{ + A: "b", + B: 2, + }, + patchData: []byte(`{"C":[{"A":"c","B":3}]}`), + }, + wantResult: testStruct{ + A: "b", + B: 2, + C: []testStructItem{ + { + A: "c", + B: 3, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotResult, err := StrategicMergePatch(tt.args.original, tt.args.patchData) + if (err != nil) != tt.wantErr { + t.Errorf("StrategicMergePatch() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotResult, tt.wantResult) { + t.Errorf("StrategicMergePatch() gotResult = %v, want %v", gotResult, tt.wantResult) + } + }) + } +}