-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaEventbus_test.go
144 lines (113 loc) · 4.06 KB
/
metaEventbus_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/kneu-messenger-pigeon/events"
"github.com/kneu-messenger-pigeon/events/mocks"
"github.com/segmentio/kafka-go"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestSendSecondaryDbLoadedEvent(t *testing.T) {
loc := time.Local
previousDatetime := time.Date(2023, 9, 1, 4, 0, 0, 0, loc)
currentDatetime := time.Date(2023, 9, 2, 4, 0, 0, 0, loc)
expectedError := errors.New("some error")
payload, _ := json.Marshal(events.SecondaryDbLoadedEvent{
CurrentSecondaryDatabaseDatetime: currentDatetime,
PreviousSecondaryDatabaseDatetime: previousDatetime,
Year: currentDatetime.Year(),
})
expectedMessage := kafka.Message{
Key: []byte(events.SecondaryDbLoadedEventName),
Value: payload,
}
t.Run("Success send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(nil)
out := &bytes.Buffer{}
eventbus := MetaEventbus{
writer: writer,
out: out,
}
err := eventbus.sendSecondaryDbLoadedEvent(currentDatetime, previousDatetime, currentDatetime.Year())
assert.NoErrorf(t, err, "Not expect for error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
assert.Contains(t, out.String(), "send SecondaryDbLoadedEvent")
})
t.Run("Empty previous datetime send", func(t *testing.T) {
p, _ := json.Marshal(events.SecondaryDbLoadedEvent{
CurrentSecondaryDatabaseDatetime: currentDatetime,
PreviousSecondaryDatabaseDatetime: time.Date(currentDatetime.Year(), 8, 1, 0, 0, 0, 0, currentDatetime.Location()),
Year: currentDatetime.Year(),
})
expected := kafka.Message{
Key: []byte(events.SecondaryDbLoadedEventName),
Value: p,
}
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expected).Return(nil)
out := &bytes.Buffer{}
eventbus := MetaEventbus{
writer: writer,
out: out,
}
err := eventbus.sendSecondaryDbLoadedEvent(currentDatetime, time.Time{}, currentDatetime.Year())
assert.NoErrorf(t, err, "Not expect for error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
assert.Contains(t, out.String(), "send SecondaryDbLoadedEvent")
})
t.Run("Failed send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(expectedError)
eventbus := MetaEventbus{
writer: writer,
out: &bytes.Buffer{},
}
err := eventbus.sendSecondaryDbLoadedEvent(currentDatetime, previousDatetime, currentDatetime.Year())
assert.Errorf(t, err, "Expect for error")
assert.Equal(t, expectedError, err, "Got unexpected error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
})
}
func TestSendCurrentYearEvent(t *testing.T) {
expectedYear := 2050
expectedError := errors.New("some error")
payload, _ := json.Marshal(events.CurrentYearEvent{
Year: expectedYear,
})
expectedMessage := kafka.Message{
Key: []byte(events.CurrentYearEventName),
Value: payload,
}
t.Run("Success send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(nil)
out := &bytes.Buffer{}
eventbus := MetaEventbus{
writer: writer,
out: out,
}
err := eventbus.sendCurrentYearEvent(expectedYear)
assert.NoErrorf(t, err, "Not expect for error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
assert.Contains(t, out.String(), "send sendCurrentYearEvent")
})
t.Run("Failed send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(expectedError)
out := &bytes.Buffer{}
eventbus := MetaEventbus{
writer: writer,
out: out,
}
err := eventbus.sendCurrentYearEvent(expectedYear)
assert.Errorf(t, err, "Expect for error")
assert.Equal(t, expectedError, err, "Got unexpected error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
assert.Contains(t, out.String(), "send sendCurrentYearEvent")
})
}