dao_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package rpc
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "go-common/app/admin/main/aegis/conf"
  9. api "go-common/app/service/main/account/api"
  10. relmod "go-common/app/service/main/relation/model"
  11. uprpc "go-common/app/service/main/up/api/v1"
  12. "github.com/golang/mock/gomock"
  13. )
  14. var (
  15. d *Dao
  16. cntx context.Context
  17. )
  18. func TestMain(m *testing.M) {
  19. if os.Getenv("DEPLOY_ENV") != "" {
  20. flag.Set("app_id", "main.archive.aegis-admin")
  21. flag.Set("conf_token", "cad913269be022e1eb8c45a8d5408d78")
  22. flag.Set("tree_id", "60977")
  23. flag.Set("conf_version", "1")
  24. flag.Set("deploy_env", "uat")
  25. flag.Set("conf_host", "config.bilibili.co")
  26. flag.Set("conf_path", "/tmp")
  27. flag.Set("region", "sh")
  28. flag.Set("zone", "sh001")
  29. } else {
  30. flag.Set("conf", "../../cmd/aegis-admin.toml")
  31. }
  32. flag.Parse()
  33. if err := conf.Init(); err != nil {
  34. panic(fmt.Sprintf("conf.Init() error(%v)", err))
  35. }
  36. d = New(conf.Conf)
  37. cntx = context.TODO()
  38. os.Exit(m.Run())
  39. }
  40. var (
  41. _Mid = int64(0)
  42. _Mids = []int64{0, 1, 2}
  43. )
  44. func WithDao(t *testing.T, f func(d *Dao)) func() {
  45. return func() {
  46. mockCtrl := gomock.NewController(t)
  47. accMock := NewMockAccRPC(mockCtrl)
  48. d.AccountClient = accMock
  49. accMock.EXPECT().Cards3(gomock.Any(), &api.MidsReq{Mids: _Mids}).Return(&api.CardsReply{Cards: map[int64]*api.Card{
  50. 10086: {
  51. Mid: _Mid,
  52. },
  53. }}, nil).AnyTimes()
  54. relMock := NewMockRelationRPC(mockCtrl)
  55. d.relRPC = relMock
  56. relMock.EXPECT().Stats(gomock.Any(), &relmod.ArgMids{Mids: _Mids}).Return(map[int64]*relmod.Stat{
  57. 10086: {
  58. Mid: 10086,
  59. },
  60. }, nil)
  61. f(d)
  62. mockCtrl.Finish()
  63. }
  64. }
  65. func WithMockAccount(t *testing.T, f func(d *Dao)) func() {
  66. return func() {
  67. mockCtrl := gomock.NewController(t)
  68. defer mockCtrl.Finish()
  69. accMock := NewMockAccRPC(mockCtrl)
  70. d.AccountClient = accMock
  71. accMock.EXPECT().Info3(gomock.Any(), &api.MidReq{Mid: _Mid}).Return(&api.InfoReply{Info: &api.Info{
  72. Mid: 10086,
  73. }}, nil).AnyTimes()
  74. accMock.EXPECT().Cards3(gomock.Any(), &api.MidsReq{Mids: _Mids}).Return(&api.CardsReply{Cards: map[int64]*api.Card{
  75. 10086: {
  76. Mid: _Mid,
  77. },
  78. }}, nil).AnyTimes()
  79. accMock.EXPECT().ProfileWithStat3(gomock.Any(), &api.MidReq{Mid: _Mid}).Return(&api.ProfileStatReply{
  80. Profile: &api.Profile{
  81. Mid: _Mid,
  82. },
  83. }, nil).AnyTimes()
  84. f(d)
  85. }
  86. }
  87. func WithMockRelation(t *testing.T, f func(d *Dao)) func() {
  88. return func() {
  89. mockCtrl := gomock.NewController(t)
  90. defer mockCtrl.Finish()
  91. relMock := NewMockRelationRPC(mockCtrl)
  92. d.relRPC = relMock
  93. relMock.EXPECT().Stats(gomock.Any(), &relmod.ArgMids{Mids: _Mids}).Return(map[int64]*relmod.Stat{
  94. 10086: {
  95. Mid: 10086,
  96. },
  97. }, nil)
  98. f(d)
  99. }
  100. }
  101. func WithMockUp(t *testing.T, f func(d *Dao)) func() {
  102. return func() {
  103. upspecial := &uprpc.UpSpecial{GroupIDs: []int64{1}}
  104. upspecialreply := &uprpc.UpSpecialReply{UpSpecial: upspecial}
  105. upspecialsreply := &uprpc.UpsSpecialReply{UpSpecials: map[int64]*uprpc.UpSpecial{0: upspecial}}
  106. upgroupsreply := &uprpc.UpGroupsReply{UpGroups: map[int64]*uprpc.UpGroup{0: {ID: 0}}}
  107. mockCtrl := gomock.NewController(t)
  108. defer mockCtrl.Finish()
  109. upMock := NewMockUpRPC(mockCtrl)
  110. d.UpClient = upMock
  111. upMock.EXPECT().UpSpecial(gomock.Any(), &uprpc.UpSpecialReq{Mid: _Mid}).Return(upspecialreply, nil)
  112. upMock.EXPECT().UpsSpecial(gomock.Any(), &uprpc.UpsSpecialReq{Mids: _Mids}).Return(upspecialsreply, nil)
  113. upMock.EXPECT().UpGroups(gomock.Any(), &uprpc.NoArgReq{}).Return(upgroupsreply, nil)
  114. f(d)
  115. }
  116. }