dao_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package lottery
  2. import (
  3. "context"
  4. "flag"
  5. "go-common/app/interface/main/creative/model/lottery"
  6. "gopkg.in/h2non/gock.v1"
  7. "os"
  8. "strings"
  9. "testing"
  10. "github.com/smartystreets/goconvey/convey"
  11. "go-common/app/interface/main/creative/conf"
  12. )
  13. var (
  14. d *Dao
  15. )
  16. func TestMain(m *testing.M) {
  17. if os.Getenv("DEPLOY_ENV") != "" {
  18. flag.Set("app_id", "main.archive.creative")
  19. flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
  20. flag.Set("tree_id", "2305")
  21. flag.Set("conf_version", "docker-1")
  22. flag.Set("deploy_env", "uat")
  23. flag.Set("conf_host", "config.bilibili.co")
  24. flag.Set("conf_path", "/tmp")
  25. flag.Set("region", "sh")
  26. flag.Set("zone", "sh001")
  27. } else {
  28. flag.Set("conf", "../../cmd/creative.toml")
  29. }
  30. flag.Parse()
  31. if err := conf.Init(); err != nil {
  32. panic(err)
  33. }
  34. d = New(conf.Conf)
  35. os.Exit(m.Run())
  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. d.client.SetTransport(gock.DefaultTransport)
  42. return r
  43. }
  44. func TestLotteryUserCheck(t *testing.T) {
  45. convey.Convey("UserCheck", t, func(ctx convey.C) {
  46. var (
  47. c = context.Background()
  48. mid = int64(0)
  49. ip = ""
  50. res = struct {
  51. Code int `json:"code"`
  52. Data struct {
  53. Result int `json:"result"`
  54. } `json:"data"`
  55. }{
  56. Code: 0,
  57. Data: struct {
  58. Result int `json:"result"`
  59. }{Result: 100},
  60. }
  61. )
  62. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  63. httpMock("GET", d.UserCheckURL).Reply(200).JSON(res)
  64. ret, err := d.UserCheck(c, mid, ip)
  65. ctx.Convey("Then err should be nil.ret should not be nil.", func(ctx convey.C) {
  66. ctx.So(err, convey.ShouldBeNil)
  67. ctx.So(ret, convey.ShouldNotBeNil)
  68. })
  69. })
  70. })
  71. }
  72. func TestLotteryNotice(t *testing.T) {
  73. convey.Convey("Notice", t, func(ctx convey.C) {
  74. var (
  75. c = context.Background()
  76. aid = int64(0)
  77. mid = int64(0)
  78. ip = ""
  79. res = struct {
  80. Code int `json:"code"`
  81. Data *lottery.Notice `json:"data"`
  82. }{
  83. Code: 0,
  84. Data: &lottery.Notice{
  85. BizID: 0,
  86. Status: 0,
  87. },
  88. }
  89. )
  90. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  91. httpMock("GET", d.NoticeURL).Reply(200).JSON(res)
  92. ret, err := d.Notice(c, aid, mid, ip)
  93. ctx.Convey("Then err should be nil.ret should not be nil.", func(ctx convey.C) {
  94. ctx.So(err, convey.ShouldBeNil)
  95. ctx.So(ret, convey.ShouldNotBeNil)
  96. })
  97. })
  98. })
  99. }