message_mail_box_test.go 14 KB

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