get_contact_list_logic_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package contact
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/label"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/wx"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "wechat-api/internal/utils/dberrorhandler"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/mock"
  16. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  17. "github.com/suyuan32/simple-admin-common/utils/pointy"
  18. )
  19. // MockContactClient is a mock implementation of the Contact client.
  20. type MockContactClient struct {
  21. mock.Mock
  22. }
  23. func (m *MockContactClient) Query() *ent.ContactQuery {
  24. args := m.Called()
  25. return args.Get(0).(*ent.ContactQuery)
  26. }
  27. // MockContactQuery is a mock implementation of the Contact query.
  28. type MockContactQuery struct {
  29. mock.Mock
  30. }
  31. func (m *MockContactQuery) Where(predicates ...predicate.Contact) *ent.ContactQuery {
  32. args := m.Called(predicates)
  33. return args.Get(0).(*ent.ContactQuery)
  34. }
  35. func (m *MockContactQuery) WithContactRelationships(f func(*ent.LabelRelationshipQuery)) *ent.ContactQuery {
  36. args := m.Called(f)
  37. return args.Get(0).(*ent.ContactQuery)
  38. }
  39. func (m *MockContactQuery) Page(ctx context.Context, page, pageSize int) (*ent.ContactPage, error) {
  40. args := m.Called(ctx, page, pageSize)
  41. return args.Get(0).(*ent.ContactPage), args.Error(1)
  42. }
  43. // MockWxClient is a mock implementation of the Wx client.
  44. type MockWxClient struct {
  45. mock.Mock
  46. }
  47. func (m *MockWxClient) Query() *ent.WxQuery {
  48. args := m.Called()
  49. return args.Get(0).(*ent.WxQuery)
  50. }
  51. // MockWxQuery is a mock implementation of the Wx query.
  52. type MockWxQuery struct {
  53. mock.Mock
  54. }
  55. func (m *MockWxQuery) Where(predicates ...predicate.Wx) *ent.WxQuery {
  56. args := m.Called(predicates)
  57. return args.Get(0).(*ent.WxQuery)
  58. }
  59. func (m *MockWxQuery) All(ctx context.Context) ([]*ent.Wx, error) {
  60. args := m.Called(ctx)
  61. return args.Get(0).([]*ent.Wx), args.Error(1)
  62. }
  63. func TestGetContactList(t *testing.T) {
  64. ctx := context.Background()
  65. mockContactClient := new(MockContactClient)
  66. mockWxClient := new(MockWxClient)
  67. svcCtx := &svc.ServiceContext{
  68. DB: &ent.Client{
  69. Contact: mockContactClient,
  70. Wx: mockWxClient,
  71. },
  72. }
  73. logic := NewGetContactListLogic(ctx, svcCtx)
  74. // Mock the ContactQuery and WxQuery
  75. mockContactQuery := new(MockContactQuery)
  76. mockContactClient.On("Query").Return(mockContactQuery)
  77. mockWxQuery := new(MockWxQuery)
  78. mockWxClient.On("Query").Return(mockWxQuery)
  79. // Prepare the expected data
  80. expectedContact := &ent.Contact{
  81. ID: 1,
  82. CreatedAt: time.Now(),
  83. UpdatedAt: time.Now(),
  84. Status: 1,
  85. WxWxid: "wxid_1",
  86. Type: 1,
  87. Wxid: "wxid_1",
  88. Account: "account_1",
  89. Nickname: "nickname_1",
  90. Markname: "markname_1",
  91. Headimg: "headimg_1",
  92. Sex: 1,
  93. Starrole: 1,
  94. Dontseeit: 1,
  95. Dontseeme: 1,
  96. Lag: 1,
  97. Gid: "gid_1",
  98. Gname: "gname_1",
  99. V3: 1,
  100. Ctype: 1,
  101. Cname: "cname_1",
  102. Cage: 25,
  103. Carea: "carea_1",
  104. Cc: "cc_1",
  105. Phone: "phone_1",
  106. Cbirthday: "cbirthday_1",
  107. Cbirtharea: "cbirtharea_1",
  108. CidcardNo: "cidcardno_1",
  109. Ctitle: "ctitle_1",
  110. Edges: ent.ContactEdges{
  111. ContactRelationships: []*ent.LabelRelationship{
  112. {
  113. LabelID: 1,
  114. Edges: ent.LabelRelationshipEdges{
  115. Labels: &ent.Label{
  116. Name: "label_1",
  117. },
  118. },
  119. },
  120. },
  121. },
  122. }
  123. expectedWx := &ent.Wx{
  124. Wxid: "wxid_1",
  125. Nickname: "wx_nickname_1",
  126. BlockList: []string{"block_1"},
  127. GroupBlockList: []string{"group_block_1"},
  128. }
  129. expectedPage := &ent.ContactPage{
  130. List: []*ent.Contact{expectedContact},
  131. PageDetails: ent.PageDetails{
  132. Total: 1,
  133. },
  134. }
  135. mockContactQuery.On("Where", mock.Anything).Return(mockContactQuery)
  136. mockContactQuery.On("WithContactRelationships", mock.Anything).Return(mockContactQuery)
  137. mockContactQuery.On("Page", ctx, 1, 10).Return(expectedPage, nil)
  138. mockWxQuery.On("Where", mock.Anything).Return(mockWxQuery)
  139. mockWxQuery.On("All", ctx).Return([]*ent.Wx{expectedWx}, nil)
  140. // Call the GetContactList method
  141. req := &types.ContactListReq{
  142. Page: 1,
  143. PageSize: 10,
  144. }
  145. resp, err := logic.GetContactList(req)
  146. // Assertions
  147. assert.NoError(t, err)
  148. assert.Equal(t, errormsg.Success, resp.Msg)
  149. assert.Equal(t, int64(1), resp.Data.Total)
  150. assert.Len(t, resp.Data.Data, 1)
  151. assert.Equal(t, expectedContact.ID, *resp.Data.Data[0].Id)
  152. assert.Equal(t, expectedContact.WxWxid, *resp.Data.Data[0].WxWxid)
  153. assert.Equal(t, expectedWx.Nickname, *resp.Data.Data[0].WxWxidNickname)
  154. assert.Len(t, resp.Data.Data[0].LabelRelationships, 1)
  155. assert.Equal(t, "label_1", *resp.Data.Data[0].LabelRelationships[0].Label)
  156. }