回頭到unit test。有一個檔案
package dbaccess

import (
 "strings"

 "github.com/spf13/viper"
 "gitlab.com/cake/goctx"
 "gitlab.com/cake/gopkg"
 "gitlab.com/cake/mgopool/v3"
 "gitlab.com/cake/workflow/wf"
 "go.mongodb.org/mongo-driver/bson"
)

var (
 DatabaseWorkflow = "m800workflow"
 CollectionTaskDef = "taskdef"
 CollectionWorkflow = "workflow"
 CollectionSession = "session"
 CollectionGlobalLocker = "global\_locker"
 namespace string

 tracer = wf.Tracer
)

var (
 localMongoDBInfo \*mgopool.DBInfo
 localPool \*mgopool.Pool
)

func init() {
 namespace = "localhost"
}

func SetLocalMongoDBInfo(localInfo \*mgopool.DBInfo) {
 localMongoDBInfo = localInfo
 DatabaseWorkflow = localMongoDBInfo.Name
}

func SetLocalMongoPool(p \*mgopool.Pool) {
 localPool = p
}

func GetLocalMongoPool() \*mgopool.Pool {
 return localPool
}

func SetNamespace(n string) {
 namespace = n
}

func GetNamespace() string {
 return namespace
}

func GetLocalMongoDBInfo() \*mgopool.DBInfo {
 name := viper.GetString("database.mgo.local.name")
 mgoUser := viper.GetString("database.mgo.local.user")
 mgoPassword := viper.GetString("database.mgo.local.password")
 mgoAuthDatabase := viper.GetString("database.mgo.local.authdatabase")
 mgoMaxConn := viper.GetInt("database.mgo.local.max\_conn")
 mgoTimeout := viper.GetDuration("database.mgo.local.timeout")
 mgoDirect := viper.GetBool("database.mgo.local.direct")
 mgoSecondary := viper.GetBool("database.mgo.local.secondary")
 mgoMonogs := viper.GetBool("database.mgo.local.mongos")
 mgoAddrs := strings.Split(viper.GetString("database.mgo.local.hosts"), ";")

 return mgopool.NewDBInfo(name, mgoAddrs, mgoUser, mgoPassword,
 mgoAuthDatabase, mgoTimeout, mgoMaxConn, mgoDirect, mgoSecondary, mgoMonogs)
}

func deleteData(ctx goctx.Context, collectionName string, selector bson.M) (err gopkg.CodeError) {

 // Use the Name field as the unique identifier for the task definition
 err = localPool.Remove(ctx, DatabaseWorkflow, collectionName, selector)

 if err != nil {
 return err
 }

 return nil
}

func upsertData[V any](ctx goctx.Context, collectionName string, selector bson.M, rawData V) (err gopkg.CodeError) {

 // Use the Name field as the unique identifier for the task definition
 \_, err = localPool.Upsert(ctx, DatabaseWorkflow, collectionName, selector, rawData)

 if err != nil {
 return err
 }

 return nil
}

func getOneData[V any](ctx goctx.Context, collectionName string, selector bson.M, result \*V) (err gopkg.CodeError) {
 return localPool.QueryOne(ctx, DatabaseWorkflow, collectionName, result, selector, nil, 0)
}

func dataCount(ctx goctx.Context, collectionName string, selector bson.M) (count int, err gopkg.CodeError) {
 return localPool.QueryCount(ctx, DatabaseWorkflow, collectionName, selector)
}

func listData[V any](ctx goctx.Context, collectionName string, selector bson.M, offset, limit int, sort ...string) (result []V, total int, err gopkg.CodeError) {
 total, err = dataCount(ctx, collectionName, nil)
 if err != nil {
 return
 }

 result = []V{}
 err = localPool.QueryAll(ctx, DatabaseWorkflow, collectionName, &result, selector, nil, offset, limit, sort...)
 return
}

其中mgo.Pool為
type Pool struct {
 name string
 config \*DBInfo
 mode readpref.Mode
 available bool
 rwLock sync.RWMutex
 client \*mongo.Client
}

