dao_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package account
  2. import (
  3. "context"
  4. "flag"
  5. "go-common/app/interface/main/creative/conf"
  6. accapi "go-common/app/service/main/account/api"
  7. relaMdl "go-common/app/service/main/relation/model"
  8. relation "go-common/app/service/main/relation/rpc/client"
  9. "go-common/library/ecode"
  10. "os"
  11. "reflect"
  12. "strings"
  13. "testing"
  14. "github.com/bouk/monkey"
  15. "github.com/golang/mock/gomock"
  16. . "github.com/smartystreets/goconvey/convey"
  17. gock "gopkg.in/h2non/gock.v1"
  18. )
  19. var (
  20. d *Dao
  21. )
  22. func TestMain(m *testing.M) {
  23. if os.Getenv("DEPLOY_ENV") != "" {
  24. flag.Set("app_id", "main.archive.creative")
  25. flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
  26. flag.Set("tree_id", "2305")
  27. flag.Set("conf_version", "docker-1")
  28. flag.Set("deploy_env", "uat")
  29. flag.Set("conf_host", "config.bilibili.co")
  30. flag.Set("conf_path", "/tmp")
  31. flag.Set("region", "sh")
  32. flag.Set("zone", "sh001")
  33. } else {
  34. flag.Set("conf", "../../cmd/creative.toml")
  35. }
  36. flag.Parse()
  37. if err := conf.Init(); err != nil {
  38. panic(err)
  39. }
  40. d = New(conf.Conf)
  41. m.Run()
  42. os.Exit(0)
  43. }
  44. func httpMock(method, url string) *gock.Request {
  45. r := gock.New(url)
  46. r.Method = strings.ToUpper(method)
  47. d.client.SetTransport(gock.DefaultTransport)
  48. d.fastClient.SetTransport(gock.DefaultTransport)
  49. return r
  50. }
  51. func TestIdentifyInfo(t *testing.T) {
  52. Convey("IdentifyInfo", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  53. var (
  54. c = context.TODO()
  55. err error
  56. mid = int64(27515256)
  57. ip = "127.0.0.1"
  58. ret int
  59. )
  60. mock := accapi.NewMockAccountClient(mockCtrl)
  61. d.acc = mock
  62. arg := &accapi.MidReq{
  63. Mid: mid,
  64. }
  65. mock.EXPECT().Profile3(gomock.Any(), arg).Return(nil, ecode.CreativeAccServiceErr)
  66. ret, err = d.IdentifyInfo(c, mid, 1, ip)
  67. So(err, ShouldNotBeNil)
  68. So(ret, ShouldNotBeNil)
  69. }))
  70. }
  71. func TestMidByName(t *testing.T) {
  72. Convey("IdentifyInfo", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  73. var (
  74. c = context.TODO()
  75. err error
  76. ret int64
  77. name = "iamname"
  78. )
  79. mock := accapi.NewMockAccountClient(mockCtrl)
  80. d.acc = mock
  81. arg := &accapi.NamesReq{
  82. Names: []string{name},
  83. }
  84. mock.EXPECT().InfosByName3(gomock.Any(), arg).Return(nil, ecode.CreativeAccServiceErr)
  85. ret, err = d.MidByName(c, name)
  86. So(err, ShouldNotBeNil)
  87. So(ret, ShouldBeZeroValue)
  88. }))
  89. }
  90. func TestInfos(t *testing.T) {
  91. Convey("IdentifyInfo", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  92. var (
  93. c = context.TODO()
  94. err error
  95. mids = []int64{2089809}
  96. ip = "127.0.0.1"
  97. )
  98. mock := accapi.NewMockAccountClient(mockCtrl)
  99. d.acc = mock
  100. mockReq := &accapi.MidsReq{
  101. Mids: mids,
  102. }
  103. mock.EXPECT().Infos3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  104. _, err = d.Infos(c, mids, ip)
  105. So(err, ShouldNotBeNil)
  106. }))
  107. }
  108. func TestProfile(t *testing.T) {
  109. Convey("Profile", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  110. var (
  111. c = context.TODO()
  112. err error
  113. mid = int64(27515256)
  114. ip = "127.0.0.1"
  115. p *accapi.Profile
  116. )
  117. mock := accapi.NewMockAccountClient(mockCtrl)
  118. d.acc = mock
  119. mockReq := &accapi.MidReq{
  120. Mid: mid,
  121. }
  122. mock.EXPECT().Profile3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  123. p, err = d.Profile(c, mid, ip)
  124. So(err, ShouldNotBeNil)
  125. So(p, ShouldBeNil)
  126. }))
  127. }
  128. func TestProfileWithStat(t *testing.T) {
  129. Convey("ProfileWithStat", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  130. var (
  131. c = context.TODO()
  132. err error
  133. mid = int64(27515256)
  134. p *accapi.ProfileStatReply
  135. )
  136. mock := accapi.NewMockAccountClient(mockCtrl)
  137. d.acc = mock
  138. mockReq := &accapi.MidReq{
  139. Mid: mid,
  140. }
  141. mock.EXPECT().ProfileWithStat3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  142. p, err = d.ProfileWithStat(c, mid)
  143. So(err, ShouldNotBeNil)
  144. So(p, ShouldBeNil)
  145. }))
  146. }
  147. func TestCard(t *testing.T) {
  148. Convey("Card", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  149. var (
  150. c = context.TODO()
  151. err error
  152. mid = int64(27515256)
  153. ip = "127.0.0.1"
  154. ret *accapi.Card
  155. )
  156. mock := accapi.NewMockAccountClient(mockCtrl)
  157. d.acc = mock
  158. mockReq := &accapi.MidReq{
  159. Mid: mid,
  160. }
  161. mock.EXPECT().Card3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
  162. ret, err = d.Card(c, mid, ip)
  163. So(err, ShouldNotBeNil)
  164. So(ret, ShouldBeNil)
  165. }))
  166. }
  167. func TestRichRelation(t *testing.T) {
  168. Convey("RichRelation", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  169. var (
  170. c = context.TODO()
  171. err error
  172. owner = int64(27515256)
  173. mids = []int64{2089809}
  174. ip = "127.0.0.1"
  175. ret map[int64]int32
  176. )
  177. mock := accapi.NewMockAccountClient(mockCtrl)
  178. d.acc = mock
  179. arg := &accapi.RichRelationReq{
  180. Owner: owner,
  181. Mids: mids,
  182. }
  183. mock.EXPECT().RichRelations3(gomock.Any(), arg).Return(nil, ecode.CreativeAccServiceErr)
  184. ret, err = d.RichRelation(c, owner, mids, ip)
  185. So(err, ShouldNotBeNil)
  186. So(ret, ShouldBeNil)
  187. }))
  188. }
  189. func TestRelationFollowers(t *testing.T) {
  190. Convey("RelationFollowers", t, func(ctx C) {
  191. var (
  192. c = context.TODO()
  193. err error
  194. mid = int64(2089809)
  195. ip = "127.0.0.1"
  196. ret map[int64]int32
  197. )
  198. mock := monkey.PatchInstanceMethod(reflect.TypeOf(d.rela), "Followers",
  199. func(_ *relation.Service, _ context.Context, _ *relaMdl.ArgMid) (res []*relaMdl.Following, err error) {
  200. return nil, ecode.CreativeAccServiceErr
  201. })
  202. defer mock.Unpatch()
  203. ret, err = d.RelationFollowers(c, mid, ip)
  204. ctx.Convey("RelationFollowers", func(ctx C) {
  205. ctx.So(err, ShouldNotBeNil)
  206. ctx.So(ret, ShouldBeNil)
  207. })
  208. })
  209. }
  210. func TestShouldFollow(t *testing.T) {
  211. Convey("ShouldFollow", t, func(ctx C) {
  212. var (
  213. c = context.TODO()
  214. err error
  215. mid = int64(2089809)
  216. fids = []int64{2089809}
  217. ip = "127.0.0.1"
  218. ret []int64
  219. )
  220. mock := monkey.PatchInstanceMethod(reflect.TypeOf(d.rela), "Relations",
  221. func(_ *relation.Service, _ context.Context, _ *relaMdl.ArgRelations) (res map[int64]*relaMdl.Following, err error) {
  222. res = make(map[int64]*relaMdl.Following)
  223. res[2089809] = &relaMdl.Following{
  224. Attribute: 0,
  225. }
  226. return res, nil
  227. })
  228. defer mock.Unpatch()
  229. ret, err = d.ShouldFollow(c, mid, fids, ip)
  230. ctx.Convey("RelationFollowers", func(ctx C) {
  231. ctx.So(err, ShouldBeNil)
  232. ctx.So(ret, ShouldNotBeNil)
  233. })
  234. })
  235. }
  236. func TestSwitchPhoneRet(t *testing.T) {
  237. var (
  238. new, old, identify int
  239. err error
  240. )
  241. new = 1
  242. Convey("switchPhoneRet", t, func(ctx C) {
  243. old = d.switchPhoneRet(new)
  244. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx C) {
  245. ctx.So(old, ShouldBeZeroValue)
  246. })
  247. })
  248. identify = 1
  249. Convey("CheckIdentify", t, func(ctx C) {
  250. err = d.CheckIdentify(identify)
  251. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx C) {
  252. ctx.So(err, ShouldEqual, ecode.UserCheckInvalidPhone)
  253. })
  254. })
  255. }
  256. func TestDao_Followers(t *testing.T) {
  257. Convey("Followers", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  258. var (
  259. c = context.TODO()
  260. err error
  261. mid = int64(2089809)
  262. fid = int64(2089809)
  263. ip = "127.0.0.1"
  264. )
  265. mock := accapi.NewMockAccountClient(mockCtrl)
  266. d.acc = mock
  267. arg := &accapi.RelationReq{
  268. Owner: mid,
  269. Mid: fid,
  270. }
  271. mock.EXPECT().Relation3(gomock.Any(), arg).Return(nil, ecode.CreativeAccServiceErr)
  272. _, err = d.Followers(c, mid, []int64{fid}, ip)
  273. Convey("Followers", func(ctx C) {
  274. ctx.So(err, ShouldBeNil)
  275. })
  276. }))
  277. }
  278. func TestDao_Relations(t *testing.T) {
  279. Convey("Relations", t, func(ctx C) {
  280. var (
  281. c = context.TODO()
  282. err error
  283. mid = int64(2089809)
  284. fids = []int64{2089809}
  285. ip = "127.0.0.1"
  286. ret map[int64]int
  287. )
  288. mock := monkey.PatchInstanceMethod(reflect.TypeOf(d.rela), "Relations",
  289. func(_ *relation.Service, _ context.Context, _ *relaMdl.ArgRelations) (res map[int64]*relaMdl.Following, err error) {
  290. return nil, ecode.CreativeAccServiceErr
  291. })
  292. defer mock.Unpatch()
  293. ret, err = d.Relations(c, mid, fids, ip)
  294. ctx.Convey("RelationFollowers", func(ctx C) {
  295. ctx.So(err, ShouldNotBeNil)
  296. ctx.So(ret, ShouldBeNil)
  297. })
  298. })
  299. }
  300. func TestDao_Relations2(t *testing.T) {
  301. Convey("Relations2", t, func(ctx C) {
  302. var (
  303. c = context.TODO()
  304. err error
  305. mid = int64(2089809)
  306. fids = []int64{2089809}
  307. ip = "127.0.0.1"
  308. ret map[int64]int
  309. )
  310. mock := monkey.PatchInstanceMethod(reflect.TypeOf(d.rela), "Relations",
  311. func(_ *relation.Service, _ context.Context, _ *relaMdl.ArgRelations) (res map[int64]*relaMdl.Following, err error) {
  312. return nil, ecode.CreativeAccServiceErr
  313. })
  314. defer mock.Unpatch()
  315. ret, err = d.Relations2(c, mid, fids, ip)
  316. ctx.Convey("RelationFollowers", func(ctx C) {
  317. ctx.So(err, ShouldNotBeNil)
  318. ctx.So(ret, ShouldBeNil)
  319. })
  320. })
  321. }
  322. func WithMock(t *testing.T, f func(mock *gomock.Controller)) func() {
  323. return func() {
  324. mockCtrl := gomock.NewController(t)
  325. defer mockCtrl.Finish()
  326. f(mockCtrl)
  327. }
  328. }