-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Initial implementation of a user defined function. - TCK tests for user defined functions. - Instructions on using user defined functions in README file. Signed-off-by: Dimitar Georgievski <dgeorgievski@gmail.com>
- Loading branch information
1 parent
f97061a
commit 97049a8
Showing
6 changed files
with
203 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2021 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package runtime | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
cesql "github.com/cloudevents/sdk-go/sql/v2" | ||
"github.com/cloudevents/sdk-go/sql/v2/function" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
) | ||
|
||
var HasPrefixCustomFunction cesql.Function | ||
|
||
func TestMain(m *testing.M) { | ||
HasPrefixCustomFunction = 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 | ||
}, | ||
) | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func Test_functionTable_AddFunction(t *testing.T) { | ||
|
||
type args struct { | ||
function cesql.Function | ||
} | ||
tests := []struct { | ||
name string | ||
table functionTable | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Add custom fixedArgs func", | ||
table: globalFunctionTable, | ||
args: args{ | ||
function: HasPrefixCustomFunction, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Fail add custom fixedArgs func if it exists", | ||
table: globalFunctionTable, | ||
args: args{ | ||
function: HasPrefixCustomFunction, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.table.AddFunction(tt.args.function); (err != nil) != tt.wantErr { | ||
t.Errorf("functionTable.AddFunction() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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