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 = [
"dao_test.go",
"http_test.go",
"mc_test.go",
"mysql_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/member/conf:go_default_library",
"//app/job/main/member/model/block:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/net/http/blademaster: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 = [
"dao.go",
"http.go",
"mc.go",
"mysql.go",
"notify.go",
],
importpath = "go-common/app/job/main/member/dao/block",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/member/conf:go_default_library",
"//app/job/main/member/model/block:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/pkg/errors: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,67 @@
package block
import (
"context"
"go-common/app/job/main/member/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
type notifyFunc func(context.Context, int64, string) error
// Dao dao
type Dao struct {
conf *conf.Config
mc *memcache.Pool
db *xsql.DB
httpClient *bm.Client
notifyFunc notifyFunc
}
// New init mysql db
func New(conf *conf.Config, mc *memcache.Pool, db *xsql.DB, client *bm.Client, notifyFunc notifyFunc) (dao *Dao) {
dao = &Dao{
conf: conf,
mc: mc,
db: db,
httpClient: client,
notifyFunc: notifyFunc,
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.mc.Close()
d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
return
}
if err = d.pingMC(c); err != nil {
return
}
return
}
// pingMc ping
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
return
}
// BeginTX is.
func (d *Dao) BeginTX(c context.Context) (tx *xsql.Tx, err error) {
if tx, err = d.db.Begin(c); err != nil {
err = errors.WithStack(err)
}
return
}

View File

@@ -0,0 +1,61 @@
package block
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/job/main/member/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestTool(t *testing.T) {
Convey("tool", t, func() {
var (
mids = []int64{1, 2, 3, 46333, 35858}
)
str := midsToParam(mids)
So(str, ShouldEqual, "1,2,3,46333,35858")
})
}
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.member-job")
flag.Set("conf_token", "VEc5eqZNZHGQi6fsx7J6lJTqOGR9SnEO")
flag.Set("tree_id", "2134")
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/member-job-dev.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
config := conf.Conf
//d.New().BlockImpl()
d = New(conf.Conf, memcache.NewPool(config.Memcache.Config), xsql.NewMySQL(config.BlockDB), bm.NewClient(config.HTTPClient), nil)
d.httpClient.SetTransport(gock.DefaultTransport)
os.Exit(m.Run())
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,46 @@
package block
import (
"context"
"fmt"
"net/url"
"strings"
"go-common/library/ecode"
"github.com/pkg/errors"
)
// SendSysMsg send sys msg.
func (d *Dao) SendSysMsg(c context.Context, code string, mids []int64, title string, content string, remoteIP string) (err error) {
params := url.Values{}
params.Set("mc", code)
params.Set("title", title)
params.Set("data_type", "4")
params.Set("context", content)
params.Set("mid_list", midsToParam(mids))
var res struct {
Code int `json:"code"`
Data *struct {
Status int8 `json:"status"`
Remark string `json:"remark"`
} `json:"data"`
}
if err = d.httpClient.Post(c, d.conf.BlockProperty.MSGURL, remoteIP, params, &res); err != nil {
err = errors.WithStack(err)
return
}
if res.Code != 0 {
err = errors.WithStack(ecode.Int(res.Code))
return
}
return
}
func midsToParam(mids []int64) (str string) {
strs := make([]string, 0, len(mids))
for _, mid := range mids {
strs = append(strs, fmt.Sprintf("%d", mid))
}
return strings.Join(strs, ",")
}

View File

