rpc_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package weeklyhonor
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. whmdl "go-common/app/interface/main/creative/model/weeklyhonor"
  7. upgrpc "go-common/app/service/main/up/api/v1"
  8. "github.com/golang/mock/gomock"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. var c = context.Background()
  12. func TestWeeklyhonorUpCount(t *testing.T) {
  13. convey.Convey("UpCount", t, func(ctx convey.C) {
  14. var (
  15. mid = int64(1627855)
  16. )
  17. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  18. count, err := d.UpCount(c, mid)
  19. ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
  20. ctx.So(err, convey.ShouldBeNil)
  21. ctx.So(count, convey.ShouldNotBeNil)
  22. })
  23. })
  24. })
  25. }
  26. func TestWeeklyhonorUpActiveLists(t *testing.T) {
  27. convey.Convey("UpActivesList", t, func(ctx convey.C) {
  28. ctx.Convey("When everything gose positive", WithMock(t, func(mockCtrl *gomock.Controller) {
  29. // mock
  30. mockUpClient := upgrpc.NewMockUpClient(mockCtrl)
  31. d.upClient = mockUpClient
  32. mockReq := upgrpc.UpListByLastIDReq{
  33. LastID: 0,
  34. Ps: 100,
  35. }
  36. mockReply := upgrpc.UpActivityListReply{
  37. UpActivitys: []*upgrpc.UpActivity{
  38. {Mid: 1},
  39. {Activity: 2},
  40. },
  41. LastID: 1,
  42. }
  43. mockUpClient.EXPECT().UpInfoActivitys(gomock.Any(), &mockReq).Return(&mockReply, nil)
  44. // test
  45. upActives, newId, err := d.UpActivesList(c, 0, 100)
  46. ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
  47. ctx.So(err, convey.ShouldBeNil)
  48. ctx.So(upActives[0], convey.ShouldEqual, mockReply.UpActivitys[0])
  49. ctx.So(newId, convey.ShouldEqual, mockReply.LastID)
  50. })
  51. }))
  52. })
  53. }
  54. func TestWeeklyhonorGetUpSwitch(t *testing.T) {
  55. convey.Convey("GetUpSwitch", t, WithMock(t, func(mockCtrl *gomock.Controller) {
  56. var (
  57. c = context.Background()
  58. mid = int64(75379101)
  59. )
  60. convey.Convey("When everything gose positive", func(ctx convey.C) {
  61. mockUpClient := upgrpc.NewMockUpClient(mockCtrl)
  62. d.upClient = mockUpClient
  63. mockReq := upgrpc.UpSwitchReq{
  64. Mid: mid,
  65. From: fromWeeklyHonor,
  66. }
  67. mockState := whmdl.HonorUnSub
  68. mockUpClient.EXPECT().UpSwitch(gomock.Any(), &mockReq).Return(&upgrpc.UpSwitchReply{State: mockState}, nil)
  69. state, err := d.GetUpSwitch(c, mid)
  70. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  71. ctx.So(err, convey.ShouldBeNil)
  72. ctx.So(state, convey.ShouldEqual, mockState)
  73. })
  74. })
  75. convey.Convey("When return err", func(ctx convey.C) {
  76. mockUpClient := upgrpc.NewMockUpClient(mockCtrl)
  77. d.upClient = mockUpClient
  78. mockReq := upgrpc.UpSwitchReq{
  79. Mid: mid,
  80. From: fromWeeklyHonor,
  81. }
  82. mockUpClient.EXPECT().UpSwitch(gomock.Any(), &mockReq).Return(nil, fmt.Errorf("mock err"))
  83. _, err := d.GetUpSwitch(c, mid)
  84. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  85. ctx.So(err, convey.ShouldNotBeNil)
  86. })
  87. })
  88. }))
  89. }