|
@@ -0,0 +1,181 @@
|
|
|
+package contact
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "wechat-api/ent"
|
|
|
+ "wechat-api/ent/contact"
|
|
|
+ "wechat-api/ent/label"
|
|
|
+ "wechat-api/ent/labelrelationship"
|
|
|
+ "wechat-api/ent/wx"
|
|
|
+ "wechat-api/internal/svc"
|
|
|
+ "wechat-api/internal/types"
|
|
|
+ "wechat-api/internal/utils/dberrorhandler"
|
|
|
+
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
+ "github.com/stretchr/testify/mock"
|
|
|
+ "github.com/suyuan32/simple-admin-common/msg/errormsg"
|
|
|
+ "github.com/suyuan32/simple-admin-common/utils/pointy"
|
|
|
+)
|
|
|
+
|
|
|
+// MockContactClient is a mock implementation of the Contact client.
|
|
|
+type MockContactClient struct {
|
|
|
+ mock.Mock
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockContactClient) Query() *ent.ContactQuery {
|
|
|
+ args := m.Called()
|
|
|
+ return args.Get(0).(*ent.ContactQuery)
|
|
|
+}
|
|
|
+
|
|
|
+// MockContactQuery is a mock implementation of the Contact query.
|
|
|
+type MockContactQuery struct {
|
|
|
+ mock.Mock
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockContactQuery) Where(predicates ...predicate.Contact) *ent.ContactQuery {
|
|
|
+ args := m.Called(predicates)
|
|
|
+ return args.Get(0).(*ent.ContactQuery)
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockContactQuery) WithContactRelationships(f func(*ent.LabelRelationshipQuery)) *ent.ContactQuery {
|
|
|
+ args := m.Called(f)
|
|
|
+ return args.Get(0).(*ent.ContactQuery)
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockContactQuery) Page(ctx context.Context, page, pageSize int) (*ent.ContactPage, error) {
|
|
|
+ args := m.Called(ctx, page, pageSize)
|
|
|
+ return args.Get(0).(*ent.ContactPage), args.Error(1)
|
|
|
+}
|
|
|
+
|
|
|
+// MockWxClient is a mock implementation of the Wx client.
|
|
|
+type MockWxClient struct {
|
|
|
+ mock.Mock
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockWxClient) Query() *ent.WxQuery {
|
|
|
+ args := m.Called()
|
|
|
+ return args.Get(0).(*ent.WxQuery)
|
|
|
+}
|
|
|
+
|
|
|
+// MockWxQuery is a mock implementation of the Wx query.
|
|
|
+type MockWxQuery struct {
|
|
|
+ mock.Mock
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockWxQuery) Where(predicates ...predicate.Wx) *ent.WxQuery {
|
|
|
+ args := m.Called(predicates)
|
|
|
+ return args.Get(0).(*ent.WxQuery)
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MockWxQuery) All(ctx context.Context) ([]*ent.Wx, error) {
|
|
|
+ args := m.Called(ctx)
|
|
|
+ return args.Get(0).([]*ent.Wx), args.Error(1)
|
|
|
+}
|
|
|
+
|
|
|
+func TestGetContactList(t *testing.T) {
|
|
|
+ ctx := context.Background()
|
|
|
+ mockContactClient := new(MockContactClient)
|
|
|
+ mockWxClient := new(MockWxClient)
|
|
|
+ svcCtx := &svc.ServiceContext{
|
|
|
+ DB: &ent.Client{
|
|
|
+ Contact: mockContactClient,
|
|
|
+ Wx: mockWxClient,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ logic := NewGetContactListLogic(ctx, svcCtx)
|
|
|
+
|
|
|
+ // Mock the ContactQuery and WxQuery
|
|
|
+ mockContactQuery := new(MockContactQuery)
|
|
|
+ mockContactClient.On("Query").Return(mockContactQuery)
|
|
|
+
|
|
|
+ mockWxQuery := new(MockWxQuery)
|
|
|
+ mockWxClient.On("Query").Return(mockWxQuery)
|
|
|
+
|
|
|
+ // Prepare the expected data
|
|
|
+ expectedContact := &ent.Contact{
|
|
|
+ ID: 1,
|
|
|
+ CreatedAt: time.Now(),
|
|
|
+ UpdatedAt: time.Now(),
|
|
|
+ Status: 1,
|
|
|
+ WxWxid: "wxid_1",
|
|
|
+ Type: 1,
|
|
|
+ Wxid: "wxid_1",
|
|
|
+ Account: "account_1",
|
|
|
+ Nickname: "nickname_1",
|
|
|
+ Markname: "markname_1",
|
|
|
+ Headimg: "headimg_1",
|
|
|
+ Sex: 1,
|
|
|
+ Starrole: 1,
|
|
|
+ Dontseeit: 1,
|
|
|
+ Dontseeme: 1,
|
|
|
+ Lag: 1,
|
|
|
+ Gid: "gid_1",
|
|
|
+ Gname: "gname_1",
|
|
|
+ V3: 1,
|
|
|
+ Ctype: 1,
|
|
|
+ Cname: "cname_1",
|
|
|
+ Cage: 25,
|
|
|
+ Carea: "carea_1",
|
|
|
+ Cc: "cc_1",
|
|
|
+ Phone: "phone_1",
|
|
|
+ Cbirthday: "cbirthday_1",
|
|
|
+ Cbirtharea: "cbirtharea_1",
|
|
|
+ CidcardNo: "cidcardno_1",
|
|
|
+ Ctitle: "ctitle_1",
|
|
|
+ Edges: ent.ContactEdges{
|
|
|
+ ContactRelationships: []*ent.LabelRelationship{
|
|
|
+ {
|
|
|
+ LabelID: 1,
|
|
|
+ Edges: ent.LabelRelationshipEdges{
|
|
|
+ Labels: &ent.Label{
|
|
|
+ Name: "label_1",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ expectedWx := &ent.Wx{
|
|
|
+ Wxid: "wxid_1",
|
|
|
+ Nickname: "wx_nickname_1",
|
|
|
+ BlockList: []string{"block_1"},
|
|
|
+ GroupBlockList: []string{"group_block_1"},
|
|
|
+ }
|
|
|
+
|
|
|
+ expectedPage := &ent.ContactPage{
|
|
|
+ List: []*ent.Contact{expectedContact},
|
|
|
+ PageDetails: ent.PageDetails{
|
|
|
+ Total: 1,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ mockContactQuery.On("Where", mock.Anything).Return(mockContactQuery)
|
|
|
+ mockContactQuery.On("WithContactRelationships", mock.Anything).Return(mockContactQuery)
|
|
|
+ mockContactQuery.On("Page", ctx, 1, 10).Return(expectedPage, nil)
|
|
|
+
|
|
|
+ mockWxQuery.On("Where", mock.Anything).Return(mockWxQuery)
|
|
|
+ mockWxQuery.On("All", ctx).Return([]*ent.Wx{expectedWx}, nil)
|
|
|
+
|
|
|
+ // Call the GetContactList method
|
|
|
+ req := &types.ContactListReq{
|
|
|
+ Page: 1,
|
|
|
+ PageSize: 10,
|
|
|
+ }
|
|
|
+ resp, err := logic.GetContactList(req)
|
|
|
+
|
|
|
+ // Assertions
|
|
|
+ assert.NoError(t, err)
|
|
|
+ assert.Equal(t, errormsg.Success, resp.Msg)
|
|
|
+ assert.Equal(t, int64(1), resp.Data.Total)
|
|
|
+ assert.Len(t, resp.Data.Data, 1)
|
|
|
+ assert.Equal(t, expectedContact.ID, *resp.Data.Data[0].Id)
|
|
|
+ assert.Equal(t, expectedContact.WxWxid, *resp.Data.Data[0].WxWxid)
|
|
|
+ assert.Equal(t, expectedWx.Nickname, *resp.Data.Data[0].WxWxidNickname)
|
|
|
+ assert.Len(t, resp.Data.Data[0].LabelRelationships, 1)
|
|
|
+ assert.Equal(t, "label_1", *resp.Data.Data[0].LabelRelationships[0].Label)
|
|
|
+}
|