From 1e3f5eb91d427e7ad884dfe9efbca719e3ac1fc8 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sun, 13 Aug 2023 20:39:36 +0700 Subject: [PATCH] add test --- internal/services/bot/bot_all/bot.go | 7 +- internal/services/bot/bot_all/bot_test.go | 92 +++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/internal/services/bot/bot_all/bot.go b/internal/services/bot/bot_all/bot.go index c041f63..1739902 100644 --- a/internal/services/bot/bot_all/bot.go +++ b/internal/services/bot/bot_all/bot.go @@ -37,8 +37,11 @@ func (bot *botAll) Process(ctx context.Context, msg messenger.Message) error { if err != nil { return err } - usernames := lo.Map(users, func(item storage.User, _ int) string { - return item.UserID + usernames := lo.FilterMap(users, func(item storage.User, _ int) (string, bool) { + if item.UserID == msg.UserID { + return "", false + } + return item.UserID, true }) if len(usernames) > 0 { bot.messenger.SendMessage( diff --git a/internal/services/bot/bot_all/bot_test.go b/internal/services/bot/bot_all/bot_test.go index 163c42b..f60298a 100644 --- a/internal/services/bot/bot_all/bot_test.go +++ b/internal/services/bot/bot_all/bot_test.go @@ -41,6 +41,98 @@ func Test_botAll_Process(t *testing.T) { }, wantErr: false, }, + { + 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 + }, + msg: messenger.Message{ + 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 + }, + msg: messenger.Message{ + 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 + }, + msg: messenger.Message{ + ChatID: "123", + UserID: "username", + Text: "hello @all", + }, + wantErr: false, + }, } for _, tt := range tests { tt := tt