首页 > 其他分享 >Go实战全家桶之十九:GO代理模式实现测试桩优化版

Go实战全家桶之十九:GO代理模式实现测试桩优化版

时间:2024-11-24 18:31:46浏览次数:6  
标签:Info connectProto ret var Go func GO suite 十九

  1. 定义接口
  2. 实现接口
  3. 实现测试桩
  4. 定义代理

测试

third.InitThirdProxyStub()

func (suite *TestTestStubSuite) Test009_ContactShopEs() {

    var ret, err = third.FindBeanThirdProxy().ContactShopListQuery([]int64{974835942347243523})
    golog.Info(esobject.AdapterFrom(ret), err)

}

INFO[ git.ichub.com/general/webcli120/goconfig/base/goutils.Info() {
     "list": [
          {
               "id": 974835942347243523,
               "created_by": 974835940126588931,
               "updated_by": 974835940126588931,
               "name": "ichubp之家",
               "short_name": "ichubp之家",
               "slug": "347dcdcf-4b3d-49aa-a983-9f670d7c2025",
               "status": "L",
               "shop_type": "single",
               "default_address": " ",
               "contactId": "53a9a98d-3125-4f9a-8db0-bd618f80e0b8",
               "main_account_num": "CA4954124",
               "Address": {
                    
               },
               "partner_id": 974835942347243523,
               "partner": "ichubp之家",
               "contact_shop_code": "CS0526255",
               "station": "icHub",
               "created_by_name": "ichubp0398",
               "updated_by_name": "ichubp0398"
          }
     ],
     "pagination": {
          "total": 1,
          "current": 1,
          "page_size": 500
     }
}nil 

接口

package thirdiface

import (
    "context"
    "github.com/micro/go-micro/v2/client"
    accountProto "icd/proto/account"
    connectProto "icd/proto/connection"
    cooProto "icd/proto/coo"
)

type ThirdIface interface {
    GetAccountByShopIds(shop_ids []int64) *accountProto.AccountAllResult

    ContactShopMemberQueryByIds(ids []int64) (*connectProto.ContactShopMemberQueryResult, error)
    ContactShopMemberQuery(ctx context.Context, in *connectProto.ContactShopMemberQueryRequest, opts ...client.CallOption) (*connectProto.ContactShopMemberQueryResult, error)

    CooDeptIdsByMmberId(memberIds []int64) (*cooProto.CooMemberJobShopMemberRelRespList, error)
    //GetAccountContactByIds(ids []int64) (*accountProto.AccountContactRecords, error)
    ContactShopListQuery(ids []int64) (*connectProto.ContactShopQueryResult, error)
}代理
package third

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "icd/general-srv/third/thirdiface"
    "icd/general-srv/third/thirdstub"
)

func InitThirdProxyStub() *ThirdProxy {
    var bean *ThirdProxy = FindBeanThirdProxy()
    bean.ThirdIface = thirdstub.FindBeanTestStub()
    return bean
}

type ThirdProxy struct {
    basedto.BaseEntitySingle
    thirdiface.ThirdIface
}

func NewThirdProxy() *ThirdProxy {
    return &ThirdProxy{
       ThirdIface: FindBeanThirdImpl(),
    }
}
 
package third

import (
    "context"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "github.com/micro/go-micro/v2/client"
    accountProto "icd/proto/account"
    connectProto "icd/proto/connection" 
    cooProto "icd/proto/coo"
)

type ThirdImpl struct {
    basedto.BaseEntitySingle
    connectionClient connectProto.ConnectionService
}

func NewThirdImpl() *ThirdImpl {
    return &ThirdImpl{}
}

func (t ThirdImpl) GetAccountByShopIds(shop_ids []int64) *accountProto.AccountAllResult {
    return GetAccountByShopIds(shop_ids)
}

