您的当前位置:首页正文

Go实战全家桶之十八:CliFacade通用聚合接口与测试框架

2024-12-01 来源:个人技术集锦


package clientfacade

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig2/configagent"
    "icd/pkg/clientfacade/dbclient/dbfacade"
    "icd/pkg/clientfacade/esclient/basefacade"
)

type CliFacade struct {
    basedto.BaseEntitySingle

    IchubConfig *ichubconfig.IchubConfig
    gocfgagent.GocfgAdapter
    basefacade.BaseFacade
    dbfacade.DbFacade

    RuleWebclient  *rulewebclient.RuleWebclient
    WebClient      *webclient.WebClient
    WebClientAdmin *webclient.WebClient
    WebClientWeb   *webclient.WebClient
}

func NewCliFacade() *CliFacade {
    return &CliFacade{
       IchubConfig:    ichubconfig.FindBeanIchubConfig(),
       WebClient:      webclient.FindBeanWebClient(),
       WebClientAdmin: webclient.FindWebClient("admin"),
       WebClientWeb:   webclient.FindWebClient("web"),
       RuleWebclient:  rulewebclient.FindBeanRuleWebclient(),
    }
}
package testiface

type TestIface interface {
    InitEnv()
    InitConfig()
}
package testinit

import (
    "github.com/micro/go-plugins/config/source/grpc/v2"
    "icd/basic"
    "icd/basic/common"
    "icd/basic/config"
    "os"
)

type InitBase struct {
        Token string
}

func (self InitBase) InitConfig() {
    configAddress := os.Getenv("CONFIGGRPCADDRESS")
    if configAddress == "" {
       configAddress = common.ConfigGrpcAddress
    }

    source := grpc.NewSource(
       grpc.WithAddress(configAddress),
       grpc.WithPath("micro"),
    )

    basic.Init(config.WithSource(source))

    return
}
package testinit

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "icd/basic/common"
)

type InitDev struct {
    InitBase
    basedto.BaseEntitySingle
}

func NewInitDev() *InitDev {
    return &InitDev{}
}

func (self *InitDev) InitEnv() {
    if common.Env == "" {
       common.Env = "dev"
    }
    self.InitConfig()
}
package testinit

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "icd/basic/common"
)

type InitTest struct {
    InitBase
    basedto.BaseEntitySingle
}

func NewInitTest() *InitTest {
    return &InitTest{}
}

func (self *InitTest) InitEnv() {
    if common.Env == "" {
       common.Env = "test"
    }
    self.InitConfig()
}
package testinit

import (
    "git.ichub.com/general/webcli120/goconfig/base/baseiface"
    "git.ichub.com/general/webcli120/goconfig/basedi"
    //"git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "github.com/sirupsen/logrus"
    
)

/*
    @Title   文件名称: init_test_init.go
    @Desp    描述:    自动注册注入
    @Company 公司:    www.ichub.com

    @Author  作者:    leijmdas@163.com  时间: 2024-11-16 04:24:23
    @Update  作者:    leijmdas@163.com  时间: 2024-11-16 04:24:23
*/

var singleNameInitTest = "*testinit.InitTest-dd14e51b-ac45-4c02-aa02-280971399f64"

// init register load
func init() {
    registerBeanInitTest()
}

// register InitTest
func registerBeanInitTest() {
    err := basedi.RegisterLoadBean(singleNameInitTest, LoadInitTest)
    if err != nil{
       logrus.Error("register bean error!", err)
    }
}

// FindBeanInitTest
func FindBeanInitTest() *InitTest {

    if bean, ok :=  basedi.FindBeanOk(singleNameInitTest); ok {
        return bean.(*InitTest)
    }

    logrus.Error("not find bean!")
    return nil
}

func LoadInitTest() baseiface.ISingleton {
    var inst = NewInitTest()
    InjectInitTest(inst)
    return inst

}

func InjectInitTest(s *InitTest)    {
    

}


package test

