From e7427f4d9c87a645ccd6f10127429df397f73ceb Mon Sep 17 00:00:00 2001 From: Tony Holdstock-Brown Date: Wed, 6 Nov 2024 17:27:31 -0800 Subject: [PATCH] Update tests --- engine_stringmap_test.go | 38 ++++++++++++++++++++++++++++++++++++-- expr_test.go | 8 ++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/engine_stringmap_test.go b/engine_stringmap_test.go index 308fee5..d9a8025 100644 --- a/engine_stringmap_test.go +++ b/engine_stringmap_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/google/cel-go/common/operators" + "github.com/google/uuid" "github.com/stretchr/testify/require" ) @@ -12,7 +13,14 @@ 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", @@ -20,6 +28,8 @@ func TestEngineStringmap(t *testing.T) { }, } 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", @@ -27,6 +37,8 @@ func TestEngineStringmap(t *testing.T) { }, } c := ExpressionPart{ + Parsed: exp, + GroupID: gid, Predicate: &Predicate{ Ident: "async.data.another", Literal: "456", @@ -36,6 +48,8 @@ func TestEngineStringmap(t *testing.T) { // Test inequality d := ExpressionPart{ + Parsed: exp, + GroupID: gid, Predicate: &Predicate{ Ident: "async.data.neq", Literal: "neq-1", @@ -43,6 +57,8 @@ func TestEngineStringmap(t *testing.T) { }, } 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", @@ -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) { @@ -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) { diff --git a/expr_test.go b/expr_test.go index 068d0d4..44e6cdd 100644 --- a/expr_test.go +++ b/expr_test.go @@ -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) @@ -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", }, }, @@ -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", }, }, @@ -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) @@ -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", }, @@ -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", },