| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //go:build test
-
- package service
-
- import (
- "git.x2erp.com/qdy/go-db/factory/mongodb"
- "go.mongodb.org/mongo-driver/bson"
- )
-
- // MockMongoDBFactory 模拟MongoDB工厂
- type MockMongoDBFactory struct {
- InsertOneFunc func(collectionName string, document interface{}) (interface{}, bool)
- InsertOneWithResultFunc func(collectionName string, document interface{}) (interface{}, bool)
- FindOneFunc func(collectionName string, filter interface{}, result interface{}) error
- FindFunc func(collectionName string, filter interface{}, result interface{}) error
- UpdateOneFunc func(collectionName string, filter interface{}, update interface{}) (bool, int64)
- DeleteOneFunc func(collectionName string, filter interface{}) (bool, int64)
- CreateIndexFunc func(collectionName string, keys interface{}) bool
- }
-
- // InsertOne 模拟插入
- func (m *MockMongoDBFactory) InsertOne(collectionName string, document interface{}) (interface{}, bool) {
- if m.InsertOneFunc != nil {
- return m.InsertOneFunc(collectionName, document)
- }
- return nil, true
- }
-
- // InsertOneWithResult 模拟插入并返回结果
- func (m *MockMongoDBFactory) InsertOneWithResult(collectionName string, document interface{}) (interface{}, bool) {
- if m.InsertOneWithResultFunc != nil {
- return m.InsertOneWithResultFunc(collectionName, document)
- }
- return nil, true
- }
-
- // FindOne 模拟查询单个
- func (m *MockMongoDBFactory) FindOne(collectionName string, filter interface{}, result interface{}) error {
- if m.FindOneFunc != nil {
- return m.FindOneFunc(collectionName, filter, result)
- }
- return nil
- }
-
- // Find 模拟查询多个
- func (m *MockMongoDBFactory) Find(collectionName string, filter interface{}, result interface{}) error {
- if m.FindFunc != nil {
- return m.FindFunc(collectionName, filter, result)
- }
- return nil
- }
-
- // UpdateOne 模拟更新
- func (m *MockMongoDBFactory) UpdateOne(collectionName string, filter interface{}, update interface{}) (bool, int64) {
- if m.UpdateOneFunc != nil {
- return m.UpdateOneFunc(collectionName, filter, update)
- }
- return true, 1
- }
-
- // DeleteOne 模拟删除
- func (m *MockMongoDBFactory) DeleteOne(collectionName string, filter interface{}) (bool, int64) {
- if m.DeleteOneFunc != nil {
- return m.DeleteOneFunc(collectionName, filter)
- }
- return true, 1
- }
-
- // CreateIndex 模拟创建索引
- func (m *MockMongoDBFactory) CreateIndex(collectionName string, keys interface{}) bool {
- if m.CreateIndexFunc != nil {
- return m.CreateIndexFunc(collectionName, keys)
- }
- return true
- }
-
- // TestConnection 模拟测试连接
- func (m *MockMongoDBFactory) TestConnection() bool {
- return true
- }
-
- // 确保MockMongoDBFactory实现MongoDBFactory接口
- var _ mongodb.MongoDBFactory = (*MockMongoDBFactory)(nil)
|