Skip to content

Commit

Permalink
Add tests for IsDuplicate()
Browse files Browse the repository at this point in the history
  • Loading branch information
ronensc committed Feb 22, 2023
1 parent 72847f7 commit 3b48145
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/config/generic_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import "github.com/netobserv/flowlogs-pipeline/pkg/utils"

type GenericMap map[string]interface{}

const duplicateFieldName = "Duplicate"

// Copy will create a flat copy of GenericMap
func (m GenericMap) Copy() GenericMap {
result := make(GenericMap, len(m))
Expand All @@ -33,7 +35,7 @@ func (m GenericMap) Copy() GenericMap {
}

func (m GenericMap) IsDuplicate() bool {
if duplicate, hasKey := m["Duplicate"]; hasKey {
if duplicate, hasKey := m[duplicateFieldName]; hasKey {
if isDuplicate, err := utils.ConvertToBool(duplicate); err == nil {
return isDuplicate
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/config/generic_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func BenchmarkGenericMap_Copy(b *testing.B) {
Expand All @@ -15,3 +17,49 @@ func BenchmarkGenericMap_Copy(b *testing.B) {
_ = m.Copy()
}
}

func TestGenericMap_IsDuplicate(t *testing.T) {
table := []struct {
name string
input GenericMap
expected bool
}{
{
"Duplicate: true",
GenericMap{duplicateFieldName: true},
true,
},
{
"Duplicate: false",
GenericMap{duplicateFieldName: false},
false,
},
{
"Missing field",
GenericMap{},
false,
},
{
"Convert 'true'",
GenericMap{duplicateFieldName: "true"},
true,
},
{
"Convert 'false'",
GenericMap{duplicateFieldName: "false"},
false,
},
{
"Conversion failure: 'maybe'",
GenericMap{duplicateFieldName: "maybe"},
false,
},
}

for _, testCase := range table {
t.Run(testCase.name, func(tt *testing.T) {
actual := testCase.input.IsDuplicate()
require.Equal(tt, testCase.expected, actual)
})
}
}

0 comments on commit 3b48145

Please sign in to comment.