dao_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package feedback
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "go-common/app/interface/main/creative/conf"
  7. "go-common/app/interface/main/creative/model/feedback"
  8. "go-common/library/ecode"
  9. "os"
  10. "strings"
  11. "testing"
  12. "github.com/smartystreets/goconvey/convey"
  13. gock "gopkg.in/h2non/gock.v1"
  14. )
  15. var (
  16. d *Dao
  17. )
  18. func TestMain(m *testing.M) {
  19. if os.Getenv("DEPLOY_ENV") != "" {
  20. flag.Set("app_id", "main.archive.creative")
  21. flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
  22. flag.Set("tree_id", "2305")
  23. flag.Set("conf_version", "docker-1")
  24. flag.Set("deploy_env", "uat")
  25. flag.Set("conf_host", "config.bilibili.co")
  26. flag.Set("conf_path", "/tmp")
  27. flag.Set("region", "sh")
  28. flag.Set("zone", "sh001")
  29. } else {
  30. flag.Set("conf", "../../cmd/creative.toml")
  31. }
  32. flag.Parse()
  33. if err := conf.Init(); err != nil {
  34. panic(err)
  35. }
  36. d = New(conf.Conf)
  37. m.Run()
  38. os.Exit(0)
  39. }
  40. func httpMock(method, url string) *gock.Request {
  41. r := gock.New(url)
  42. r.Method = strings.ToUpper(method)
  43. d.client.SetTransport(gock.DefaultTransport)
  44. return r
  45. }
  46. func TestCloseSession(t *testing.T) {
  47. var (
  48. sessionID int64
  49. ip string
  50. c = context.TODO()
  51. err error
  52. res struct {
  53. Code int `json:"code"`
  54. }
  55. )
  56. convey.Convey("1", t, func(ctx convey.C) {
  57. defer gock.OffAll()
  58. httpMock("Post", d.closeURI).Reply(-502)
  59. err = d.CloseSession(c, sessionID, ip)
  60. ctx.Convey("1", func(ctx convey.C) {
  61. ctx.So(err, convey.ShouldNotBeNil)
  62. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  63. })
  64. })
  65. convey.Convey("2", t, func(ctx convey.C) {
  66. res.Code = 20010
  67. js, _ := json.Marshal(res)
  68. defer gock.OffAll()
  69. httpMock("Post", d.closeURI).Reply(200).JSON(string(js))
  70. err = d.CloseSession(c, sessionID, ip)
  71. ctx.Convey("2", func(ctx convey.C) {
  72. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  73. })
  74. })
  75. convey.Convey("3", t, func(ctx convey.C) {
  76. res.Code = 0
  77. js, _ := json.Marshal(res)
  78. defer gock.OffAll()
  79. httpMock("Post", d.closeURI).Reply(200).JSON(string(js))
  80. err = d.CloseSession(c, sessionID, ip)
  81. ctx.Convey("2", func(ctx convey.C) {
  82. ctx.So(err, convey.ShouldBeNil)
  83. })
  84. })
  85. }
  86. func TestAddFeedback(t *testing.T) {
  87. var (
  88. c = context.TODO()
  89. err error
  90. res struct {
  91. Code int `json:"code"`
  92. }
  93. mid, tagID, sessionID int64
  94. qq, content, aid, browser, imgURL, platform, ip string
  95. )
  96. convey.Convey("1", t, func(ctx convey.C) {
  97. defer gock.OffAll()
  98. httpMock("Post", d.addURI).Reply(-502)
  99. err = d.AddFeedback(c, mid, tagID, sessionID, qq, content, aid, browser, imgURL, platform, ip)
  100. ctx.Convey("1", func(ctx convey.C) {
  101. ctx.So(err, convey.ShouldNotBeNil)
  102. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  103. })
  104. })
  105. convey.Convey("2", t, func(ctx convey.C) {
  106. res.Code = 20010
  107. js, _ := json.Marshal(res)
  108. defer gock.OffAll()
  109. httpMock("Post", d.addURI).Reply(200).JSON(string(js))
  110. err = d.AddFeedback(c, mid, tagID, sessionID, qq, content, aid, browser, imgURL, platform, ip)
  111. ctx.Convey("2", func(ctx convey.C) {
  112. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  113. })
  114. })
  115. convey.Convey("3", t, func(ctx convey.C) {
  116. res.Code = 0
  117. js, _ := json.Marshal(res)
  118. defer gock.OffAll()
  119. httpMock("Post", d.addURI).Reply(200).JSON(string(js))
  120. err = d.AddFeedback(c, mid, tagID, sessionID, qq, content, aid, browser, imgURL, platform, ip)
  121. ctx.Convey("2", func(ctx convey.C) {
  122. ctx.So(err, convey.ShouldBeNil)
  123. })
  124. })
  125. }
  126. func TestDetail(t *testing.T) {
  127. var (
  128. c = context.TODO()
  129. err error
  130. res struct {
  131. Code int `json:"code"`
  132. Data []*feedback.Reply `json:"data"`
  133. }
  134. mid, sessionID int64
  135. ip string
  136. data []*feedback.Reply
  137. )
  138. convey.Convey("1", t, func(ctx convey.C) {
  139. defer gock.OffAll()
  140. httpMock("Get", d.detailURI).Reply(-502)
  141. data, err = d.Detail(c, mid, sessionID, ip)
  142. ctx.Convey("1", func(ctx convey.C) {
  143. ctx.So(err, convey.ShouldNotBeNil)
  144. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  145. ctx.So(data, convey.ShouldBeNil)
  146. })
  147. })
  148. convey.Convey("2", t, func(ctx convey.C) {
  149. res.Code = 20010
  150. res.Data = make([]*feedback.Reply, 0)
  151. js, _ := json.Marshal(res)
  152. defer gock.OffAll()
  153. httpMock("Get", d.detailURI).Reply(200).JSON(string(js))
  154. data, err = d.Detail(c, mid, sessionID, ip)
  155. ctx.Convey("2", func(ctx convey.C) {
  156. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  157. ctx.So(data, convey.ShouldBeNil)
  158. })
  159. })
  160. convey.Convey("3", t, func(ctx convey.C) {
  161. res.Code = 0
  162. res.Data = make([]*feedback.Reply, 0)
  163. res.Data = append(res.Data, &feedback.Reply{})
  164. js, _ := json.Marshal(res)
  165. defer gock.OffAll()
  166. httpMock("Get", d.detailURI).Reply(200).JSON(string(js))
  167. data, err = d.Detail(c, mid, sessionID, ip)
  168. ctx.Convey("3", func(ctx convey.C) {
  169. ctx.So(err, convey.ShouldBeNil)
  170. ctx.So(data, convey.ShouldNotBeNil)
  171. })
  172. })
  173. }
  174. func TestFeedbacks(t *testing.T) {
  175. var (
  176. c = context.TODO()
  177. err error
  178. res struct {
  179. Code int `json:"code"`
  180. Data []*feedback.Feedback `json:"data"`
  181. Count int64 `json:"total"`
  182. }
  183. mid, ps, pn, cnt int64
  184. tagID = int64(1)
  185. end, platform, ip string
  186. start = "1090"
  187. state = "open"
  188. data []*feedback.Feedback
  189. )
  190. convey.Convey("1", t, func(ctx convey.C) {
  191. defer gock.OffAll()
  192. httpMock("Get", d.listURI).Reply(-502)
  193. data, cnt, err = d.Feedbacks(c, mid, ps, pn, tagID, state, start, end, platform, ip)
  194. ctx.Convey("1", func(ctx convey.C) {
  195. ctx.So(err, convey.ShouldNotBeNil)
  196. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  197. ctx.So(data, convey.ShouldBeNil)
  198. ctx.So(cnt, convey.ShouldEqual, 0)
  199. })
  200. })
  201. convey.Convey("2", t, func(ctx convey.C) {
  202. res.Code = 20010
  203. res.Data = make([]*feedback.Feedback, 0)
  204. js, _ := json.Marshal(res)
  205. defer gock.OffAll()
  206. httpMock("Get", d.listURI).Reply(200).JSON(string(js))
  207. data, cnt, err = d.Feedbacks(c, mid, ps, pn, tagID, state, start, end, platform, ip)
  208. ctx.Convey("2", func(ctx convey.C) {
  209. ctx.So(err, convey.ShouldEqual, ecode.CreativeFeedbackErr)
  210. ctx.So(data, convey.ShouldBeNil)
  211. ctx.So(cnt, convey.ShouldEqual, 0)
  212. })
  213. })
  214. convey.Convey("3", t, func(ctx convey.C) {
  215. res.Code = 0
  216. res.Data = make([]*feedback.Feedback, 0)
  217. res.Data = append(res.Data, &feedback.Feedback{})
  218. res.Count = 100
  219. js, _ := json.Marshal(res)
  220. defer gock.OffAll()
  221. httpMock("Get", d.listURI).Reply(200).JSON(string(js))
  222. data, cnt, err = d.Feedbacks(c, mid, ps, pn, tagID, state, start, end, platform, ip)
  223. ctx.Convey("3", func(ctx convey.C) {
  224. ctx.So(err, convey.ShouldBeNil)
  225. ctx.So(data, convey.ShouldNotBeNil)
  226. ctx.So(cnt, convey.ShouldNotEqual, 0)
  227. })
  228. })
  229. }