dao_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "math/big"
  6. "net"
  7. "os"
  8. "reflect"
  9. "testing"
  10. "go-common/app/job/main/passport/conf"
  11. "go-common/library/database/sql"
  12. "github.com/bouk/monkey"
  13. "github.com/smartystreets/goconvey/convey"
  14. "go-common/library/database/hbase.v2"
  15. )
  16. var (
  17. d *Dao
  18. )
  19. func TestMain(m *testing.M) {
  20. if os.Getenv("DEPLOY_ENV") != "" {
  21. flag.Set("app_id", "main.passport.passport-user-job")
  22. flag.Set("conf_token", "f5c791689788882beaef2903735949ea")
  23. flag.Set("tree_id", "3074")
  24. flag.Set("conf_version", "docker-1")
  25. flag.Set("deploy_env", "uat")
  26. flag.Set("conf_host", "config.bilibili.co")
  27. flag.Set("conf_path", "/tmp")
  28. flag.Set("region", "sh")
  29. flag.Set("zone", "sh001")
  30. } else {
  31. flag.Set("conf", "../cmd/passport-job.toml")
  32. }
  33. flag.Parse()
  34. if err := conf.Init(); err != nil {
  35. panic(err)
  36. }
  37. d = New(conf.Conf)
  38. os.Exit(m.Run())
  39. }
  40. // InetAtoN convert ip addr to int64.
  41. func InetAtoN(ip string) int64 {
  42. ret := big.NewInt(0)
  43. ret.SetBytes(net.ParseIP(ip).To4())
  44. return ret.Int64()
  45. }
  46. func TestDaoPing(t *testing.T) {
  47. var c = context.Background()
  48. convey.Convey("Ping", t, func(ctx convey.C) {
  49. err := d.Ping(c)
  50. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  51. ctx.So(err, convey.ShouldBeNil)
  52. })
  53. })
  54. }
  55. func TestDaoClose(t *testing.T) {
  56. convey.Convey("Close", t, func(ctx convey.C) {
  57. monkey.PatchInstanceMethod(reflect.TypeOf(d.logDB), "Close", func(_ *sql.DB) error {
  58. return nil
  59. })
  60. monkey.PatchInstanceMethod(reflect.TypeOf(d.loginLogHBase), "Close", func(_ *hbase.Client) error {
  61. return nil
  62. })
  63. monkey.PatchInstanceMethod(reflect.TypeOf(d.pwdLogHBase), "Close", func(_ *hbase.Client) error {
  64. return nil
  65. })
  66. defer monkey.UnpatchAll()
  67. var err error
  68. d.Close()
  69. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  70. ctx.So(err, convey.ShouldBeNil)
  71. })
  72. })
  73. }