123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package vip
- import (
- "context"
- "fmt"
- . "github.com/smartystreets/goconvey/convey"
- "go-common/app/service/live/xuser/model"
- "go-common/library/database/sql"
- "go-common/library/log"
- xtime "go-common/library/time"
- "math/rand"
- "testing"
- "time"
- )
- func Test_getUserLevelTable(t *testing.T) {
- initd()
- Convey("test get user_x table name by uid", t, func() {
- var uid = int64(123)
- table := getUserLevelTable(uid)
- So(table, ShouldEqual, "user_2")
- })
- }
- func Test_getUserVipRecordTable(t *testing.T) {
- initd()
- Convey("test get user_vip_record_x table name by uid", t, func() {
- var uid = rand.Int63()
- log.Info("Test_getUserVipRecordTable uid(%d)", uid)
- table := getUserVipRecordTable(uid)
- t := fmt.Sprintf(_userVipRecordPrefix, uid%_userVipRecordCount)
- So(table, ShouldEqual, t)
- })
- }
- func TestDao_GetVipFromDB(t *testing.T) {
- initd()
- Convey("test get vip from db", t, testWithTestUser(func(u *TestUser) {
- log.Info("TestDao_GetVipFromDB uid(%d), table(%s)", u.Uid, getUserLevelTable(u.Uid))
- var (
- ctx = context.Background()
- err error
- info *model.VipInfo
- )
- // delete random uid at begin
- err = d.deleteVip(ctx, u.Uid)
- So(err, ShouldBeNil)
- // get nil result from db
- Convey("get nil result from db", func() {
- info, err = d.GetVipFromDB(ctx, u.Uid)
- So(err, ShouldResemble, sql.ErrNoRows)
- So(info, ShouldNotBeNil)
- So(info.Vip, ShouldEqual, 0)
- So(info.VipTime, ShouldEqual, "")
- So(info.Svip, ShouldEqual, 0)
- So(info.SvipTime, ShouldEqual, "")
- })
- // insert and then get
- Convey("insert and then get", func() {
- var info2 *model.VipInfo
- info = &model.VipInfo{
- Vip: 1,
- VipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
- Svip: 1,
- SvipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
- }
- err = d.createVip(ctx, u.Uid, info)
- So(err, ShouldBeNil)
- info2, err = d.GetVipFromDB(ctx, u.Uid)
- log.Info("TestDao_GetVipFromDB info2(%v)", info2)
- So(err, ShouldBeNil)
- So(info, ShouldResemble, info2)
- })
- // valid info but expired vip time
- Convey("insert valid but expired vip time", func() {
- info = &model.VipInfo{
- Vip: 1,
- VipTime: time.Now().AddDate(0, -1, 0).Format(model.TimeNano),
- Svip: 1,
- SvipTime: time.Now().AddDate(-1, 0, 0).Format(model.TimeNano),
- }
- err = d.createVip(ctx, u.Uid, info)
- So(err, ShouldBeNil)
- info2, err := d.GetVipFromDB(ctx, u.Uid)
- log.Info("TestDao_GetVipFromDB info2(%v)", info2)
- So(err, ShouldBeNil)
- So(info2.Vip, ShouldEqual, 0)
- So(info2.Svip, ShouldEqual, 0)
- So(info2.VipTime, ShouldEqual, info.VipTime)
- So(info2.SvipTime, ShouldEqual, info.SvipTime)
- })
- }))
- }
- func TestDao_AddVip(t *testing.T) {
- initd()
- Convey("test add vip", t, testWithTestUser(func(u *TestUser) {
- log.Info("TestDao_GetVipFromDB uid(%d), table(%s)", u.Uid, getUserLevelTable(u.Uid))
- var (
- ctx = context.Background()
- err error
- info *model.VipInfo
- row int64
- )
- // create one row at begin
- info = &model.VipInfo{
- Vip: 1,
- VipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
- Svip: 1,
- SvipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
- }
- err = d.createVip(ctx, u.Uid, info)
- So(err, ShouldBeNil)
- // empty vip time, should return err
- Convey("empty vip time, should return err", func() {
- row, err = d.AddVip(ctx, u.Uid, 0, 0)
- So(row, ShouldEqual, 0)
- So(err, ShouldResemble, errUpdateVipTimeInvalid)
- })
- // add vip and svip time
- Convey("add vip and svip time", func() {
- // add one month vip
- dt := xtime.Time(30 * 86400)
- vtime, err := time.Parse(model.TimeNano, info.VipTime)
- So(err, ShouldBeNil)
- newvtime := xtime.Time(vtime.Unix()) + dt
- log.Info("TestDao_AddVip info(%v), oldvt(%v), newvtime(%v), dt(%v)", info, vtime.Unix(), newvtime, dt)
- row, err := d.AddVip(ctx, u.Uid, newvtime, 0)
- So(row, ShouldEqual, 1)
- So(err, ShouldBeNil)
- info2, err := d.GetVipFromDB(ctx, u.Uid)
- log.Info("TestDao_AddVip info2(%v)", info2)
- So(err, ShouldBeNil)
- So(info2.Vip, ShouldEqual, info.Vip)
- So(info2.Svip, ShouldEqual, info.Svip)
- So(info2.SvipTime, ShouldEqual, info.SvipTime)
- So(info2.VipTime, ShouldEqual, newvtime.Time().Format(model.TimeNano))
- })
- }))
- }
|