mongo.DBInfo為
type DBInfo struct {
 Name string
 User string
 Password string
 AuthDatabase string
 Addrs []string
 MaxConn int
 MaxConnectAttempts int
 Timeout time.Duration
 SocketTimeout time.Duration
 ReadMode readpref.Mode
 // if not set, follow mongoDB default
 // 4.2: 1
 // 5.0: majority
 WriteConcern \*writeconcern.WriteConcern
 Direct bool
 Mongos bool
}
是否可以使用go.mongodb.org/mongo-driver/mongo/integration/mtest產生unit test
단위 테스트로 돌아갑니다. 하나의 파일만 있습니다.패키지 dbaccessimport ( "문자열" "github.com/spf13/viper" "gitlab.com/cake/goctx" "gitlab.com/cake/gopkg" "gitlab.com/cake/mgopool/v3" "gitlab.com/cake/workflow/wf" "go.mongodb.org/mongo-driver/bson")var ( 데이터베이스워크플로우 = "m800워크플로우" CollectionTaskDef = "taskdef" 컬렉션워크플로우 = "워크플로우" CollectionSession = "세션" CollectionGlobalLocker = "global\_locker" 네임스페이스 문자열 추적자 = wf.Tracer)var ( localMongoDBInfo \*mgopool.DBInfo localPool \*mgopool.Pool)함수 init() { 네임스페이스 = "localhost"}func SetLocalMongoDBInfo(localInfo \*mgopool.DBInfo) { localMongoDBInfo = localInfo 데이터베이스 워크플로우 = localMongoDBInfo.Name}func SetLocalMongoPool(p \*mgopool.Pool) { localPool = p}func GetLocalMongoPool() \*mgopool.Pool { return localPool}함수 SetNamespace(n 문자열) { namespace = n}함수 GetNamespace() 문자열 { 반환 이름 공간}func GetLocalMongoDBInfo() \*mgopool.DBInfo { name := viper.GetString("database.mgo.local.name") mgoUser := viper.GetString("database.mgo.local.user") mgoPassword := viper.GetString("database.mgo.local.password") mgoAuthDatabase := viper.GetString("database.mgo.local.authdatabase") mgoMaxConn := viper.GetInt("database.mgo.local.max\_conn") mgoTimeout := viper.GetDuration("database.mgo.local.timeout") mgoDirect := viper.GetBool("database.mgo.local.direct") mgoSecondary := viper.GetBool("database.mgo.local.secondary") mgoMonogs := viper.GetBool("database.mgo.local.mongos") mgoAddrs := strings.Split(viper.GetString("database.mgo.local.hosts"), ";") 반환 mgopool.NewDBInfo(이름, mgoAddrs, mgoUser, mgoPassword, mgoAuthDatabase, mgoTimeout, mgoMaxConn, mgoDirect, mgoSecondary, mgoMonogs)}func deleteData(ctx goctx.Context, collectionName string, selector bson.M) (err gopkg.CodeError) { // 작업 정의의 고유 식별자로 Name 필드를 사용합니다. err = localPool.Remove(ctx, DatabaseWorkflow, collectionName, selector) if err != nil { 반환 err } 반환 nil}func upsertData[V any](ctx goctx.Context, collectionName string, selector bson.M, rawData V) (err gopkg.CodeError) { // Name 필드를 작업 정의의 고유 식별자로 사용합니다. \_, err = localPool.Upsert(ctx, DatabaseWorkflow, collectionName, selector, rawData) if err != nil { 반환 err } 반환 nil}func getOneData[V any](ctx goctx.Context, collectionName string, selector bson.M, result \*V) (err gopkg.CodeError) { return localPool.QueryOne(ctx, DatabaseWorkflow, collectionName, result, selector, nil, 0)}func dataCount(ctx goctx.Context, collectionName string, selector bson.M) (count int, err gopkg.CodeError) { return localPool.QueryCount(ctx, DatabaseWorkflow, collectionName, selector)}func listData[V any](ctx goctx.Context, collectionName string, selector bson.M, offset, limit int, sort ...string) (result []V, total int, err gopkg.CodeError) { total, err = dataCount(ctx, collectionName, nil) if err != nil { return } result = []V{} err = localPool.QueryAll(ctx, DatabaseWorkflow, collectionName, &result, selector, nil, offset, limit, sort...) 반환}그 중 mgo.Pool유형 풀 구조체 { 이름 문자열 구성 \*DBInfo 모드 readpref.Mode 사용 가능한 부울 rwLock sync.RWMutex 클라이언트 \*mongo.Client}mongo.DBInfo유형 DBInfo 구조체 { 이름 문자열 사용자 문자열 비밀번호 문자열 인증 데이터베이스 문자열 주소 []문자열 MaxConn int 최대 연결 시도 횟수 int 시간 초과 시간.기간 소켓 시간 제한 시간.기간 ReadMode readpref.Mode // 설정되지 않은 경우, 몽고DB 기본값을 따름 // 4.2: 1 // 5.0: 다수 WriteConcern \*writeconcern.WriteConcern 직접 부울 몽고 bool}사용 가능 여부는 go.mongodb.org/mongo-driver/mongo/integration/mtest 제품 단위 테스트에서 확인할 수 있습니다.