-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
92 lines (80 loc) · 2.23 KB
/
event_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"errors"
"log/slog"
"testing"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// MockLambdaCaller is a mock implementation of the lambdaCaller interface
type MockLambdaCaller struct {
mock.Mock
}
func (m *MockLambdaCaller) Invoke(data []byte) (messages.InvokeResponse, error) {
args := m.Called(data)
return args.Get(0).(messages.InvokeResponse), args.Error(1)
}
func TestRunLambdaEvent(t *testing.T) {
t.Parallel()
tests := map[string]struct {
event string
parseJSON bool
invokeResp messages.InvokeResponse
invokeErr error
expectedErr string
}{
"successful invocation without JSON parsing": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{
Payload: []byte(`{"response": "success"}`),
},
invokeErr: nil,
expectedErr: "",
},
"successful invocation with JSON parsing": {
event: `{"key": "value"}`,
parseJSON: true,
invokeResp: messages.InvokeResponse{
Payload: []byte(`{"response": "{\"innerKey\": \"innerValue\"}"}`),
},
invokeErr: nil,
expectedErr: "",
},
"invoke error": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{},
invokeErr: errors.New("invoke error"),
expectedErr: "invoke failed: invoke error",
},
"unmarshal error": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{
Payload: []byte(`invalid json`),
},
invokeErr: nil,
expectedErr: "unmarshal response failed: invalid character 'i' looking for beginning of value",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
mockLambdaRPC := new(MockLambdaCaller)
logger := slog.Default()
mockLambdaRPC.On("Invoke", []byte(tt.event)).Return(tt.invokeResp, tt.invokeErr)
err := RunLambdaEvent(context.Background(), mockLambdaRPC, tt.event, tt.parseJSON, logger)
if tt.expectedErr == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedErr)
}
mockLambdaRPC.AssertExpectations(t)
})
}
}