74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"testing"
|
||
|
|
||
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||
|
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
|
||
|
"github.com/charmbracelet/log"
|
||
|
"github.com/golang/mock/gomock"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
type TestHelper struct {
|
||
|
t *testing.T
|
||
|
ctrl *gomock.Controller
|
||
|
mockNotificationService *notification.MockNotificationService
|
||
|
mockWorker *MockWorker
|
||
|
srv *Server
|
||
|
}
|
||
|
|
||
|
func testConfig(t *testing.T) *model.Config {
|
||
|
f, err := ioutil.TempFile("", "birthdaybot-")
|
||
|
require.NoError(t, err)
|
||
|
require.NoError(t, f.Close())
|
||
|
require.NoError(t, os.Remove(f.Name()))
|
||
|
|
||
|
return &model.Config{BirthdayFile: f.Name()}
|
||
|
}
|
||
|
|
||
|
func SetupTestHelper(t *testing.T) *TestHelper {
|
||
|
th := &TestHelper{t: t}
|
||
|
th.ctrl = gomock.NewController(t)
|
||
|
|
||
|
th.mockNotificationService = notification.NewMockNotificationService(th.ctrl)
|
||
|
notificationServices := []notification.NotificationService{th.mockNotificationService}
|
||
|
|
||
|
th.mockWorker = NewMockWorker(th.ctrl)
|
||
|
workers := []Worker{th.mockWorker}
|
||
|
th.mockWorker.EXPECT().Start().Times(1)
|
||
|
th.mockWorker.EXPECT().Stop().Times(1)
|
||
|
|
||
|
birthdays := []*model.Birthday{
|
||
|
{
|
||
|
Name: "John",
|
||
|
Email: "john@doe.com",
|
||
|
Phone: "1234",
|
||
|
YearOfBirth: 2022,
|
||
|
MonthOfBirth: 1,
|
||
|
DayOfBirth: 1,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var err error
|
||
|
th.srv, err = New(
|
||
|
WithConfig(testConfig(t)),
|
||
|
WithLogger(log.New(os.Stderr)),
|
||
|
WithBirthdays(birthdays),
|
||
|
WithNotificationServices(notificationServices),
|
||
|
WithWorkers(workers),
|
||
|
)
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
th.srv.Start()
|
||
|
|
||
|
return th
|
||
|
}
|
||
|
|
||
|
func (th *TestHelper) TearDown() {
|
||
|
th.srv.Stop()
|
||
|
th.ctrl.Finish()
|
||
|
}
|