dao_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package activity
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "go-common/app/interface/main/creative/conf"
  7. "go-common/app/interface/main/creative/model/activity"
  8. "os"
  9. "strings"
  10. "testing"
  11. "github.com/smartystreets/goconvey/convey"
  12. gock "gopkg.in/h2non/gock.v1"
  13. )
  14. var (
  15. d *Dao
  16. )
  17. func TestMain(m *testing.M) {
  18. if os.Getenv("DEPLOY_ENV") != "" {
  19. flag.Set("app_id", "main.archive.creative")
  20. flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
  21. flag.Set("tree_id", "2305")
  22. flag.Set("conf_version", "docker-1")
  23. flag.Set("deploy_env", "uat")
  24. flag.Set("conf_host", "config.bilibili.co")
  25. flag.Set("conf_path", "/tmp")
  26. flag.Set("region", "sh")
  27. flag.Set("zone", "sh001")
  28. } else {
  29. flag.Set("conf", "../../cmd/creative.toml")
  30. }
  31. flag.Parse()
  32. if err := conf.Init(); err != nil {
  33. panic(err)
  34. }
  35. d = New(conf.Conf)
  36. m.Run()
  37. os.Exit(0)
  38. }
  39. func httpMock(method, url string) *gock.Request {
  40. r := gock.New(url)
  41. r.Method = strings.ToUpper(method)
  42. d.client.SetTransport(gock.DefaultTransport)
  43. return r
  44. }
  45. func Test_Likes(t *testing.T) {
  46. var (
  47. c = context.TODO()
  48. err error
  49. missionID = int64(10364)
  50. likeCnt int
  51. )
  52. convey.Convey("Likes", t, func(ctx convey.C) {
  53. defer gock.OffAll()
  54. url := fmt.Sprintf(d.ActLikeURI, 10364)
  55. httpMock("GET", url).Reply(200).JSON(`{"code":0,"data":{"id":53,"mid":27515310,"uname":"1qs314567","state":2,"type":2,"position":4,"url":"http://i0.hdslb.com/bfs/article/578a61e7caf47b5deaa80940b2806f1d9ce53dde.png","md5":"79d02f08c2b7b0d2b30a6b6a4f61c97e","info":"{\"width\":325,\"height\":50}","ctime":1499764110,"mtime":1499828335},"message":"","ttl":1}`)
  56. likeCnt, err = d.Likes(c, missionID)
  57. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx convey.C) {
  58. ctx.So(err, convey.ShouldBeNil)
  59. ctx.So(likeCnt, convey.ShouldBeGreaterThanOrEqualTo, 0)
  60. })
  61. })
  62. }
  63. func Test_Protocol(t *testing.T) {
  64. var (
  65. c = context.TODO()
  66. err error
  67. missionID = int64(10364)
  68. p *activity.Protocol
  69. )
  70. convey.Convey("Protocol", t, func(ctx convey.C) {
  71. defer gock.OffAll()
  72. url := fmt.Sprintf(d.ActProtocolURI, 10364)
  73. httpMock("GET", url).Reply(200).JSON(`{"code":0,"data":{"id":"231","sid":"10364","protocol":"说明不超过201234567890123","types":"","tags":"","hot":"0","bgm_id":"0","paster_id":"0","oids":"","screen_set":"1"}}`)
  74. p, err = d.Protocol(c, missionID)
  75. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx convey.C) {
  76. ctx.So(err, convey.ShouldBeNil)
  77. ctx.So(p, convey.ShouldNotBeNil)
  78. })
  79. })
  80. }
  81. func Test_Subject(t *testing.T) {
  82. var (
  83. c = context.TODO()
  84. err error
  85. missionID = int64(10364)
  86. act *activity.Activity
  87. )
  88. convey.Convey("Subject", t, func(ctx convey.C) {
  89. defer gock.OffAll()
  90. url := fmt.Sprintf(d.ActSubjectURI, 10364)
  91. httpMock("GET", url).Reply(200).JSON(`{"code":0,"data":{"id":"231","sid":"10364","Subject":"说明不超过201234567890123","types":"","tags":"","hot":"0","bgm_id":"0","paster_id":"0","oids":"","screen_set":"1"}}`)
  92. act, err = d.Subject(c, missionID)
  93. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx convey.C) {
  94. ctx.So(err, convey.ShouldBeNil)
  95. ctx.So(act, convey.ShouldNotBeNil)
  96. })
  97. })
  98. }
  99. func Test_Activities(t *testing.T) {
  100. var (
  101. c = context.TODO()
  102. err error
  103. acts []*activity.Activity
  104. )
  105. convey.Convey("Activities", t, func(ctx convey.C) {
  106. defer gock.OffAll()
  107. url := d.ActAllListURI
  108. httpMock("GET", url).Reply(200).JSON(`{"code":0,"data":[{"id":10329,"oid":0,"type":4,"state":1,"stime":"2018-10-14 14:32:00","etime":"2018-12-17 00:00:00","ctime":"2018-08-30 18:03:30","mtime":"2018-10-17 18:27:44","name":"这是一个标题,123kkkk有点长有点长,可能展示不下,只能展示13个字","author":"jinchenchen","act_url":"","lstime":"2018-09-08 00:00:00","letime":"2018-12-01 00:00:00","cover":"//uat-i0.hdslb.com/bfs/test/static/20181017/fb8f33d1a41042b9a1ebb515fdc19d94/nBljdwyCo.jpg","dic":"sdf","flag":"33","uetime":"0000-00-00 00:00:00","ustime":"0000-00-00 00:00:00","level":"0","h5_cover":"","rank":"123","like_limit":"1","android_url":"","ios_url":"","fan_limit_max":"0","fan_limit_min":"0","tags":"","hot":0,"bgm_id":123,"paster_id":123,"oids":"10110549|10110164|10110536|10110546","screen_set":1,"protocol":"活动说明,123MMMMM展示展示,只能展示20个,展示不下,显示"}]}`)
  109. acts, err = d.Activities(c)
  110. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx convey.C) {
  111. ctx.So(err, convey.ShouldBeNil)
  112. ctx.So(acts, convey.ShouldNotBeNil)
  113. })
  114. })
  115. }
  116. func Test_MissionOnlineByTid(t *testing.T) {
  117. var (
  118. c = context.TODO()
  119. err error
  120. tid = int16(160)
  121. plat = int16(1)
  122. mm []*activity.ActWithTP
  123. )
  124. convey.Convey("MissionOnlineByTid", t, func(ctx convey.C) {
  125. defer gock.OffAll()
  126. url := d.ActOnlineByTypeURL
  127. httpMock("GET", url).Reply(200).JSON(`
  128. {
  129. "code": 0,
  130. "data": [
  131. {
  132. "id": 10329,
  133. "oid": 0,
  134. "type": 4,
  135. "state": 1,
  136. "stime": "2018-10-14 14:32:00",
  137. "etime": "2018-12-17 00:00:00",
  138. "ctime": "2018-08-30 18:03:30",
  139. "mtime": "2018-10-17 18:27:44",
  140. "name": "这是一个",
  141. "author": "jinchenchen",
  142. "act_url": "",
  143. "lstime": "2018-09-08 00:00:00",
  144. "letime": "2018-12-01 00:00:00",
  145. "cover": "//uat-i0.hdslb.com/bfs/test/static/20181017/fb8f33d1a41042b9a1ebb515fdc19d94/nBljdwyCo.jpg",
  146. "dic": "sdf",
  147. "flag": "33",
  148. "uetime": "0000-00-00 00:00:00",
  149. "ustime": "0000-00-00 00:00:00",
  150. "level": "0",
  151. "h5_cover": "",
  152. "rank": "123",
  153. "like_limit": "1",
  154. "android_url": "",
  155. "ios_url": "",
  156. "fan_limit_max": "0",
  157. "fan_limit_min": "0",
  158. "tags": "",
  159. "types": "",
  160. "hot": 0,
  161. "bgm_id": 123,
  162. "paster_id": 123,
  163. "oids": "10110549|10110164|10110536|10110546",
  164. "screen_set": 1,
  165. "protocol": "活动说明"
  166. }
  167. ]
  168. }
  169. `)
  170. mm, err = d.MissionOnlineByTid(c, tid, plat)
  171. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx convey.C) {
  172. ctx.So(err, convey.ShouldBeNil)
  173. ctx.So(mm, convey.ShouldNotBeNil)
  174. })
  175. })
  176. }
  177. func Test_Unbind(t *testing.T) {
  178. var (
  179. c = context.TODO()
  180. err error
  181. missionID = int64(10364)
  182. aid = int64(10110788)
  183. ip = "127.0.0.1"
  184. )
  185. convey.Convey("Unbind", t, func(ctx convey.C) {
  186. defer gock.OffAll()
  187. url := fmt.Sprintf(d.ActUpdateURI, 10364)
  188. httpMock("POST", url).Reply(200).JSON(`{"code":0,"data":""}`)
  189. err = d.Unbind(c, aid, missionID, ip)
  190. ctx.Convey("Then err should be nil.has should not be nil.", func(ctx convey.C) {
  191. ctx.So(err, convey.ShouldBeNil)
  192. })
  193. })
  194. }