func (t ThirdImpl) ContactShopMemberQueryByIds(ids []int64) (*connectProto.ContactShopMemberQueryResult, error) {
    return ContactShopMemberQueryByIds(ids)
}
func (t ThirdImpl) ContactShopMemberQuery(ctx context.Context, in *connectProto.ContactShopMemberQueryRequest, opts ...client.CallOption) (*connectProto.ContactShopMemberQueryResult, error) {

    return t.connectionClient.ContactShopMemberQuery(ctx, in, opts...)
}
func (t ThirdImpl) CooDeptIdsByMmberId(memberIds []int64) (*cooProto.CooMemberJobShopMemberRelRespList, error) {

    return CooDeptIdsByMmberId(memberIds)
}
func (t ThirdImpl) ContactShopListQuery(ids []int64) (*connectProto.ContactShopQueryResult, error) {

    return ContactShopListQuery(ids)
}
package thirdstub

import (
    "context"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "git.ichub.com/general/webcli120/goweb/pagemodel"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient/webfacade"
    "github.com/gogf/gf/v2/util/gconv"
    "github.com/micro/go-micro/v2/client"
    "github.com/olivere/elastic/v7"
    "icd/general-srv/third/thirdstub/thirddto"
    "icd/pkg/clifacade/esclient/esobject"
    accountProto "icd/proto/account"
    connectProto "icd/proto/connection"
    cooProto "icd/proto/coo"
)

type ThirdStub struct {
    basedto.BaseEntitySingle
}

func NewThirdStub() *ThirdStub {
    return &ThirdStub{}
}

func (t ThirdStub) GetAccountContactByIds(ids []int64) (*accountProto.AccountContactRecords, error) {
    return nil, nil
}
func (t ThirdStub) Ret2AccountAllResult(ret *pagemodel.PageResult[*thirddto.AccountStub]) *accountProto.AccountAllResult {
    var result = accountProto.AccountAllResult{
       PageResult: &accountProto.PaginationResult{},
    }
    result.PageResult.PageSize = gconv.Uint32(ret.PageSize)
    result.PageResult.Current = uint32(ret.PageCurrent)
    result.PageResult.Total = uint64(ret.Total)

    return &result
}
func (t ThirdStub) GetAccountByShopIds(shop_ids []int64) *accountProto.AccountAllResult {
    var q = elastic.NewTermsQuery("root_shop_id", gconv.SliceAny(shop_ids)...)
    var qq = webfacade.DefaultOf[*thirddto.AccountStub](q)
    var ret = qq.GeneralQueryMax()
    qq.SetSource("id,full_name,root_shop_id")
    var data = make([]*accountProto.Account, 0)
    golog.Info(ret)
    gconv.Structs(ret.Data, &data)
    var rets = t.Ret2AccountAllResult(ret)
    rets.Data = data

    return rets
}
func (t ThirdStub) Ret2ContactShopMemberQueryResult(ret *pagemodel.PageResult[*thirddto.ContactShopMemberStub]) *connectProto.ContactShopMemberQueryResult {
    var result = connectProto.ContactShopMemberQueryResult{
       Pagination: &connectProto.Pagination{},
    }
    result.Pagination.PageSize = gconv.Uint32(ret.PageSize)
    result.Pagination.Current = uint32(ret.PageCurrent)
    result.Pagination.Total = uint32(ret.Total)

    return &result
}
func (t ThirdStub) ContactShopMemberQueryByIds(ids []int64) (*connectProto.ContactShopMemberQueryResult, error) {
    var q = elastic.NewTermsQuery("id", gconv.SliceAny(ids)...)
    var qq = webfacade.DefaultOf[*thirddto.ContactShopMemberStub](q)
    var ret = qq.GeneralQueryMax()
    golog.Info("[ContactShopMemberQueryByIds] =", ret)
    var rets = t.Ret2ContactShopMemberQueryResult(ret)
    var data = make([]*connectProto.ContactShopMember, 0)

    err := gconv.Structs(ret.Data, &data)
    if err != nil {
       golog.Info("ContactShopMemberQueryByIds] ", err)
       return nil, err
    }
    rets.List = data
    return rets, nil
}
func (t ThirdStub) ContactShopMemberQuery(ctx context.Context, in *connectProto.ContactShopMemberQueryRequest, opts ...client.CallOption) (*connectProto.ContactShopMemberQueryResult, error) {
    // ContactShopMemberQuery(ctx context.Context, in *ContactShopMemberQueryRequest, opts ...client.CallOption) (*ContactShopMemberQueryResult, error)
    var q = elastic.NewTermsQuery("id", gconv.SliceAny(in.Ids)...)
    var qq = webfacade.DefaultOf[*thirddto.ContactShopMemberStub](q)
    var ret = qq.GeneralQueryMax()
    golog.Info("[ContactShopMemberQueryByIds] =", ret)
    var rets = t.Ret2ContactShopMemberQueryResult(ret)
    var data = make([]*connectProto.ContactShopMember, 0)

    err := gconv.Structs(ret.Data, &data)
    if err != nil {
       golog.Info("ContactShopMemberQueryByIds] ", err)
       return nil, err
    }
    rets.List = data
    return rets, nil

}
func (t ThirdStub) CooDeptIdsByMmberId(memberIds []int64) (*cooProto.CooMemberJobShopMemberRelRespList, error) {
    var q = elastic.NewBoolQuery()
    q.Must(elastic.NewTermQuery("active", true))
    q.Must(elastic.NewTermsQuery("shop_member_id", gconv.SliceAny(memberIds)...))
    var qq = webfacade.DefaultOf[*thirddto.CooMemberJobRel](q)
    var ret = qq.GeneralQueryMax()

    var c = cooProto.CooMemberJobShopMemberRelRespList{
       //Data:[]cooProto.CooMemberJobShopMemberRel{},
       PageResult: &cooProto.PaginationResult{},
    }
    c.PageResult.PageSize = int64(ret.PageSize)
    c.PageResult.Current = int64(ret.PageCurrent)
    c.PageResult.Total = int64(ret.Total)

    gconv.Structs(ret.Data, &c.Data)
    golog.Info(ret, esobject.AdapterFrom(c))
    return &c, ret.Result2Error() //third.CooDeptIdsByMmberId(memberIds)
}

