need_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "go-common/app/admin/main/apm/dao/mock"
  7. "go-common/app/admin/main/apm/model/need"
  8. "github.com/bouk/monkey"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. //TestServiceNeedList is
  12. func TestServiceNeedList(t *testing.T) {
  13. convey.Convey("TestServiceNeedList", t, func() {
  14. arg := &need.NListReq{
  15. Status: 1,
  16. Ps: 10,
  17. Pn: 1,
  18. }
  19. guard := mock.MockDaoNeedInfoCount(svr.dao, 0, nil)
  20. defer guard.Unpatch()
  21. res, _, err := svr.NeedInfoList(context.Background(), arg, "fengshanshan")
  22. for _, v := range res {
  23. t.Logf("res:%+v", v)
  24. }
  25. convey.So(res, convey.ShouldNotBeNil)
  26. convey.So(err, convey.ShouldBeNil)
  27. })
  28. }
  29. //TestServiceNeedEdit is
  30. func TestServiceNeedEdit(t *testing.T) {
  31. convey.Convey("TestServiceNeedEdit", t, func() {
  32. arg := &need.NEditReq{
  33. ID: 147,
  34. Title: "22222",
  35. }
  36. err := svr.NeedInfoEdit(context.Background(), arg, "fengshanshan")
  37. convey.So(err, convey.ShouldBeNil)
  38. })
  39. convey.Convey("TestServiceNeedEdit status err", t, func() {
  40. arg := &need.NEditReq{
  41. ID: 99,
  42. Title: "22222",
  43. }
  44. err := svr.NeedInfoEdit(context.Background(), arg, "fengshanshan")
  45. convey.So(err, convey.ShouldEqual, 70018)
  46. })
  47. convey.Convey("TestServiceNeedEdit no access", t, func() {
  48. arg := &need.NEditReq{
  49. ID: 147,
  50. Title: "22222",
  51. }
  52. err := svr.NeedInfoEdit(context.Background(), arg, "fss")
  53. convey.So(err, convey.ShouldEqual, -403)
  54. })
  55. }
  56. //TestServiceNeedVerify is
  57. func TestServiceNeedVerify(t *testing.T) {
  58. convey.Convey("TestServiceNeedVerify status err", t, func() {
  59. arg := &need.NVerifyReq{
  60. ID: 117,
  61. Status: 2,
  62. }
  63. _, err := svr.NeedInfoVerify(context.Background(), arg)
  64. convey.So(err, convey.ShouldEqual, 70019)
  65. })
  66. convey.Convey("TestServiceNeedVerify not exist", t, func() {
  67. arg := &need.NVerifyReq{
  68. ID: 10000,
  69. Status: 1,
  70. }
  71. _, err := svr.NeedInfoVerify(context.Background(), arg)
  72. convey.So(err, convey.ShouldEqual, 70017)
  73. })
  74. }
  75. func TestServiceNeedVote(t *testing.T) {
  76. convey.Convey("TestServiceNeedVote", t, func() {
  77. arg := &need.Likereq{
  78. ReqID: 148,
  79. LikeType: 1,
  80. }
  81. err := svr.NeedInfoVote(context.Background(), arg, "fengshanshan")
  82. convey.So(err, convey.ShouldBeNil)
  83. })
  84. convey.Convey("TestServiceNeedVote not exist", t, func() {
  85. arg := &need.Likereq{
  86. ReqID: 789,
  87. LikeType: 1,
  88. }
  89. err := svr.NeedInfoVote(context.Background(), arg, "fengshanshan")
  90. convey.So(err, convey.ShouldEqual, 70017)
  91. })
  92. }
  93. func TestServiceNeedVoteList(t *testing.T) {
  94. convey.Convey("NeedVoteList", t, func(ctx convey.C) {
  95. var (
  96. c = context.Background()
  97. arg = &need.Likereq{
  98. ReqID: 11,
  99. LikeType: 1,
  100. }
  101. resp = []*need.UserLikes{
  102. {ID: 1, User: "fengshanshan", LikeType: 1},
  103. {ID: 2, User: "fengshanshan", LikeType: 2},
  104. }
  105. )
  106. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  107. svr.dao.MockVoteInfoCounts(6, nil)
  108. svr.dao.MockVoteInfoList(resp, nil)
  109. res, count, err := svr.NeedVoteList(c, arg)
  110. ctx.Convey("Then err should be nil.res,count should not be nil.", func(ctx convey.C) {
  111. ctx.So(err, convey.ShouldBeNil)
  112. ctx.So(count, convey.ShouldNotBeNil)
  113. ctx.So(res, convey.ShouldNotBeNil)
  114. })
  115. ctx.Reset(func() {
  116. monkey.UnpatchAll()
  117. })
  118. })
  119. })
  120. }
  121. func TestServiceNeedInfoAdd(t *testing.T) {
  122. convey.Convey("NeedInfoAdd", t, func(ctx convey.C) {
  123. var (
  124. c = context.Background()
  125. req = &need.NAddReq{
  126. Title: "32323",
  127. Content: "ewewerw",
  128. }
  129. username = "fengshanshan"
  130. )
  131. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  132. guard := svr.dao.MockNeedInfoAdd(nil)
  133. defer guard.Unpatch()
  134. err := svr.NeedInfoAdd(c, req, username)
  135. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  136. ctx.So(err, convey.ShouldBeNil)
  137. })
  138. })
  139. ctx.Convey("When return err", func(ctx convey.C) {
  140. guard := svr.dao.MockNeedInfoAdd(fmt.Errorf("aaa"))
  141. defer guard.Unpatch()
  142. err := svr.NeedInfoAdd(c, req, username)
  143. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  144. ctx.So(err, convey.ShouldNotBeNil)
  145. })
  146. })
  147. })
  148. }
  149. func TestServiceSendWeMessage(t *testing.T) {
  150. convey.Convey("SendWeMessage", t, func(convCtx convey.C) {
  151. var (
  152. c = context.Background()
  153. title = "有个xxxxxxx"
  154. task = need.VerifyType[need.NeedReview]
  155. result = need.VerifyType[need.VerifyAccept]
  156. sender = "fengshanshan"
  157. receiver = []string{"fengshanshan"}
  158. )
  159. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  160. err := svr.SendWeMessage(c, title, task, result, sender, receiver)
  161. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  162. convCtx.So(err, convey.ShouldBeNil)
  163. })
  164. })
  165. })
  166. }