rpc_test.go 2.6 KB

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