func (t ThirdStub) ContactShopListQuery(ids []int64) (*connectProto.ContactShopQueryResult, error) {

    var q = elastic.NewBoolQuery()
    q.Must(elastic.NewTermsQuery("id", gconv.SliceAny(ids)...))
    var qq = webfacade.DefaultOf[*thirddto.ContactShopEs](q)
    var ret = qq.GeneralQueryMax()
    golog.Info(ret)
    var rets = &connectProto.ContactShopQueryResult{
       Pagination: &connectProto.Pagination{},
    }
    rets.Pagination.PageSize = uint32(ret.PageSize)
    rets.Pagination.Current = uint32(ret.PageCurrent)
    rets.Pagination.Total = uint32(ret.Total)

    gconv.Structs(ret.Data, &rets.List)
    golog.Info(ret, esobject.AdapterFrom(rets))
    return rets, ret.Result2Error()
}

测试套全文

package thirdtest

import (
    "git.ichub.com/general/webcli120/goconfig/ichubconfig"
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient/webfacade"
    "github.com/gogf/gf/v2/util/gconv"
    "github.com/olivere/elastic/v7"
    "github.com/sirupsen/logrus"
    "github.com/stretchr/testify/suite"
    "icd/general-srv/third"
    "icd/general-srv/third/thirdstub"
    cooProto "icd/proto/coo"

    "icd/general-srv/third/thirdstub/thirddto"
    "icd/pkg/clifacade/esclient/esobject"
    "icd/pkg/clifacade/test/testenv"

    "testing"
)

type TestTestStubSuite struct {
    suite.Suite

    inst    *thirdstub.ThirdStub
    rootdir string

    dbtype string
    cfg    *ichubconfig.IchubConfig
}

func TestTestStubSuites(t *testing.T) {
    suite.Run(t, new(TestTestStubSuite))
}

