Skip to content
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

[test]: add test #5

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
run: make lint

- name: Test
run: go test -v ./...
run: make test
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ install-golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(CURDIR) v1.61.0

lint:
$(CURDIR)/golangci-lint run --verbose
$(CURDIR)/golangci-lint run --verbose

test:
@echo "Running tests with coverage..."
@go test -race -cover ./...
5 changes: 3 additions & 2 deletions example/lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"

fake "github.com/NextOption/aws/lambda"
"github.com/aws/aws-sdk-go/aws"
real "github.com/aws/aws-sdk-go/service/lambda"
)

Expand Down Expand Up @@ -124,9 +126,8 @@ func main() {
fmt.Println("result: ", orderProcessResult)
}

event := real.InvocationTypeEvent
invokeOutput, err = GetServerless().InvokeWithContext(context.TODO(), &real.InvokeInput{
InvocationType: &event,
InvocationType: aws.String(real.InvocationTypeEvent),
FunctionName: &fn2Name,
Payload: inputPayload,
})
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ module github.com/NextOption/aws

go 1.23

require github.com/aws/aws-sdk-go v1.55.5
require (
github.com/aws/aws-sdk-go v1.55.5
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
306 changes: 306 additions & 0 deletions lambda/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
package lambda

import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/lambda"
"reflect"
"testing"
)

func Test_isAsyncInvoke(t *testing.T) {
type args struct {
input lambda.InvokeInput
}
tests := []struct {
name string
args args
want bool
}{
{
name: "InvocationType is Event",
args: args{
input: lambda.InvokeInput{
InvocationType: aws.String(lambda.InvocationTypeEvent),
},
},
want: true,
},
{
name: "InvocationType is RequestResponse",
args: args{
input: lambda.InvokeInput{
InvocationType: aws.String(lambda.InvocationTypeRequestResponse),
},
},
want: false,
},
{
name: "InvocationType is nil",
args: args{
input: lambda.InvokeInput{
InvocationType: nil,
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAsyncInvoke(tt.args.input); got != tt.want {
t.Errorf("isAsyncInvoke() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewFakeLambda(t *testing.T) {
tests := []struct {
name string
opts []Option
want *Fake
}{
{
name: "No options",
opts: nil,
want: &Fake{
mpLambdaFunc: make(map[string]FuncHandler),
},
},
{
name: "With one function",
opts: []Option{
WithFunction("testFunc", func(ctx context.Context, input []byte) ([]byte, error) {
return []byte("output"), nil
}),
},
want: &Fake{
mpLambdaFunc: map[string]FuncHandler{
"testFunc": func(ctx context.Context, input []byte) ([]byte, error) {
return []byte("output"), nil
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewFakeLambda(tt.opts...)
if len(got.mpLambdaFunc) != len(tt.want.mpLambdaFunc) {
t.Errorf("NewFakeLambda() mpLambdaFunc length = %v, want %v", len(got.mpLambdaFunc), len(tt.want.mpLambdaFunc))
return
}
for key, wantFunc := range tt.want.mpLambdaFunc {
gotFunc, ok := got.mpLambdaFunc[key]
if !ok {
t.Errorf("NewFakeLambda() missing function %v", key)
continue
}
gotOutput, gotErr := gotFunc(context.TODO(), []byte("input"))
wantOutput, wantErr := wantFunc(context.TODO(), []byte("input"))
if !reflect.DeepEqual(gotOutput, wantOutput) || !reflect.DeepEqual(gotErr, wantErr) {
t.Errorf("NewFakeLambda() function %v = (%v, %v), want (%v, %v)", key, gotOutput, gotErr, wantOutput, wantErr)
}
}
})
}
}

func TestFake_validateInput(t *testing.T) {
type fields struct {
mpLambdaFunc map[string]FuncHandler
}
type args struct {
in *lambda.InvokeInput
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "No input",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{},
},
args: args{
in: nil,
},
wantErr: true,
},
{
name: "No function name",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{},
},
args: args{
in: &lambda.InvokeInput{},
},
wantErr: true,
},
{
name: "Function not found",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{},
},
args: args{
in: &lambda.InvokeInput{
FunctionName: aws.String("nonExistentFunc"),
},
},
wantErr: true,
},
{
name: "Valid input",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{
"testFunc": func(ctx context.Context, input []byte) ([]byte, error) {
return []byte("output"), nil
},
},
},
args: args{
in: &lambda.InvokeInput{
FunctionName: aws.String("testFunc"),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &Fake{
mpLambdaFunc: tt.fields.mpLambdaFunc,
}
if err := f.validateInput(tt.args.in); (err != nil) != tt.wantErr {
t.Errorf("validateInput() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestFake_InvokeWithContext(t *testing.T) {
type fields struct {
mpLambdaFunc map[string]FuncHandler
}
type args struct {
ctx context.Context
in *lambda.InvokeInput
opts []request.Option
}
tests := []struct {
name string
fields fields
args args
want *lambda.InvokeOutput
wantErr bool
}{
{
name: "No input",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{},
},
args: args{
ctx: context.TODO(),
in: nil,
},
want: nil,
wantErr: true,
},
{
name: "Function not found",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{},
},
args: args{
ctx: context.TODO(),
in: &lambda.InvokeInput{
FunctionName: aws.String("nonExistentFunc"),
},
},
want: nil,
wantErr: true,
},
{
name: "Valid input, sync invoke",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{
"testFunc": func(ctx context.Context, input []byte) ([]byte, error) {
return []byte("output"), nil
},
},
},
args: args{
ctx: context.TODO(),
in: &lambda.InvokeInput{
FunctionName: aws.String("testFunc"),
Payload: []byte("input"),
},
},
want: &lambda.InvokeOutput{
Payload: []byte("output"),
},
wantErr: false,
},
{
name: "Valid input, async invoke",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{
"testFunc": func(ctx context.Context, input []byte) ([]byte, error) {
return []byte("output"), nil
},
},
},
args: args{
ctx: context.TODO(),
in: &lambda.InvokeInput{
FunctionName: aws.String("testFunc"),
Payload: []byte("input"),
InvocationType: aws.String(lambda.InvocationTypeEvent),
},
},
want: &lambda.InvokeOutput{
StatusCode: aws.Int64(202),
},
wantErr: false,
},
{
name: "Handler returns error",
fields: fields{
mpLambdaFunc: map[string]FuncHandler{
"errorFunc": func(ctx context.Context, input []byte) ([]byte, error) {
return nil, fmt.Errorf("handler error")
},
},
},
args: args{
ctx: context.TODO(),
in: &lambda.InvokeInput{
FunctionName: aws.String("errorFunc"),
Payload: []byte("input"),
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &Fake{
mpLambdaFunc: tt.fields.mpLambdaFunc,
}
got, err := f.InvokeWithContext(tt.args.ctx, tt.args.in, tt.args.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("InvokeWithContext() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("InvokeWithContext() got = %v, want %v", got, tt.want)
}
})
}
}
Loading