2020-04-04 08:36:52 +00:00
|
|
|
package dynfunc
|
2020-03-29 17:14:12 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-20 00:14:31 +00:00
|
|
|
"context"
|
2020-03-29 17:14:12 +00:00
|
|
|
"errors"
|
2020-03-29 17:23:13 +00:00
|
|
|
"fmt"
|
2020-03-29 17:14:12 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-03-29 17:23:13 +00:00
|
|
|
|
2021-06-20 19:29:46 +00:00
|
|
|
"github.com/xdrm-io/aicra/api"
|
2021-06-21 20:46:04 +00:00
|
|
|
"github.com/xdrm-io/aicra/internal/config"
|
2020-03-29 17:14:12 +00:00
|
|
|
)
|
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
func TestInputValidation(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
input map[string]reflect.Type
|
|
|
|
fn interface{}
|
|
|
|
err error
|
2020-03-29 17:14:12 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "missing context",
|
|
|
|
input: map[string]reflect.Type{},
|
|
|
|
fn: func() {},
|
|
|
|
err: ErrMissingHandlerContextArgument,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "invalid context",
|
|
|
|
input: map[string]reflect.Type{},
|
|
|
|
fn: func(int) {},
|
|
|
|
err: ErrInvalidHandlerContextArgument,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "no input 0 given",
|
|
|
|
input: map[string]reflect.Type{},
|
|
|
|
fn: func(context.Context) {},
|
|
|
|
err: nil,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "no input 1 given",
|
|
|
|
input: map[string]reflect.Type{},
|
|
|
|
fn: func(context.Context, int) {},
|
|
|
|
err: ErrUnexpectedInput,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no input 2 given",
|
|
|
|
input: map[string]reflect.Type{},
|
|
|
|
fn: func(context.Context, int, string) {},
|
|
|
|
err: ErrUnexpectedInput,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1 input 0 given",
|
|
|
|
input: map[string]reflect.Type{
|
2020-03-29 17:14:12 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context) {},
|
|
|
|
err: ErrMissingHandlerInputArgument,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input non-struct given",
|
|
|
|
input: map[string]reflect.Type{
|
2020-03-29 17:14:12 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, int) {},
|
|
|
|
err: ErrMissingParamArgument,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "unexported input",
|
|
|
|
input: map[string]reflect.Type{
|
2020-03-29 17:14:12 +00:00
|
|
|
"test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{}) {},
|
|
|
|
err: ErrUnexportedName,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input empty struct given",
|
|
|
|
input: map[string]reflect.Type{
|
2020-03-29 17:14:12 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{}) {},
|
|
|
|
err: ErrMissingConfigArgument,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input invalid given",
|
|
|
|
input: map[string]reflect.Type{
|
2020-03-29 17:14:12 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 string }) {},
|
|
|
|
err: ErrWrongParamTypeFromConfig,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input valid given",
|
|
|
|
input: map[string]reflect.Type{
|
2020-03-29 17:14:12 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 int }) {},
|
|
|
|
err: nil,
|
2020-03-29 17:14:12 +00:00
|
|
|
},
|
2021-04-18 16:26:37 +00:00
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input ptr empty struct given",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(int)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{}) {},
|
|
|
|
err: ErrMissingConfigArgument,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input ptr invalid given",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(int)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 string }) {},
|
|
|
|
err: ErrWrongParamTypeFromConfig,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input ptr invalid ptr type given",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(int)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *string }) {},
|
|
|
|
err: ErrWrongParamTypeFromConfig,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 input ptr valid given",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(int)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *int }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid string",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(string("")),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 string }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid uint",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(uint(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 uint }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid float64",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(float64(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 float64 }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid []byte",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf([]byte("")),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 []byte }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid []rune",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf([]rune("")),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 []rune }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid *string",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(string)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *string }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid *uint",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(uint)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *uint }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid *float64",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new(float64)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *float64 }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid *[]byte",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new([]byte)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *[]byte }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 valid *[]rune",
|
|
|
|
input: map[string]reflect.Type{
|
2021-04-18 16:26:37 +00:00
|
|
|
"Test1": reflect.TypeOf(new([]rune)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func(context.Context, struct{ Test1 *[]rune }) {},
|
|
|
|
err: nil,
|
2021-04-18 16:26:37 +00:00
|
|
|
},
|
2020-03-29 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-03-29 17:14:12 +00:00
|
|
|
// mock spec
|
2021-06-19 22:47:04 +00:00
|
|
|
s := Signature{
|
2021-06-21 20:46:04 +00:00
|
|
|
Input: tc.input,
|
2020-03-29 17:14:12 +00:00
|
|
|
Output: nil,
|
|
|
|
}
|
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
err := s.ValidateInput(reflect.TypeOf(tc.fn))
|
|
|
|
if err == nil && tc.err != nil {
|
|
|
|
t.Fatalf("expected an error: '%s'", tc.err.Error())
|
2021-06-19 22:47:04 +00:00
|
|
|
}
|
2021-06-21 20:46:04 +00:00
|
|
|
if err != nil && tc.err == nil {
|
|
|
|
t.Fatalf("unexpected error: '%s'", err.Error())
|
2021-06-19 22:47:04 +00:00
|
|
|
}
|
2020-03-29 17:14:12 +00:00
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
if err != nil && tc.err != nil {
|
|
|
|
if !errors.Is(err, tc.err) {
|
|
|
|
t.Fatalf("expected the error <%s> got <%s>", tc.err, err)
|
2021-04-18 17:25:31 +00:00
|
|
|
}
|
2021-06-19 22:47:04 +00:00
|
|
|
}
|
2020-03-29 17:14:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 17:23:13 +00:00
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
func TestOutputValidation(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
output map[string]reflect.Type
|
|
|
|
fn interface{}
|
|
|
|
err error
|
2020-03-29 17:23:13 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "no output missing err",
|
|
|
|
output: map[string]reflect.Type{},
|
|
|
|
fn: func() {},
|
|
|
|
err: ErrMissingHandlerErrorArgument,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
2020-03-29 17:31:08 +00:00
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "no output invalid err",
|
|
|
|
output: map[string]reflect.Type{},
|
|
|
|
fn: func() bool { return true },
|
|
|
|
err: ErrInvalidHandlerErrorArgument,
|
2020-03-29 17:31:08 +00:00
|
|
|
},
|
2020-03-29 17:23:13 +00:00
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "1 output none required",
|
|
|
|
output: map[string]reflect.Type{},
|
|
|
|
fn: func(context.Context) (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: nil,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
2021-06-19 22:47:04 +00:00
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "no output 1 required",
|
|
|
|
output: map[string]reflect.Type{
|
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
fn: func() api.Err { return api.ErrSuccess },
|
|
|
|
err: ErrMissingHandlerOutputArgument,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "invalid int output",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (int, api.Err) { return 0, api.ErrSuccess },
|
|
|
|
err: ErrWrongOutputArgumentType,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "invalid int ptr output",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (*int, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: ErrWrongOutputArgumentType,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "invalid struct output",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (struct{ Test1 int }, api.Err) { return struct{ Test1 int }{Test1: 1}, api.ErrSuccess },
|
|
|
|
err: ErrWrongOutputArgumentType,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "unexported param",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: ErrUnexportedName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing output param",
|
|
|
|
output: map[string]reflect.Type{
|
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
fn: func() (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: ErrMissingConfigArgument,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "invalid output param",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (*struct{ Test1 string }, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: ErrWrongParamTypeFromConfig,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "valid param",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (*struct{ Test1 int }, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: nil,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "2 valid params",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:23:13 +00:00
|
|
|
"Test1": reflect.TypeOf(int(0)),
|
2021-06-21 20:46:04 +00:00
|
|
|
"Test2": reflect.TypeOf(string("")),
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (*struct {
|
|
|
|
Test1 int
|
|
|
|
Test2 string
|
|
|
|
}, api.Err) {
|
|
|
|
return nil, api.ErrSuccess
|
|
|
|
},
|
|
|
|
err: nil,
|
2020-03-29 17:23:13 +00:00
|
|
|
},
|
2020-03-29 17:31:08 +00:00
|
|
|
{
|
2021-06-21 20:46:04 +00:00
|
|
|
name: "nil type ignore typecheck",
|
|
|
|
output: map[string]reflect.Type{
|
2020-03-29 17:31:08 +00:00
|
|
|
"Test1": nil,
|
|
|
|
},
|
2021-06-21 20:46:04 +00:00
|
|
|
fn: func() (*struct{ Test1 int }, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: nil,
|
2020-03-29 17:31:08 +00:00
|
|
|
},
|
2020-03-29 17:23:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-03-29 17:23:13 +00:00
|
|
|
// mock spec
|
2021-06-19 22:47:04 +00:00
|
|
|
s := Signature{
|
2020-03-29 17:23:13 +00:00
|
|
|
Input: nil,
|
2021-06-21 20:46:04 +00:00
|
|
|
Output: tc.output,
|
2020-03-29 17:23:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
err := s.ValidateOutput(reflect.TypeOf(tc.fn))
|
|
|
|
if !errors.Is(err, tc.err) {
|
|
|
|
t.Fatalf("expected the error <%s> got <%s>", tc.err, err)
|
2020-03-29 17:23:13 +00:00
|
|
|
}
|
2021-06-21 20:46:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceValidation(t *testing.T) {
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
in []*config.Parameter
|
|
|
|
out []*config.Parameter
|
|
|
|
fn interface{}
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing context",
|
|
|
|
fn: func() {},
|
|
|
|
err: ErrMissingHandlerContextArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid context",
|
|
|
|
fn: func(int) {},
|
|
|
|
err: ErrInvalidHandlerContextArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing error",
|
|
|
|
fn: func(context.Context) {},
|
|
|
|
err: ErrMissingHandlerErrorArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid error",
|
|
|
|
fn: func(context.Context) int { return 1 },
|
|
|
|
err: ErrInvalidHandlerErrorArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no in no out",
|
|
|
|
fn: func(context.Context) api.Err { return api.ErrSuccess },
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unamed in",
|
|
|
|
in: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "", // should be ignored
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) api.Err { return api.ErrSuccess },
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing in",
|
|
|
|
in: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) api.Err { return api.ErrSuccess },
|
|
|
|
err: ErrMissingHandlerInputArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid in",
|
|
|
|
in: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context, struct{ Test1 int }) api.Err { return api.ErrSuccess },
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "optional in not ptr",
|
|
|
|
in: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context, struct{ Test1 int }) api.Err { return api.ErrSuccess },
|
|
|
|
err: ErrWrongParamTypeFromConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid optional in",
|
|
|
|
in: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context, struct{ Test1 *int }) api.Err { return api.ErrSuccess },
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "unamed out",
|
|
|
|
out: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "", // should be ignored
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) api.Err { return api.ErrSuccess },
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing out struct",
|
|
|
|
out: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) api.Err { return api.ErrSuccess },
|
|
|
|
err: ErrMissingHandlerOutputArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid out struct type",
|
|
|
|
out: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) (int, api.Err) { return 0, api.ErrSuccess },
|
|
|
|
err: ErrWrongOutputArgumentType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing out",
|
|
|
|
out: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: ErrMissingConfigArgument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid out",
|
|
|
|
out: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) (*struct{ Test1 int }, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "optional out not ptr",
|
|
|
|
out: []*config.Parameter{
|
|
|
|
{
|
|
|
|
Rename: "Test1",
|
|
|
|
GoType: reflect.TypeOf(int(0)),
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fn: func(context.Context) (*struct{ Test1 *int }, api.Err) { return nil, api.ErrSuccess },
|
|
|
|
err: ErrWrongParamTypeFromConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
service := config.Service{
|
|
|
|
Input: make(map[string]*config.Parameter),
|
|
|
|
Output: make(map[string]*config.Parameter),
|
2020-03-29 17:23:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 20:46:04 +00:00
|
|
|
// fill service with arguments
|
|
|
|
if tc.in != nil && len(tc.in) > 0 {
|
|
|
|
for i, in := range tc.in {
|
|
|
|
service.Input[fmt.Sprintf("%d", i)] = in
|
2020-03-29 17:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 20:46:04 +00:00
|
|
|
if tc.out != nil && len(tc.out) > 0 {
|
|
|
|
for i, out := range tc.out {
|
|
|
|
service.Output[fmt.Sprintf("%d", i)] = out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s := BuildSignature(service)
|
|
|
|
|
|
|
|
err := s.ValidateInput(reflect.TypeOf(tc.fn))
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, tc.err) {
|
|
|
|
t.Fatalf("expected the error <%s> got <%s>", tc.err, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = s.ValidateOutput(reflect.TypeOf(tc.fn))
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, tc.err) {
|
|
|
|
t.Fatalf("expected the error <%s> got <%s>", tc.err, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// no error encountered but expected 1
|
|
|
|
if tc.err != nil {
|
|
|
|
t.Fatalf("expected an error <%v>", tc.err)
|
|
|
|
}
|
2020-03-29 17:23:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|