123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- package dao
- import (
- "context"
- xsql "database/sql"
- "fmt"
- "reflect"
- "testing"
- "time"
- "go-common/app/interface/main/feedback/model"
- "go-common/library/database/sql"
- "github.com/bouk/monkey"
- "github.com/smartystreets/goconvey/convey"
- )
- func TestDaoJudgeSsnRecord(t *testing.T) {
- convey.Convey("JudgeSsnRecord", t, func(ctx convey.C) {
- var (
- c = context.Background()
- sid = int64(1)
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- cnt, err := d.JudgeSsnRecord(c, sid)
- ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(cnt, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.selSSnID.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selSSnID), "Exec",
- func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("d.selSSnID.Exec Error")
- })
- defer guard.Unpatch()
- _, err := d.JudgeSsnRecord(c, sid)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoSession(t *testing.T) {
- convey.Convey("Session", t, func(ctx convey.C) {
- var (
- c = context.Background()
- buvid = ""
- system = ""
- version = ""
- mid = int64(1)
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- ssn, err := d.Session(c, buvid, system, version, mid)
- ctx.Convey("Then err should be nil.ssn should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.SkipSo(ssn, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoSessionCount(t *testing.T) {
- var (
- c = context.Background()
- mid = int64(0)
- )
- convey.Convey("SessionCount", t, func(ctx convey.C) {
- cnt, err := d.SessionCount(c, mid)
- ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(cnt, convey.ShouldNotBeNil)
- })
- })
- }
- func TestDaoUpSsnMtime(t *testing.T) {
- convey.Convey("UpSsnMtime", t, func(ctx convey.C) {
- var (
- c = context.Background()
- now = time.Now()
- id = int64(1)
- )
- convey.Convey("When everything is correct", func(ctx convey.C) {
- err := d.UpSsnMtime(c, now, id)
- ctx.Convey("Then err should be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- })
- })
- convey.Convey("When d.upSsnMtime.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.upSsnMtime), "Exec",
- func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("d.upSsnMtime.Exec Error")
- })
- defer guard.Unpatch()
- err := d.UpSsnMtime(c, now, id)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoTxUpSsnMtime(t *testing.T) {
- convey.Convey("TxUpSsnMtime", t, func(ctx convey.C) {
- var (
- tx, _ = d.BeginTran(context.Background())
- now = time.Now()
- id = int64(0)
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- err := d.TxUpSsnMtime(tx, now, id)
- ctx.Convey("Then err should be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- })
- })
- ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
- func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("tx.Exec Error")
- })
- defer guard.Unpatch()
- err := d.TxUpSsnMtime(tx, now, id)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- ctx.Reset(func() {
- tx.Rollback()
- })
- })
- }
- func TestDaoSessionIDByTagID(t *testing.T) {
- convey.Convey("SessionIDByTagID", t, func(ctx convey.C) {
- var (
- c = context.Background()
- tagID = []int64{1, 2}
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- sid, err := d.SessionIDByTagID(c, tagID)
- ctx.Convey("Then err should be nil.sid should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.SkipSo(sid, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
- func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
- return nil, fmt.Errorf("d.dbMs.Query Error")
- })
- defer guard.Unpatch()
- _, err := d.SessionIDByTagID(c, tagID)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoSessionBySsnID(t *testing.T) {
- convey.Convey("SessionBySsnID", t, func(ctx convey.C) {
- var (
- c = context.Background()
- sid = []int64{1, 2}
- state = "3"
- start = time.Now()
- end = time.Now()
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- ssns, err := d.SessionBySsnID(c, sid, state, start, end)
- ctx.Convey("Then err should be nil.ssns should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.SkipSo(ssns, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
- func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
- return nil, fmt.Errorf("d.dbMs.Query Error")
- })
- defer guard.Unpatch()
- _, err := d.SessionBySsnID(c, sid, state, start, end)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoSSnBySsnIDAllSate(t *testing.T) {
- convey.Convey("SSnBySsnIDAllSate", t, func(ctx convey.C) {
- var (
- c = context.Background()
- sid = []int64{1, 2}
- start = time.Now()
- end = time.Now()
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- ssns, err := d.SSnBySsnIDAllSate(c, sid, start, end)
- ctx.Convey("Then err should be nil.ssns should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.SkipSo(ssns, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
- func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
- return nil, fmt.Errorf("d.dbMs.Query Error")
- })
- defer guard.Unpatch()
- _, err := d.SSnBySsnIDAllSate(c, sid, start, end)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoSessionByMid(t *testing.T) {
- convey.Convey("SessionByMid", t, func(ctx convey.C) {
- var (
- c = context.Background()
- mid = int64(1)
- platform = "ios"
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- ssns, err := d.SessionByMid(c, mid, platform)
- ctx.Convey("Then err should be nil.ssns should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(ssns, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
- func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
- return nil, fmt.Errorf("d.dbMs.Query Error")
- })
- defer guard.Unpatch()
- _, err := d.SessionByMid(c, mid, platform)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoTxUpdateSessionState(t *testing.T) {
- convey.Convey("TxUpdateSessionState", t, func(ctx convey.C) {
- var (
- tx, _ = d.BeginTran(context.Background())
- state = int(0)
- sid = int64(0)
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- err := d.TxUpdateSessionState(tx, state, sid)
- ctx.Convey("Then err should be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- })
- })
- ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
- func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("tx.Exec Error")
- })
- defer guard.Unpatch()
- err := d.TxUpdateSessionState(tx, state, sid)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- ctx.Reset(func() {
- tx.Rollback()
- })
- })
- }
- func TestDaoUpdateSessionState(t *testing.T) {
- convey.Convey("UpdateSessionState", t, func(ctx convey.C) {
- var (
- c = context.Background()
- state = int(0)
- sid = int64(0)
- )
- ctx.Convey("UpdateSessionState", func(ctx convey.C) {
- err := d.UpdateSessionState(c, state, sid)
- ctx.Convey("Then err should be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- })
- })
- ctx.Convey("When d.upSsnSta.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.upSsnSta), "Exec",
- func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("d.upSsnSta.Exec Error")
- })
- defer guard.Unpatch()
- err := d.UpdateSessionState(c, state, sid)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoTags(t *testing.T) {
- convey.Convey("Tags", t, func(ctx convey.C) {
- var (
- c = context.Background()
- mold = int(1)
- platform = "ios"
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- tMap, err := d.Tags(c, mold, platform)
- ctx.Convey("Then err should be nil.tMap should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(tMap, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
- func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
- return nil, fmt.Errorf("d.dbMs.Query Error")
- })
- defer guard.Unpatch()
- _, err := d.Tags(c, mold, platform)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoAddSession(t *testing.T) {
- convey.Convey("AddSession", t, func(ctx convey.C) {
- var (
- c = context.Background()
- s = &model.Session{}
- )
- ctx.Convey("When everthing is correct", func(ctx convey.C) {
- id, err := d.AddSession(c, s)
- ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(id, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.inSsn.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.inSsn), "Exec",
- func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("d.inSsn.Exec Error")
- })
- defer guard.Unpatch()
- _, err := d.AddSession(c, s)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoTxAddSession(t *testing.T) {
- convey.Convey("TxAddSession", t, func(ctx convey.C) {
- var (
- tx, _ = d.BeginTran(context.Background())
- s = &model.Session{}
- )
- ctx.Convey("When everthing is correct", func(ctx convey.C) {
- id, err := d.TxAddSession(tx, s)
- ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(id, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
- func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("tx.Exec Error")
- })
- defer guard.Unpatch()
- _, err := d.TxAddSession(tx, s)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- ctx.Reset(func() {
- tx.Rollback()
- })
- })
- }
- func TestDaoAddSessionTag(t *testing.T) {
- convey.Convey("AddSessionTag", t, func(ctx convey.C) {
- var (
- c = context.Background()
- sessionID = int64(0)
- tagID = int64(0)
- now = time.Now()
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- id, err := d.AddSessionTag(c, sessionID, tagID, now)
- ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(id, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.inSsnTag.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.inSsnTag), "Exec",
- func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("d.inSsnTag.Exec Error")
- })
- defer guard.Unpatch()
- _, err := d.AddSessionTag(c, sessionID, tagID, now)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoTxAddSessionTag(t *testing.T) {
- convey.Convey("TxAddSessionTag", t, func(ctx convey.C) {
- var (
- tx, _ = d.BeginTran(context.Background())
- sessionID = int64(0)
- tagID = int64(0)
- now = time.Now()
- )
- ctx.Convey("When everthing is correct", func(ctx convey.C) {
- id, err := d.TxAddSessionTag(tx, sessionID, tagID, now)
- ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(id, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
- func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("tx.Exec Error")
- })
- defer guard.Unpatch()
- _, err := d.TxAddSessionTag(tx, sessionID, tagID, now)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- ctx.Reset(func() {
- tx.Rollback()
- })
- })
- }
- func TestDaoUpdateSession(t *testing.T) {
- convey.Convey("UpdateSession", t, func(ctx convey.C) {
- var (
- c = context.Background()
- s = &model.Session{}
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- affected, err := d.UpdateSession(c, s)
- ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(affected, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.upSsn.Exec gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.upSsn), "Exec",
- func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
- return nil, fmt.Errorf("d.upSsn.Exec Error")
- })
- defer guard.Unpatch()
- _, err := d.UpdateSession(c, s)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoTagIDBySid(t *testing.T) {
- convey.Convey("TagIDBySid", t, func(ctx convey.C) {
- var (
- c = context.Background()
- sids = []int64{1, 2}
- )
- ctx.Convey("When everything is correct", func(ctx convey.C) {
- tsMap, err := d.TagIDBySid(c, sids)
- ctx.Convey("Then err should be nil.tsMap should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldBeNil)
- ctx.So(tsMap, convey.ShouldNotBeNil)
- })
- })
- ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
- guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
- func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
- return nil, fmt.Errorf("d.dbMs.Query Error")
- })
- defer guard.Unpatch()
- _, err := d.TagIDBySid(c, sids)
- ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
- ctx.So(err, convey.ShouldNotBeNil)
- })
- })
- })
- }
- func TestDaoplatConvert(t *testing.T) {
- convey.Convey("platConvert", t, func(ctx convey.C) {
- var (
- platform = "a,b"
- )
- ctx.Convey("When the original string is 'a,b'", func(ctx convey.C) {
- s := platConvert(platform)
- ctx.Convey("Then s should equal \"a\",\"b\".", func(ctx convey.C) {
- ctx.So(s, convey.ShouldEqual, "\"a\",\"b\"")
- })
- })
- })
- }
|