Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisecheng committed Jan 8, 2025
1 parent 8660fc9 commit 5269a9f
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions pkg/ottl/ottlfuncs/func_murmur3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package ottlfuncs

import (
"context"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,11 +17,12 @@ import (

func Test_Murmur3(t *testing.T) {
tests := []struct {
name string
oArgs *Murmur3Arguments[any]
variant murmur3Variant
expected any
funcError string
name string
oArgs ottl.Arguments
variant murmur3Variant
expected any
funcError string
createError string
}{
{
name: "string in Murmur3Hash",
Expand Down Expand Up @@ -117,11 +120,22 @@ func Test_Murmur3(t *testing.T) {
variant: Murmur3Hash,
funcError: "expected string but got nil",
},
{
name: "Invalid args",
oArgs: nil,
variant: Murmur3Hash,
createError: "Murmur3HashFactory args must be of type *Murmur3Arguments[K]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := createMurmur3Function[any](tt.oArgs, tt.variant)
if tt.createError != "" {
require.ErrorContains(t, err, tt.createError)
return
}
assert.NoError(t, err)

result, err := exprFunc(context.Background(), nil)
if tt.funcError != "" {
require.ErrorContains(t, err, tt.funcError)
Expand All @@ -131,3 +145,34 @@ func Test_Murmur3(t *testing.T) {
})
}
}

func TestCreateMurmur3Func(t *testing.T) {
factory := NewMurmur3Hash128Factory[any]()
fCtx := ottl.FunctionContext{}

// invalid args
exprFunc, err := factory.CreateFunction(fCtx, nil)
assert.Error(t, err)
assert.Nil(t, exprFunc)

// valid args
exprFunc, err = factory.CreateFunction(
fCtx, &Murmur3Arguments[any]{
Target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return "Hello World", nil
},
},
})
assert.NoError(t, err)
assert.NotNil(t, exprFunc)
}

func TestMurmur3VariantUnknownCase(t *testing.T) {
var invalidVariant murmur3Variant = 9999
result := invalidVariant.String()
expectedString := fmt.Sprintf("Unknown(%d)", invalidVariant)
if !strings.Contains(result, expectedString) {
t.Errorf("%q does not contain %q", result, expectedString)
}
}

0 comments on commit 5269a9f

Please sign in to comment.