message_mail_box_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/mysql"
  4. "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
  5. "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
  6. "reflect"
  7. "testing"
  8. )
  9. func initMysql() {
  10. entity.Mysql = &mysql.Mysql{
  11. Address: "192.168.3.217:4000",
  12. UserName: "root",
  13. PassWord: "=PDT49#80Z!RVv52_z",
  14. DBName: "base_service",
  15. MaxOpenConns: 5,
  16. MaxIdleConns: 5,
  17. }
  18. entity.Mysql.Init()
  19. }
  20. func init() {
  21. initMysql()
  22. }
  23. func TestMessaggeService_CloseChatSession(t *testing.T) {
  24. type args struct {
  25. in *messagecenter.CloseSessionReq
  26. }
  27. tests := []struct {
  28. name string
  29. args args
  30. wantErrorCode bool
  31. }{
  32. // TODO: Add test cases.
  33. {
  34. name: "关闭会话",
  35. args: args{
  36. in: &messagecenter.CloseSessionReq{
  37. SessionId: 514,
  38. },
  39. }},
  40. }
  41. for _, tt := range tests {
  42. t.Run(tt.name, func(t *testing.T) {
  43. b := MessaggeService{}
  44. if gotErrorCode := b.CloseChatSession(tt.args.in); gotErrorCode != tt.wantErrorCode {
  45. t.Errorf("CloseChatSession() = %v, want %v", gotErrorCode, tt.wantErrorCode)
  46. }
  47. })
  48. }
  49. }
  50. func TestMessaggeService_Count(t *testing.T) {
  51. type args struct {
  52. newUserId int64
  53. userType int64
  54. entUserId int64
  55. }
  56. tests := []struct {
  57. name string
  58. args args
  59. wantCount int
  60. wantLast map[string]interface{}
  61. wantErr bool
  62. }{
  63. // TODO: Add test cases.
  64. {
  65. name: "未读信息总数",
  66. args: args{
  67. newUserId: 831,
  68. userType: 2,
  69. },
  70. wantCount: 0,
  71. },
  72. {
  73. name: "未读信息总数",
  74. args: args{
  75. newUserId: 831,
  76. userType: 1,
  77. entUserId: 2,
  78. },
  79. wantCount: 0,
  80. },
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.name, func(t *testing.T) {
  84. b := MessaggeService{}
  85. gotCount, gotLast, err := b.Count(tt.args.newUserId, tt.args.userType, tt.args.entUserId)
  86. if (err != nil) != tt.wantErr {
  87. t.Errorf("Count() error = %v, wantErr %v", err, tt.wantErr)
  88. return
  89. }
  90. if gotCount != tt.wantCount {
  91. t.Errorf("Count() gotCount = %v, want %v", gotCount, tt.wantCount)
  92. }
  93. if !reflect.DeepEqual(gotLast, tt.wantLast) {
  94. t.Errorf("Count() gotLast = %v, want %v", gotLast, tt.wantLast)
  95. }
  96. })
  97. }
  98. }
  99. func TestMessaggeService_CreateChatSession(t *testing.T) {
  100. type args struct {
  101. in *messagecenter.ChatSessionReq
  102. }
  103. tests := []struct {
  104. name string
  105. args args
  106. wantErrorCode bool
  107. wantSessionId int64
  108. }{
  109. // TODO: Add test cases.
  110. {
  111. name: "会话新建",
  112. args: args{
  113. in: &messagecenter.ChatSessionReq{
  114. EntId: 14184,
  115. UserId: 4962,
  116. CustomerServiceId: 3,
  117. AppId: "10000",
  118. CustomerserviceName: "客服1",
  119. },
  120. },
  121. wantSessionId: 0,
  122. wantErrorCode: true,
  123. },
  124. }
  125. for _, tt := range tests {
  126. t.Run(tt.name, func(t *testing.T) {
  127. b := MessaggeService{}
  128. gotErrorCode, gotSessionId := b.CreateChatSession(tt.args.in)
  129. if gotErrorCode != tt.wantErrorCode {
  130. t.Errorf("CreateChatSession() gotErrorCode = %v, want %v", gotErrorCode, tt.wantErrorCode)
  131. }
  132. if gotSessionId != tt.wantSessionId {
  133. t.Errorf("CreateChatSession() gotSessionId = %v, want %v", gotSessionId, tt.wantSessionId)
  134. }
  135. })
  136. }
  137. }
  138. func TestMessaggeService_FindMessage(t *testing.T) {
  139. type args struct {
  140. in *messagecenter.MessageReq
  141. }
  142. tests := []struct {
  143. name string
  144. args args
  145. want *[]map[string]interface{}
  146. want1 int64
  147. }{
  148. // TODO: Add test cases.
  149. {
  150. name: "客服获取历史信息查询",
  151. args: args{in: &messagecenter.MessageReq{
  152. MsgType: 4,
  153. UserType: 1,
  154. SendId: 10,
  155. PageSize: 10,
  156. NewUserId: 10,
  157. EntId: 1,
  158. }},
  159. want1: 0,
  160. }, {
  161. name: "客服获取历史信息查询升序",
  162. args: args{in: &messagecenter.MessageReq{
  163. MsgType: 4,
  164. UserType: 1,
  165. SendId: 10,
  166. PageSize: 10,
  167. NewUserId: 10,
  168. EntId: 1,
  169. Sort: "asc",
  170. LastId: 18277,
  171. }},
  172. want1: 0,
  173. },
  174. {
  175. name: "客服获取历史信息查询降序",
  176. args: args{in: &messagecenter.MessageReq{
  177. MsgType: 4,
  178. UserType: 1,
  179. SendId: 10,
  180. PageSize: 10,
  181. NewUserId: 10,
  182. EntId: 1,
  183. Sort: "desc",
  184. LastId: 18277,
  185. }},
  186. want1: 0,
  187. },
  188. {
  189. name: "用户获取客服历史信息查询",
  190. args: args{in: &messagecenter.MessageReq{
  191. MsgType: 4,
  192. UserType: 2,
  193. SendId: 1,
  194. PageSize: 10,
  195. NewUserId: 10,
  196. EntId: 1,
  197. }},
  198. want1: 0,
  199. },
  200. {
  201. name: "用户获取历史信息查询",
  202. args: args{in: &messagecenter.MessageReq{
  203. MsgType: 2,
  204. UserType: 2,
  205. SendId: 10,
  206. PageSize: 10,
  207. NewUserId: 10,
  208. }},
  209. want1: 0,
  210. }, {
  211. name: "用户获取历史信息查询",
  212. args: args{in: &messagecenter.MessageReq{
  213. MsgType: 5,
  214. UserType: 2,
  215. SendId: 1,
  216. PageSize: 10,
  217. NewUserId: 10,
  218. }},
  219. want1: 0,
  220. },
  221. {
  222. name: "用户获取历史信息查询",
  223. args: args{in: &messagecenter.MessageReq{
  224. MsgType: 2,
  225. UserType: 2,
  226. SendId: 57683,
  227. PageSize: 10,
  228. NewUserId: 10,
  229. EntId: 1,
  230. LastId: 200000,
  231. }},
  232. want1: 0,
  233. },
  234. }
  235. for _, tt := range tests {
  236. t.Run(tt.name, func(t *testing.T) {
  237. b := MessaggeService{}
  238. got, got1 := b.FindMessage(tt.args.in)
  239. if !reflect.DeepEqual(got, tt.want) {
  240. t.Errorf("FindMessage() got = %v, want %v", got, tt.want)
  241. }
  242. if got1 != tt.want1 {
  243. t.Errorf("FindMessage() got1 = %v, want %v", got1, tt.want1)
  244. }
  245. })
  246. }
  247. }
  248. func TestMessaggeService_SaveAutoReplyMsg(t *testing.T) {
  249. type args struct {
  250. userType int64
  251. entId int64
  252. entUserId int64
  253. userId int64
  254. content string
  255. appId string
  256. nowFormat string
  257. }
  258. tests := []struct {
  259. name string
  260. args args
  261. want bool
  262. }{
  263. // TODO: Add test cases.
  264. {
  265. name: "会话保存以及消息保存",
  266. args: args{
  267. userType: 1,
  268. entId: 14184,
  269. entUserId: 3,
  270. userId: 831,
  271. content: "你与用户创建会话",
  272. appId: "10000",
  273. nowFormat: "2022-08-02 12:12:12",
  274. },
  275. },
  276. {
  277. name: "会话保存以及消息保存",
  278. args: args{
  279. userType: 2,
  280. entId: 14184,
  281. entUserId: 3,
  282. userId: 831,
  283. content: "你与用户创建会话",
  284. appId: "10000",
  285. nowFormat: "2022-08-02 12:12:12",
  286. },
  287. },
  288. {
  289. name: "会话保存以及消息保存",
  290. args: args{
  291. userType: 0,
  292. entId: 14184,
  293. entUserId: 3,
  294. userId: 831,
  295. content: "你与用户创建会话",
  296. appId: "10000",
  297. nowFormat: "2022-08-02 12:12:12",
  298. },
  299. },
  300. }
  301. for _, tt := range tests {
  302. t.Run(tt.name, func(t *testing.T) {
  303. m := &MessaggeService{}
  304. 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 {
  305. t.Errorf("SaveAutoReplyMsg() = %v, want %v", got, tt.want)
  306. }
  307. })
  308. }
  309. }
  310. func TestMessaggeService_SaveMessage(t *testing.T) {
  311. type args struct {
  312. in *messagecenter.MessageEntity
  313. }
  314. tests := []struct {
  315. name string
  316. args args
  317. wantErrorCode bool
  318. wantErrorMsg string
  319. wantContent string
  320. wantMessageId int64
  321. }{
  322. // TODO: Add test cases.
  323. {name: "1、用户给客服发送信息",
  324. args: args{
  325. in: &messagecenter.MessageEntity{
  326. Title: "11",
  327. Content: "问在吗?",
  328. Item: 8,
  329. ItemType: 5,
  330. Link: "1111",
  331. Appid: "10000",
  332. ReceiveId: 514,
  333. OwnType: int64(1),
  334. Type: 1,
  335. NewUserId: 10,
  336. },
  337. },
  338. }, {name: "2、用户给机器人发送信息",
  339. args: args{
  340. in: &messagecenter.MessageEntity{
  341. Title: "11",
  342. Content: "问在吗?",
  343. Item: 8,
  344. ItemType: 4,
  345. Link: "1111",
  346. Appid: "10000",
  347. ReceiveId: 514,
  348. OwnType: int64(1),
  349. Type: 1,
  350. NewUserId: 10,
  351. },
  352. },
  353. }, {name: "3、机器人给用户发送信息",
  354. args: args{
  355. in: &messagecenter.MessageEntity{
  356. Title: "11",
  357. Content: "问在吗?",
  358. Item: 8,
  359. ItemType: 4,
  360. Link: "1111",
  361. Appid: "10000",
  362. SendId: 514,
  363. OwnType: int64(2),
  364. Type: 1,
  365. NewUserId: 10,
  366. },
  367. },
  368. }, {name: "4、人工客服给用户发送信息",
  369. args: args{
  370. in: &messagecenter.MessageEntity{
  371. Title: "11",
  372. Content: "问在吗?",
  373. Item: 8,
  374. ItemType: 4,
  375. Link: "1111",
  376. Appid: "10000",
  377. ReceiveId: 10,
  378. SendId: 514,
  379. OwnType: int64(2),
  380. Type: 1,
  381. NewUserId: 10,
  382. },
  383. },
  384. }, {name: "5、用户给用户发送信息",
  385. args: args{
  386. in: &messagecenter.MessageEntity{
  387. Title: "11",
  388. Content: "问在吗?",
  389. Item: 8,
  390. ItemType: 2,
  391. Link: "1111",
  392. Appid: "10000",
  393. ReceiveId: 57683,
  394. OwnType: int64(2),
  395. Type: 1,
  396. NewUserId: 10,
  397. },
  398. },
  399. }, {name: "6、机器人给用户发送信息",
  400. args: args{
  401. in: &messagecenter.MessageEntity{
  402. Title: "11",
  403. Content: "问在吗?",
  404. Item: 8,
  405. ItemType: 6,
  406. Link: "1111",
  407. Appid: "10000",
  408. ReceiveId: 514,
  409. OwnType: int64(1),
  410. Type: 1,
  411. NewUserId: 10,
  412. },
  413. },
  414. },
  415. {name: "7、机器人给用户发送信息",
  416. args: args{
  417. in: &messagecenter.MessageEntity{
  418. Title: "11",
  419. Content: "问在吗?",
  420. Item: 8,
  421. ItemType: 6,
  422. Link: "1111",
  423. Appid: "10000",
  424. SendId: 514,
  425. OwnType: int64(2),
  426. ReceiveId: 10,
  427. Type: 1,
  428. NewUserId: 10,
  429. },
  430. }},
  431. {name: "7、机器人给用户发送信息",
  432. args: args{
  433. in: &messagecenter.MessageEntity{
  434. Title: "11",
  435. Content: "问在吗?",
  436. Item: 8,
  437. ItemType: 6,
  438. Link: "1111",
  439. Appid: "10000",
  440. SendId: 2,
  441. OwnType: int64(2),
  442. Type: 1,
  443. NewUserId: 10,
  444. },
  445. }},
  446. {name: "7、机器人给用户发送信息",
  447. args: args{
  448. in: &messagecenter.MessageEntity{
  449. Title: "11",
  450. Content: "问在吗?",
  451. Item: 8,
  452. ItemType: 6,
  453. Link: "1111",
  454. Appid: "10000",
  455. SendId: 3,
  456. OwnType: int64(2),
  457. Type: 1,
  458. NewUserId: 10,
  459. },
  460. },
  461. },
  462. {name: "会话标识不存在",
  463. args: args{
  464. in: &messagecenter.MessageEntity{
  465. Title: "11",
  466. Content: "问在吗?",
  467. Item: 8,
  468. ItemType: 4,
  469. Link: "1111",
  470. Appid: "10000",
  471. ReceiveId: 640,
  472. OwnType: int64(1),
  473. Type: 1,
  474. NewUserId: 10,
  475. },
  476. },
  477. },
  478. {name: "会话标识不属于此用户",
  479. args: args{
  480. in: &messagecenter.MessageEntity{
  481. Title: "11",
  482. Content: "问在吗?",
  483. Item: 8,
  484. ItemType: 4,
  485. Link: "1111",
  486. Appid: "10000",
  487. ReceiveId: 514,
  488. OwnType: int64(1),
  489. Type: 1,
  490. NewUserId: 20,
  491. },
  492. },
  493. },
  494. }
  495. for _, tt := range tests {
  496. t.Run(tt.name, func(t *testing.T) {
  497. b := MessaggeService{}
  498. gotErrorCode, gotErrorMsg, gotContent, gotMessageId := b.SaveMessage(tt.args.in)
  499. if gotErrorCode != tt.wantErrorCode {
  500. t.Errorf("SaveMessage() gotErrorCode = %v, want %v", gotErrorCode, tt.wantErrorCode)
  501. }
  502. if gotErrorMsg != tt.wantErrorMsg {
  503. t.Errorf("SaveMessage() gotErrorMsg = %v, want %v", gotErrorMsg, tt.wantErrorMsg)
  504. }
  505. if gotContent != tt.wantContent {
  506. t.Errorf("SaveMessage() gotContent = %v, want %v", gotContent, tt.wantContent)
  507. }
  508. if gotMessageId != tt.wantMessageId {
  509. t.Errorf("SaveMessage() gotMessageId = %v, want %v", gotMessageId, tt.wantMessageId)
  510. }
  511. })
  512. }
  513. }
  514. func TestMessaggeService_UserList(t *testing.T) {
  515. type args struct {
  516. in *messagecenter.UserReq
  517. }
  518. tests := []struct {
  519. name string
  520. args args
  521. wantData *[]map[string]interface{}
  522. wantErr bool
  523. }{
  524. // TODO: Add test cases.
  525. {
  526. name: "客服查询用户列表",
  527. args: args{
  528. in: &messagecenter.UserReq{
  529. Phone: "13164329511",
  530. UserType: 1,
  531. StartTime: "2022-01-05 12:12:12",
  532. EndTime: "2022-01-05 12:12:12",
  533. NewUserId: 42941,
  534. EntUserId: 42711,
  535. },
  536. },
  537. }, {
  538. name: "用户列表用户查询",
  539. args: args{
  540. in: &messagecenter.UserReq{
  541. Phone: "",
  542. UserType: 2,
  543. StartTime: "",
  544. EndTime: "",
  545. NewUserId: 42941,
  546. EntUserId: 42711,
  547. },
  548. },
  549. }, {
  550. name: "客服用户列表用户查询",
  551. args: args{
  552. in: &messagecenter.UserReq{
  553. Phone: "",
  554. UserType: 1,
  555. StartTime: "",
  556. EndTime: "",
  557. NewUserId: 4338,
  558. EntUserId: 14929,
  559. },
  560. },
  561. },
  562. }
  563. for _, tt := range tests {
  564. t.Run(tt.name, func(t *testing.T) {
  565. b := MessaggeService{}
  566. gotData, err := b.UserList(tt.args.in)
  567. if (err != nil) != tt.wantErr {
  568. t.Errorf("UserList() error = %v, wantErr %v", err, tt.wantErr)
  569. return
  570. }
  571. if !reflect.DeepEqual(gotData, tt.wantData) {
  572. t.Errorf("UserList() gotData = %v, want %v", gotData, tt.wantData)
  573. }
  574. })
  575. }
  576. }
  577. func TestMessaggeService_UpdateReadById(t *testing.T) {
  578. type args struct {
  579. in *messagecenter.ReadStateReq
  580. }
  581. tests := []struct {
  582. name string
  583. args args
  584. wantErrorCode bool
  585. }{
  586. // TODO: Add test cases.
  587. {
  588. name: "已读状态修改",
  589. args: args{
  590. in: &messagecenter.ReadStateReq{
  591. MessageId: 11612,
  592. },
  593. },
  594. wantErrorCode: true,
  595. },
  596. }
  597. for _, tt := range tests {
  598. t.Run(tt.name, func(t *testing.T) {
  599. b := MessaggeService{}
  600. if gotErrorCode := b.UpdateReadById(tt.args.in); gotErrorCode != tt.wantErrorCode {
  601. t.Errorf("UpdateReadById() = %v, want %v", gotErrorCode, tt.wantErrorCode)
  602. }
  603. })
  604. }
  605. }