func (suite *TestTestStubSuite) SetupSuite() {
    logrus.Info(" setup suite")
    testenv.FindBeanReleaseEnv().Init()
    suite.inst = thirdstub.FindBeanTestStub()

    third.InitThirdProxyStub()
}

func (suite *TestTestStubSuite) TearDownSuite() {
    logrus.Info(" teardown suite")

}
func (suite *TestTestStubSuite) SetupTest() {
    logrus.Info(" setup test")
}

// 764236805152309249
func (suite *TestTestStubSuite) TearDownTest() {
    logrus.Info(" teardown test")
}

func (suite *TestTestStubSuite) Test001_GetAccountByShopIds() {
    logrus.Info("  Test001_GetAccountByShopIds test")
    var ret = suite.inst.GetAccountByShopIds([]int64{673093338758840321})
    golog.Info(esobject.AdapterFrom(ret))

}
func (suite *TestTestStubSuite) Test002_ContactShopMemberQueryByIds() {
    logrus.Info(" Test002_ContactShopMemberQueryByIds  test")
    var ret, err = suite.inst.ContactShopMemberQueryByIds([]int64{764236805152309249})
    golog.Info("Test002_ContactShopMemberQueryByIds=", esobject.AdapterFrom(ret), err)

}

func (suite *TestTestStubSuite) Test003_ContactShopMemberQueryByIds() {
    logrus.Info(" Test002_ContactShopMemberQueryByIds  test")
    var ret, err = suite.inst.ContactShopMemberQueryByIds([]int64{764236805152309249})
    golog.Info("Test002_ContactShopMemberQueryByIds=", esobject.AdapterFrom(ret), err)

}

//CooMemberJob CooMemberJobRel

func (suite *TestTestStubSuite) Test005_CooMemberJobRel() {
    logrus.Info("    test")
    var q = elastic.NewMatchAllQuery()
    var qq = webfacade.DefaultOf[*thirddto.CooMemberJobRel](q)
    var ret = qq.GeneralQuery()
    var c = cooProto.CooMemberJobShopMemberRelRespList{
       //Data:[]cooProto.CooMemberJobShopMemberRel{},
    }
    gconv.Structs(ret.Data, &c.Data)
    golog.Info(ret, esobject.AdapterFrom(c))
    //895568159726403585
}
func (suite *TestTestStubSuite) Test006_ContactShopMemberQueryByIds() {
    logrus.Info("    test")
    var q = elastic.NewMatchAllQuery()
    var qq = webfacade.DefaultOf[*thirddto.CooMemberJob](q)
    var ret = qq.GeneralQuery()
    var models = []*cooProto.CooMemberJobShopMemberRel{}

    golog.Info(ret)
    gconv.Structs(ret.Data, &models)
    for i := 0; i < len(models); i++ {
       models[i] = ret.Data[i].ToCooMemberJobShopMemberRel()
    }
    golog.Info(esobject.AdapterFrom(models))

}
func (suite *TestTestStubSuite) Test007_CooMemberJobRel() {
    var ret, err = thirdstub.FindBeanTestStub().CooDeptIdsByMmberId([]int64{895571843400531969})
    //
    golog.Info(err, esobject.AdapterFrom(ret)) //ContactShopEs
}
func (suite *TestTestStubSuite) Test008_ContactShopEs() {

    var q = elastic.NewMatchAllQuery()
    var qq = webfacade.DefaultOf[*thirddto.ContactShopEs](q)
    var ret = qq.GeneralQuery()
    golog.Info(ret)

}
func (suite *TestTestStubSuite) Test009_ContactShopEs() {

    var ret, err = third.FindBeanThirdProxy().ContactShopListQuery([]int64{974835942347243523})
    golog.Info(esobject.AdapterFrom(ret), err)

}
func (suite *TestTestStubSuite) Test009_CooSbuEs() {

    var q = elastic.NewMatchAllQuery()
    var qq = webfacade.DefaultOf[*thirddto.CooSbuEs](q)
    var ret = qq.GeneralQueryMax()
    golog.Info(ret)

}

