package service import ( "log" "reflect" "strings" "testing" "app.yhyue.com/moapp/jybase/mysql" "app.yhyue.com/moapp/jybase/redis" IC "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/init" "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter" _ "github.com/go-sql-driver/mysql" ) func initMysql() { IC.BaseMysql = &mysql.Mysql{ Address: "192.168.3.217:4000", UserName: "root", PassWord: "=PDT49#80Z!RVv52_z", DBName: "base_service", MaxOpenConns: 5, MaxIdleConns: 5, } IC.BaseMysql.Init() IC.MainMysql = &mysql.Mysql{ Address: "192.168.3.149:3306", UserName: "root", PassWord: "Topnet123", DBName: "jianyu", MaxOpenConns: 5, MaxIdleConns: 5, } IC.MainMysql.Init() redis.InitRedisBySize(strings.Join([]string{"other=192.168.3.11:1712"}, ","), 100, 30, 300) } func init() { initMysql() } func TestMessaggeService_CloseChatSession(t *testing.T) { type args struct { in *messagecenter.CloseSessionReq } tests := []struct { name string args args wantErrorCode bool }{ // TODO: Add test cases. { name: "关闭会话", args: args{ in: &messagecenter.CloseSessionReq{ SessionId: 514, }, }}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} if gotErrorCode := b.CloseChatSession(tt.args.in); gotErrorCode != tt.wantErrorCode { t.Errorf("CloseChatSession() = %v, want %v", gotErrorCode, tt.wantErrorCode) } }) } } func TestMessaggeService_Count(t *testing.T) { type args struct { newUserId int64 userType int64 entUserId int64 } tests := []struct { name string args args wantCount int wantLast map[string]interface{} wantErr bool }{ // TODO: Add test cases. { name: "未读信息总数", args: args{ newUserId: 831, userType: 2, }, wantCount: 0, }, { name: "未读信息总数", args: args{ newUserId: 831, userType: 1, entUserId: 2, }, wantCount: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} gotCount, gotLast, err := b.Count(tt.args.newUserId) if (err != nil) != tt.wantErr { t.Errorf("Count() error = %v, wantErr %v", err, tt.wantErr) return } if gotCount != tt.wantCount { t.Errorf("Count() gotCount = %v, want %v", gotCount, tt.wantCount) } if !reflect.DeepEqual(gotLast, tt.wantLast) { t.Errorf("Count() gotLast = %v, want %v", gotLast, tt.wantLast) } }) } } func TestMessaggeService_CreateChatSession(t *testing.T) { type args struct { in *messagecenter.ChatSessionReq } tests := []struct { name string args args wantErrorCode bool wantSessionId int64 }{ // TODO: Add test cases. { name: "会话新建", args: args{ in: &messagecenter.ChatSessionReq{ EntId: 14184, UserId: 4962, CustomerServiceId: 3, AppId: "10000", CustomerserviceName: "客服1", }, }, wantSessionId: 0, wantErrorCode: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} gotErrorCode, gotSessionId := b.CreateChatSession(tt.args.in) if gotErrorCode != tt.wantErrorCode { t.Errorf("CreateChatSession() gotErrorCode = %v, want %v", gotErrorCode, tt.wantErrorCode) } if gotSessionId != tt.wantSessionId { t.Errorf("CreateChatSession() gotSessionId = %v, want %v", gotSessionId, tt.wantSessionId) } }) } } /* func TestMessaggeService_FindMessage(t *testing.T) { type args struct { in *messagecenter.MessageReq } tests := []struct { name string args args want *[]map[string]interface{} want1 int64 }{ // TODO: Add test cases. { name: "客服获取历史信息查询", args: args{in: &messagecenter.MessageReq{ MsgType: 5, UserType: 1, SendId: 10, PageSize: 10, NewUserId: 4338, EntId: 1, }}, want1: 0, }, { name: "客服获取历史信息查询升序", args: args{in: &messagecenter.MessageReq{ MsgType: 4, UserType: 1, SendId: 10, PageSize: 10, NewUserId: 10, EntId: 1, Sort: "asc", LastId: 18277, }}, want1: 0, }, { name: "客服获取历史信息查询降序", args: args{in: &messagecenter.MessageReq{ MsgType: 4, UserType: 1, SendId: 10, PageSize: 10, NewUserId: 10, EntId: 1, Sort: "desc", LastId: 18277, }}, want1: 0, }, { name: "用户获取客服历史信息查询", args: args{in: &messagecenter.MessageReq{ MsgType: 4, UserType: 2, SendId: 1, PageSize: 10, NewUserId: 10, EntId: 1, }}, want1: 0, }, { name: "用户获取历史信息查询", args: args{in: &messagecenter.MessageReq{ MsgType: 2, UserType: 2, SendId: 10, PageSize: 10, NewUserId: 10, }}, want1: 0, }, { name: "用户获取历史信息查询", args: args{in: &messagecenter.MessageReq{ MsgType: 5, UserType: 2, SendId: 1, PageSize: 10, NewUserId: 10, }}, want1: 0, }, { name: "用户获取历史信息查询", args: args{in: &messagecenter.MessageReq{ MsgType: 2, UserType: 2, SendId: 10, PageSize: 10, NewUserId: 831, EntId: 1, LastId: 200000, }}, want1: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} got := b.FindMessage(tt.args.in) if !reflect.DeepEqual(got, tt.want) { t.Errorf("FindMessage() got = %v, want %v", got, tt.want) } }) } } */ func TestMessaggeService_SaveAutoReplyMsg(t *testing.T) { type args struct { userType int64 entId int64 entUserId int64 userId int64 content string appId string nowFormat string } tests := []struct { name string args args want bool }{ // TODO: Add test cases. { name: "会话保存以及消息保存", args: args{ userType: 1, entId: 14184, entUserId: 3, userId: 831, content: "你与用户创建会话", appId: "10000", nowFormat: "2022-08-02 12:12:12", }, }, { name: "会话保存以及消息保存", args: args{ userType: 2, entId: 14184, entUserId: 3, userId: 831, content: "你与用户创建会话", appId: "10000", nowFormat: "2022-08-02 12:12:12", }, }, { name: "会话保存以及消息保存", args: args{ userType: 0, entId: 14184, entUserId: 3, userId: 831, content: "你与用户创建会话", appId: "10000", nowFormat: "2022-08-02 12:12:12", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { m := &MessaggeService{} if got, _ := m.SaveAutoReplyMsg(tt.args.userType, tt.args.entId, tt.args.entUserId, tt.args.userId, tt.args.content, tt.args.appId, tt.args.nowFormat); got != tt.want { t.Errorf("SaveAutoReplyMsg() = %v, want %v", got, tt.want) } }) } } func TestMessaggeService_SaveMessage(t *testing.T) { type args struct { in *messagecenter.MessageEntity } tests := []struct { name string args args wantErrorCode bool wantErrorMsg string wantContent string wantMessageId int64 }{ // TODO: Add test cases. {name: "1、用户给客服发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 5, Link: "1111", Appid: "10000", ReceiveId: 514, OwnType: int64(1), Type: 1, NewUserId: 10, }, }, }, {name: "2、用户给机器人发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 4, Link: "1111", Appid: "10000", ReceiveId: 514, OwnType: int64(1), Type: 1, NewUserId: 10, }, }, }, {name: "3、机器人给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 4, Link: "1111", Appid: "10000", SendId: 514, OwnType: int64(2), Type: 1, NewUserId: 10, }, }, }, {name: "4、人工客服给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 4, Link: "1111", Appid: "10000", ReceiveId: 10, SendId: 514, OwnType: int64(2), Type: 1, NewUserId: 10, }, }, }, {name: "5、用户给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗11111?", Item: 8, ItemType: 2, Link: "1111", Appid: "10000", ReceiveId: 831, OwnType: int64(2), Type: 1, NewUserId: 10, }, }, }, {name: "6、机器人给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 6, Link: "1111", Appid: "10000", ReceiveId: 514, OwnType: int64(1), Type: 1, NewUserId: 10, }, }, }, {name: "7、机器人给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗111?", Item: 8, ItemType: 6, Link: "1111", Appid: "10000", SendId: 514, OwnType: int64(2), ReceiveId: 10, Type: 1, NewUserId: 10, }, }}, {name: "8、机器人给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 6, Link: "1111", Appid: "10000", SendId: 2, OwnType: int64(2), Type: 1, NewUserId: 10, }, }}, {name: "9、机器人给用户发送信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 6, Link: "1111", Appid: "10000", SendId: 3, OwnType: int64(2), Type: 1, NewUserId: 10, }, }, }, {name: "10、机器人接受系统信息", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 6, Link: "1111", Appid: "10000", NewUserId: 10, OwnType: int64(3), Type: 1, ReceiveId: 514, }, }, }, {name: "11会话标识不存在", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 4, Link: "1111", Appid: "10000", ReceiveId: 640, OwnType: int64(1), Type: 1, NewUserId: 10, }, }, }, {name: "12会话标识不属于此用户", args: args{ in: &messagecenter.MessageEntity{ Title: "11", Content: "问在吗?", Item: 8, ItemType: 4, Link: "1111", Appid: "10000", ReceiveId: 514, OwnType: int64(1), Type: 1, NewUserId: 20, }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} gotErrorCode, gotErrorMsg, gotContent, gotMessageId, nowForm := b.SaveMessage(tt.args.in) log.Println(nowForm) if gotErrorCode != tt.wantErrorCode { t.Errorf("SaveMessage() gotErrorCode = %v, want %v", gotErrorCode, tt.wantErrorCode) } if gotErrorMsg != tt.wantErrorMsg { t.Errorf("SaveMessage() gotErrorMsg = %v, want %v", gotErrorMsg, tt.wantErrorMsg) } if gotContent != tt.wantContent { t.Errorf("SaveMessage() gotContent = %v, want %v", gotContent, tt.wantContent) } if gotMessageId != tt.wantMessageId { t.Errorf("SaveMessage() gotMessageId = %v, want %v", gotMessageId, tt.wantMessageId) } }) } } func TestMessaggeService_UserList(t *testing.T) { type args struct { in *messagecenter.UserReq } tests := []struct { name string args args wantData *[]map[string]interface{} wantErr bool }{ // TODO: Add test cases. { name: "客服查询用户列表", args: args{ in: &messagecenter.UserReq{ Phone: "", UserType: 1, StartTime: "2022-01-05 12:12:12", EndTime: "2022-10-05 12:12:12", NewUserId: 42941, EntUserId: 42711, }, }, }, { name: "用户列表用户查询", args: args{ in: &messagecenter.UserReq{ Phone: "", UserType: 2, StartTime: "", EndTime: "", NewUserId: 42941, EntUserId: 42711, }, }, }, { name: "客服用户列表用户查询", args: args{ in: &messagecenter.UserReq{ Phone: "", UserType: 1, StartTime: "", EndTime: "", NewUserId: 4338, EntUserId: 14929, }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} gotData, _, err := b.UserList(tt.args.in) if (err != nil) != tt.wantErr { t.Errorf("UserList() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(gotData, tt.wantData) { t.Errorf("UserList() gotData = %v, want %v", gotData, tt.wantData) } }) } } func TestMessaggeService_UpdateReadById(t *testing.T) { type args struct { in *messagecenter.ReadStateReq } tests := []struct { name string args args wantErrorCode bool }{ // TODO: Add test cases. { name: "已读状态修改", args: args{ in: &messagecenter.ReadStateReq{ MessageId: 11612, }, }, wantErrorCode: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} if gotErrorCode := b.UpdateReadById(tt.args.in); gotErrorCode != tt.wantErrorCode { t.Errorf("UpdateReadById() = %v, want %v", gotErrorCode, tt.wantErrorCode) } }) } } func TestChat(t *testing.T) { type args struct { in *messagecenter.MessageSaveReq } tests := []struct { name string args args wantErrorCode bool wantErrorMsg string wantContent string wantMessageId int64 }{ {name: "发送群聊", args: args{ in: &messagecenter.MessageSaveReq{ Title: "标题?", Content: "hello world1", Item: 8, MessageType: 1, Link: "", Appid: "10000", ReceiverIds: []int64{1, 2, 3}, GroupIds: []int64{8}, Type: 1, SendId: 333, }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := MessaggeService{} b.Chat(tt.args.in) }) } }