-
Notifications
You must be signed in to change notification settings - Fork 8
/
publisher_test.go
108 lines (101 loc) · 2.44 KB
/
publisher_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
package outbox
import (
"database/sql"
"errors"
"testing"
"time"
"github.com/google/uuid"
time2 "github.com/pkritiotis/outbox/internal/time"
uuid2 "github.com/pkritiotis/outbox/internal/uuid"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
store := &MockStore{}
expectedOutbox := Publisher{
store: store,
time: time2.NewTimeProvider(),
uuid: uuid2.NewUUIDProvider(),
}
got := NewPublisher(store)
assert.Equal(t, got, expectedOutbox)
}
func TestOutbox_Add(t *testing.T) {
sampleTx := sql.Tx{}
sampleUUID, _ := uuid.NewUUID()
sampleTime := time.Now()
timeProvider := &time2.MockProvider{}
timeProvider.On("Now").Return(sampleTime)
uuidProvider := &uuid2.MockProvider{}
uuidProvider.On("NewUUID").Return(sampleUUID)
sampleMessage := Message{
Key: "testKey",
Headers: map[string]string{
"testHeader": "testValue",
},
Body: []byte("testvalue"),
Topic: "testTopic",
}
tests := map[string]struct {
msg Message
store Store
tx *sql.Tx
expErr error
}{
"Successful Send Record should return without error": {
msg: sampleMessage,
store: func() *MockStore {
mp := MockStore{}
or := Record{
ID: sampleUUID,
Message: sampleMessage,
State: PendingDelivery,
CreatedOn: sampleTime.UTC(),
LockID: nil,
LockedOn: nil,
ProcessedOn: nil,
NumberOfAttempts: 0,
LastAttemptOn: nil,
Error: nil,
}
mp.On("AddRecordTx", or, &sampleTx).Return(nil)
return &mp
}(),
tx: &sampleTx,
expErr: nil,
},
"Failure in Send Record should return error": {
msg: sampleMessage,
store: func() *MockStore {
mp := MockStore{}
or := Record{
ID: sampleUUID,
Message: sampleMessage,
State: PendingDelivery,
CreatedOn: sampleTime.UTC(),
LockID: nil,
LockedOn: nil,
ProcessedOn: nil,
NumberOfAttempts: 0,
LastAttemptOn: nil,
Error: nil,
}
mp.On("AddRecordTx", or, &sampleTx).Return(errors.New("error"))
return &mp
}(),
tx: &sampleTx,
expErr: errors.New("error"),
},
}
for name, test := range tests {
tt := test
t.Run(name, func(t *testing.T) {
s := Publisher{
store: tt.store,
time: timeProvider,
uuid: uuidProvider,
}
err := s.Send(tt.msg, tt.tx)
assert.Equal(t, tt.expErr, err)
})
}
}