dao_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package weeklyhonor
  2. import (
  3. "flag"
  4. "os"
  5. "strings"
  6. "testing"
  7. "go-common/app/job/main/creative/conf"
  8. "github.com/golang/mock/gomock"
  9. "gopkg.in/h2non/gock.v1"
  10. )
  11. var (
  12. d *Dao
  13. )
  14. func TestMain(m *testing.M) {
  15. if os.Getenv("DEPLOY_ENV") != "" {
  16. flag.Set("app_id", "main.archive.creative-job")
  17. flag.Set("conf_token", "43943fda0bb311e8865c66d44b23cda7")
  18. flag.Set("tree_id", "16037")
  19. flag.Set("conf_version", "docker-1")
  20. flag.Set("deploy_env", "uat")
  21. flag.Set("conf_host", "config.bilibili.co")
  22. flag.Set("conf_path", "/tmp")
  23. flag.Set("region", "sh")
  24. flag.Set("zone", "sh001")
  25. } else {
  26. flag.Set("conf", "../../cmd/creative-job.toml")
  27. }
  28. flag.Parse()
  29. if err := conf.Init(); err != nil {
  30. panic(err)
  31. }
  32. d = New(conf.Conf)
  33. os.Exit(m.Run())
  34. }
  35. func httpMock(method, url string) *gock.Request {
  36. r := gock.New(url)
  37. r.Method = strings.ToUpper(method)
  38. d.httpClient.SetTransport(gock.DefaultTransport)
  39. return r
  40. }
  41. func WithMock(t *testing.T, f func(controller *gomock.Controller)) func() {
  42. ctrl := gomock.NewController(t)
  43. defer ctrl.Finish()
  44. return func() {
  45. f(ctrl)
  46. }
  47. }