birthdaybot/server/helpers_test.go

81 lines
2 KiB
Go
Raw Normal View History

package server
import (
"io/ioutil"
"os"
"testing"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
2023-07-10 12:43:59 +01:00
notification_mocks "git.ctrlz.es/mgdelacroix/birthdaybot/notification/mocks"
server_mocks "git.ctrlz.es/mgdelacroix/birthdaybot/server/mocks"
"github.com/charmbracelet/log"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)
type TestHelper struct {
t *testing.T
ctrl *gomock.Controller
2023-07-10 12:43:59 +01:00
mockNotificationService *notification_mocks.MockNotificationService
mockWorker *server_mocks.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()))
2023-07-11 12:27:50 +01:00
return &model.Config{
Web: &model.WebConfig{Enabled: true, Port: 0},
2023-07-11 12:27:50 +01:00
Birthdays: &model.BirthdaysConfig{File: f.Name()},
}
}
func SetupTestHelper(t *testing.T, opts ...Option) *TestHelper {
th := &TestHelper{t: t}
th.ctrl = gomock.NewController(t)
2023-07-10 12:43:59 +01:00
th.mockNotificationService = notification_mocks.NewMockNotificationService(th.ctrl)
notificationServices := []notification.NotificationService{th.mockNotificationService}
2023-07-10 12:43:59 +01:00
th.mockWorker = server_mocks.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,
},
}
serverOpts := append([]Option{
WithConfig(testConfig(t)),
WithLogger(log.New(os.Stderr)),
WithBirthdays(birthdays),
WithNotificationServices(notificationServices),
WithWorkers(workers),
}, opts...)
var err error
th.srv, err = New(serverOpts...)
require.NoError(t, err)
th.srv.Start()
return th
}
func (th *TestHelper) TearDown() {
th.srv.Stop()
th.ctrl.Finish()
}