dao_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package ads
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "testing"
  7. "go-common/app/service/main/resource/conf"
  8. "go-common/library/cache/redis"
  9. "github.com/smartystreets/goconvey/convey"
  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.app-svr.resource-service")
  17. flag.Set("conf_token", "y79sErNhxggjvULS0O8Czas9PaxHBF5o")
  18. flag.Set("tree_id", "3232")
  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/resource-service-test.toml")
  27. }
  28. flag.Parse()
  29. if err := conf.Init(); err != nil {
  30. panic(err)
  31. }
  32. d = New(conf.Conf)
  33. m.Run()
  34. os.Exit(0)
  35. }
  36. // func CleanCache() {
  37. // pool := redis.NewPool(conf.Conf.Redis.Ads.Config)
  38. // pool.Get(context.TODO()).Do("FLUSHDB")
  39. // }
  40. func WithDao(f func(d *Dao)) func() {
  41. return func() {
  42. convey.Reset(func() {
  43. pool := redis.NewPool(conf.Conf.Redis.Ads.Config)
  44. pool.Get(context.TODO()).Do("FLUSHDB")
  45. })
  46. f(d)
  47. }
  48. }
  49. func TestDaoPing(t *testing.T) {
  50. convey.Convey("Ping", t, func(ctx convey.C) {
  51. err := d.Ping(context.TODO())
  52. ctx.Convey("Err should be nil", func() {
  53. ctx.So(err, convey.ShouldBeNil)
  54. })
  55. })
  56. }