dao_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package dynamic
  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_LotteryBind(t *testing.T) {
  44. convey.Convey("LotteryBind", t, func(ctx convey.C) {
  45. var (
  46. c = context.Background()
  47. aid = int64(10110826)
  48. mid = int64(2089809)
  49. lid = 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.LotteryBindURL).Reply(200).JSON(`{"code":0,"data":""}`)
  55. err := d.LotteryBind(c, lid, aid, mid, ip)
  56. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  57. ctx.So(err, convey.ShouldBeNil)
  58. })
  59. })
  60. })
  61. }
  62. func Test_UserCheck(t *testing.T) {
  63. convey.Convey("UserCheck", t, func(ctx convey.C) {
  64. var (
  65. err error
  66. c = context.Background()
  67. mid = int64(2089809)
  68. ip = "127.0.0.1"
  69. ret int
  70. )
  71. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  72. defer gock.OffAll()
  73. httpMock("GET", d.UserCheckURL).Reply(200).JSON(`{"code":0,"data":{"result":1}}`)
  74. ret, err = d.UserCheck(c, mid, ip)
  75. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  76. ctx.So(err, convey.ShouldBeNil)
  77. ctx.So(ret, convey.ShouldNotBeNil)
  78. })
  79. })
  80. })
  81. }