mysql_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package vip
  2. import (
  3. "context"
  4. "fmt"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/service/live/xuser/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. "math/rand"
  11. "testing"
  12. "time"
  13. )
  14. func Test_getUserLevelTable(t *testing.T) {
  15. initd()
  16. Convey("test get user_x table name by uid", t, func() {
  17. var uid = int64(123)
  18. table := getUserLevelTable(uid)
  19. So(table, ShouldEqual, "user_2")
  20. })
  21. }
  22. func Test_getUserVipRecordTable(t *testing.T) {
  23. initd()
  24. Convey("test get user_vip_record_x table name by uid", t, func() {
  25. var uid = rand.Int63()
  26. log.Info("Test_getUserVipRecordTable uid(%d)", uid)
  27. table := getUserVipRecordTable(uid)
  28. t := fmt.Sprintf(_userVipRecordPrefix, uid%_userVipRecordCount)
  29. So(table, ShouldEqual, t)
  30. })
  31. }
  32. func TestDao_GetVipFromDB(t *testing.T) {
  33. initd()
  34. Convey("test get vip from db", t, testWithTestUser(func(u *TestUser) {
  35. log.Info("TestDao_GetVipFromDB uid(%d), table(%s)", u.Uid, getUserLevelTable(u.Uid))
  36. var (
  37. ctx = context.Background()
  38. err error
  39. info *model.VipInfo
  40. )
  41. // delete random uid at begin
  42. err = d.deleteVip(ctx, u.Uid)
  43. So(err, ShouldBeNil)
  44. // get nil result from db
  45. Convey("get nil result from db", func() {
  46. info, err = d.GetVipFromDB(ctx, u.Uid)
  47. So(err, ShouldResemble, sql.ErrNoRows)
  48. So(info, ShouldNotBeNil)
  49. So(info.Vip, ShouldEqual, 0)
  50. So(info.VipTime, ShouldEqual, "")
  51. So(info.Svip, ShouldEqual, 0)
  52. So(info.SvipTime, ShouldEqual, "")
  53. })
  54. // insert and then get
  55. Convey("insert and then get", func() {
  56. var info2 *model.VipInfo
  57. info = &model.VipInfo{
  58. Vip: 1,
  59. VipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
  60. Svip: 1,
  61. SvipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
  62. }
  63. err = d.createVip(ctx, u.Uid, info)
  64. So(err, ShouldBeNil)
  65. info2, err = d.GetVipFromDB(ctx, u.Uid)
  66. log.Info("TestDao_GetVipFromDB info2(%v)", info2)
  67. So(err, ShouldBeNil)
  68. So(info, ShouldResemble, info2)
  69. })
  70. // valid info but expired vip time
  71. Convey("insert valid but expired vip time", func() {
  72. info = &model.VipInfo{
  73. Vip: 1,
  74. VipTime: time.Now().AddDate(0, -1, 0).Format(model.TimeNano),
  75. Svip: 1,
  76. SvipTime: time.Now().AddDate(-1, 0, 0).Format(model.TimeNano),
  77. }
  78. err = d.createVip(ctx, u.Uid, info)
  79. So(err, ShouldBeNil)
  80. info2, err := d.GetVipFromDB(ctx, u.Uid)
  81. log.Info("TestDao_GetVipFromDB info2(%v)", info2)
  82. So(err, ShouldBeNil)
  83. So(info2.Vip, ShouldEqual, 0)
  84. So(info2.Svip, ShouldEqual, 0)
  85. So(info2.VipTime, ShouldEqual, info.VipTime)
  86. So(info2.SvipTime, ShouldEqual, info.SvipTime)
  87. })
  88. }))
  89. }
  90. func TestDao_AddVip(t *testing.T) {
  91. initd()
  92. Convey("test add vip", t, testWithTestUser(func(u *TestUser) {
  93. log.Info("TestDao_GetVipFromDB uid(%d), table(%s)", u.Uid, getUserLevelTable(u.Uid))
  94. var (
  95. ctx = context.Background()
  96. err error
  97. info *model.VipInfo
  98. row int64
  99. )
  100. // create one row at begin
  101. info = &model.VipInfo{
  102. Vip: 1,
  103. VipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
  104. Svip: 1,
  105. SvipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
  106. }
  107. err = d.createVip(ctx, u.Uid, info)
  108. So(err, ShouldBeNil)
  109. // empty vip time, should return err
  110. Convey("empty vip time, should return err", func() {
  111. row, err = d.AddVip(ctx, u.Uid, 0, 0)
  112. So(row, ShouldEqual, 0)
  113. So(err, ShouldResemble, errUpdateVipTimeInvalid)
  114. })
  115. // add vip and svip time
  116. Convey("add vip and svip time", func() {
  117. // add one month vip
  118. dt := xtime.Time(30 * 86400)
  119. vtime, err := time.Parse(model.TimeNano, info.VipTime)
  120. So(err, ShouldBeNil)
  121. newvtime := xtime.Time(vtime.Unix()) + dt
  122. log.Info("TestDao_AddVip info(%v), oldvt(%v), newvtime(%v), dt(%v)", info, vtime.Unix(), newvtime, dt)
  123. row, err := d.AddVip(ctx, u.Uid, newvtime, 0)
  124. So(row, ShouldEqual, 1)
  125. So(err, ShouldBeNil)
  126. info2, err := d.GetVipFromDB(ctx, u.Uid)
  127. log.Info("TestDao_AddVip info2(%v)", info2)
  128. So(err, ShouldBeNil)
  129. So(info2.Vip, ShouldEqual, info.Vip)
  130. So(info2.Svip, ShouldEqual, info.Svip)
  131. So(info2.SvipTime, ShouldEqual, info.SvipTime)
  132. So(info2.VipTime, ShouldEqual, newvtime.Time().Format(model.TimeNano))
  133. })
  134. }))
  135. }