dao_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "reflect"
  7. "testing"
  8. "go-common/app/service/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-service")
  19. flag.Set("conf_token", "f0a341c60edec0e51fd0fbb731c00b39")
  20. flag.Set("tree_id", "80847")
  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-service.toml")
  29. }
  30. flag.Parse()
  31. if err := conf.Init(); err != nil {
  32. panic(err)
  33. }
  34. d = New(conf.Conf)
  35. m.Run()
  36. os.Exit(0)
  37. }
  38. func TestDaoPing(t *testing.T) {
  39. var c = context.Background()
  40. convey.Convey("Ping", t, func(ctx convey.C) {
  41. err := d.Ping(c)
  42. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  43. ctx.So(err, convey.ShouldBeNil)
  44. })
  45. })
  46. }
  47. func TestDaoClose(t *testing.T) {
  48. convey.Convey("Close", t, func(ctx convey.C) {
  49. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Close", func(_ *sql.DB) error {
  50. return nil
  51. })
  52. defer monkey.UnpatchAll()
  53. var err error
  54. d.Close()
  55. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  56. ctx.So(err, convey.ShouldBeNil)
  57. })
  58. })
  59. }
  60. func TestDaoBeginTran(t *testing.T) {
  61. var c = context.Background()
  62. convey.Convey("BeginTran", t, func(ctx convey.C) {
  63. res, err := d.BeginTran(c)
  64. ctx.Convey("Then err should be nil. res should not be nil.", func(ctx convey.C) {
  65. ctx.So(err, convey.ShouldBeNil)
  66. ctx.So(res, convey.ShouldNotBeNil)
  67. })
  68. })
  69. }