dao_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package manager
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "testing"
  7. "go-common/app/service/main/up/conf"
  8. "go-common/app/service/main/up/dao"
  9. "go-common/app/service/main/up/dao/global"
  10. "go-common/library/net/http/blademaster"
  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", dao.AppID)
  19. flag.Set("conf_token", dao.UatToken)
  20. flag.Set("tree_id", dao.TreeID)
  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/up-service.toml")
  29. }
  30. if os.Getenv("UT_LOCAL_TEST") != "" {
  31. flag.Set("conf", "../../cmd/up-service.toml")
  32. }
  33. flag.Parse()
  34. if err := conf.Init(); err != nil {
  35. panic(err)
  36. }
  37. global.Init(conf.Conf)
  38. d = New(conf.Conf)
  39. d.HTTPClient = blademaster.NewClient(conf.Conf.HTTPClient.Normal)
  40. m.Run()
  41. os.Exit(0)
  42. }
  43. func TestManagerPing(t *testing.T) {
  44. var (
  45. c = context.TODO()
  46. )
  47. convey.Convey("Ping", t, func(ctx convey.C) {
  48. err := d.Ping(c)
  49. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  50. ctx.So(err, convey.ShouldBeNil)
  51. })
  52. })
  53. }
  54. func TestManagerprepareAndExec(t *testing.T) {
  55. var (
  56. c = context.TODO()
  57. db = d.managerDB
  58. sqlstr = _selectByID
  59. args = interface{}(0)
  60. )
  61. convey.Convey("prepareAndExec", t, func(ctx convey.C) {
  62. res, err := prepareAndExec(c, db, sqlstr, args)
  63. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  64. ctx.So(err, convey.ShouldBeNil)
  65. ctx.So(res, convey.ShouldNotBeNil)
  66. })
  67. })
  68. }
  69. func TestManagerprepareAndQuery(t *testing.T) {
  70. var (
  71. c = context.TODO()
  72. db = d.managerDB
  73. sqlstr = _selectByID
  74. args = interface{}(0)
  75. )
  76. convey.Convey("prepareAndQuery", t, func(ctx convey.C) {
  77. rows, err := prepareAndQuery(c, db, sqlstr, args)
  78. ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
  79. ctx.So(err, convey.ShouldBeNil)
  80. ctx.So(rows, convey.ShouldNotBeNil)
  81. })
  82. })
  83. }
  84. func TestManagerGetUNamesByUids(t *testing.T) {
  85. var (
  86. c = context.TODO()
  87. uids = []int64{100}
  88. )
  89. convey.Convey("GetUNamesByUids", t, func(ctx convey.C) {
  90. res, err := d.GetUNamesByUids(c, uids)
  91. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  92. ctx.So(err, convey.ShouldBeNil)
  93. ctx.So(res, convey.ShouldNotBeNil)
  94. })
  95. })
  96. }
  97. func TestManagerGetUIDByNames(t *testing.T) {
  98. var (
  99. c = context.TODO()
  100. names = []string{"wangzhe01"}
  101. )
  102. convey.Convey("GetUIDByNames", t, func(ctx convey.C) {
  103. res, err := d.GetUIDByNames(c, names)
  104. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  105. ctx.So(err, convey.ShouldBeNil)
  106. ctx.So(res, convey.ShouldNotBeNil)
  107. })
  108. })
  109. }