dao_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "reflect"
  7. "testing"
  8. "go-common/app/job/main/passport-sns/conf"
  9. "go-common/library/database/sql"
  10. "github.com/bouk/monkey"
  11. "github.com/smartystreets/goconvey/convey"
  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.passport.passport-sns-job")
  19. flag.Set("conf_token", "5aa702b77df8a25d5af539aea08b2d03")
  20. flag.Set("tree_id", "80868")
  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/passport-sns-job.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 TestDaoPing(t *testing.T) {
  38. var c = context.Background()
  39. convey.Convey("Ping", t, func(ctx convey.C) {
  40. err := d.Ping(c)
  41. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  42. ctx.So(err, convey.ShouldBeNil)
  43. })
  44. })
  45. }
  46. func TestDaoClose(t *testing.T) {
  47. convey.Convey("Close", t, func(ctx convey.C) {
  48. monkey.PatchInstanceMethod(reflect.TypeOf(d.asoDB), "Close", func(_ *sql.DB) error {
  49. return nil
  50. })
  51. monkey.PatchInstanceMethod(reflect.TypeOf(d.snsDB), "Close", func(_ *sql.DB) error {
  52. return nil
  53. })
  54. defer monkey.UnpatchAll()
  55. var err error
  56. d.Close()
  57. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  58. ctx.So(err, convey.ShouldBeNil)
  59. })
  60. })
  61. }