@@ -0,0 +1,55 @@
package block
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestBlockSendSysMsg(t *testing.T) {
convey.Convey("SendSysMsg", t, func(convCtx convey.C) {
var (
c = context.Background()
code = ""
mids = []int64{}
title = ""
content = ""
remoteIP = ""
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
defer gock.OffAll()
httpMock("POST", d.conf.BlockProperty.MSGURL).Reply(200).JSON(`{"code":0,"data":{"status":1,"remark":"test"}}`)
err := d.SendSysMsg(c, code, mids, title, content, remoteIP)
//println("err==",err)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
convCtx.Convey("When everything goes negative", func(convCtx convey.C) {
defer gock.OffAll()
httpMock("POST", d.conf.BlockProperty.MSGURL).Reply(200).JSON(`{"code":500,"data":{"status":1,"remark":"test"}}`)
//d.httpClient.SetTransport(gock.DefaultTransport)
err := d.SendSysMsg(c, code, mids, title, content, remoteIP)
convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestBlockmidsToParam(t *testing.T) {
convey.Convey("midsToParam", t, func(convCtx convey.C) {
var (
mids = []int64{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
str := midsToParam(mids)
convCtx.Convey("Then str should not be nil.", func(convCtx convey.C) {
convCtx.So(str, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,70 @@
package block
import (
"context"
"fmt"
"go-common/library/cache/memcache"
"github.com/pkg/errors"
)
func userKey(mid int64) (key string) {
key = fmt.Sprintf("u_%d", mid)
return
}
func syncBlockTypeID() (key string) {
return "sync_bt_cur_id"
}
//SetSyncBlockTypeID is.
func (d *Dao) SetSyncBlockTypeID(c context.Context, id int64) (err error) {
var (
key = syncBlockTypeID()
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: key, Object: id, Expiration: 3600 * 24, Flags: memcache.FlagJSON}); err != nil {
err = errors.WithStack(err)
return
}
return
}
//SyncBlockTypeID is.
func (d *Dao) SyncBlockTypeID(c context.Context) (id int64, err error) {
var (
key = syncBlockTypeID()
conn = d.mc.Get(c)
item *memcache.Item
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.WithStack(err)
return
}
if err = conn.Scan(item, &id); err != nil {
err = errors.WithStack(err)
return
}
return
}
// DeleteUserCache is.
func (d *Dao) DeleteUserCache(c context.Context, mid int64) (err error) {
var (
key = userKey(mid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
err = errors.WithStack(err)
return
}
return
}

View File

@@ -0,0 +1,86 @@
package block
import (
"context"
"reflect"
"testing"
"go-common/library/cache/memcache"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestBlockuserKey(t *testing.T) {
convey.Convey("userKey", t, func(convCtx convey.C) {
var (
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
key := userKey(mid)
convCtx.Convey("Then key should not be nil.", func(convCtx convey.C) {
convCtx.So(key, convey.ShouldNotBeNil)
})
})
})
}
func TestBlocksyncBlockTypeID(t *testing.T) {
convey.Convey("syncBlockTypeID", t, func(convCtx convey.C) {
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
key := syncBlockTypeID()
convCtx.Convey("Then key should not be nil.", func(convCtx convey.C) {
convCtx.So(key, convey.ShouldNotBeNil)
})
})
})
}
func TestBlockSetSyncBlockTypeID(t *testing.T) {
convey.Convey("SetSyncBlockTypeID", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.SetSyncBlockTypeID(c, id)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestBlockSyncBlockTypeID(t *testing.T) {
convey.Convey("SyncBlockTypeID", t, func(convCtx convey.C) {
var (
c = context.Background()
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
id, err := d.SyncBlockTypeID(c)
convCtx.Convey("Then err should be nil.id should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(id, convey.ShouldNotBeNil)
})
})
})
}
func TestBlockDeleteUserCache(t *testing.T) {
convey.Convey("DeleteUserCache", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
convCtx.Convey("connect memcache failed", func(convCtx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
return memcache.MockWith(memcache.ErrConnClosed)
})
defer guard.Unpatch()
err := d.DeleteUserCache(c, mid)
convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,143 @@
package block
import (
"context"
"fmt"
model "go-common/app/job/main/member/model/block"
xsql "go-common/library/database/sql"
"github.com/pkg/errors"
)
const (
_upsertUser = `INSERT INTO block_user (mid,status) VALUES (?,?) ON DUPLICATE KEY UPDATE status=?`
_userStatusList = `SELECT id,mid FROM block_user WHERE status=? AND id>? LIMIT ?`
_userExtra = `SELECT id,mid,credit_answer_flag,action_time FROM block_extra WHERE mid=? ORDER BY id DESC LIMIT 1`
_userLastHistory = `SELECT id,mid,source,area,action,start_time,duration FROM block_history_%d WHERE mid=? ORDER BY id DESC LIMIT 1`
_upsertAddBlockCount = `INSERT INTO block_user_detail (mid,block_count) VALUES (?,1) ON DUPLICATE KEY UPDATE block_count=block_count+1`
_upsertExtra = `INSERT INTO block_extra (mid,credit_answer_flag,action_time) VALUES (?,?,?) ON DUPLICATE KEY UPDATE credit_answer_flag=? , action_time=?`
_insertHistory = `INSERT INTO block_history_%d (mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify) VALUES (?,?,?,?,?,?,?,?,?,?,?)`
)
func historyIdx(mid int64) int64 {
return mid % 10
}
// UserStatusList is.
func (d *Dao) UserStatusList(c context.Context, status model.BlockStatus, startID int64, limit int) (maxID int64, mids []int64, err error) {
var (
rows *xsql.Rows
)
if rows, err = d.db.Query(c, _userStatusList, status, startID, limit); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
var (
id, mid int64
)
if err = rows.Scan(&id, &mid); err != nil {
err = errors.WithStack(err)
return
}
if maxID < id {
maxID = id
}
mids = append(mids, mid)
}
if err = rows.Err(); err != nil {
err = errors.WithStack(err)
return
}
return
}
// UserLastHistory is.
func (d *Dao) UserLastHistory(c context.Context, mid int64) (his *model.DBHistory, err error) {
var (
sql = fmt.Sprintf(_userLastHistory, historyIdx(mid))
)
row := d.db.QueryRow(c, sql, mid)
his = &model.DBHistory{}
if err = row.Scan(&his.ID, &his.MID, &his.Source, &his.Area, &his.Action, &his.StartTime, &his.Duration); err != nil {
if err == xsql.ErrNoRows {
err = nil
his = nil
return
}
err = errors.WithStack(err)
return
}
return
}
// UserExtra is.
func (d *Dao) UserExtra(c context.Context, mid int64) (ex *model.DBExtra, err error) {
row := d.db.QueryRow(c, _userExtra, mid)
ex = &model.DBExtra{}
if err = row.Scan(&ex.ID, &ex.MID, &ex.CreditAnswerFlag, &ex.ActionTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
ex = nil
return
}
err = errors.WithStack(err)
return
}
return
}
// TxUpsertUser is.
func (d *Dao) TxUpsertUser(c context.Context, tx *xsql.Tx, mid int64, status model.BlockStatus) (count int64, err error) {
rows, err := tx.Exec(_upsertUser, mid, status, status)
if err != nil {
err = errors.WithStack(err)
return
}
return rows.RowsAffected()
}
// InsertExtra is.
func (d *Dao) InsertExtra(c context.Context, ex *model.DBExtra) (err error) {
if _, err = d.db.Exec(c, _upsertExtra, ex.MID, ex.CreditAnswerFlag, ex.ActionTime, ex.CreditAnswerFlag, ex.ActionTime); err != nil {
err = errors.WithStack(err)
return
}
return
}
// TxUpsertExtra is.
func (d *Dao) TxUpsertExtra(c context.Context, tx *xsql.Tx, ex *model.DBExtra) (err error) {
if _, err = tx.Exec(_upsertExtra, ex.MID, ex.CreditAnswerFlag, ex.ActionTime, ex.CreditAnswerFlag, ex.ActionTime); err != nil {
err = errors.WithStack(err)
return
}
return
}
//TxInsertHistory is.
func (d *Dao) TxInsertHistory(c context.Context, tx *xsql.Tx, h *model.DBHistory) (err error) {
var (
sql = fmt.Sprintf(_insertHistory, historyIdx(h.MID))
)
if _, err = tx.Exec(sql, h.MID, h.AdminID, h.AdminName, h.Source, h.Area, h.Reason, h.Comment, h.Action, h.StartTime, h.Duration, h.Notify); err != nil {
err = errors.WithStack(err)
return
}
return
}
//UpsertAddBlockCount is.
func (d *Dao) UpsertAddBlockCount(c context.Context, mid int64) (err error) {
if _, err = d.db.Exec(c, _upsertAddBlockCount, mid); err != nil {
err = errors.WithStack(err)
return
}
return
}

View File

@@ -0,0 +1,277 @@
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)
})
})
})
}

View File

@@ -0,0 +1,16 @@
package block
import (
"context"
"github.com/pkg/errors"
)
// AccountNotify is
func (d *Dao) AccountNotify(c context.Context, mid int64) (err error) {
action := "blockUser"
if err = d.notifyFunc(c, mid, action); err != nil {
err = errors.Errorf("mid(%d) s.accountNotify.Send(%+v) error(%+v)", mid, action, err)
}
return
}