-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
129 lines (103 loc) · 4.15 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"encoding/json"
"github.com/DATA-DOG/go-sqlmock"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
"testing"
)
func Test_handler_handleRequestReturnsErrorWhenRequestEmpty(t *testing.T) {
// given
handler := handler{}
// when
response, err := handler.handleRequest(context.Background(), events.SQSEvent{})
// then
assert.NotNil(t, err)
assert.Equal(t, events.SQSEventResponse{BatchItemFailures: []events.SQSBatchItemFailure(nil)}, response)
}
func Test_handler_handleRequestPutsMessageInBatchItemFailuresWhenMessageHasNoBody(t *testing.T) {
// given
handler := handler{}
records := []events.SQSMessage{{MessageId: "123"}}
// when
response, err := handler.handleRequest(context.Background(), events.SQSEvent{Records: records})
// then
assert.Nil(t, err)
assert.Equal(t, events.SQSEventResponse{BatchItemFailures: []events.SQSBatchItemFailure{{ItemIdentifier: "123"}}}, response)
}
func Test_handler_handleRequestPutsMessageInBatchItemFailuresWhenBodyIsNotWellFormed(t *testing.T) {
// given
handler := handler{}
records := []events.SQSMessage{{MessageId: "123", Body: ""}}
// when
response, err := handler.handleRequest(context.Background(), events.SQSEvent{Records: records})
// then
assert.Nil(t, err)
assert.Equal(t, events.SQSEventResponse{BatchItemFailures: []events.SQSBatchItemFailure{{ItemIdentifier: "123"}}}, response)
}
func Test_handler_handleRequestPutsMessageInBatchItemFailuresWhenRecordDoesNotExistInDatabase(t *testing.T) {
// Given
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("opening stub repository connexion: %v", err)
}
defer db.Close()
emptyRow := sqlmock.NewRows([]string{"title", "name", "role_name"})
mock.ExpectQuery("^select (.*)$").WithArgs(42).WillReturnRows(emptyRow)
handler := handler{dbConx: db}
m := message{WorkshopSignupId: 42}
body, _ := json.Marshal(m)
records := []events.SQSMessage{{MessageId: "123", Body: string(body)}}
// when
response, err := handler.handleRequest(context.Background(), events.SQSEvent{Records: records})
// then
assert.Nil(t, err)
assert.Equal(t, events.SQSEventResponse{BatchItemFailures: []events.SQSBatchItemFailure{{ItemIdentifier: "123"}}}, response)
}
type SpyingDynDs struct {
recordThatWasStored *WorkshopSignupRecord
}
func (s SpyingDynDs) Store(thing interface{}) error {
record := thing.(WorkshopSignupRecord)
*s.recordThatWasStored = record
return nil
}
func Test_handler_handleRequestReturnsNoErrorAddsToDynamoAndWritesFileToS3WhenThereAreRecordsInTheDatabase(t *testing.T) {
// Given
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
t.Fatalf("opening stub repository connexion: %v", err)
}
defer db.Close()
rows := sqlmock.NewRows([]string{"title", "name", "role_name"}).
AddRow("Some Workshop Title", "Some Person Name", "Some role")
sql := "select w.title, concat(p.forename, ' ', p.surname) as name, r.role_name " +
"from people p join workshop_signups s on s.people_id = p.id " +
"join workshops w on s.workshop_id = w.id " +
"join roles r on s.role_id = r.id " +
"where s.id = $1"
mock.ExpectQuery(sql).WithArgs(42).WillReturnRows(rows)
var recordThatWasStored WorkshopSignupRecord
handler := handler{
dbConx: db,
datastore: SpyingDynDs{recordThatWasStored: &recordThatWasStored},
}
body, _ := json.Marshal(message{WorkshopSignupId: 42})
records := []events.SQSMessage{{MessageId: "123", Body: string(body)}}
// When
response, err := handler.handleRequest(context.Background(), events.SQSEvent{Records: records})
// Then
// we make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
// no errors nor failed message deliveries
assert.Nil(t, err)
assert.Equal(t, events.SQSEventResponse{BatchItemFailures: []events.SQSBatchItemFailure(nil)}, response)
// record written to datastore
assert.Equal(t, "Some Workshop Title", recordThatWasStored.WorkshopTitle)
assert.Equal(t, "Some Person Name", recordThatWasStored.Name)
assert.Equal(t, "Some role", recordThatWasStored.Role)
assert.Equal(t, 42, recordThatWasStored.WorkshopSignupId)
}