-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaEventbus_test.go
106 lines (84 loc) · 3.11 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
package main
import (
"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 TestSendSecondaryDbLessonProcessedEventName(t *testing.T) {
previousDatetime := time.Date(2023, 9, 1, 4, 0, 0, 0, time.Local)
currentDatetime := time.Date(2023, 9, 2, 4, 0, 0, 0, time.Local)
origEvent := events.SecondaryDbLoadedEvent{
CurrentSecondaryDatabaseDatetime: currentDatetime,
PreviousSecondaryDatabaseDatetime: previousDatetime,
Year: previousDatetime.Year(),
}
expectedError := errors.New("some error")
payload, _ := json.Marshal(events.SecondaryDbLessonProcessedEvent{
CurrentSecondaryDatabaseDatetime: currentDatetime,
PreviousSecondaryDatabaseDatetime: previousDatetime,
Year: previousDatetime.Year(),
})
expectedMessage := kafka.Message{
Key: []byte(events.SecondaryDbLessonProcessedEventName),
Value: payload,
}
t.Run("Success send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(nil)
eventbus := MetaEventbus{writer: writer}
err := eventbus.sendSecondaryDbLessonProcessedEventName(origEvent)
assert.NoErrorf(t, err, "Not expect for error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
})
t.Run("Failed send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(expectedError)
eventbus := MetaEventbus{writer: writer}
err := eventbus.sendSecondaryDbLessonProcessedEventName(origEvent)
assert.Errorf(t, err, "Expect for error")
assert.Equal(t, expectedError, err, "Got unexpected error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
})
}
func TestSendLessonTypesList(t *testing.T) {
lessonTypesList := []events.LessonType{
{
Id: 30,
ShortName: "Лек",
LongName: "Лекція",
},
}
expectedError := errors.New("some error")
expectedYear := 2045
payload, _ := json.Marshal(events.LessonTypesList{
Year: expectedYear,
List: lessonTypesList,
})
expectedMessage := kafka.Message{
Key: []byte(events.LessonTypesListName),
Value: payload,
}
t.Run("Success send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(nil)
eventbus := MetaEventbus{writer: writer}
err := eventbus.sendLessonTypesList(lessonTypesList, expectedYear)
assert.NoErrorf(t, err, "Not expect for error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
})
t.Run("Failed send", func(t *testing.T) {
writer := mocks.NewWriterInterface(t)
writer.On("WriteMessages", context.Background(), expectedMessage).Return(expectedError)
eventbus := MetaEventbus{writer: writer}
err := eventbus.sendLessonTypesList(lessonTypesList, expectedYear)
assert.Errorf(t, err, "Expect for error")
assert.Equal(t, expectedError, err, "Got unexpected error")
writer.AssertNumberOfCalls(t, "WriteMessages", 1)
})
}