dao_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package medal
  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/medal"
  8. "go-common/library/ecode"
  9. "io"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "testing"
  16. "github.com/bouk/monkey"
  17. "github.com/smartystreets/goconvey/convey"
  18. gock "gopkg.in/h2non/gock.v1"
  19. )
  20. var (
  21. d *Dao
  22. )
  23. func TestMain(m *testing.M) {
  24. if os.Getenv("DEPLOY_ENV") != "" {
  25. flag.Set("app_id", "main.archive.creative")
  26. flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
  27. flag.Set("tree_id", "2305")
  28. flag.Set("conf_version", "docker-1")
  29. flag.Set("deploy_env", "uat")
  30. flag.Set("conf_host", "config.bilibili.co")
  31. flag.Set("conf_path", "/tmp")
  32. flag.Set("region", "sh")
  33. flag.Set("zone", "sh001")
  34. } else {
  35. flag.Set("conf", "../../cmd/creative.toml")
  36. }
  37. flag.Parse()
  38. if err := conf.Init(); err != nil {
  39. panic(err)
  40. }
  41. d = New(conf.Conf)
  42. m.Run()
  43. os.Exit(0)
  44. }
  45. func httpMock(method, url string) *gock.Request {
  46. r := gock.New(url)
  47. r.Method = strings.ToUpper(method)
  48. d.client.SetTransport(gock.DefaultTransport)
  49. return r
  50. }
  51. func TestRank(t *testing.T) {
  52. var (
  53. c = context.TODO()
  54. err error
  55. mid int64
  56. data = make([]*medal.FansRank, 0)
  57. res = &struct {
  58. Code int `json:"code"`
  59. Data []*medal.FansRank `json:"data"`
  60. }{}
  61. )
  62. convey.Convey("1", t, func(ctx convey.C) {
  63. defer gock.OffAll()
  64. httpMock("Do", d.fansRankURI).Reply(-502)
  65. data, err = d.Rank(c, mid)
  66. ctx.Convey("1", func(ctx convey.C) {
  67. ctx.So(err, convey.ShouldNotBeNil)
  68. ctx.So(data, convey.ShouldBeNil)
  69. })
  70. })
  71. convey.Convey("2", t, func(ctx convey.C) {
  72. defer gock.OffAll()
  73. res.Code = 0
  74. res.Data = append(res.Data, &medal.FansRank{})
  75. js, _ := json.Marshal(res)
  76. httpMock("Do", d.fansRankURI).Reply(200).JSON(string(js))
  77. data, err = d.Rank(c, mid)
  78. ctx.Convey("2", func(ctx convey.C) {
  79. ctx.So(err, convey.ShouldNotBeNil)
  80. ctx.So(data, convey.ShouldBeNil)
  81. })
  82. })
  83. }
  84. func TestRename(t *testing.T) {
  85. var (
  86. c = context.TODO()
  87. err error
  88. mid int64
  89. name, ck, ak string
  90. res = &struct {
  91. Code int `json:"code"`
  92. }{}
  93. )
  94. convey.Convey("1", t, func(ctx convey.C) {
  95. defer gock.OffAll()
  96. httpMock("Do", d.fansRankURI).Reply(-502)
  97. err = d.Rename(c, mid, name, ak, ck)
  98. ctx.Convey("1", func(ctx convey.C) {
  99. ctx.So(err, convey.ShouldNotBeNil)
  100. })
  101. })
  102. convey.Convey("2", t, func(ctx convey.C) {
  103. defer gock.OffAll()
  104. res.Code = 0
  105. ak = "iamak"
  106. js, _ := json.Marshal(res)
  107. httpMock("Do", d.fansRankURI).Reply(200).JSON(string(js))
  108. err = d.Rename(c, mid, name, ak, ck)
  109. ctx.Convey("2", func(ctx convey.C) {
  110. ctx.So(err, convey.ShouldNotBeNil)
  111. })
  112. })
  113. }
  114. func TestRecentFans(t *testing.T) {
  115. var (
  116. c = context.TODO()
  117. err error
  118. mid = int64(2089809)
  119. data = make([]*medal.RecentFans, 0)
  120. res = &struct {
  121. Code int `json:"code"`
  122. Data []*medal.RecentFans `json:"data"`
  123. }{}
  124. )
  125. convey.Convey("1", t, func(ctx convey.C) {
  126. defer gock.OffAll()
  127. httpMock("Do", d.recentFansURI).Reply(-502)
  128. data, err = d.RecentFans(c, mid)
  129. ctx.Convey("1", func(ctx convey.C) {
  130. ctx.So(err, convey.ShouldNotBeNil)
  131. ctx.So(data, convey.ShouldBeNil)
  132. })
  133. })
  134. convey.Convey("2", t, func(ctx convey.C) {
  135. defer gock.OffAll()
  136. res.Code = 0
  137. res.Data = append(res.Data, &medal.RecentFans{})
  138. js, _ := json.Marshal(res)
  139. httpMock("Do", d.recentFansURI).Reply(200).JSON(string(js))
  140. data, err = d.RecentFans(c, mid)
  141. ctx.Convey("2", func(ctx convey.C) {
  142. ctx.So(err, convey.ShouldNotBeNil)
  143. ctx.So(data, convey.ShouldBeNil)
  144. })
  145. })
  146. convey.Convey("3", t, func(ctx convey.C) {
  147. req := &http.Request{
  148. Header: make(map[string][]string),
  149. }
  150. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  151. monkeyNewGetRequest(req, ecode.CreativeFansMedalErr)
  152. data, err = d.RecentFans(c, mid)
  153. ctx.Convey("3", func(ctx convey.C) {
  154. ctx.So(err, convey.ShouldNotBeNil)
  155. ctx.So(data, convey.ShouldBeNil)
  156. })
  157. })
  158. }
  159. func monkeyNewGetRequest(req *http.Request, err error) {
  160. monkey.Patch(http.NewRequest, func(_, _ string, _ io.Reader) (*http.Request, error) {
  161. return req, err
  162. })
  163. }
  164. func TestMedal(t *testing.T) {
  165. var (
  166. c = context.TODO()
  167. err error
  168. mid = int64(2089809)
  169. data = &medal.Medal{}
  170. res = &struct {
  171. Code int `json:"code"`
  172. Data *medal.Medal `json:"data"`
  173. }{}
  174. )
  175. convey.Convey("1", t, func(ctx convey.C) {
  176. defer gock.OffAll()
  177. httpMock("Do", d.getMedalURI).Reply(-502)
  178. data, err = d.Medal(c, mid)
  179. ctx.Convey("1", func(ctx convey.C) {
  180. ctx.So(err, convey.ShouldNotBeNil)
  181. ctx.So(data, convey.ShouldBeNil)
  182. })
  183. })
  184. convey.Convey("2", t, func(ctx convey.C) {
  185. defer gock.OffAll()
  186. res.Code = 0
  187. res.Data = &medal.Medal{
  188. UID: "2089809",
  189. }
  190. js, _ := json.Marshal(res)
  191. httpMock("Do", d.getMedalURI).Reply(200).JSON(string(js))
  192. data, err = d.Medal(c, mid)
  193. ctx.Convey("2", func(ctx convey.C) {
  194. ctx.So(err, convey.ShouldNotBeNil)
  195. ctx.So(data, convey.ShouldBeNil)
  196. })
  197. })
  198. }
  199. func TestStatus(t *testing.T) {
  200. var (
  201. c = context.TODO()
  202. err error
  203. mid = int64(2089809)
  204. data *medal.Status
  205. res = &struct {
  206. Code int `json:"code"`
  207. Data *medal.Status `json:"data"`
  208. }{}
  209. )
  210. convey.Convey("1", t, func(ctx convey.C) {
  211. defer gock.OffAll()
  212. httpMock("Do", d.checkStatusURI).Reply(-502)
  213. data, err = d.Status(c, mid)
  214. ctx.Convey("1", func(ctx convey.C) {
  215. ctx.So(err, convey.ShouldNotBeNil)
  216. ctx.So(data, convey.ShouldBeNil)
  217. })
  218. })
  219. convey.Convey("2", t, func(ctx convey.C) {
  220. defer gock.OffAll()
  221. res.Code = 20032
  222. js, _ := json.Marshal(res)
  223. httpMock("Do", d.checkStatusURI).Reply(200).JSON(string(js))
  224. data, err = d.Status(c, mid)
  225. ctx.Convey("2", func(ctx convey.C) {
  226. ctx.So(err, convey.ShouldNotBeNil)
  227. ctx.So(data, convey.ShouldBeNil)
  228. ctx.So(res, convey.ShouldNotBeNil)
  229. })
  230. })
  231. }
  232. func TestOpenMedal(t *testing.T) {
  233. var (
  234. c = context.TODO()
  235. err error
  236. mid int64
  237. name string
  238. res = &struct {
  239. Code int `json:"code"`
  240. Msg string `json:"msg"`
  241. }{}
  242. )
  243. convey.Convey("1", t, func(ctx convey.C) {
  244. defer gock.OffAll()
  245. httpMock("Do", d.openMedalURI).Reply(-502)
  246. err = d.OpenMedal(c, mid, name)
  247. ctx.Convey("1", func(ctx convey.C) {
  248. ctx.So(err, convey.ShouldNotBeNil)
  249. })
  250. })
  251. convey.Convey("2", t, func(ctx convey.C) {
  252. defer gock.OffAll()
  253. res.Code = 0
  254. res.Msg = "ok"
  255. js, _ := json.Marshal(res)
  256. httpMock("Do", d.openMedalURI).Reply(200).JSON(string(js))
  257. err = d.OpenMedal(c, mid, name)
  258. ctx.Convey("2", func(ctx convey.C) {
  259. ctx.So(err, convey.ShouldNotBeNil)
  260. })
  261. })
  262. convey.Convey("3", t, func(ctx convey.C) {
  263. res.Code = 0
  264. res.Msg = "ok"
  265. req := &http.Request{
  266. URL: &url.URL{},
  267. Header: make(map[string][]string),
  268. }
  269. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  270. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  271. monkeyNewGetRequest(req, nil)
  272. err = d.OpenMedal(c, mid, name)
  273. ctx.Convey("3", func(ctx convey.C) {
  274. ctx.So(err, convey.ShouldNotBeNil)
  275. })
  276. })
  277. }
  278. func TestCheckMedal(t *testing.T) {
  279. var (
  280. c = context.TODO()
  281. err error
  282. mid int64
  283. valid int
  284. name string
  285. res = &struct {
  286. Code int `json:"code"`
  287. Data struct {
  288. Enable bool `json:"enable"`
  289. } `json:"data"`
  290. }{}
  291. )
  292. convey.Convey("1", t, func(ctx convey.C) {
  293. defer gock.OffAll()
  294. httpMock("Do", d.checkMedalURI).Reply(-502)
  295. valid, err = d.CheckMedal(c, mid, name)
  296. ctx.Convey("1", func(ctx convey.C) {
  297. ctx.So(err, convey.ShouldNotBeNil)
  298. ctx.So(valid, convey.ShouldBeGreaterThanOrEqualTo, 0)
  299. })
  300. })
  301. convey.Convey("2", t, func(ctx convey.C) {
  302. defer gock.OffAll()
  303. res.Code = 0
  304. js, _ := json.Marshal(res)
  305. httpMock("Do", d.checkMedalURI).Reply(200).JSON(string(js))
  306. valid, err = d.CheckMedal(c, mid, name)
  307. ctx.Convey("2", func(ctx convey.C) {
  308. ctx.So(err, convey.ShouldNotBeNil)
  309. ctx.So(valid, convey.ShouldBeGreaterThanOrEqualTo, 0)
  310. })
  311. })
  312. convey.Convey("3", t, func(ctx convey.C) {
  313. res.Code = 0
  314. req := &http.Request{
  315. URL: &url.URL{},
  316. Header: make(map[string][]string),
  317. }
  318. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  319. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  320. monkeyNewGetRequest(req, ecode.CreativeFansMedalErr)
  321. valid, err = d.CheckMedal(c, mid, name)
  322. ctx.Convey("3", func(ctx convey.C) {
  323. ctx.So(err, convey.ShouldNotBeNil)
  324. ctx.So(valid, convey.ShouldBeGreaterThanOrEqualTo, 0)
  325. })
  326. })
  327. }