설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mock_mongodb.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //go:build test
  2. package service
  3. import (
  4. "git.x2erp.com/qdy/go-db/factory/mongodb"
  5. "go.mongodb.org/mongo-driver/bson"
  6. )
  7. // MockMongoDBFactory 模拟MongoDB工厂
  8. type MockMongoDBFactory struct {
  9. InsertOneFunc func(collectionName string, document interface{}) (interface{}, bool)
  10. InsertOneWithResultFunc func(collectionName string, document interface{}) (interface{}, bool)
  11. FindOneFunc func(collectionName string, filter interface{}, result interface{}) error
  12. FindFunc func(collectionName string, filter interface{}, result interface{}) error
  13. UpdateOneFunc func(collectionName string, filter interface{}, update interface{}) (bool, int64)
  14. DeleteOneFunc func(collectionName string, filter interface{}) (bool, int64)
  15. CreateIndexFunc func(collectionName string, keys interface{}) bool
  16. }
  17. // InsertOne 模拟插入
  18. func (m *MockMongoDBFactory) InsertOne(collectionName string, document interface{}) (interface{}, bool) {
  19. if m.InsertOneFunc != nil {
  20. return m.InsertOneFunc(collectionName, document)
  21. }
  22. return nil, true
  23. }
  24. // InsertOneWithResult 模拟插入并返回结果
  25. func (m *MockMongoDBFactory) InsertOneWithResult(collectionName string, document interface{}) (interface{}, bool) {
  26. if m.InsertOneWithResultFunc != nil {
  27. return m.InsertOneWithResultFunc(collectionName, document)
  28. }
  29. return nil, true
  30. }
  31. // FindOne 模拟查询单个
  32. func (m *MockMongoDBFactory) FindOne(collectionName string, filter interface{}, result interface{}) error {
  33. if m.FindOneFunc != nil {
  34. return m.FindOneFunc(collectionName, filter, result)
  35. }
  36. return nil
  37. }
  38. // Find 模拟查询多个
  39. func (m *MockMongoDBFactory) Find(collectionName string, filter interface{}, result interface{}) error {
  40. if m.FindFunc != nil {
  41. return m.FindFunc(collectionName, filter, result)
  42. }
  43. return nil
  44. }
  45. // UpdateOne 模拟更新
  46. func (m *MockMongoDBFactory) UpdateOne(collectionName string, filter interface{}, update interface{}) (bool, int64) {
  47. if m.UpdateOneFunc != nil {
  48. return m.UpdateOneFunc(collectionName, filter, update)
  49. }
  50. return true, 1
  51. }
  52. // DeleteOne 模拟删除
  53. func (m *MockMongoDBFactory) DeleteOne(collectionName string, filter interface{}) (bool, int64) {
  54. if m.DeleteOneFunc != nil {
  55. return m.DeleteOneFunc(collectionName, filter)
  56. }
  57. return true, 1
  58. }
  59. // CreateIndex 模拟创建索引
  60. func (m *MockMongoDBFactory) CreateIndex(collectionName string, keys interface{}) bool {
  61. if m.CreateIndexFunc != nil {
  62. return m.CreateIndexFunc(collectionName, keys)
  63. }
  64. return true
  65. }
  66. // TestConnection 模拟测试连接
  67. func (m *MockMongoDBFactory) TestConnection() bool {
  68. return true
  69. }
  70. // 确保MockMongoDBFactory实现MongoDBFactory接口
  71. var _ mongodb.MongoDBFactory = (*MockMongoDBFactory)(nil)