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,70 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"hbase.go",
"memcache.go",
"mysql.go",
"rpc.go",
],
importpath = "go-common/app/interface/main/creative/dao/weeklyhonor",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/up/util/hbaseutil:go_default_library",
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/weeklyhonor:go_default_library",
"//app/service/main/up/api/v1:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/hbase.v2:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/tsuna/gohbase/hrpc: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"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"hbase_test.go",
"memcache_test.go",
"mysql_test.go",
"rpc_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/weeklyhonor:go_default_library",
"//app/service/main/up/api/v1:go_default_library",
"//library/database/hbase.v2:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/golang/mock/gomock:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/github.com/tsuna/gohbase/hrpc:go_default_library",
],
)

View File

@@ -0,0 +1,69 @@
package weeklyhonor
import (
"context"
"time"
"go-common/app/interface/main/creative/conf"
up "go-common/app/service/main/up/api/v1"
"go-common/library/cache/memcache"
"go-common/library/database/hbase.v2"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao is data dao.
type Dao struct {
c *conf.Config
// hbase
hbase *hbase.Client
hbaseTimeOut time.Duration
// db
db *sql.DB
// mc
mc *memcache.Pool
mcExpire int32
mcClickExpire int32
// grpc
upClient up.UpClient
}
// New init dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
// hbase
hbase: hbase.NewClient(c.HBaseOld.Config),
hbaseTimeOut: time.Duration(time.Millisecond * 200),
// db
db: sql.NewMySQL(c.DB.Creative),
// mc
mc: memcache.NewPool(c.Memcache.Honor.Config),
mcExpire: int32(time.Duration(c.Memcache.Honor.HonorExpire) / time.Second),
mcClickExpire: int32(time.Duration(c.Memcache.Honor.ClickExpire) / time.Second),
}
var err error
if d.upClient, err = up.NewClient(c.UpClient); err != nil {
panic(err)
}
return
}
// Ping ping success.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingMySQL(c); err != nil {
log.Error("s.pingMySQL.Ping err(%v)", err)
}
return
}
// Close hbase close
func (d *Dao) Close() (err error) {
if d.hbase != nil {
d.hbase.Close()
}
if d.db != nil {
d.db.Close()
}
return
}

View File

@@ -0,0 +1,46 @@
package weeklyhonor
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/creative/conf"
"github.com/golang/mock/gomock"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func WithMock(t *testing.T, f func(mock *gomock.Controller)) func() {
return func() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
f(mockCtrl)
}
}

View File

@@ -0,0 +1,63 @@
package weeklyhonor
import (
"context"
"strconv"
"go-common/app/admin/main/up/util/hbaseutil"
model "go-common/app/interface/main/creative/model/weeklyhonor"
"go-common/library/ecode"
"go-common/library/log"
"github.com/tsuna/gohbase/hrpc"
)
// reverse for string.
func reverseString(s string) string {
rs := []rune(s)
l := len(rs)
for f, t := 0, l-1; f < t; f, t = f+1, t-1 {
rs[f], rs[t] = rs[t], rs[f]
}
ns := string(rs)
if l < 10 {
for i := 0; i < 10-l; i++ {
ns = ns + "0"
}
}
return ns
}
func honorRowKey(id int64, date string) string {
idStr := strconv.FormatInt(id, 10)
s := reverseString(idStr) + date
return s
}
// HonorStat get up honor.
func (d *Dao) HonorStat(c context.Context, mid int64, date string) (stat *model.HonorStat, err error) {
var (
result *hrpc.Result
ctx, cancel = context.WithTimeout(c, d.hbaseTimeOut)
tableName = "up_honorary_weekly"
key = honorRowKey(mid, date)
)
defer cancel()
if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
log.Error("HonorStat d.hbase.GetStr tableName(%s) mid(%d) key(%v) error(%v)", tableName, mid, key, err)
err = ecode.CreativeDataErr
return
}
if result == nil {
return
}
stat = new(model.HonorStat)
parser := hbaseutil.Parser{}
err = parser.Parse(result.Cells, stat)
if err != nil {
log.Error("failed to parser hbase, tableName(%s) mid(%d) key(%v) stat(%+v) err (%v)", tableName, mid, key, stat, err)
return
}
log.Info("HonorStat d.hbase.GetStr tableName(%s) mid(%d) key(%v) stat(%+v)", tableName, mid, key, stat)
return
}

