Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"bfs_test.go",
"dao_test.go",
"playerCheck_test.go",
"reply_test.go",
"session_test.go",
"tag_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/feedback/conf:go_default_library",
"//app/interface/main/feedback/model:go_default_library",
"//library/database/sql:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"bfs.go",
"dao.go",
"playerCheck.go",
"reply.go",
"session.go",
"tag.go",
],
importpath = "go-common/app/interface/main/feedback/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/feedback/conf:go_default_library",
"//app/interface/main/feedback/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/time:go_default_library",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,82 @@
package dao
import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"hash"
"io"
"net/http"
"strconv"
"go-common/library/log"
)
const (
_uploadURL = "/%s/%s"
_template = "%s\n%s\n%s\n%d\n"
_method = "PUT"
)
var (
errUpload = errors.New("Upload failed")
)
// Upload upload picture or log file to bfs
func (d *Dao) Upload(c context.Context, fileName, fileType string, expire int64, body io.Reader) (location string, err error) {
var (
url string
req *http.Request
resp *http.Response
header http.Header
code string
)
bfsConf := d.c.Bfs
url = fmt.Sprintf(bfsConf.Addr+_uploadURL, bfsConf.Bucket, fileName)
if req, err = http.NewRequest(_method, url, body); err != nil {
log.Error("http.NewRequest() Upload(%v) error(%v)", url, err)
return
}
authorization := authorize(bfsConf.Key, bfsConf.Secret, _method, bfsConf.Bucket, fileName, expire)
req.Header.Set("Host", bfsConf.Addr)
req.Header.Add("Date", fmt.Sprint(expire))
req.Header.Add("Authorization", authorization)
req.Header.Add("Content-Type", fileType)
resp, err = d.bfsClient.Do(req)
if err != nil {
log.Error("httpClient.Do(%s) error(%v)", url, err)
return
}
if resp.StatusCode != http.StatusOK {
log.Error("Upload url(%s) http.statuscode:%d", url, resp.StatusCode)
err = errUpload
return
}
header = resp.Header
code = header.Get("Code")
if code != strconv.Itoa(http.StatusOK) {
log.Error("Upload url(%s) code:%s", url, code)
err = errUpload
return
}
location = header.Get("Location")
return
}
// authorize returns authorization for upload file to bfs
func authorize(key, secret, method, bucket, file string, expire int64) (authorization string) {
var (
content string
mac hash.Hash
signature string
)
content = fmt.Sprintf(_template, method, bucket, file, expire)
mac = hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(content))
signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
return
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"context"
"fmt"
"io"
"net/http"
"reflect"
"testing"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUpload(t *testing.T) {
convey.Convey("Upload", t, func(ctx convey.C) {
var (
c = context.Background()
fileName = ""
fileType = ""
expire = int64(0)
body io.Reader
)
bfsConf := d.c.Bfs
url := fmt.Sprintf(bfsConf.Addr+_uploadURL, bfsConf.Bucket, fileName)
ctx.Convey("When everything is correct", func(ctx convey.C) {
httpMock("PUT", url).Reply(200).SetHeaders(map[string]string{
"Code": "200",
"Location": "SomePlace",
})
location, err := d.Upload(c, fileName, fileType, expire, body)
ctx.Convey("Then err should be nil.location should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(location, convey.ShouldNotBeNil)
})
})
ctx.Convey("When d.bfsClient.Do gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.bfsClient), "Do",
func(_ *http.Client, _ *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("d.bfsClient.Do Error")
})
defer guard.Unpatch()
_, err := d.Upload(c, fileName, fileType, expire, body)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
ctx.Convey("When http request status != 200", func(ctx convey.C) {
httpMock("PUT", url).Reply(404)
_, err := d.Upload(c, fileName, fileType, expire, body)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
ctx.Convey("When http request Code in header != 200", func(ctx convey.C) {
httpMock("PUT", url).Reply(404).SetHeaders(map[string]string{
"Code": "404",
"Location": "SomePlace",
})
_, err := d.Upload(c, fileName, fileType, expire, body)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoauthorize(t *testing.T) {
var (
key = "1234"
secret = "2345"
method = ""
bucket = ""
file = ""
expire = int64(0)
)
convey.Convey("authorize", t, func(ctx convey.C) {
authorization := authorize(key, secret, method, bucket, file, expire)
ctx.Convey("Then authorization should not be nil.", func(ctx convey.C) {
ctx.So(authorization, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,72 @@
package dao
import (
"context"
"go-common/app/interface/main/feedback/conf"
"go-common/library/database/sql"
"net/http"
)
// Dao is feedback dao.
type Dao struct {
// conf
c *conf.Config
// db
dbMs *sql.DB
//db stmt
// session
selSsn *sql.Stmt
selSsnByMid *sql.Stmt
selTagID *sql.Stmt
inSsn *sql.Stmt
inSsnTag *sql.Stmt
upSsn *sql.Stmt
upSsnMtime *sql.Stmt
upSsnSta *sql.Stmt
selSSnID *sql.Stmt
selSSnCntByMid *sql.Stmt
// reply
selReply *sql.Stmt
selReplyByMid *sql.Stmt
selReplyBySid *sql.Stmt
inReply *sql.Stmt
// tag
selTagBySsnID *sql.Stmt
// bfs
bfsClient *http.Client
}
// New dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
dbMs: sql.NewMySQL(c.MySQL.Master),
}
// session
d.selSsn = d.dbMs.Prepared(_selSsn)
d.selSsnByMid = d.dbMs.Prepared(_selSsnByMid)
d.inSsn = d.dbMs.Prepared(_inSsn)
d.inSsnTag = d.dbMs.Prepared(_inSsnTag)
d.upSsn = d.dbMs.Prepared(_upSsn)
d.upSsnMtime = d.dbMs.Prepared(_upSsnMtime)
d.upSsnSta = d.dbMs.Prepared(_upSsnState)
d.selSSnID = d.dbMs.Prepared(_selSSnID)
d.selSSnCntByMid = d.dbMs.Prepared(_selSSnCntByMid)
// reply
d.selReply = d.dbMs.Prepared(_selReply)
d.selReplyByMid = d.dbMs.Prepared(_selReplyByMid)
d.inReply = d.dbMs.Prepared(_inReply)
d.selTagID = d.dbMs.Prepared(_selTagID)
d.selReplyBySid = d.dbMs.Prepared(_selReplyBySid)
d.selTagBySsnID = d.dbMs.Prepared(_selTagBySsnID)
// init bfs http client
d.bfsClient = http.DefaultClient
return
}
// BeginTran begin tran.
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
tx, err = d.dbMs.Begin(c)
return
}

View File

@@ -0,0 +1,45 @@
package dao
import (
"flag"
"go-common/app/interface/main/feedback/conf"
"os"
"strings"
"testing"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.feedback")
flag.Set("conf_token", "a0b043ef740cfe08097a2353f6e086f5")
flag.Set("tree_id", "2179")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/convey-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
//d.bfsClient.SetTransport(gock.DefaultTransport)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,28 @@
package dao
import (
"context"
"time"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
_inPlayCheckSQL = `INSERT INTO campus_network_check_record (platform,check_time,isp,region,school,mid,ip,ip_change_times,cdn,connect_speed,io_speed,aid,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
)
// InPlayCheck insert player check info into db
func (d *Dao) InPlayCheck(c context.Context, platform, isp, ipChangeTimes int, mid, checkTime, aid, connectSpeed, ioSpeed int64, region, school, ip, cdn string) (rows int64, err error) {
var (
now = time.Now()
checkTime2 = xtime.Time(checkTime).Time()
)
res, err := d.dbMs.Exec(c, _inPlayCheckSQL, platform, checkTime2, isp, region, school, mid, ip, ipChangeTimes, cdn, connectSpeed, ioSpeed, aid, now, now)
if err != nil {
log.Error("d.InPlayCheck.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,52 @@
package dao
import (
"context"
xsql "database/sql"
"fmt"
"reflect"
"testing"
"go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoInPlayCheck(t *testing.T) {
convey.Convey("InPlayCheck", t, func(ctx convey.C) {
var (
c = context.TODO()
platform = int(0)
isp = int(0)
ipChangeTimes = int(0)
mid = int64(0)
checkTime = int64(0)
aid = int64(0)
connectSpeed = int64(0)
ioSpeed = int64(0)
region = ""
school = ""
ip = ""
cdn = ""
)
ctx.Convey("When everything is correct", func(ctx convey.C) {
rows, err := d.InPlayCheck(c, platform, isp, ipChangeTimes, mid, checkTime, aid, connectSpeed, ioSpeed, region, school, ip, cdn)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Exec",
func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (xsql.Result, error) {
return nil, fmt.Errorf("d.dbMs.Exec Error")
})
defer guard.Unpatch()
_, err := d.InPlayCheck(c, platform, isp, ipChangeTimes, mid, checkTime, aid, connectSpeed, ioSpeed, region, school, ip, cdn)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,96 @@
package dao
import (
"context"
"go-common/app/interface/main/feedback/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_inReply = "INSERT INTO reply (session_id,reply_id,content,img_url,log_url,ctime,mtime) VALUES (?,?,?,?,?,?,?)"
_selReply = "SELECT reply_id,type,content,img_url,log_url,ctime FROM reply WHERE session_id=? ORDER BY id DESC LIMIT ?,?"
_selReplyByMid = "SELECT r.reply_id,r.type,r.content,r.img_url,r.log_url,r.ctime FROM reply r INNER JOIN session s ON r.session_id=s.id WHERE s.mid=? ORDER BY r.id DESC LIMIT ?,?"
_selReplyBySid = "SELECT r.reply_id,r.type,r.content,r.img_url,r.log_url,r.ctime FROM reply r INNER JOIN session s ON r.session_id=s.id WHERE r.session_id=? AND s.mid=? ORDER BY r.id"
)
// TxAddReply implements add a new reply record
func (d *Dao) TxAddReply(tx *sql.Tx, r *model.Reply) (id int64, err error) {
res, err := tx.Exec(_inReply, r.SessionID, r.ReplyID, r.Content, r.ImgURL, r.LogURL, r.CTime, r.MTime)
if err != nil {
log.Error("AddReply tx.Exec() error(%v)", err)
return
}
return res.LastInsertId()
}
// AddReply insert reply.
func (d *Dao) AddReply(c context.Context, r *model.Reply) (id int64, err error) {
res, err := d.inReply.Exec(c, r.SessionID, r.ReplyID, r.Content, r.ImgURL, r.LogURL, r.CTime, r.MTime)
if err != nil {
log.Error("AddReply error(%v)", err)
return
}
return res.LastInsertId()
}
// Replys returns corresponding user feedback reply records
func (d *Dao) Replys(c context.Context, ssnID int64, offset, limit int) (rs []model.Reply, err error) {
rows, err := d.selReply.Query(c, ssnID, offset, limit)
if err != nil {
log.Error("d.selReply.Query() error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var r = model.Reply{}
if err = rows.Scan(&r.ReplyID, &r.Type, &r.Content, &r.ImgURL, &r.LogURL, &r.CTime); err != nil {
log.Error("row.Scan() error(%s)", err)
rs = nil
return
}
rs = append(rs, r)
}
return
}
// WebReplys get by ssnID.
func (d *Dao) WebReplys(c context.Context, ssnID, mid int64) (rs []*model.Reply, err error) {
rows, err := d.selReplyBySid.Query(c, ssnID, mid)
if err != nil {
log.Error("d.selReply.Query(%d, %d) error(%v)", ssnID, mid, err)
return
}
defer rows.Close()
for rows.Next() {
var r = &model.Reply{}
if err = rows.Scan(&r.ReplyID, &r.Type, &r.Content, &r.ImgURL, &r.LogURL, &r.CTime); err != nil {
log.Error("row.Scan() error(%s)", err)
rs = nil
return
}
rs = append(rs, r)
}
return
}
// ReplysByMid returns corresponding user feedback reply records by mid
func (d *Dao) ReplysByMid(c context.Context, mid int64, offset, limit int) (rs []model.Reply, err error) {
rows, err := d.selReplyByMid.Query(c, mid, offset, limit)
if err != nil {
log.Error("d.selReplyByMid.Query() error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var r = model.Reply{}
if err = rows.Scan(&r.ReplyID, &r.Type, &r.Content, &r.ImgURL, &r.LogURL, &r.CTime); err != nil {
log.Error("row.Scan() error(%s)", err)
rs = nil
return
}
rs = append(rs, r)
}
return
}

View File

@@ -0,0 +1,158 @@
package dao
import (
"context"
xsql "database/sql"
"fmt"
"reflect"
"testing"
"go-common/app/interface/main/feedback/model"
"go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTxAddReply(t *testing.T) {
convey.Convey("TxAddReply", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
r = &model.Reply{}
)
ctx.Convey("When everything is correct", func(ctx convey.C) {
id, err := d.TxAddReply(tx, r)
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.TxAddReply(tx, r)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
tx.Rollback()
})
})
}
func TestDaoAddReply(t *testing.T) {
convey.Convey("AddReply", t, func(ctx convey.C) {
var (
c = context.TODO()
r = &model.Reply{}
)
ctx.Convey("When everything is correct", func(ctx convey.C) {
id, err := d.AddReply(c, r)
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.inReply.Exec gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.inReply), "Exec",
func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
return nil, fmt.Errorf("d.inReply.Exec Error")
})
defer guard.Unpatch()
_, err := d.AddReply(c, r)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoReplys(t *testing.T) {
convey.Convey("Replys", t, func(ctx convey.C) {
var (
c = context.TODO()
ssnID = int64(3131)
offset = int(1)
limit = int(10)
)
ctx.Convey("When everything is correct", func(ctx convey.C) {
rs, err := d.Replys(c, ssnID, offset, limit)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.SkipSo(rs, convey.ShouldNotBeNil)
})
})
ctx.Convey("When d.selReply.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selReply), "Query",
func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (*sql.Rows, error) {
return nil, fmt.Errorf("d.selReply.Query Error")
})
defer guard.Unpatch()
_, err := d.Replys(c, ssnID, offset, limit)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoWebReplys(t *testing.T) {
convey.Convey("WebReplys", t, func(ctx convey.C) {
var (
c = context.TODO()
ssnID = int64(3131)
mid = int64(1313)
)
ctx.Convey("When everything is correct", func(ctx convey.C) {
rs, err := d.WebReplys(c, ssnID, mid)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.SkipSo(rs, convey.ShouldNotBeNil)
})
})
ctx.Convey("When d.selReplyBySid.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selReplyBySid), "Query",
func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (*sql.Rows, error) {
return nil, fmt.Errorf("d.selReplyBySid.Query Error")
})
defer guard.Unpatch()
_, err := d.WebReplys(c, ssnID, mid)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoReplysByMid(t *testing.T) {
convey.Convey("ReplysByMid", t, func(ctx convey.C) {
var (
c = context.TODO()
mid = int64(11424224)
offset = int(1)
limit = int(10)
)
ctx.Convey("ReplysByMid", func(ctx convey.C) {
rs, err := d.ReplysByMid(c, mid, offset, limit)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.SkipSo(rs, convey.ShouldNotBeNil)
})
})
ctx.Convey("When d.selReplyByMid.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selReplyByMid), "Query",
func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (*sql.Rows, error) {
return nil, fmt.Errorf("d.selReplyByMid.Query Error")
})
defer guard.Unpatch()
_, err := d.ReplysByMid(c, mid, offset, limit)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,280 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/app/interface/main/feedback/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
"strings"
)
const (
_selSsn = "SELECT id,mid,content,img_url,log_url,state,ctime FROM session WHERE buvid=? AND system=? AND version=? AND mid=?"
_selSsnByMid = "SELECT id,mid,content,img_url,log_url,state,ctime FROM session WHERE mid=? AND platform IN (%s)"
_selSSnCntByMid = `SELECT COUNT(id) AS count FROM session WHERE mid=? AND state IN (0,1,2) AND platform IN ("ugc","article")`
_inSsn = "INSERT INTO session (buvid,system,version,mid,aid,content,img_url,log_url,device,channel,ip,net_state,net_operator,agency_area,platform,browser,qq,email,state,laster_time,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
_upSsn = "UPDATE session SET device=?,channel=?,ip=?,net_state=?,net_operator=?,agency_area=?,platform=?,browser=?,qq=?,email=?,state=?,laster_time=?,mtime=? WHERE id=?"
_upSsnMtime = "UPDATE session SET mtime=? WHERE id=?"
_upSsnState = "UPDATE session SET state=? where id=?"
_selTagByPlat = "SELECT id,name,platform,type FROM tag where type=? AND platform IN (%s)"
_selSsnIDbByTagID = "SELECT session_id FROM session_tag WHERE tag_id IN (%s)"
_selSSnBySsnID = "SELECT id,content,ctime,state FROM session WHERE id IN (%s) AND state IN (%s) AND session.ctime>? AND session.ctime<? ORDER BY id DESC"
_selSSnBySsnIDAllSate = "SELECT id,content,ctime,state FROM session WHERE id IN (%s) AND session.ctime>? AND session.ctime<? ORDER BY id DESC"
_selSSnID = "select id from session where id=? limit 1"
)
// JudgeSsnRecord judge session is exist or not .
func (d *Dao) JudgeSsnRecord(c context.Context, sid int64) (cnt int, err error) {
res, err := d.selSSnID.Exec(c, sid)
if err != nil {
log.Error("d.upSsnSta.Exec error(%v)", err)
return
}
res.RowsAffected()
return
}
// Session select feedback session
func (d *Dao) Session(c context.Context, buvid, system, version string, mid int64) (ssn *model.Session, err error) {
row := d.selSsn.QueryRow(c, buvid, system, version, mid)
ssn = &model.Session{}
if err = row.Scan(&ssn.ID, &ssn.Mid, &ssn.Content, &ssn.ImgURL, &ssn.LogURL, &ssn.State, &ssn.CTime); err != nil {
if err == sql.ErrNoRows {
err, ssn = nil, nil
} else {
log.Error("row.Scan() error(%v)", err)
}
}
return
}
//SessionCount session count.
func (d *Dao) SessionCount(c context.Context, mid int64) (cnt int, err error) {
row := d.selSSnCntByMid.QueryRow(c, mid)
if err = row.Scan(&cnt); err != nil {
log.Error("row.Scan error(%v)", err)
}
return
}
// UpSsnMtime up ssn mtime.
func (d *Dao) UpSsnMtime(c context.Context, now time.Time, id int64) (err error) {
_, err = d.upSsnMtime.Exec(c, now, id)
if err != nil {
log.Error("d.upSsnMtime error(%v)", err)
}
return
}
// TxUpSsnMtime up ssn mtime.
func (d *Dao) TxUpSsnMtime(tx *sql.Tx, now time.Time, id int64) (err error) {
_, err = tx.Exec(_upSsnMtime, now, id)
if err != nil {
log.Error("d.upSsnMtime error(%v)", err)
}
return
}
// SessionIDByTagID session find by time state and mid .
func (d *Dao) SessionIDByTagID(c context.Context, tagID []int64) (sid []int64, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSsnIDbByTagID, xstr.JoinInts(tagID)))
if err != nil {
log.Error("d.dbMs.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var id int64
if err = rows.Scan(&id); err != nil {
log.Error("row.Scan() error(%v)", err)
return
}
sid = append(sid, id)
}
return
}
// SessionBySsnID get session by ssnID
func (d *Dao) SessionBySsnID(c context.Context, sid []int64, state string, start, end time.Time) (ssns []*model.Session, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSSnBySsnID, xstr.JoinInts(sid), state), start, end)
if err != nil {
log.Error("d.selSSnBySsnID.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
ssn := &model.Session{}
if err = rows.Scan(&ssn.ID, &ssn.Content, &ssn.CTime, &ssn.State); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
ssns = append(ssns, ssn)
}
return
}
// SSnBySsnIDAllSate get all SSn by ssnID all sate.
func (d *Dao) SSnBySsnIDAllSate(c context.Context, sid []int64, start, end time.Time) (ssns []*model.Session, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSSnBySsnIDAllSate, xstr.JoinInts(sid)), start, end)
if err != nil {
log.Error("d.selSSnBySsnID.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
ssn := &model.Session{}
if err = rows.Scan(&ssn.ID, &ssn.Content, &ssn.CTime, &ssn.State); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
ssns = append(ssns, ssn)
}
return
}
// SessionByMid select feedback session by mid
func (d *Dao) SessionByMid(c context.Context, mid int64, platform string) (ssns []*model.Session, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSsnByMid, platConvert(platform)), mid)
if err != nil {
log.Error("d.dbMs.Query error(%v)", err)
return
}
defer rows.Close()
ssns = []*model.Session{}
for rows.Next() {
ssn := &model.Session{}
if err = rows.Scan(&ssn.ID, &ssn.Mid, &ssn.Content, &ssn.ImgURL, &ssn.LogURL, &ssn.State, &ssn.CTime); err != nil {
log.Error("row.Scan() error(%v)", err)
return
}
ssns = append(ssns, ssn)
}
return
}
// TxUpdateSessionState up date session state.
func (d *Dao) TxUpdateSessionState(tx *sql.Tx, state int, sid int64) (err error) {
res, err := tx.Exec(_upSsnState, state, sid)
if err != nil {
log.Error("d.upSsnSta.Exec error(%v)", err)
return
}
_, err = res.RowsAffected()
return
}
// UpdateSessionState update session state.
func (d *Dao) UpdateSessionState(c context.Context, state int, sid int64) (err error) {
res, err := d.upSsnSta.Exec(c, state, sid)
if err != nil {
log.Error("d.upSsnSta.Exec error(%v)", err)
return
}
_, err = res.RowsAffected()
return
}
// Tags get tags.
func (d *Dao) Tags(c context.Context, mold int, platform string) (tMap map[string][]*model.Tag, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selTagByPlat, platConvert(platform)), mold)
if err != nil {
log.Error("d.selTagByPlat.Query error(%v)", err)
return
}
defer rows.Close()
tMap = make(map[string][]*model.Tag)
for rows.Next() {
tag := &model.Tag{}
if err = rows.Scan(&tag.ID, &tag.Name, &tag.Platform, &tag.Type); err != nil {
log.Error("row.Scan() error(%v)", err)
return
}
if tag.Type == 0 {
tMap[tag.Platform] = append(tMap[tag.Platform], tag)
}
}
return
}
// AddSession add feedback session
func (d *Dao) AddSession(c context.Context, s *model.Session) (id int64, err error) {
res, err := d.inSsn.Exec(c, s.Buvid, s.System, s.Version, s.Mid, s.Aid, s.Content, s.ImgURL, s.LogURL, s.Device, s.Channel, s.IP, s.NetState, s.NetOperator, s.AgencyArea, s.Platform, s.Browser, s.QQ, s.Email, s.State, s.LasterTime, s.CTime, s.MTime)
if err != nil {
log.Error("AddSession tx.Exec() error(%v)", err)
return
}
return res.LastInsertId()
}
// TxAddSession add session
func (d *Dao) TxAddSession(tx *sql.Tx, s *model.Session) (id int64, err error) {
res, err := tx.Exec(_inSsn, s.Buvid, s.System, s.Version, s.Mid, s.Aid, s.Content, s.ImgURL, s.LogURL, s.Device, s.Channel, s.IP, s.NetState, s.NetOperator, s.AgencyArea, s.Platform, s.Browser, s.QQ, s.Email, s.State, s.LasterTime, s.CTime, s.MTime)
if err != nil {
log.Error("AddSession tx.Exec() error(%v)", err)
return
}
return res.LastInsertId()
}
// AddSessionTag add feedback session and tag.
func (d *Dao) AddSessionTag(c context.Context, sessionID, tagID int64, now time.Time) (id int64, err error) {
res, err := d.inSsnTag.Exec(c, sessionID, tagID, now)
if err != nil {
log.Error("AddSession tx.Exec() error(%v)", err)
return
}
return res.LastInsertId()
}
// TxAddSessionTag add session tag.
func (d *Dao) TxAddSessionTag(tx *sql.Tx, sessionID, tagID int64, now time.Time) (id int64, err error) {
res, err := tx.Exec(_inSsnTag, sessionID, tagID, now)
if err != nil {
log.Error("TxAddSessionTag tx.Exec() error(%v)", err)
return
}
return res.LastInsertId()
}
// UpdateSession update feedback session
func (d *Dao) UpdateSession(c context.Context, s *model.Session) (affected int64, err error) {
res, err := d.upSsn.Exec(c, s.Device, s.Channel, s.IP, s.NetState, s.NetOperator, s.AgencyArea, s.Platform, s.Browser, s.QQ, s.Email, s.State, s.LasterTime, s.MTime, s.ID)
if err != nil {
log.Error("UpdateSession tx.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// TagIDBySid get tagid by sid.
func (d *Dao) TagIDBySid(c context.Context, sids []int64) (tsMap map[int64]int64, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selTagID, xstr.JoinInts(sids)))
if err != nil {
log.Error("d.dbMs.Query() error(%v)", err)
return
}
defer rows.Close()
tsMap = make(map[int64]int64)
for rows.Next() {
var (
tid int64
sid int64
)
if err = rows.Scan(&tid, &sid); err != nil {
log.Error("row.Scan() error(%v)", err)
return
}
tsMap[tid] = sid
}
return
}
// platConvert plat covert.
func platConvert(platform string) (s string) {
s = `"` + strings.Replace(platform, ",", `","`, -1) + `"`
return
}

View File

@@ -0,0 +1,518 @@
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\"")
})
})
})
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"fmt"
"go-common/app/interface/main/feedback/model"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_selTagBySsnID = "SELECT session_tag.session_id,tag.id,tag.name,tag.platform,tag.type FROM tag INNER JOIN session_tag ON tag.id =session_tag.tag_id WHERE session_id IN (%s)"
_selTagID = "SELECT tag_id,session_id FROM session_tag WHERE session_id IN (%s)"
_inSsnTag = "INSERT INTO session_tag (session_id,tag_id,ctime) VALUES (?,?,?)"
)
// TagBySsnID get tag by ssnID.
func (d *Dao) TagBySsnID(c context.Context, sids []int64) (tagMap map[int64][]*model.Tag, err error) {
rows, err := d.dbMs.Query(c, fmt.Sprintf(_selTagBySsnID, xstr.JoinInts(sids)))
if err != nil {
log.Error("d.dbMs.Query error(%v)", err)
return
}
defer rows.Close()
tagMap = make(map[int64][]*model.Tag)
for rows.Next() {
var (
tag = &model.Tag{}
sid int64
)
if err = rows.Scan(&sid, &tag.ID, &tag.Name, &tag.Platform, &tag.Type); err != nil {
log.Error("row.Scan() error(%v)", err)
return
}
if tag.Type == 0 {
tagMap[sid] = append(tagMap[sid], tag)
}
}
return
}

View File

@@ -0,0 +1,35 @@
package dao
import (
"context"
"fmt"
"reflect"
"testing"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTagBySsnID(t *testing.T) {
convey.Convey("Given function TagBySsnID", t, func(ctx convey.C) {
ctx.Convey("When everyting is correct", func(ctx convey.C) {
tagMap, err := d.TagBySsnID(context.Background(), []int64{1, 2})
ctx.Convey("Then err should be nil.tapMap should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tagMap, convey.ShouldNotBeNil)
})
})
ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
return nil, fmt.Errorf("d.dbMs.Query Error")
})
defer guard.Unpatch()
_, err := d.TagBySsnID(context.Background(), []int64{1, 2})
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}