-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3_handler_test.go
98 lines (82 loc) · 1.88 KB
/
s3_handler_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
93
94
95
96
97
98
package g8_test
import (
"context"
"fmt"
"io"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/JSainsburyPLC/g8"
)
func TestS3Handler_SingleMessage(t *testing.T) {
timesCalled := 0
h := g8.S3Handler(func(c *g8.S3Context) error {
timesCalled++
assert.Equal(t, "12345", c.EventRecord.S3.Object.Key)
assert.NotEmpty(t, c.CorrelationID)
return nil
}, g8.HandlerConfig{Logger: zerolog.New(io.Discard)})
err := h(context.Background(), events.S3Event{
Records: []events.S3EventRecord{
{
S3: events.S3Entity{
Object: events.S3Object{
Key: "12345",
},
},
},
}})
assert.Nil(t, err)
assert.Equal(t, 1, timesCalled)
}
func TestS3Handler_MultipleMessages(t *testing.T) {
timesCalled := 0
h := g8.S3Handler(func(c *g8.S3Context) error {
timesCalled++
assert.Equal(t, fmt.Sprintf("key-%d", timesCalled), c.EventRecord.S3.Object.Key)
assert.NotEmpty(t, c.CorrelationID)
return nil
}, g8.HandlerConfig{Logger: zerolog.New(io.Discard)})
err := h(context.Background(), events.S3Event{
Records: []events.S3EventRecord{
{
S3: events.S3Entity{
Object: events.S3Object{
Key: "key-1",
},
},
},
{
S3: events.S3Entity{
Object: events.S3Object{
Key: "key-2",
},
},
},
}})
assert.Nil(t, err)
assert.Equal(t, 2, timesCalled)
}
func TestS3Handler_HandlerError(t *testing.T) {
timesCalled := 0
handlerFunc := func(c *g8.S3Context) error {
timesCalled++
return assert.AnError
}
h := g8.S3Handler(handlerFunc, g8.HandlerConfig{
Logger: zerolog.New(io.Discard),
})
err := h(context.Background(), events.S3Event{
Records: []events.S3EventRecord{
{
S3: events.S3Entity{
Object: events.S3Object{
Key: "key-2",
},
},
},
}})
assert.Equal(t, assert.AnError, err)
assert.Equal(t, 1, timesCalled)
}