View File

@@ -0,0 +1,104 @@
package weeklyhonor
import (
"bytes"
"context"
"encoding/binary"
"reflect"
"testing"
"go-common/library/database/hbase.v2"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
"github.com/tsuna/gohbase/hrpc"
)
func TestWeeklyhonorreverseString(t *testing.T) {
convey.Convey("reverseString", t, func(ctx convey.C) {
var (
s = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := reverseString(s)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorhonorRowKey(t *testing.T) {
convey.Convey("honorRowKey", t, func(ctx convey.C) {
var (
id = int64(0)
date = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := honorRowKey(id, date)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorHonorStat(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
date = "20181112"
val1 int32 = 500
val2 int32 = -2
cs = getMockCells(val1, val2)
)
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.hbase), "GetStr", func(_ *hbase.Client, _ context.Context, _, _ string, _ ...func(hrpc.Call) error) (*hrpc.Result, error) {
res := &hrpc.Result{
Cells: cs,
}
return res, nil
})
defer guard.Unpatch()
convey.Convey("honorStat", t, func() {
stat, err := d.HonorStat(c, mid, date)
convey.So(err, convey.ShouldBeNil)
convey.So(stat, convey.ShouldNotBeNil)
convey.So(stat.Play, convey.ShouldEqual, val1)
convey.So(stat.FansInc, convey.ShouldEqual, val2)
convey.So(stat.Rank0, convey.ShouldEqual, val1)
})
}
func getMockCells(v1, v2 int32) []*hrpc.Cell {
s1 := make([]byte, 0)
buf1 := bytes.NewBuffer(s1)
s2 := make([]byte, 0)
buf2 := bytes.NewBuffer(s2)
binary.Write(buf1, binary.BigEndian, v1)
binary.Write(buf2, binary.BigEndian, v2)
cells := []*hrpc.Cell{
{
Qualifier: []byte("play"),
Value: buf1.Bytes(),
Family: []byte("f"),
},
{
Qualifier: []byte("play_last_w"),
Value: buf1.Bytes(),
Family: []byte("f"),
},
{
Qualifier: []byte("fans_inc"),
Value: buf2.Bytes(),
Family: []byte("f"),
},
{
Qualifier: []byte("rank0"),
Value: buf1.Bytes(),
Family: []byte("r"),
},
}
return cells
}

View File

@@ -0,0 +1,120 @@
package weeklyhonor
import (
"context"
"fmt"
model "go-common/app/interface/main/creative/model/weeklyhonor"
"go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_statKey = "hs_%d_%s"
_honorKey = "ho_%d_%s"
_clickKey = "hc_%d"
)
func statKey(mid int64, date string) string {
return fmt.Sprintf(_statKey, mid, date)
}
func honorKey(mid int64, date string) string {
return fmt.Sprintf(_honorKey, mid, date)
}
func honorClickKey(mid int64) string {
return fmt.Sprint(_clickKey, mid)
}
// StatMC get stat cache.
func (d *Dao) StatMC(c context.Context, mid int64, date string) (hs *model.HonorStat, err error) {
var (
key = statKey(mid, date)
conn = d.mc.Get(c)
)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(r, &hs); err != nil {
log.Error("conn.Scan(%s) error(%v)", r.Value, err)
hs = nil
}
return
}
// HonorMC get honor cache.
func (d *Dao) HonorMC(c context.Context, mid int64, date string) (res *model.HonorLog, err error) {
var (
key = honorKey(mid, date)
conn = d.mc.Get(c)
)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(r, &res); err != nil {
log.Error("conn.Scan(%s) error(%v)", r.Value, err)
res = nil
}
return
}
// SetStatMC add stat cache.
func (d *Dao) SetStatMC(c context.Context, mid int64, date string, hs *model.HonorStat) (err error) {
var (
key = statKey(mid, date)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: key, Object: hs, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
log.Error("memcache.Set(%s) error(%v)", key, err)
}
return
}
// SetHonorMC add honor cache.
func (d *Dao) SetHonorMC(c context.Context, mid int64, date string, hs *model.HonorLog) (err error) {
var (
key = honorKey(mid, date)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: key, Object: hs, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
log.Error("memcache.Set(%s) error(%v)", key, err)
}
return
}
// SetClickMC add click cache
func (d *Dao) SetClickMC(c context.Context, mid int64) (err error) {
key := honorClickKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: key, Value: []byte{1}, Expiration: d.mcClickExpire}); err != nil {
log.Error("memcache.Set(%s) error(%v)", key, err)
}
return
}
// ClickMC get click cache
func (d *Dao) ClickMC(c context.Context, mid int64) (err error) {
key := honorClickKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
_, err = conn.Get(key)
return
}

View File

@@ -0,0 +1,151 @@
package weeklyhonor
import (
"context"
model "go-common/app/interface/main/creative/model/weeklyhonor"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestWeeklyhonorstatKey(t *testing.T) {
convey.Convey("statKey", t, func(ctx convey.C) {
var (
mid = int64(0)
date = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := statKey(mid, date)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorhonorKey(t *testing.T) {
convey.Convey("honorKey", t, func(ctx convey.C) {
var (
mid = int64(0)
date = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := honorKey(mid, date)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorhonorClickKey(t *testing.T) {
convey.Convey("honorClickKey", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := honorClickKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorStatMC(t *testing.T) {
convey.Convey("StatMC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
date = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
hs, err := d.StatMC(c, mid, date)
ctx.Convey("Then err should be nil.hs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(hs, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorHonorMC(t *testing.T) {
convey.Convey("HonorMC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
date = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.HonorMC(c, mid, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorSetStatMC(t *testing.T) {
convey.Convey("SetStatMC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
date = ""
hs = &model.HonorStat{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetStatMC(c, mid, date, hs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestWeeklyhonorSetHonorMC(t *testing.T) {
convey.Convey("SetHonorMC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
date = ""
hs = &model.HonorLog{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetHonorMC(c, mid, date, hs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestWeeklyhonorSetClickMC(t *testing.T) {
convey.Convey("SetClickMC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetClickMC(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestWeeklyhonorClickMC(t *testing.T) {
convey.Convey("ClickMC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.ClickMC(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,58 @@
package weeklyhonor
import (
"context"
model "go-common/app/interface/main/creative/model/weeklyhonor"
"go-common/library/log"
)
const (
_honorLogsSQL = `SELECT id,mid,hid,count,ctime,mtime FROM weeklyhonor WHERE mid=?`
_upsertCountSQL = `INSERT INTO weeklyhonor (mid,hid,count) VALUES (?,?,?) ON DUPLICATE KEY UPDATE count=count+1`
_upsertClickCountSQL = `INSERT INTO weeklyhonor_click (mid,count) VALUES (?,?) ON DUPLICATE KEY UPDATE count=count+1`
)
// pingMySQL check mysql connection.
func (d *Dao) pingMySQL(c context.Context) error {
return d.db.Ping(c)
}
// HonorLogs .
func (d *Dao) HonorLogs(c context.Context, mid int64) (hls map[int]*model.HonorLog, err error) {
rows, err := d.db.Query(c, _honorLogsSQL, mid)
if err != nil {
log.Error("d.db.Query(%s,%d) error(%v)", _honorLogsSQL, mid, err)
return
}
defer rows.Close()
hls = make(map[int]*model.HonorLog)
for rows.Next() {
h := new(model.HonorLog)
if err = rows.Scan(&h.ID, &h.MID, &h.HID, &h.Count, &h.CTime, &h.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
hls[h.HID] = h
}
err = rows.Err()
return
}
// UpsertCount .
func (d *Dao) UpsertCount(c context.Context, mid int64, hid int) (err error) {
if _, err = d.db.Exec(c, _upsertCountSQL, mid, hid, 1); err != nil {
log.Error("d.db.Exec(%s,%d,%d) error(%v)", _upsertCountSQL, mid, hid, err)
return
}
return
}
// UpsertClickCount log weeklyhonor click count
func (d *Dao) UpsertClickCount(c context.Context, mid int64) (err error) {
if _, err = d.db.Exec(c, _upsertClickCountSQL, mid, 1); err != nil {
log.Error("d.db.Exec(%s,%d) error(%v)", _upsertClickCountSQL, mid, err)
return
}
return
}

View File

@@ -0,0 +1,69 @@
package weeklyhonor
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestWeeklyhonorpingMySQL(t *testing.T) {
convey.Convey("pingMySQL", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.pingMySQL(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestWeeklyhonorHonorLogs(t *testing.T) {
convey.Convey("HonorLogs", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
hls, err := d.HonorLogs(c, mid)
ctx.Convey("Then err should be nil.hls should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(hls, convey.ShouldNotBeNil)
})
})
})
}
func TestWeeklyhonorUpsertCount(t *testing.T) {
convey.Convey("UpsertCount", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
hid = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.UpsertCount(c, mid, hid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestWeeklyhonorUpsertClickCount(t *testing.T) {
convey.Convey("UpsertClickCount", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(500)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.UpsertClickCount(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,37 @@
package weeklyhonor
import (
"context"
up "go-common/app/service/main/up/api/v1"
"go-common/library/log"
)
const fromWeeklyHonor = 1
// ChangeUpSwitch change up switch on/off
func (d *Dao) ChangeUpSwitch(c context.Context, mid int64, state uint8) (err error) {
req := up.UpSwitchReq{
Mid: mid,
From: fromWeeklyHonor,
State: state,
}
if _, err = d.upClient.SetUpSwitch(c, &req); err != nil {
log.Error("d.upClient.SetUpSwitch req(%+v),err(%v)", req, err)
}
return
}
// GetUpSwitch get up switch state
func (d *Dao) GetUpSwitch(c context.Context, mid int64) (state uint8, err error) {
req := up.UpSwitchReq{
Mid: mid,
From: fromWeeklyHonor,
}
reply, err := d.upClient.UpSwitch(c, &req)
if err != nil {
log.Error("d.upClient.UpSwitch req(%+v),err(%v)", req, err)
return
}
state = reply.GetState()
return
}

View File

@@ -0,0 +1,89 @@
package weeklyhonor
import (
"context"
"fmt"
"testing"
whmdl "go-common/app/interface/main/creative/model/weeklyhonor"
up "go-common/app/service/main/up/api/v1"
"github.com/golang/mock/gomock"
"github.com/smartystreets/goconvey/convey"
)
func TestWeeklyhonorChangeUpSwitch(t *testing.T) {
convey.Convey("ChangeUpSwitch", t, WithMock(t, func(mockCtrl *gomock.Controller) {
var (
c = context.Background()
mid = int64(75379101)
state = whmdl.HonorSub
)
convey.Convey("When everything gose positive", func(ctx convey.C) {
mockUpClient := up.NewMockUpClient(mockCtrl)
d.upClient = mockUpClient
mockReq := up.UpSwitchReq{
Mid: mid,
From: fromWeeklyHonor,
State: state,
}
mockUpClient.EXPECT().SetUpSwitch(gomock.Any(), &mockReq).Return(&up.NoReply{}, nil)
err := d.ChangeUpSwitch(c, mid, state)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
convey.Convey("When return err", func(ctx convey.C) {
mockUpClient := up.NewMockUpClient(mockCtrl)
d.upClient = mockUpClient
mockReq := up.UpSwitchReq{
Mid: mid,
From: fromWeeklyHonor,
State: state,
}
mockUpClient.EXPECT().SetUpSwitch(gomock.Any(), &mockReq).Return(nil, fmt.Errorf("mock err"))
err := d.ChangeUpSwitch(c, mid, state)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
}))
}
func TestWeeklyhonorGetUpSwitch(t *testing.T) {
convey.Convey("GetUpSwitch", t, WithMock(t, func(mockCtrl *gomock.Controller) {
var (
c = context.Background()
mid = int64(75379101)
)
convey.Convey("When everything gose positive", func(ctx convey.C) {
mockUpClient := up.NewMockUpClient(mockCtrl)
d.upClient = mockUpClient
mockReq := up.UpSwitchReq{
Mid: mid,
From: fromWeeklyHonor,
}
mockState := whmdl.HonorUnSub
mockUpClient.EXPECT().UpSwitch(gomock.Any(), &mockReq).Return(&up.UpSwitchReply{State: mockState}, nil)
state, err := d.GetUpSwitch(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(state, convey.ShouldEqual, mockState)
})
})
convey.Convey("When return err", func(ctx convey.C) {
mockUpClient := up.NewMockUpClient(mockCtrl)
d.upClient = mockUpClient
mockReq := up.UpSwitchReq{
Mid: mid,
From: fromWeeklyHonor,
}
mockUpClient.EXPECT().UpSwitch(gomock.Any(), &mockReq).Return(nil, fmt.Errorf("mock err"))
_, err := d.GetUpSwitch(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
}))
}