-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1047 Expose AddFunction API for CESQL Parser #1051
Merged
lionelvillard
merged 5 commits into
cloudevents:main
from
dgeorgievski:1047-expose-addfunction-api-cesql
Jun 11, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01cc71b
Added:
dgeorgievski 31e1907
Moved tests assets for user defined fuctions from sql/v2/test to sql/…
dgeorgievski f811b66
moved runtime test to runtime/test dir
dgeorgievski 22596a9
Corrected test functions naming format and copyright year
dgeorgievski b877ab4
Merge branch 'main' into 1047-expose-addfunction-api-cesql
dgeorgievski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: User defined functions | ||
tests: | ||
- name: HASPREFIX (1) | ||
expression: "HASPREFIX('abcdef', 'ab')" | ||
result: true | ||
- name: HASPREFIX (2) | ||
expression: "HASPREFIX('abcdef', 'abcdef')" | ||
result: true | ||
- name: HASPREFIX (3) | ||
expression: "HASPREFIX('abcdef', '')" | ||
result: true | ||
- name: HASPREFIX (4) | ||
expression: "HASPREFIX('abcdef', 'gh')" | ||
result: false | ||
- name: HASPREFIX (5) | ||
expression: "HASPREFIX('abcdef', 'abcdefg')" | ||
result: false | ||
|
||
- name: KONKAT (1) | ||
expression: "KONKAT('a', 'b', 'c')" | ||
result: abc | ||
- name: KONKAT (2) | ||
expression: "KONKAT()" | ||
result: "" | ||
- name: KONKAT (3) | ||
expression: "KONKAT('a')" | ||
result: "a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/* | ||
Copyright 2024 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package runtime_test | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
cesql "github.com/cloudevents/sdk-go/sql/v2" | ||
"github.com/cloudevents/sdk-go/sql/v2/function" | ||
"github.com/cloudevents/sdk-go/sql/v2/parser" | ||
ceruntime "github.com/cloudevents/sdk-go/sql/v2/runtime" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/cloudevents/sdk-go/v2/binding/spec" | ||
"github.com/cloudevents/sdk-go/v2/event" | ||
"github.com/cloudevents/sdk-go/v2/test" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var TCKFileNames = []string{ | ||
"user_defined_functions", | ||
} | ||
|
||
var TCKUserDefinedFunctions = []cesql.Function{ | ||
function.NewFunction( | ||
"HASPREFIX", | ||
[]cesql.Type{cesql.StringType, cesql.StringType}, | ||
nil, | ||
func(event cloudevents.Event, i []interface{}) (interface{}, error) { | ||
str := i[0].(string) | ||
prefix := i[1].(string) | ||
|
||
return strings.HasPrefix(str, prefix), nil | ||
}, | ||
), | ||
function.NewFunction( | ||
"KONKAT", | ||
[]cesql.Type{}, | ||
cesql.TypePtr(cesql.StringType), | ||
func(event cloudevents.Event, i []interface{}) (interface{}, error) { | ||
var sb strings.Builder | ||
for _, v := range i { | ||
sb.WriteString(v.(string)) | ||
} | ||
return sb.String(), nil | ||
}, | ||
), | ||
} | ||
|
||
type ErrorType string | ||
|
||
const ( | ||
ParseError ErrorType = "parse" | ||
MathError ErrorType = "math" | ||
CastError ErrorType = "cast" | ||
MissingAttributeError ErrorType = "missingAttribute" | ||
MissingFunctionError ErrorType = "missingFunction" | ||
FunctionEvaluationError ErrorType = "functionEvaluation" | ||
) | ||
|
||
type TckFile struct { | ||
Name string `json:"name"` | ||
Tests []TckTestCase `json:"tests"` | ||
} | ||
|
||
type TckTestCase struct { | ||
Name string `json:"name"` | ||
Expression string `json:"expression"` | ||
|
||
Result interface{} `json:"result"` | ||
Error ErrorType `json:"error"` | ||
|
||
Event *cloudevents.Event `json:"event"` | ||
EventOverrides map[string]interface{} `json:"eventOverrides"` | ||
} | ||
|
||
func (tc TckTestCase) InputEvent(t *testing.T) cloudevents.Event { | ||
var inputEvent cloudevents.Event | ||
if tc.Event != nil { | ||
inputEvent = *tc.Event | ||
} else { | ||
inputEvent = test.FullEvent() | ||
} | ||
|
||
// Make sure the event is v1 | ||
inputEvent.SetSpecVersion(event.CloudEventsVersionV1) | ||
|
||
for k, v := range tc.EventOverrides { | ||
require.NoError(t, spec.V1.SetAttribute(inputEvent.Context, k, v)) | ||
} | ||
|
||
return inputEvent | ||
} | ||
|
||
func (tc TckTestCase) ExpectedResult() interface{} { | ||
switch tc.Result.(type) { | ||
case int: | ||
return int32(tc.Result.(int)) | ||
case float64: | ||
return int32(tc.Result.(float64)) | ||
case bool: | ||
return tc.Result.(bool) | ||
} | ||
return tc.Result | ||
} | ||
|
||
func TestFunctionTableAddFunction(t *testing.T) { | ||
|
||
type args struct { | ||
functions []cesql.Function | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Add user functions to global table", | ||
|
||
args: args{ | ||
functions: TCKUserDefinedFunctions, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Fail add user functions to global table", | ||
args: args{ | ||
functions: TCKUserDefinedFunctions, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
for _, fn := range tt.args.functions { | ||
if err := ceruntime.AddFunction(fn); (err != nil) != tt.wantErr { | ||
t.Errorf("AddFunction() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUserFunctions(t *testing.T) { | ||
tckFiles := make([]TckFile, 0, len(TCKFileNames)) | ||
|
||
_, basePath, _, _ := runtime.Caller(0) | ||
basePath, _ = path.Split(basePath) | ||
|
||
for _, testFile := range TCKFileNames { | ||
testFilePath := path.Join(basePath, "tck", testFile+".yaml") | ||
|
||
t.Logf("Loading file %s", testFilePath) | ||
file, err := os.Open(testFilePath) | ||
require.NoError(t, err) | ||
|
||
fileBytes, err := io.ReadAll(file) | ||
require.NoError(t, err) | ||
|
||
tckFileModel := TckFile{} | ||
require.NoError(t, yaml.Unmarshal(fileBytes, &tckFileModel)) | ||
|
||
tckFiles = append(tckFiles, tckFileModel) | ||
} | ||
|
||
for i, file := range tckFiles { | ||
i := i | ||
t.Run(file.Name, func(t *testing.T) { | ||
for j, testCase := range tckFiles[i].Tests { | ||
j := j | ||
testCase := testCase | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
t.Parallel() | ||
testCase := tckFiles[i].Tests[j] | ||
|
||
t.Logf("Test expression: '%s'", testCase.Expression) | ||
|
||
if testCase.Error == ParseError { | ||
_, err := parser.Parse(testCase.Expression) | ||
require.NotNil(t, err) | ||
return | ||
} | ||
|
||
expr, err := parser.Parse(testCase.Expression) | ||
require.NoError(t, err) | ||
require.NotNil(t, expr) | ||
|
||
inputEvent := testCase.InputEvent(t) | ||
result, err := expr.Evaluate(inputEvent) | ||
|
||
if testCase.Error != "" { | ||
require.NotNil(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, testCase.ExpectedResult(), result) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove newline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing sql tck yaml files use new line to separate tests cases.
https://github.com/cloudevents/sdk-go/blob/main/sql/v2/test/tck/string_builtin_functions.yaml#L12-L18
I was following their format. I think the new line between consecutive tests cases should be kept but the new line at the end of the file should be removed.