123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- package block
- import (
- "context"
- "database/sql"
- "fmt"
- "reflect"
- "testing"
- model "go-common/app/job/main/member/model/block"
- xsql "go-common/library/database/sql"
- "github.com/bouk/monkey"
- "github.com/smartystreets/goconvey/convey"
- )
- func TestBlockhistoryIdx(t *testing.T) {
- convey.Convey("historyIdx", t, func(convCtx convey.C) {
- var (
- mid = int64(0)
- )
- convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
- p1 := historyIdx(mid)
- convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
- convCtx.So(p1, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestBlockUserStatusList(t *testing.T) {
- convey.Convey("UserStatusList", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- status model.BlockStatus
- startID = int64(0)
- limit = int(10)
- )
- rows, _ := d.db.Query(c, _userStatusList, status, startID, limit)
- convCtx.Convey("UserStatusList success", func(convCtx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(rows), "Scan", func(_ *xsql.Rows, _ ...interface{}) error {
- return nil
- })
- defer guard.Unpatch()
- maxID, mids, err := d.UserStatusList(c, status, startID, limit)
- convCtx.Convey("Then err should be nil.maxID,mids should be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldBeNil)
- convCtx.So(mids, convey.ShouldNotBeNil)
- convCtx.So(maxID, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestBlockUserLastHistory(t *testing.T) {
- convey.Convey("UserLastHistory", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- mid = int64(0)
- )
- convey.Convey("UserLastHistory success", func() {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), fmt.Sprintf(_userLastHistory, historyIdx(mid)), 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
- return nil
- })
- defer monkey.UnpatchAll()
- his, err := d.UserLastHistory(c, mid)
- convCtx.So(err, convey.ShouldBeNil)
- convCtx.So(his, convey.ShouldNotBeNil)
- })
- convey.Convey("UserLastHistory err", func() {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), fmt.Sprintf(_userLastHistory, historyIdx(mid)), 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
- return fmt.Errorf("row.Scan error")
- })
- defer monkey.UnpatchAll()
- his, err := d.UserLastHistory(c, mid)
- convCtx.So(err, convey.ShouldNotBeNil)
- convCtx.So(his, convey.ShouldNotBeNil)
- })
- convey.Convey("UserLastHistory no record", func() {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), fmt.Sprintf(_userLastHistory, historyIdx(mid)), 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
- return sql.ErrNoRows
- })
- defer monkey.UnpatchAll()
- his, err := d.UserLastHistory(c, mid)
- convCtx.So(err, convey.ShouldBeNil)
- convCtx.So(his, convey.ShouldBeNil)
- })
- })
- }
- func TestBlockUserExtra(t *testing.T) {
- convey.Convey("UserExtra", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- mid = int64(0)
- )
- convey.Convey("UserExtra success", func() {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), _userExtra, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
- return nil
- })
- defer monkey.UnpatchAll()
- ex, err := d.UserExtra(c, mid)
- convCtx.So(err, convey.ShouldBeNil)
- convCtx.So(ex, convey.ShouldNotBeNil)
- })
- convey.Convey("UserExtra err", func() {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), _userExtra, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
- return fmt.Errorf("row.Scan error")
- })
- defer monkey.UnpatchAll()
- ex, err := d.UserExtra(c, mid)
- convCtx.So(err, convey.ShouldNotBeNil)
- convCtx.So(ex, convey.ShouldNotBeNil)
- })
- convey.Convey("UserExtra no record", func() {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), _userExtra, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
- return sql.ErrNoRows
- })
- defer monkey.UnpatchAll()
- ex, err := d.UserExtra(c, mid)
- convCtx.So(err, convey.ShouldBeNil)
- convCtx.So(ex, convey.ShouldBeNil)
- })
- })
- }
- func TestBlockTxUpsertUser(t *testing.T) {
- convey.Convey("TxUpsertUser", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- tx, _ = d.BeginTX(c)
- mid = int64(0)
- status model.BlockStatus
- )
- convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
- count, err := d.TxUpsertUser(c, tx, mid, status)
- convCtx.Convey("Then err should be nil.count should not be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldBeNil)
- convCtx.So(count, convey.ShouldNotBeNil)
- })
- })
- convCtx.Reset(func() {
- tx.Commit()
- })
- })
- }
- func TestBlockInsertExtra(t *testing.T) {
- convey.Convey("InsertExtra", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- ex = &model.DBExtra{}
- )
- convCtx.Convey("InsertExtra success", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, nil
- })
- defer monkey.UnpatchAll()
- err := d.InsertExtra(c, ex)
- convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldBeNil)
- })
- })
- convCtx.Convey("InsertExtra err", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, fmt.Errorf("insert err")
- })
- defer monkey.UnpatchAll()
- err := d.InsertExtra(c, ex)
- convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestBlockTxUpsertExtra(t *testing.T) {
- convey.Convey("TxUpsertExtra", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- tx, _ = d.BeginTX(c)
- ex = &model.DBExtra{}
- )
- convCtx.Convey("TxUpsertExtra success", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, nil
- })
- defer monkey.UnpatchAll()
- err := d.TxUpsertExtra(c, tx, ex)
- convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldBeNil)
- })
- })
- convCtx.Convey("TxUpsertExtra err", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, fmt.Errorf("insert err")
- })
- defer monkey.UnpatchAll()
- err := d.TxUpsertExtra(c, tx, ex)
- convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldNotBeNil)
- })
- })
- convCtx.Reset(func() {
- tx.Commit()
- })
- })
- }
- func TestBlockTxInsertHistory(t *testing.T) {
- convey.Convey("TxInsertHistory", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- tx, _ = d.BeginTX(c)
- h = &model.DBHistory{}
- )
- convCtx.Convey("TxUpsertExtra success", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, nil
- })
- defer monkey.UnpatchAll()
- err := d.TxInsertHistory(c, tx, h)
- convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldBeNil)
- })
- })
- convCtx.Convey("TxUpsertExtra err", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, fmt.Errorf("insert err")
- })
- defer monkey.UnpatchAll()
- err := d.TxInsertHistory(c, tx, h)
- convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldNotBeNil)
- })
- })
- convCtx.Reset(func() {
- tx.Commit()
- })
- })
- }
- func TestBlockUpsertAddBlockCount(t *testing.T) {
- convey.Convey("UpsertAddBlockCount", t, func(convCtx convey.C) {
- var (
- c = context.Background()
- mid = int64(0)
- )
- convCtx.Convey("UpsertAddBlockCount success", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, nil
- })
- defer monkey.UnpatchAll()
- err := d.UpsertAddBlockCount(c, mid)
- convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldBeNil)
- })
- })
- convCtx.Convey("InsertExtra err", func(convCtx convey.C) {
- monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
- return nil, fmt.Errorf("insert err")
- })
- defer monkey.UnpatchAll()
- err := d.UpsertAddBlockCount(c, mid)
- convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
- convCtx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
|