dao_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package vip
  2. import (
  3. "context"
  4. "flag"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/service/live/xuser/conf"
  7. "math/rand"
  8. "path/filepath"
  9. "testing"
  10. )
  11. // vip dao and conf
  12. var (
  13. d *Dao
  14. )
  15. type TestUser struct {
  16. Uid int64
  17. }
  18. // initd init vip dao
  19. func initd() {
  20. dir, _ := filepath.Abs("../../cmd/test.toml")
  21. flag.Set("conf", dir)
  22. flag.Set("deploy_env", "uat")
  23. conf.Init()
  24. d = New(conf.Conf)
  25. }
  26. func initTestUser() *TestUser {
  27. return &TestUser{
  28. Uid: int64(rand.Int31()),
  29. }
  30. }
  31. func (t *TestUser) Reset() {
  32. d.ClearCache(context.Background(), t.Uid)
  33. d.deleteVip(context.Background(), t.Uid)
  34. }
  35. func testWithTestUser(f func(u *TestUser)) func() {
  36. u := initTestUser()
  37. return func() {
  38. f(u)
  39. u.Reset()
  40. }
  41. }
  42. func TestToInt(t *testing.T) {
  43. var (
  44. err error
  45. out int
  46. )
  47. Convey("test toInt", t, func() {
  48. out, err = toInt(1)
  49. So(out, ShouldEqual, 1)
  50. So(err, ShouldBeNil)
  51. })
  52. Convey("test toInt", t, func() {
  53. out, err = toInt("123")
  54. So(out, ShouldEqual, 123)
  55. So(err, ShouldBeNil)
  56. })
  57. Convey("test toInt", t, func() {
  58. out, err = toInt("test")
  59. So(out, ShouldEqual, 0)
  60. So(err, ShouldNotBeNil)
  61. })
  62. }