-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamodb_handler_test.go
91 lines (75 loc) · 2.04 KB
/
dynamodb_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
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 TestDynamoDbHandler_SingleMessage(t *testing.T) {
timesCalled := 0
h := g8.DynamoDbHandler(func(c *g8.DynamoDbContext) error {
timesCalled++
assert.Equal(t, "event1", c.EventRecord.EventName)
assert.IsType(t, events.DynamoDBStreamRecord{}, c.EventRecord.Change)
assert.NotEmpty(t, c.CorrelationID)
return nil
}, g8.HandlerConfig{Logger: zerolog.New(io.Discard)})
err := h(context.Background(), events.DynamoDBEvent{
Records: []events.DynamoDBEventRecord{
{
EventName: "event1",
Change: events.DynamoDBStreamRecord{},
},
},
})
assert.Nil(t, err)
assert.Equal(t, 1, timesCalled)
}
func TestDynamoDbHandler_MultipleMessages(t *testing.T) {
timesCalled := 0
h := g8.DynamoDbHandler(func(c *g8.DynamoDbContext) error {
timesCalled++
assert.Equal(t, fmt.Sprintf("event-%d", timesCalled), c.EventRecord.EventName)
assert.NotEmpty(t, c.CorrelationID)
assert.IsType(t, events.DynamoDBStreamRecord{}, c.EventRecord.Change)
return nil
}, g8.HandlerConfig{Logger: zerolog.New(io.Discard)})
err := h(context.Background(), events.DynamoDBEvent{
Records: []events.DynamoDBEventRecord{
{
EventName: "event-1",
Change: events.DynamoDBStreamRecord{},
},
{
EventName: "event-2",
Change: events.DynamoDBStreamRecord{},
},
},
})
assert.Nil(t, err)
assert.Equal(t, 2, timesCalled)
}
func TestDynamoDbHandler_HandlerError(t *testing.T) {
timesCalled := 0
handlerFunc := func(c *g8.DynamoDbContext) error {
timesCalled++
return assert.AnError
}
h := g8.DynamoDbHandler(handlerFunc, g8.HandlerConfig{
Logger: zerolog.New(io.Discard),
})
err := h(context.Background(), events.DynamoDBEvent{
Records: []events.DynamoDBEventRecord{
{
EventName: "event-1",
Change: events.DynamoDBStreamRecord{},
},
},
})
assert.Equal(t, assert.AnError, err)
assert.Equal(t, 1, timesCalled)
}