dao_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package order
  2. import (
  3. "context"
  4. "flag"
  5. "go-common/app/interface/main/videoup/conf"
  6. "os"
  7. "strings"
  8. "testing"
  9. "github.com/smartystreets/goconvey/convey"
  10. gock "gopkg.in/h2non/gock.v1"
  11. )
  12. var (
  13. d *Dao
  14. )
  15. func TestMain(m *testing.M) {
  16. if os.Getenv("DEPLOY_ENV") != "" {
  17. flag.Set("app_id", "main.archive.videoup")
  18. flag.Set("conf_token", "9772c9629b00ac09af29a23004795051")
  19. flag.Set("tree_id", "2306")
  20. flag.Set("conf_version", "docker-1")
  21. flag.Set("deploy_env", "uat")
  22. flag.Set("conf_host", "config.bilibili.co")
  23. flag.Set("conf_path", "/tmp")
  24. flag.Set("region", "sh")
  25. flag.Set("zone", "sh001")
  26. } else {
  27. flag.Set("conf", "../cmd/videoup.toml")
  28. }
  29. flag.Parse()
  30. if err := conf.Init(); err != nil {
  31. panic(err)
  32. }
  33. d = New(conf.Conf)
  34. m.Run()
  35. os.Exit(0)
  36. }
  37. func httpMock(method, url string) *gock.Request {
  38. r := gock.New(url)
  39. r.Method = strings.ToUpper(method)
  40. d.client.SetTransport(gock.DefaultTransport)
  41. return r
  42. }
  43. func Test_PubTime(t *testing.T) {
  44. convey.Convey("PubTime", t, func(ctx convey.C) {
  45. var (
  46. err error
  47. c = context.Background()
  48. mid = int64(2089809)
  49. orderID = int64(111)
  50. ip = "127.0.0.1"
  51. )
  52. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  53. defer gock.OffAll()
  54. httpMock("Post", d.launchTimeURI).Reply(200).JSON(`{"code":21022,"data":""}`)
  55. _, err = d.PubTime(c, mid, orderID, ip)
  56. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  57. ctx.So(err, convey.ShouldNotBeNil)
  58. })
  59. })
  60. })
  61. }
  62. func Test_BindOrder(t *testing.T) {
  63. convey.Convey("BindOrder", t, func(ctx convey.C) {
  64. var (
  65. err error
  66. c = context.Background()
  67. mid = int64(2089809)
  68. aid = int64(10110826)
  69. orderID = int64(111)
  70. ip = "127.0.0.1"
  71. )
  72. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  73. defer gock.OffAll()
  74. httpMock("Post", d.useExeOrderURI).Reply(200).JSON(`{"code":21022,"data":""}`)
  75. err = d.BindOrder(c, mid, aid, orderID, ip)
  76. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  77. ctx.So(err, convey.ShouldNotBeNil)
  78. })
  79. })
  80. })
  81. }
  82. func Test_Ups(t *testing.T) {
  83. convey.Convey("Ups", t, func(ctx convey.C) {
  84. var (
  85. err error
  86. c = context.Background()
  87. )
  88. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  89. defer gock.OffAll()
  90. httpMock("GET", d.upsURI).Reply(200).JSON(`{"code":21022,"data":""}`)
  91. _, err = d.Ups(c)
  92. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  93. ctx.So(err, convey.ShouldNotBeNil)
  94. })
  95. })
  96. })
  97. }
  98. func Test_ExecuteOrders(t *testing.T) {
  99. convey.Convey("ExecuteOrders", t, func(ctx convey.C) {
  100. var (
  101. err error
  102. c = context.Background()
  103. mid = int64(2089809)
  104. ip = "127.0.0.1"
  105. )
  106. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  107. defer gock.OffAll()
  108. httpMock("GET", d.executeOrdersURI).Reply(200).JSON(`{"code":21022,"data":""}`)
  109. _, err = d.ExecuteOrders(c, mid, ip)
  110. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  111. ctx.So(err, convey.ShouldNotBeNil)
  112. })
  113. })
  114. })
  115. }