Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Nov 7, 2024
1 parent 5883577 commit e7427f4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 36 additions & 2 deletions engine_stringmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,40 @@ import (
"testing"

"github.com/google/cel-go/common/operators"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

func TestEngineStringmap(t *testing.T) {
ctx := context.Background()
s := newStringEqualityMatcher(testConcurrency).(*stringLookup)

gid := newGroupID(4, 2) // optimized to 2 == matches.
exp := &ParsedExpression{
EvaluableID: uuid.NewSHA1(uuid.NameSpaceURL, []byte("eq-neq")),
}

a := ExpressionPart{
Parsed: exp,
GroupID: gid,
Predicate: &Predicate{
Ident: "async.data.id",
Literal: "123",
Operator: operators.Equals,
},
}
b := ExpressionPart{
Parsed: &ParsedExpression{EvaluableID: uuid.NewSHA1(uuid.NameSpaceURL, []byte("eq-single"))},
GroupID: newGroupID(1, 0), // This belongs to a "different" expression, but is the same pred.
Predicate: &Predicate{
Ident: "async.data.id",
Literal: "123",
Operator: operators.Equals,
},
}
c := ExpressionPart{
Parsed: exp,
GroupID: gid,
Predicate: &Predicate{
Ident: "async.data.another",
Literal: "456",
Expand All @@ -36,13 +48,17 @@ func TestEngineStringmap(t *testing.T) {

// Test inequality
d := ExpressionPart{
Parsed: exp,
GroupID: gid,
Predicate: &Predicate{
Ident: "async.data.neq",
Literal: "neq-1",
Operator: operators.NotEquals,
},
}
e := ExpressionPart{
Parsed: &ParsedExpression{EvaluableID: uuid.NewSHA1(uuid.NameSpaceURL, []byte("neq-single"))},
GroupID: newGroupID(1, 0), // This belongs to a "different" expression, but is the same pred.
Predicate: &Predicate{
Ident: "async.data.neq",
Literal: "neq-2",
Expand Down Expand Up @@ -134,7 +150,11 @@ func TestEngineStringmap(t *testing.T) {
},
})
require.NoError(t, err)
require.Equal(t, 4, len(found)) // matching plus inequality

// This should match "neq-single" and eq-single only. It shouldn't
// match the eq-neq expression, as the "async.data.nother" part wasn't matched
// and there's expression optimization to test this.
require.Equal(t, 2, len(found))
})

t.Run("It matches data with null neq", func(t *testing.T) {
Expand All @@ -147,9 +167,23 @@ func TestEngineStringmap(t *testing.T) {
},
})
require.NoError(t, err)
require.Equal(t, 4, len(found)) // matching plus inequality
require.Equal(t, 2, len(found)) // matching plus inequality
})

t.Run("It matches data with expression optimizations in group ID", func(t *testing.T) {
found, err := s.Match(ctx, map[string]any{
"async": map[string]any{
"data": map[string]any{
"id": "123",
"another": "456",
"neq": "lol",
},
},
})
require.NoError(t, err)

require.Equal(t, 4, len(found))
})
}

func TestEngineStringmap_DuplicateValues(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestEvaluate_Strings(t *testing.T) {
ctx := context.Background()
parser := NewTreeParser(NewCachingCompiler(newEnv(), nil))

expected := tex(`event.data.account_id == "yes" && event.data.match == "true"`)
expected := tex(`event.data.account_id == "yes" && event.data.another == "ok" && event.data.match == "true"`)
loader := newEvalLoader()
loader.AddEval(expected)

Expand All @@ -146,6 +146,7 @@ func TestEvaluate_Strings(t *testing.T) {
"event": map[string]any{
"data": map[string]any{
"account_id": "yes",
"another": "ok",
"match": "true",
},
},
Expand All @@ -167,6 +168,7 @@ func TestEvaluate_Strings(t *testing.T) {
"event": map[string]any{
"data": map[string]any{
"account_id": "yes",
"another": "ok",
"match": "no",
},
},
Expand All @@ -186,7 +188,7 @@ func TestEvaluate_Strings_Inequality(t *testing.T) {
ctx := context.Background()
parser := NewTreeParser(NewCachingCompiler(newEnv(), nil))

expected := tex(`event.data.account_id == "yes" && event.data.neq != "neq"`)
expected := tex(`event.data.account_id == "yes" && event.data.another == "ok" && event.data.neq != "neq"`)
loader := newEvalLoader()
loader.AddEval(expected)

Expand All @@ -208,6 +210,7 @@ func TestEvaluate_Strings_Inequality(t *testing.T) {
"event": map[string]any{
"data": map[string]any{
"account_id": "yes",
"another": "ok",
"match": "true",
"neq": "nah",
},
Expand All @@ -233,6 +236,7 @@ func TestEvaluate_Strings_Inequality(t *testing.T) {
"event": map[string]any{
"data": map[string]any{
"account_id": "yes",
"another": "ok",
"match": "no",
"neq": "nah",
},
Expand Down

0 comments on commit e7427f4

Please sign in to comment.