Create & Init Project...
This commit is contained in:
67
app/interface/main/videoup/dao/account/BUILD
Normal file
67
app/interface/main/videoup/dao/account/BUILD
Normal 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",
|
||||
"mc_limit_test.go",
|
||||
"rpc_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
rundir = ".",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/interface/main/videoup/conf:go_default_library",
|
||||
"//app/service/main/account/api:go_default_library",
|
||||
"//app/service/main/relation/model:go_default_library",
|
||||
"//app/service/main/relation/rpc/client:go_default_library",
|
||||
"//library/cache/memcache:go_default_library",
|
||||
"//library/ecode: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/gopkg.in/h2non/gock.v1:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"dao.go",
|
||||
"mc_limit.go",
|
||||
"rpc.go",
|
||||
],
|
||||
importpath = "go-common/app/interface/main/videoup/dao/account",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/main/videoup/conf:go_default_library",
|
||||
"//app/service/main/account/api:go_default_library",
|
||||
"//app/service/main/relation/model:go_default_library",
|
||||
"//app/service/main/relation/rpc/client:go_default_library",
|
||||
"//library/cache/memcache:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster: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"],
|
||||
)
|
122
app/interface/main/videoup/dao/account/dao.go
Normal file
122
app/interface/main/videoup/dao/account/dao.go
Normal file
@ -0,0 +1,122 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go-common/app/interface/main/videoup/conf"
|
||||
accapi "go-common/app/service/main/account/api"
|
||||
relation "go-common/app/service/main/relation/rpc/client"
|
||||
"go-common/library/cache/memcache"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
)
|
||||
|
||||
const (
|
||||
_addFollowingURI = "/x/internal/relation/following/add"
|
||||
)
|
||||
|
||||
// Dao dao is account dao.
|
||||
type Dao struct {
|
||||
// config
|
||||
c *conf.Config
|
||||
// rpc
|
||||
rela *relation.Service
|
||||
acc accapi.AccountClient
|
||||
// memcache
|
||||
mc *memcache.Pool
|
||||
mcSubExp, mcLimitAddBasicExp int32
|
||||
client *bm.Client
|
||||
addFollowingURL string
|
||||
}
|
||||
|
||||
// New new a account dao.
|
||||
func New(c *conf.Config) (d *Dao) {
|
||||
d = &Dao{
|
||||
c: c,
|
||||
// rpc
|
||||
rela: relation.New(c.RelationRPC),
|
||||
// access memcache
|
||||
mc: memcache.NewPool(c.Memcache.Account.Config),
|
||||
mcSubExp: int32(time.Duration(c.Memcache.Account.SubmitExpire) / time.Second),
|
||||
mcLimitAddBasicExp: int32(time.Duration(c.Limit.AddBasicExp) / time.Second),
|
||||
client: bm.NewClient(c.HTTPClient.Write),
|
||||
addFollowingURL: c.Host.APICo + _addFollowingURI,
|
||||
}
|
||||
var err error
|
||||
if d.acc, err = accapi.NewClient(c.AccClient); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Close close resource.
|
||||
func (d *Dao) Close() {
|
||||
if d.mc != nil {
|
||||
d.mc.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Ping ping success.
|
||||
func (d *Dao) Ping(c context.Context) (err error) {
|
||||
if err = d.pingMemcache(c); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// IdentifyInfo 获取用户实名认证状态
|
||||
func (d *Dao) IdentifyInfo(c context.Context, ip string, mid int64) (err error) {
|
||||
var mf *accapi.Profile
|
||||
if mf, err = d.Profile(c, mid, ip); err != nil {
|
||||
log.Error("d.Profile mid(%d),ip(%s),error(%v)", mid, ip, err)
|
||||
err = ecode.CreativeAccServiceErr
|
||||
return
|
||||
}
|
||||
if mf.Identification == 1 {
|
||||
return
|
||||
}
|
||||
//switch for FrontEnd return json format, return OldPhone, and newError
|
||||
if err = d.switchIDInfoRet(mf.TelStatus); err != nil {
|
||||
log.Error("switchIDInfoRet res(%v)", mf.TelStatus)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Dao) switchIDInfoRet(phoneRet int32) (err error) {
|
||||
switch phoneRet {
|
||||
case 0:
|
||||
err = ecode.UserCheckNoPhone
|
||||
case 1:
|
||||
err = nil
|
||||
case 2:
|
||||
err = ecode.UserCheckInvalidPhone
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddFollowing 添加关注
|
||||
func (d *Dao) AddFollowing(c context.Context, mid, fid int64, src int, ip string) (err error) {
|
||||
params := url.Values{}
|
||||
params.Set("mid", strconv.FormatInt(mid, 10))
|
||||
params.Set("fid", strconv.FormatInt(fid, 10))
|
||||
params.Set("src", strconv.Itoa(src))
|
||||
var res struct {
|
||||
Code int `json:"code"`
|
||||
}
|
||||
if err = d.client.Post(c, d.addFollowingURL, ip, params, &res); err != nil {
|
||||
log.Error("d.client.Do uri(%s) mid(%d) fid(%d) res(%+v) error(%v)", d.addFollowingURL+"?"+params.Encode(), mid, fid, res, err)
|
||||
return
|
||||
}
|
||||
log.Info("acc AddFollowing url(%s)", d.addFollowingURL+"?"+params.Encode())
|
||||
if res.Code != 0 {
|
||||
log.Error("acc AddFollowing (%+s)|(%+d)|(%+d)|(%+d)|(%s) (%+v)", d.addFollowingURL, mid, fid, src, ip, res)
|
||||
err = ecode.CreativeAccServiceErr
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
66
app/interface/main/videoup/dao/account/dao_test.go
Normal file
66
app/interface/main/videoup/dao/account/dao_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"go-common/app/interface/main/videoup/conf"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/smartystreets/goconvey/convey"
|
||||
gock "gopkg.in/h2non/gock.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
d *Dao
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if os.Getenv("DEPLOY_ENV") != "" {
|
||||
flag.Set("app_id", "main.archive.videoup")
|
||||
flag.Set("conf_token", "9772c9629b00ac09af29a23004795051")
|
||||
flag.Set("tree_id", "2306")
|
||||
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/videoup.toml")
|
||||
}
|
||||
flag.Parse()
|
||||
if err := conf.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
d = New(conf.Conf)
|
||||
m.Run()
|
||||
os.Exit(0)
|
||||
}
|
||||
func httpMock(method, url string) *gock.Request {
|
||||
r := gock.New(url)
|
||||
r.Method = strings.ToUpper(method)
|
||||
d.client.SetTransport(gock.DefaultTransport)
|
||||
return r
|
||||
}
|
||||
|
||||
func TestAddFollowing(t *testing.T) {
|
||||
convey.Convey("AddFollowing", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
fid = int64(2089810)
|
||||
src = 173
|
||||
ip = "127.0.0.1"
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
defer gock.OffAll()
|
||||
httpMock("Post", d.addFollowingURL).Reply(200).JSON(`{"code":0,"data":""}`)
|
||||
err := d.AddFollowing(c, mid, fid, src, ip)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
132
app/interface/main/videoup/dao/account/mc_limit.go
Normal file
132
app/interface/main/videoup/dao/account/mc_limit.go
Normal file
@ -0,0 +1,132 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go-common/library/cache/memcache"
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
const (
|
||||
_addMidAndTitlePrefix = "add_midtitle_"
|
||||
_addMidHalfMinPrefix = "add_midhafmin_"
|
||||
)
|
||||
|
||||
func limitMidHafMin(mid int64) string {
|
||||
return _addMidHalfMinPrefix + strconv.FormatInt(mid, 10)
|
||||
}
|
||||
|
||||
func limitMidSameTitle(mid int64, title string) string {
|
||||
ms := md5.Sum([]byte(title))
|
||||
return _addMidAndTitlePrefix + strconv.FormatInt(mid, 10) + "_" + hex.EncodeToString(ms[:])
|
||||
}
|
||||
|
||||
// HalfMin fn
|
||||
func (d *Dao) HalfMin(c context.Context, mid int64) (exist bool, ts uint64, err error) {
|
||||
var (
|
||||
conn = d.mc.Get(c)
|
||||
rp *memcache.Item
|
||||
)
|
||||
defer conn.Close()
|
||||
key := limitMidHafMin(mid)
|
||||
rp, err = conn.Get(key)
|
||||
if err != nil {
|
||||
if err == memcache.ErrNotFound {
|
||||
err = nil
|
||||
} else {
|
||||
log.Error("conn.Get error(%v) | key(%s) mid(%d)", err, key, mid)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err = conn.Scan(rp, &ts); err != nil {
|
||||
log.Error("conn.Scan(%s) error(%v)", rp.Value, err)
|
||||
return
|
||||
}
|
||||
log.Info("HalfMin key(%s) ts(%d)", key, ts)
|
||||
if ts != 0 {
|
||||
exist = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddHalfMin fn
|
||||
func (d *Dao) AddHalfMin(c context.Context, mid int64) (err error) {
|
||||
conn := d.mc.Get(c)
|
||||
defer conn.Close()
|
||||
key := limitMidHafMin(mid)
|
||||
ts := time.Now().Unix()
|
||||
if err = conn.Set(&memcache.Item{Key: key, Object: ts, Flags: memcache.FlagJSON, Expiration: d.mcLimitAddBasicExp}); err != nil {
|
||||
log.Error("memcache.set error(%v) | key(%s) mid(%d)", err, key, mid)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DelHalfMin func
|
||||
func (d *Dao) DelHalfMin(c context.Context, mid int64) (err error) {
|
||||
conn := d.mc.Get(c)
|
||||
defer conn.Close()
|
||||
if err = conn.Delete(limitMidHafMin(mid)); err == memcache.ErrNotFound {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SubmitCache get user submit cache.
|
||||
func (d *Dao) SubmitCache(c context.Context, mid int64, title string) (exist int8, err error) {
|
||||
var (
|
||||
conn = d.mc.Get(c)
|
||||
rp *memcache.Item
|
||||
)
|
||||
defer conn.Close()
|
||||
key := limitMidSameTitle(mid, title)
|
||||
rp, err = conn.Get(key)
|
||||
if err != nil {
|
||||
if err == memcache.ErrNotFound {
|
||||
err = nil
|
||||
} else {
|
||||
log.Error("conn.Get error(%v) | key(%s) mid(%d) title(%s)", err, key, mid, title)
|
||||
}
|
||||
}
|
||||
if rp != nil {
|
||||
exist = int8(binary.BigEndian.Uint64(rp.Value))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddSubmitCache add submit cache into mc.
|
||||
func (d *Dao) AddSubmitCache(c context.Context, mid int64, title string) (err error) {
|
||||
conn := d.mc.Get(c)
|
||||
defer conn.Close()
|
||||
key := limitMidSameTitle(mid, title)
|
||||
bs := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bs, 1)
|
||||
if err = conn.Set(&memcache.Item{Key: key, Object: bs, Flags: memcache.FlagJSON, Expiration: d.mcSubExp}); err != nil {
|
||||
log.Error("memcache.set error(%v) | key(%s) mid(%d) title(%s)", err, key, mid, title)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DelSubmitCache func
|
||||
func (d *Dao) DelSubmitCache(c context.Context, mid int64, title string) (err error) {
|
||||
conn := d.mc.Get(c)
|
||||
defer conn.Close()
|
||||
if err = conn.Delete(limitMidSameTitle(mid, title)); err == memcache.ErrNotFound {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Dao) pingMemcache(c context.Context) (err error) {
|
||||
conn := d.mc.Get(c)
|
||||
defer conn.Close()
|
||||
if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
|
||||
log.Error("mc.ping.Store error(%v)", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
179
app/interface/main/videoup/dao/account/mc_limit_test.go
Normal file
179
app/interface/main/videoup/dao/account/mc_limit_test.go
Normal file
@ -0,0 +1,179 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-common/library/cache/memcache"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/monkey"
|
||||
"github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestAccountlimitMidHafMin(t *testing.T) {
|
||||
convey.Convey("limitMidHafMin", t, func(ctx convey.C) {
|
||||
var (
|
||||
mid = int64(2089809)
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
p1 := limitMidHafMin(mid)
|
||||
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(p1, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountlimitMidSameTitle(t *testing.T) {
|
||||
convey.Convey("limitMidSameTitle", t, func(ctx convey.C) {
|
||||
var (
|
||||
mid = int64(2089809)
|
||||
title = "iamtitle"
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
p1 := limitMidSameTitle(mid, title)
|
||||
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(p1, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountHalfMin(t *testing.T) {
|
||||
convey.Convey("HalfMin", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
exist, ts, err := d.HalfMin(c, mid)
|
||||
ctx.Convey("Then err should be nil.exist,ts should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
ctx.So(ts, convey.ShouldNotBeNil)
|
||||
ctx.So(exist, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountAddHalfMin(t *testing.T) {
|
||||
convey.Convey("AddHalfMin", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
err := d.AddHalfMin(c, mid)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountDelHalfMin(t *testing.T) {
|
||||
convey.Convey("DelHalfMin", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
err := d.DelHalfMin(c, mid)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountSubmitCache(t *testing.T) {
|
||||
convey.Convey("SubmitCache", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
title = "iamtitle"
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
exist, err := d.SubmitCache(c, mid, title)
|
||||
ctx.Convey("Then err should be nil.exist should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
ctx.So(exist, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountAddSubmitCache(t *testing.T) {
|
||||
convey.Convey("AddSubmitCache", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
title = "iamtitle"
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
err := d.AddSubmitCache(c, mid, title)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountDelSubmitCache(t *testing.T) {
|
||||
convey.Convey("DelSubmitCache", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
title = "iamtitle"
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
err := d.DelSubmitCache(c, mid, title)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountpingMemcache(t *testing.T) {
|
||||
convey.Convey("pingMemcache", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
err error
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
|
||||
return memcache.MockWith(memcache.ErrNotFound)
|
||||
})
|
||||
defer connGuard.Unpatch()
|
||||
err = d.pingMemcache(c)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
82
app/interface/main/videoup/dao/account/rpc.go
Normal file
82
app/interface/main/videoup/dao/account/rpc.go
Normal file
@ -0,0 +1,82 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
accapi "go-common/app/service/main/account/api"
|
||||
relaMdl "go-common/app/service/main/relation/model"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
// Profile get profile from rpc
|
||||
func (d *Dao) Profile(c context.Context, mid int64, ip string) (p *accapi.Profile, err error) {
|
||||
arg := &accapi.MidReq{
|
||||
Mid: mid,
|
||||
}
|
||||
var rpcRes *accapi.ProfileReply
|
||||
if rpcRes, err = d.acc.Profile3(c, arg); err != nil {
|
||||
log.Error("d.acc.Profile3 error(%v) | mid(%d) ip(%s) arg(%v)", err, mid, ip, arg)
|
||||
err = ecode.CreativeAccServiceErr
|
||||
}
|
||||
if rpcRes != nil {
|
||||
p = rpcRes.Profile
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Cards get cards from rpc
|
||||
func (d *Dao) Cards(c context.Context, mids []int64, ip string) (cards map[int64]*accapi.Card, err error) {
|
||||
var res *accapi.CardsReply
|
||||
if len(mids) == 0 {
|
||||
return
|
||||
}
|
||||
arg := &accapi.MidsReq{
|
||||
Mids: mids,
|
||||
}
|
||||
if res, err = d.acc.Cards3(c, arg); err != nil {
|
||||
log.Error("d.acc.Cards3 error(%v) | mids(%v) ip(%s) arg(%v)", err, mids, ip, arg)
|
||||
err = ecode.CreativeAccServiceErr
|
||||
}
|
||||
if res != nil {
|
||||
cards = res.Cards
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Infos get infos from rpc
|
||||
func (d *Dao) Infos(c context.Context, mids []int64, ip string) (infos map[int64]*accapi.Info, err error) {
|
||||
var res *accapi.InfosReply
|
||||
arg := &accapi.MidsReq{
|
||||
Mids: mids,
|
||||
}
|
||||
infos = make(map[int64]*accapi.Info)
|
||||
if res, err = d.acc.Infos3(c, arg); err != nil {
|
||||
log.Error("d.acc.Infos3 error(%v) | mids(%v) ip(%s) arg(%v)", err, mids, ip, arg)
|
||||
err = ecode.CreativeAccServiceErr
|
||||
}
|
||||
if res != nil {
|
||||
infos = res.Infos
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Relations get all relation state.
|
||||
func (d *Dao) Relations(c context.Context, mid int64, fids []int64, ip string) (res map[int64]int, err error) {
|
||||
var rls map[int64]*relaMdl.Following
|
||||
if rls, err = d.rela.Relations(c, &relaMdl.ArgRelations{Mid: mid, Fids: fids, RealIP: ip}); err != nil {
|
||||
log.Error("d.rela.Relations mid(%d)|ip(%s)|error(%v)", mid, ip, err)
|
||||
err = ecode.CreativeAccServiceErr
|
||||
return
|
||||
}
|
||||
if len(rls) == 0 {
|
||||
log.Info("d.rela.Relations mid(%d)|ip(%s)", mid, ip)
|
||||
return
|
||||
}
|
||||
res = make(map[int64]int, len(rls))
|
||||
for _, v := range rls {
|
||||
res[v.Mid] = int(v.Attribute)
|
||||
}
|
||||
log.Info("d.rela.Relations mid(%d)|res(%+v)|rls(%+v)|ip(%s)", mid, res, rls, ip)
|
||||
return
|
||||
}
|
107
app/interface/main/videoup/dao/account/rpc_test.go
Normal file
107
app/interface/main/videoup/dao/account/rpc_test.go
Normal file
@ -0,0 +1,107 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
relaMdl "go-common/app/service/main/relation/model"
|
||||
"go-common/app/service/main/relation/rpc/client"
|
||||
|
||||
"github.com/bouk/monkey"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
accapi "go-common/app/service/main/account/api"
|
||||
"go-common/library/ecode"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
func WithMock(t *testing.T, f func(mock *gomock.Controller)) func() {
|
||||
return func() {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
f(mockCtrl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountProfile(t *testing.T) {
|
||||
Convey("1", t, WithMock(t, func(mockCtrl *gomock.Controller) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
ip = "127.0.0.1"
|
||||
err error
|
||||
p *accapi.Profile
|
||||
)
|
||||
mock := accapi.NewMockAccountClient(mockCtrl)
|
||||
d.acc = mock
|
||||
mockReq := &accapi.MidReq{
|
||||
Mid: mid,
|
||||
}
|
||||
mock.EXPECT().Profile3(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
|
||||
p, err = d.Profile(c, mid, ip)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(p, ShouldBeNil)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_Cards(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
ip = "127.0.0.1"
|
||||
err error
|
||||
)
|
||||
Convey("Cards", t, WithMock(t, func(mockCtrl *gomock.Controller) {
|
||||
mock := accapi.NewMockAccountClient(mockCtrl)
|
||||
d.acc = mock
|
||||
mockReq := &accapi.MidsReq{
|
||||
Mids: []int64{mid},
|
||||
}
|
||||
res := &accapi.CardsReply{}
|
||||
mock.EXPECT().Cards3(gomock.Any(), mockReq).Return(res, nil)
|
||||
_, err = d.Cards(c, []int64{mid}, ip)
|
||||
So(err, ShouldBeNil)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_Infos(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
ip = "127.0.0.1"
|
||||
err error
|
||||
)
|
||||
Convey("Infos", t, WithMock(t, func(mockCtrl *gomock.Controller) {
|
||||
mock := accapi.NewMockAccountClient(mockCtrl)
|
||||
d.acc = mock
|
||||
mockReq := &accapi.MidsReq{
|
||||
Mids: []int64{mid},
|
||||
}
|
||||
res := &accapi.InfosReply{}
|
||||
mock.EXPECT().Infos3(gomock.Any(), mockReq).Return(res, nil)
|
||||
_, err = d.Infos(c, []int64{mid}, ip)
|
||||
So(err, ShouldBeNil)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_Relations(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
mid = int64(2089809)
|
||||
ip = "127.0.0.1"
|
||||
err error
|
||||
)
|
||||
Convey("Relations", t, func(ctx C) {
|
||||
mock := monkey.PatchInstanceMethod(reflect.TypeOf(d.rela), "Relations",
|
||||
func(_ *relation.Service, _ context.Context, _ *relaMdl.ArgRelations) (res map[int64]*relaMdl.Following, err error) {
|
||||
res = make(map[int64]*relaMdl.Following)
|
||||
res[2089809] = &relaMdl.Following{}
|
||||
return res, nil
|
||||
})
|
||||
defer mock.Unpatch()
|
||||
_, err = d.Relations(c, mid, []int64{mid}, ip)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user