-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_test.go
102 lines (87 loc) · 2.55 KB
/
engine_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
package main
import (
"context"
"fmt"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"
)
type EngineSuite struct {
suite.Suite
filename string
calSvcMock *CalendarServiceMock
telCliMock *TelegramClientMock
lastChkdDao LastCheckedDao
calendarId string
engine Engine
}
func TestEngineSuite(t *testing.T) {
suite.Run(t, new(EngineSuite))
}
func (s *EngineSuite) SetupSuite() {
s.filename = fmt.Sprintf("%s_%d.txt", "test_last_checked", time.Now().Unix())
s.calendarId = "someCalendarId"
}
func (s *EngineSuite) SetupTest() {
s.calSvcMock = &CalendarServiceMock{}
s.telCliMock = &TelegramClientMock{}
s.lastChkdDao = NewLastCheckedDao(Config{LastCheckedFile: s.filename})
s.engine = Engine{
cfg: Config{
CalendarId: s.calendarId,
},
calSvc: s.calSvcMock,
telcli: s.telCliMock,
lastChkdDao: s.lastChkdDao,
}
_ = os.Remove(s.filename)
}
func (s *EngineSuite) TearDownSuite() {
_ = os.Remove(s.filename)
}
func (s *EngineSuite) TestFirstRun() {
ctx := context.Background()
s.calSvcMock.On("GetRecentEvents", ctx, mock.Anything).Return([]CalendarEvent{}, nil)
// SUT
s.Require().NoError(s.engine.Work(ctx))
s.Require().Len(s.calSvcMock.Calls, 1)
calledTime, ok := s.calSvcMock.Calls[0].Arguments[1].(time.Time)
s.Require().True(ok)
s.Assert().WithinDuration(time.Now().Add(-time.Hour), calledTime, 5*time.Second)
newLastChecked, isExist, err := s.lastChkdDao.GetLastChecked()
s.Require().NoError(err)
s.Assert().True(isExist)
s.Assert().WithinDuration(time.Now(), newLastChecked, 5*time.Second)
}
func (s *EngineSuite) TestReceiveEvents() {
ctx := context.Background()
lastChecked := time.Now().Add(-time.Minute)
s.Require().NoError(s.lastChkdDao.SetLastChecked(lastChecked))
notifiedEvent := CalendarEvent{
Title: "Should be notified",
Start: "start",
End: "end",
Creator: "someone else",
}
ignoredEvent := CalendarEvent{
Title: "Should be ignored",
Start: "start",
End: "end",
Creator: s.calendarId,
}
s.calSvcMock.On("GetRecentEvents", ctx, mock.Anything).Return([]CalendarEvent{
notifiedEvent,
ignoredEvent,
}, nil)
s.telCliMock.On("NotifyEvent", mock.Anything).Return(nil)
// SUT
s.Require().NoError(s.engine.Work(ctx))
s.Require().Len(s.calSvcMock.Calls, 1)
calledTime, ok := s.calSvcMock.Calls[0].Arguments[1].(time.Time)
s.Require().True(ok)
s.Assert().WithinDuration(lastChecked, calledTime, time.Second)
s.telCliMock.AssertNumberOfCalls(s.T(), "NotifyEvent", 1)
s.telCliMock.AssertCalled(s.T(), "NotifyEvent", notifiedEvent)
}