butler/internal/services/bot/bot_all/bot_test.go

155 lines
4.1 KiB
Go
Raw Normal View History

2023-08-13 13:22:15 +00:00
package bot_all
import (
"context"
"testing"
"go.uber.org/mock/gomock"
"git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger"
messenger_mocks "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger/mocks"
"git.3crabs.ru/VLADIMIR/butler/internal/modules/storage"
storage_mocks "git.3crabs.ru/VLADIMIR/butler/internal/modules/storage/mocks"
)
func Test_botAll_Process(t *testing.T) {
t.Parallel()
tests := []struct {
name string
messenger func(ctrl *gomock.Controller) messenger.IMessenger
storage func(ctrl *gomock.Controller) storage.IStorage
2023-08-13 14:13:17 +00:00
msg *messenger.Message
2023-08-13 13:22:15 +00:00
wantErr bool
}{
{
name: "hello message",
messenger: func(ctrl *gomock.Controller) messenger.IMessenger {
m := messenger_mocks.NewMockIMessenger(ctrl)
m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(0)
return m
},
storage: func(ctrl *gomock.Controller) storage.IStorage {
m := storage_mocks.NewMockIStorage(ctrl)
m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1)
m.EXPECT().GetAllUsersByChatID(gomock.Any(), gomock.Any()).Times(0)
return m
},
2023-08-13 14:13:17 +00:00
msg: &messenger.Message{
2023-08-13 13:22:15 +00:00
ChatID: "123",
UserID: "username",
Text: "hello",
},
wantErr: false,
},
2023-08-13 13:39:36 +00:00
{
name: "message with all and empty users",
messenger: func(ctrl *gomock.Controller) messenger.IMessenger {
m := messenger_mocks.NewMockIMessenger(ctrl)
m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(0)
return m
},
storage: func(ctrl *gomock.Controller) storage.IStorage {
m := storage_mocks.NewMockIStorage(ctrl)
m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1)
m.EXPECT().
GetAllUsersByChatID(gomock.Any(), gomock.Any()).
Return([]storage.User{{UserID: "username"}}, nil).
Times(1)
return m
},
2023-08-13 14:13:17 +00:00
msg: &messenger.Message{
2023-08-13 13:39:36 +00:00
ChatID: "123",
UserID: "username",
Text: "hello @all",
},
wantErr: false,
},
{
name: "message with all and one user",
messenger: func(ctrl *gomock.Controller) messenger.IMessenger {
m := messenger_mocks.NewMockIMessenger(ctrl)
m.EXPECT().
SendMessage(
gomock.Any(),
&messenger.Message{
ChatID: "123",
Text: "username2",
},
).
Times(1)
return m
},
storage: func(ctrl *gomock.Controller) storage.IStorage {
m := storage_mocks.NewMockIStorage(ctrl)
m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1)
m.EXPECT().
GetAllUsersByChatID(gomock.Any(), gomock.Any()).
Return([]storage.User{
{UserID: "username"},
{UserID: "username2"},
}, nil).
Times(1)
return m
},
2023-08-13 14:13:17 +00:00
msg: &messenger.Message{
2023-08-13 13:39:36 +00:00
ChatID: "123",
UserID: "username",
Text: "hello @all",
},
wantErr: false,
},
{
name: "message with all and one user",
messenger: func(ctrl *gomock.Controller) messenger.IMessenger {
m := messenger_mocks.NewMockIMessenger(ctrl)
m.EXPECT().
SendMessage(
gomock.Any(),
&messenger.Message{
ChatID: "123",
Text: "username2 username3",
},
).
Times(1)
return m
},
storage: func(ctrl *gomock.Controller) storage.IStorage {
m := storage_mocks.NewMockIStorage(ctrl)
m.EXPECT().UpsertUser(gomock.Any(), storage.User{ChatID: "123", UserID: "username"}).Times(1)
m.EXPECT().
GetAllUsersByChatID(gomock.Any(), gomock.Any()).
Return([]storage.User{
{UserID: "username"},
{UserID: "username2"},
{UserID: "username3"},
}, nil).
Times(1)
return m
},
2023-08-13 14:13:17 +00:00
msg: &messenger.Message{
2023-08-13 13:39:36 +00:00
ChatID: "123",
UserID: "username",
Text: "hello @all",
},
wantErr: false,
},
2023-08-13 13:22:15 +00:00
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
bot := &botAll{
messenger: tt.messenger(ctrl),
storage: tt.storage(ctrl),
}
if err := bot.Process(ctx, tt.msg); (err != nil) != tt.wantErr {
t.Errorf("botAll.Process() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}