api_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package archive
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/interface/main/creative/model/archive"
  6. "go-common/library/ecode"
  7. "testing"
  8. "github.com/smartystreets/goconvey/convey"
  9. gock "gopkg.in/h2non/gock.v1"
  10. )
  11. func TestFlowJudge(t *testing.T) {
  12. var (
  13. c = context.TODO()
  14. err error
  15. business = int64(1)
  16. groupID = int64(2)
  17. oids = []int64{1, 2, 3}
  18. hitOids []int64
  19. )
  20. convey.Convey("FlowJudge", t, func(ctx convey.C) {
  21. defer gock.OffAll()
  22. httpMock("GET", d.flowJudge).Reply(200).JSON(`{"code":20001}`)
  23. hitOids, err = d.FlowJudge(c, business, groupID, oids)
  24. ctx.Convey("Then err should be nil.hitOids should not be nil.", func(ctx convey.C) {
  25. ctx.So(err, convey.ShouldNotBeNil)
  26. ctx.So(hitOids, convey.ShouldBeNil)
  27. })
  28. })
  29. }
  30. func TestArchiveSimpleArchive(t *testing.T) {
  31. var (
  32. c = context.TODO()
  33. aid = int64(10110817)
  34. ip = "127.0.0.1"
  35. err error
  36. sa *archive.SpArchive
  37. res struct {
  38. Code int `json:"code"`
  39. Data *archive.SpArchive `json:"data"`
  40. }
  41. )
  42. convey.Convey("4", t, func(ctx convey.C) {
  43. defer gock.OffAll()
  44. httpMock("GET", d.simpleArchive).Reply(-502)
  45. sa, err = d.SimpleArchive(c, aid, ip)
  46. ctx.Convey("Then err should be nil.sa should not be nil.", func(ctx convey.C) {
  47. ctx.So(err, convey.ShouldNotBeNil)
  48. ctx.So(err, convey.ShouldEqual, ecode.CreativeArchiveAPIErr)
  49. ctx.So(sa, convey.ShouldBeNil)
  50. })
  51. })
  52. convey.Convey("1", t, func(ctx convey.C) {
  53. defer gock.OffAll()
  54. httpMock("GET", d.simpleArchive).Reply(200).JSON(`{"code":20001}`)
  55. sa, err = d.SimpleArchive(c, aid, ip)
  56. ctx.Convey("Then err should be nil.sa should not be nil.", func(ctx convey.C) {
  57. ctx.So(err, convey.ShouldNotBeNil)
  58. ctx.So(err, convey.ShouldEqual, ecode.CreativeArchiveAPIErr)
  59. ctx.So(sa, convey.ShouldBeNil)
  60. })
  61. })
  62. convey.Convey("2", t, func(ctx convey.C) {
  63. res.Code = 0
  64. res.Data = &archive.SpArchive{
  65. Aid: aid,
  66. Title: "iamtitle",
  67. Mid: 2089809,
  68. }
  69. js, _ := json.Marshal(res)
  70. defer gock.OffAll()
  71. httpMock("GET", d.simpleArchive).Reply(200).JSON(string(js))
  72. sa, err = d.SimpleArchive(c, aid, ip)
  73. ctx.Convey("Then err should be nil.sa should not be nil.", func(ctx convey.C) {
  74. ctx.So(err, convey.ShouldBeNil)
  75. ctx.So(sa, convey.ShouldNotBeNil)
  76. })
  77. })
  78. convey.Convey("3", t, func(ctx convey.C) {
  79. res.Code = 0
  80. res.Data = nil
  81. js, _ := json.Marshal(res)
  82. defer gock.OffAll()
  83. httpMock("GET", d.simpleArchive).Reply(200).JSON(string(js))
  84. sa, err = d.SimpleArchive(c, aid, ip)
  85. ctx.Convey("Then err should be nil.sa should not be nil.", func(ctx convey.C) {
  86. ctx.So(err, convey.ShouldBeNil)
  87. ctx.So(sa, convey.ShouldBeNil)
  88. })
  89. })
  90. }
  91. func TestArchiveSimpleVideos(t *testing.T) {
  92. var (
  93. c = context.TODO()
  94. aid = int64(10110817)
  95. ip = "127.0.0.1"
  96. res struct {
  97. Code int `json:"code"`
  98. Data []*archive.SpVideo `json:"data"`
  99. }
  100. )
  101. convey.Convey("1", t, func(ctx convey.C) {
  102. defer gock.OffAll()
  103. js, _ := json.Marshal(res)
  104. httpMock("GET", d.simpleVideos).Reply(200).JSON(string(js))
  105. vs, err := d.SimpleVideos(c, aid, ip)
  106. ctx.Convey("Then err should be nil.vs should not be nil.", func(ctx convey.C) {
  107. ctx.So(err, convey.ShouldBeNil)
  108. ctx.So(vs, convey.ShouldBeNil)
  109. })
  110. })
  111. convey.Convey("2", t, func(ctx convey.C) {
  112. defer gock.OffAll()
  113. res.Code = 20001
  114. res.Data = nil
  115. js, _ := json.Marshal(res)
  116. httpMock("GET", d.simpleVideos).Reply(200).JSON(string(js))
  117. vs, err := d.SimpleVideos(c, aid, ip)
  118. ctx.Convey("Then err should be nil.vs should not be nil.", func(ctx convey.C) {
  119. ctx.So(err, convey.ShouldNotBeNil)
  120. ctx.So(vs, convey.ShouldBeNil)
  121. })
  122. })
  123. convey.Convey("3", t, func(ctx convey.C) {
  124. defer gock.OffAll()
  125. res.Code = 0
  126. res.Data = append(res.Data, &archive.SpVideo{
  127. Cid: 1,
  128. Index: 1,
  129. Title: "1title",
  130. }, &archive.SpVideo{
  131. Cid: 2,
  132. Index: 2,
  133. Title: "2title",
  134. })
  135. js, _ := json.Marshal(res)
  136. httpMock("GET", d.simpleVideos).Reply(200).JSON(string(js))
  137. vs, err := d.SimpleVideos(c, aid, ip)
  138. ctx.Convey("Then err should be nil.vs should not be nil.", func(ctx convey.C) {
  139. ctx.So(err, convey.ShouldBeNil)
  140. ctx.So(vs, convey.ShouldNotBeNil)
  141. })
  142. })
  143. }
  144. func TestArchiveView(t *testing.T) {
  145. var (
  146. c = context.TODO()
  147. mid = int64(2089809)
  148. aid = int64(10110817)
  149. ip = "127.0.0.1"
  150. )
  151. convey.Convey("View", t, func(ctx convey.C) {
  152. av, err := d.View(c, mid, aid, ip, 0, 0)
  153. ctx.Convey("Then err should be nil.av should not be nil.", func(ctx convey.C) {
  154. ctx.So(err, convey.ShouldBeNil)
  155. ctx.So(av, convey.ShouldNotBeNil)
  156. })
  157. })
  158. }
  159. func TestArchiveViews(t *testing.T) {
  160. var (
  161. c = context.TODO()
  162. mid = int64(2089809)
  163. aids = []int64{10110817, 10110816}
  164. ip = "127.0.0.1"
  165. )
  166. convey.Convey("Views", t, func(ctx convey.C) {
  167. avm, err := d.Views(c, mid, aids, ip)
  168. ctx.Convey("Then err should be nil.avm should not be nil.", func(ctx convey.C) {
  169. ctx.So(err, convey.ShouldBeNil)
  170. ctx.So(avm, convey.ShouldNotBeNil)
  171. })
  172. })
  173. }
  174. func TestArchiveDel(t *testing.T) {
  175. var (
  176. c = context.TODO()
  177. mid = int64(2089809)
  178. aid = int64(10110817)
  179. ip = "127.0.0.1"
  180. )
  181. convey.Convey("Del", t, func(ctx convey.C) {
  182. err := d.Del(c, mid, aid, ip)
  183. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  184. ctx.So(err, convey.ShouldEqual, ecode.ArchiveAlreadyDel)
  185. })
  186. })
  187. }
  188. func TestArchiveVideoByCid(t *testing.T) {
  189. var (
  190. c = context.TODO()
  191. cid = int64(10134702)
  192. ip = "127.0.0.1"
  193. )
  194. convey.Convey("VideoByCid", t, func(ctx convey.C) {
  195. v, err := d.VideoByCid(c, cid, ip)
  196. ctx.Convey("Then err should be nil.v should not be nil.", func(ctx convey.C) {
  197. ctx.So(err, convey.ShouldBeNil)
  198. ctx.So(v, convey.ShouldNotBeNil)
  199. })
  200. })
  201. }
  202. func TestArchiveUpArchives(t *testing.T) {
  203. var (
  204. c = context.TODO()
  205. mid = int64(2089809)
  206. pn = int64(1)
  207. ps = int64(10)
  208. group = int64(0)
  209. ip = "127.0.0.1"
  210. )
  211. convey.Convey("UpArchives", t, func(ctx convey.C) {
  212. aids, count, err := d.UpArchives(c, mid, pn, ps, group, ip)
  213. ctx.Convey("Then err should be nil.aids,count should not be nil.", func(ctx convey.C) {
  214. ctx.So(err, convey.ShouldBeNil)
  215. ctx.So(count, convey.ShouldNotBeNil)
  216. ctx.So(aids, convey.ShouldNotBeNil)
  217. })
  218. })
  219. }
  220. func TestArchiveDescFormat(t *testing.T) {
  221. var (
  222. c = context.TODO()
  223. )
  224. convey.Convey("DescFormat", t, func(ctx convey.C) {
  225. descs, err := d.DescFormat(c)
  226. ctx.Convey("Then err should be nil.descs should not be nil.", func(ctx convey.C) {
  227. ctx.So(err, convey.ShouldBeNil)
  228. ctx.So(descs, convey.ShouldNotBeNil)
  229. })
  230. })
  231. }
  232. func TestArchiveVideoJam(t *testing.T) {
  233. var (
  234. c = context.TODO()
  235. ip = "127.0.0.1"
  236. )
  237. convey.Convey("VideoJam", t, func(ctx convey.C) {
  238. level, err := d.VideoJam(c, ip)
  239. ctx.Convey("Then err should be nil.level should not be nil.", func(ctx convey.C) {
  240. ctx.So(err, convey.ShouldBeNil)
  241. ctx.So(level, convey.ShouldNotBeNil)
  242. })
  243. })
  244. }