标签:Info,connectProto,ret,var,Go,func,GO,suite,十九
From: https://blog.csdn.net/leijmdas/article/details/144007872

相关文章

  • Go 语言冲锋舟:在高并发与分布式浪潮中破浪前行
    在软件开发的浩瀚海洋中,技术的浪潮不断涌现,而Go语言(或称Golang)无疑是一艘在高并发与分布式系统的浪潮中迅速破浪前行的“冲锋舟”。作为一门由Google开发的编程语言,Go语言自诞生以来,凭借其简洁高效的特性,迅速在处理并发、高性能计算、云原生和分布式系统等领域取得了无......
  • Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default-c
    idea下的maven项目执行test目录下的main方法报错Failedtoexecutegoalorg.codehaus.mojo:exec-maven-plugin:1.6.0:exec(default-cli)同时报错描述中还有错误找不到主类的提示。但是查看target/test-classes目录下的字节码文件确实有生成也能正常运行。解决办法这个的......
  • 旅游景点推荐系统Python毕设源码论文Django,VUE
        博主介绍:......
  • (附源码)django电子商务平台中推荐系统-计算机毕设 31902
    django电子商务平台中推荐系统目 录摘要1绪论1.1研究背景1.2 研究意义1.3论文结构与章节安排2系统分析2.1可行性分析2.2系统流程分析2.2.1注册流程2.2.1登录流程2.2.3 数据删除流程2.3 系统功能分析2.4系统用例分析2.5本章小结3......
  • Go:context.Context
    什么是context?context是Go标准库中用来管理任务生命周期和跨API数据传递的工具。它的主要应用场景是在并发编程中,尤其是处理像HTTP请求这样有超时限制或需要手动取消的任务。为了更通俗地理解,可以把context想象成一个任务的「管理员」,它可以:通知任务何时结束(比如超......
  • Google账号注册教程
    开篇:去搜索了下注册教程,发现找不到我原本学习的博客了,没找到我注册的版本博客了,决定手写一个,嘿嘿,成功了别忘了回来留下你的评论前提:能使用Google搜索引擎(魔法),不能使用那就收藏吧,以后肯定会用到的首先进入创建Google账号的界面,点击创建账号,选择个人用途姓氏可......
  • django的model时间怎么转时间戳
    在Django中,模型(Model)中的日期和时间字段通常使用Django的DateTimeField或相关字段来存储。如果你想要将这些日期时间字段转换为Unix时间戳(即自1970年1月1日以来的秒数),你可以使用Python的datetime模块中的timestamp()方法。以下是一个例子,展示了如何将Django模型中的DateTim......
  • 2024年11月17日 星期天 Go语言基础
    今日格言坚持每天进步一点点~一个人也可以是一个团队~学习全栈开发,做自己喜欢的产品~~Go语言的创始人Go语言的创始人有三位,分别是:RobertGriesemer:他参与开发了JavaHotSpot虚拟机。RobPike:他是Go语言项目的总负责人,曾是贝尔实验室Unix团队的成员,参与过Plan9、Inf......
  • Google Play养号:2024最新攻略
    GooglePlay的监测越来越严格,如果账号状态不稳定或权重过低,不仅不利于应用审核和发布,还可能导致应用曝光受限,甚至应用在GooglePlay上被移除、账号无法发布新应用或重新发布已有应用。因此,Google Play养号是十分重要的,从多方面提升账号权重,可以参考以下内容。一、基本操作......
  • 40分钟学 Go 语言高并发:Context包与并发控制
    Context包与并发控制学习目标知识点掌握程度应用场景context原理深入理解实现机制并发控制和请求链路追踪超时控制掌握超时设置和处理API请求超时、任务限时控制取消信号传播理解取消机制和传播链优雅退出、资源释放context最佳实践掌握使用规范和技巧工程实践中的常见场......