dao_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package account
  2. import (
  3. "context"
  4. "flag"
  5. accapi "go-common/app/service/main/account/api"
  6. "go-common/app/service/main/assist/conf"
  7. "go-common/library/ecode"
  8. "os"
  9. "testing"
  10. "github.com/golang/mock/gomock"
  11. "github.com/smartystreets/goconvey/convey"
  12. )
  13. var (
  14. d *Dao
  15. )
  16. func TestMain(m *testing.M) {
  17. if os.Getenv("DEPLOY_ENV") != "" {
  18. flag.Set("app_id", "main.archive.assist-service")
  19. flag.Set("conf_token", "6e0dae2c95d90ff8d0da53460ef11ae8")
  20. flag.Set("tree_id", "2084")
  21. flag.Set("conf_version", "docker-1")
  22. flag.Set("deploy_env", "uat")
  23. flag.Set("conf_host", "config.bilibili.co")
  24. flag.Set("conf_path", "/tmp")
  25. flag.Set("region", "sh")
  26. flag.Set("zone", "sh001")
  27. } else {
  28. flag.Set("conf", "../cmd/assist-service.toml")
  29. }
  30. flag.Parse()
  31. if err := conf.Init(); err != nil {
  32. panic(err)
  33. }
  34. d = New(conf.Conf)
  35. m.Run()
  36. os.Exit(0)
  37. }
  38. func WithMock(t *testing.T, f func(mock *gomock.Controller)) func() {
  39. return func() {
  40. mockCtrl := gomock.NewController(t)
  41. defer mockCtrl.Finish()
  42. f(mockCtrl)
  43. }
  44. }
  45. func TestIdentifyInfo(t *testing.T) {
  46. convey.Convey("1", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  47. var (
  48. c = context.Background()
  49. mid = int64(2089809)
  50. ip = "127.0.0.1"
  51. err error
  52. )
  53. mock := accapi.NewMockAccountClient(mockCtrl)
  54. d.acc = mock
  55. mockReq := &accapi.MidReq{
  56. Mid: mid,
  57. }
  58. mock.EXPECT().Profile3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  59. err = d.IdentifyInfo(c, mid, ip)
  60. convey.So(err, convey.ShouldNotBeNil)
  61. }))
  62. convey.Convey("2", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  63. var (
  64. c = context.Background()
  65. mid = int64(2089809)
  66. ip = "127.0.0.1"
  67. err error
  68. )
  69. mock := accapi.NewMockAccountClient(mockCtrl)
  70. d.acc = mock
  71. mockReq := &accapi.MidReq{
  72. Mid: mid,
  73. }
  74. rpcRes := &accapi.ProfileReply{
  75. Profile: &accapi.Profile{
  76. Identification: 0,
  77. TelStatus: 2,
  78. },
  79. }
  80. mock.EXPECT().Profile3(gomock.Any(), mockReq).Return(rpcRes, nil)
  81. err = d.IdentifyInfo(c, mid, ip)
  82. convey.So(err, convey.ShouldNotBeNil)
  83. }))
  84. }
  85. func TestIsFollow(t *testing.T) {
  86. convey.Convey("1", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  87. var (
  88. c = context.Background()
  89. mid = int64(2089809)
  90. assistMid = int64(11)
  91. err error
  92. follow bool
  93. )
  94. mock := accapi.NewMockAccountClient(mockCtrl)
  95. d.acc = mock
  96. mockReq := &accapi.RelationReq{
  97. Mid: assistMid,
  98. Owner: mid,
  99. }
  100. mock.EXPECT().Relation3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  101. follow, err = d.IsFollow(c, mid, assistMid)
  102. convey.So(err, convey.ShouldNotBeNil)
  103. convey.So(follow, convey.ShouldBeFalse)
  104. }))
  105. }
  106. func TestCard(t *testing.T) {
  107. convey.Convey("TestCard", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  108. var (
  109. c = context.Background()
  110. mid = int64(2089809)
  111. err error
  112. res *accapi.Card
  113. )
  114. mock := accapi.NewMockAccountClient(mockCtrl)
  115. d.acc = mock
  116. mockReq := &accapi.MidReq{
  117. Mid: mid,
  118. }
  119. mock.EXPECT().Card3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  120. res, err = d.Card(c, mid, "")
  121. convey.So(err, convey.ShouldNotBeNil)
  122. convey.So(res, convey.ShouldBeNil)
  123. }))
  124. }