From 58d56dde64306b1cf2d56204f32a22eaf3f11515 Mon Sep 17 00:00:00 2001 From: Zubair Haque Date: Wed, 3 May 2023 02:54:50 -0500 Subject: [PATCH] chore: adding tests for the removeConflicts function & the Convert2WaitBackoff function (#2600) Signed-off-by: zhaque44 --- common/expr/eval_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ common/retry_test.go | 21 ++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/common/expr/eval_test.go b/common/expr/eval_test.go index c03309957b..4fcc06db65 100644 --- a/common/expr/eval_test.go +++ b/common/expr/eval_test.go @@ -2,6 +2,7 @@ package expr import ( "fmt" + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -61,3 +62,44 @@ func TestEvalBool(t *testing.T) { assert.NoError(t, err) assert.True(t, pass) } + +func TestRemoveConflictingKeys(t *testing.T) { + testCases := []struct { + name string + input map[string]interface{} + output map[string]interface{} + }{ + { + name: "remove conflicting keys", + input: map[string]interface{}{ + "a.b": 1, + "a": 2, + }, + output: map[string]interface{}{ + "a.b": 1, + }, + }, + { + name: "no conflicts", + input: map[string]interface{}{ + "a": 1, + "b": 2, + "c.d": 3, + }, + output: map[string]interface{}{ + "a": 1, + "b": 2, + "c.d": 3, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := removeConflicts(tc.input) + if !reflect.DeepEqual(result, tc.output) { + t.Errorf("expected %v, but got %v", tc.output, result) + } + }) + } +} diff --git a/common/retry_test.go b/common/retry_test.go index 708d57569e..05abc81995 100644 --- a/common/retry_test.go +++ b/common/retry_test.go @@ -24,6 +24,7 @@ import ( "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/util/wait" apicommon "github.com/argoproj/argo-events/pkg/apis/common" ) @@ -117,3 +118,23 @@ func TestRetryFailure(t *testing.T) { assert.Contains(t, err.Error(), "after retries") assert.Contains(t, err.Error(), "this is an error") } + +func TestConvert2WaitBackoff(t *testing.T) { + factor := apicommon.NewAmount("1.0") + jitter := apicommon.NewAmount("1") + duration := apicommon.FromString("1s") + backoff := apicommon.Backoff{ + Duration: &duration, + Factor: &factor, + Jitter: &jitter, + Steps: 2, + } + waitBackoff, err := Convert2WaitBackoff(&backoff) + assert.NoError(t, err) + assert.Equal(t, wait.Backoff{ + Duration: 1 * time.Second, + Factor: 1.0, + Jitter: 1.0, + Steps: 2, + }, *waitBackoff) +}