import (
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "github.com/olivere/elastic/v7"
    "github.com/sirupsen/logrus"
    "github.com/stretchr/testify/suite"
    "icd/pkg/dbclient/dbfacade"
    "icd/pkg/esclient/basefacade"
    "icd/pkg/test/testinit"
    "testing"
)

type TestDbFacadeSuite struct {
    suite.Suite
    dbfacade.DbFacade
    basefacade.BaseFacade //FindBeanEsFacade()
}

func TestDbFacadeSuites(t *testing.T) {
    suite.Run(t, new(TestDbFacadeSuite))
}

func (self *TestDbFacadeSuite) SetupSuite() {
    logrus.Info(" setup self")
    testinit.FindBeanInitTest().InitEnv()

}

func (self *TestDbFacadeSuite) TearDownSuite() {
    logrus.Info(" teardown self")

}
func (self *TestDbFacadeSuite) SetupTest() {
    logrus.Info(" setup test")
}

func (self *TestDbFacadeSuite) TearDownTest() {
    logrus.Info(" teardown test")
}

func (self *TestDbFacadeSuite) Test001_DbFacadeServiceApiList() {
    var db = self.DbFacadeServiceApiList()
    db.SetPageSize(2)
    var pageret = db.Query()
    golog.Info(pageret)
}
func (self *TestDbFacadeSuite) Test002_DbFacadeServiceApiList() {
    var db = self.DbFacadeServiceItem()
    db.SetPageSize(2)
    var pageret = db.Query()
    golog.Info(pageret)
}
func (self *TestDbFacadeSuite) Test003_esFacadeServiceApiList() {
    var qq = self.EsFacadeApplicationEsOf(elastic.NewMatchAllQuery())
    qq.SetPageSize(2)
    var pageret = qq.GeneralQuery()
    golog.Info(pageret)
}

package dbfacade

import (
    "git.ichub.com/general/webcli120/code/postgres/db/model"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goweb/generaldb/generaldao"
    "icd/general-srv/entity"
)

type DbFacade struct {
    basedto.BaseEntitySingle
}

func NewDbFacade() *DbFacade {
    return &DbFacade{}
}

func (self *DbFacade) DbFacadeServiceApiList() *generaldao.BaseDao[int64, *model.ServiceApiList] {
    return generaldao.NewBaseDao[int64, *model.ServiceApiList]()

}
func (self *DbFacade) DbFacadeServiceItem() *generaldao.BaseDao[int64, *entity.ServiceItem] {
    return generaldao.NewBaseDao[int64, *entity.ServiceItem]()

}
package basefacade

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goweb/page"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient/esfacade"
    "github.com/olivere/elastic/v7"

    "icd/general-srv/esentity"
    proto "icd/general-srv/proto"
)

type BaseFacade struct {
    basedto.BaseEntitySingle `json:"-""`

    //esdto.BaseObject         `json:"-"`
}

func NewBaseFacade() *BaseFacade {
    return &BaseFacade{}
}

func Ret2Select(ret *page.PageResult) []*proto.Select {
    plist := make([]*proto.Select, 0)

    for _, v := range Ret2Buckets("domain", ret) {
       vv := v.(map[string]any)
       plist = append(plist, &proto.Select{
          Name: vv["key"].(string),
       })
    }
    return plist
}
func Ret2Buckets(aggKey string, ret *page.PageResult) []any {

    var dataAgg = ret.DataAgg.(map[string]any)
    var aggValue = dataAgg[aggKey].(map[string]any)
    return aggValue["buckets"].([]any)

}

func (s *BaseFacade) EsFacadeApplicationEsOf(q elastic.Query) *esfacade.EsFacade[*esentity.ApplicationEs] {
    return esfacade.DefaultOf[*esentity.ApplicationEs](q)
}

func (s *BaseFacade) EsFacadeApplicationEs() *esfacade.EsFacade[*esentity.ApplicationEs] {
    return esfacade.Default[*esentity.ApplicationEs]()
}

显示全文