github.com/Go-To-Byte/DouSheng/user_center@v0.0.0-20230524130918-ad531c1a3f6a/apps/user/impl/user_test.go (about)

     1  // @Author: Ciusyan 2023/1/23
     2  package impl_test
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  	"github.com/infraboard/mcube/logger/zap"
     8  	"github.com/stretchr/testify/assert"
     9  	"testing"
    10  
    11  	"github.com/Go-To-Byte/DouSheng/dou_kit/conf"
    12  	"github.com/Go-To-Byte/DouSheng/dou_kit/ioc"
    13  
    14  	"github.com/Go-To-Byte/DouSheng/user_center/apps/user"
    15  	// 驱动加载所有需要放入IOC的实例
    16  	_ "github.com/Go-To-Byte/DouSheng/user_center/common/all"
    17  )
    18  
    19  var (
    20  	service user.ServiceServer
    21  )
    22  
    23  func TestRegister(t *testing.T) {
    24  	should := assert.New(t)
    25  	newUser := user.NewLoginAndRegisterRequest()
    26  	newUser.Username = "test001"
    27  	newUser.Password = "222222"
    28  	token, err := service.Register(context.Background(), newUser)
    29  
    30  	if should.NoError(err) {
    31  		fmt.Println(token)
    32  		fmt.Println(token.UserId)
    33  		fmt.Println(token.Token)
    34  	}
    35  }
    36  
    37  func TestLogin(t *testing.T) {
    38  	should := assert.New(t)
    39  	newUser := user.NewLoginAndRegisterRequest()
    40  	newUser.Username = "ciusyan"
    41  	newUser.Password = "222222"
    42  	token, err := service.Login(context.Background(), newUser)
    43  
    44  	if should.NoError(err) {
    45  		fmt.Println(token)
    46  		fmt.Println(token.UserId)
    47  		fmt.Println(token.Token)
    48  	}
    49  }
    50  
    51  func TestUserInfo(t *testing.T) {
    52  	should := assert.New(t)
    53  	req := user.NewUserInfoRequest()
    54  	req.Token = "xVXqrDdHbVG2uOKVE0BOnLj8"
    55  	req.UserId = 21
    56  	info, err := service.UserInfo(context.Background(), req)
    57  
    58  	if should.NoError(err) {
    59  		fmt.Println(info)
    60  		fmt.Println(info.User.IsFollow)
    61  	}
    62  }
    63  
    64  func BenchmarkUserServiceImpl_UserInfo(b *testing.B) {
    65  	req := user.NewUserInfoRequest()
    66  	req.Token = "kHdNO8b6zobfML4DF5WPuW7T"
    67  	req.UserId = 16
    68  	for i := 0; i < b.N; i++ {
    69  		_, _ = service.UserInfo(context.Background(), req)
    70  	}
    71  }
    72  
    73  func TestUserMap(t *testing.T) {
    74  	should := assert.New(t)
    75  	req := user.NewUserMapRequest()
    76  	req.Token = "kHdNO8b6zobfML4DF5WPuW7T"
    77  	req.UserIds = []int64{1, 2, 4, 16, 17, 18}
    78  	info, err := service.UserMap(context.Background(), req)
    79  
    80  	if should.NoError(err) {
    81  		fmt.Println(info)
    82  	}
    83  }
    84  
    85  func BenchmarkUserServiceImpl_UserMap(b *testing.B) {
    86  	req := user.NewUserMapRequest()
    87  	req.Token = "kHdNO8b6zobfML4DF5WPuW7T"
    88  	req.UserIds = []int64{1, 2, 4, 16, 17, 18}
    89  	for i := 0; i < b.N; i++ {
    90  		_, _ = service.UserMap(context.Background(), req)
    91  	}
    92  }
    93  
    94  func init() {
    95  
    96  	// 加载配置文件
    97  	if err := conf.LoadConfigFromToml("../../../etc/config.toml"); err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	// 初始化全局Logger
   102  	if err := zap.DevelopmentSetup(); err != nil {
   103  		panic(err)
   104  	}
   105  
   106  	// 初始化IOC容器
   107  	if err := ioc.InitAllDependencies(); err != nil {
   108  		panic(err)
   109  	}
   110  
   111  	// 从IOC中获取接口实现
   112  	service = ioc.GetGrpcDependency(user.AppName).(user.ServiceServer)
   113  }