-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_test.go
98 lines (86 loc) · 2.37 KB
/
processor_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 email_sender
import (
"io"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)
func TestEmailProcessor_Process(t *testing.T) {
t.Parallel()
type fields struct {
reader *MockReader
sender *MockSender
errHandler *MockErrHandler
templateParser *MockTemplateParser
}
tests := []struct {
name string
prepareMocks func(t *testing.T, fields fields)
n int
want bool
wantErr bool
}{
{
name: "success",
prepareMocks: func(t *testing.T, f fields) {
var mockCustomers = make([]*Customer, 0)
for i := 0; i < 3; i++ {
mockCustomers = append(mockCustomers, &Customer{
Title: RandomString(10, AlphaNumericCharacters),
FirstName: RandomString(10, AlphaNumericCharacters),
LastName: RandomString(10, AlphaNumericCharacters),
Email: RandomString(10, AlphaNumericCharacters),
})
}
f.reader.EXPECT().Read(10).Return(mockCustomers, io.EOF)
mockEmails := make([]*Email, 0)
for i := 0; i < 3; i++ {
mockEmails = append(mockEmails, &Email{
From: RandomString(10, AlphaNumericCharacters),
To: RandomString(10, AlphaNumericCharacters),
Subject: RandomString(10, AlphaNumericCharacters),
MimeType: "plain/text",
Body: RandomString(10, AlphaNumericCharacters),
})
}
f.templateParser.EXPECT().ParseEmails(gomock.Any()).DoAndReturn(
func(cs []*Customer) []*Email {
require.Len(t, cs, 3)
return mockEmails
})
f.sender.EXPECT().Send(mockEmails).Return(nil, nil)
f.errHandler.EXPECT().HandleErr(gomock.Len(0))
},
n: 10,
want: false,
wantErr: false,
},
}
ctrl := gomock.NewController(t)
t.Cleanup(ctrl.Finish)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := fields{
reader: NewMockReader(ctrl),
sender: NewMockSender(ctrl),
errHandler: NewMockErrHandler(ctrl),
templateParser: NewMockTemplateParser(ctrl),
}
tt.prepareMocks(t, f)
e := &EmailProcessor{
reader: f.reader,
sender: f.sender,
errHandler: f.errHandler,
templateParser: f.templateParser,
}
got, err := e.Process(tt.n)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}