dao_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package message
  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/message"
  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 TestGetUpList(t *testing.T) {
  46. var (
  47. c = context.TODO()
  48. err error
  49. mid = int64(2089809)
  50. ak, ck, ip string
  51. data []*message.Message
  52. res = &struct {
  53. Code int `json:"code"`
  54. Data []*message.Message `json:"data"`
  55. }{}
  56. )
  57. convey.Convey("1", t, func(ctx convey.C) {
  58. defer gock.OffAll()
  59. httpMock("Do", d.getUpListURL).Reply(-502)
  60. data, err = d.GetUpList(c, mid, ak, ck, ip)
  61. ctx.Convey("1", func(ctx convey.C) {
  62. ctx.So(err, convey.ShouldNotBeNil)
  63. ctx.So(data, convey.ShouldHaveLength, 0)
  64. })
  65. })
  66. convey.Convey("2", t, func(ctx convey.C) {
  67. defer gock.OffAll()
  68. res.Code = 0
  69. res.Data = append(res.Data, &message.Message{})
  70. js, _ := json.Marshal(res)
  71. ck = "iamck"
  72. httpMock("Do", d.getUpListURL).Reply(200).JSON(string(js))
  73. data, err = d.GetUpList(c, mid, ak, ck, ip)
  74. ctx.Convey("2", func(ctx convey.C) {
  75. ctx.So(err, convey.ShouldNotBeNil)
  76. ctx.So(data, convey.ShouldHaveLength, 0)
  77. })
  78. })
  79. }