goaidb/protocol/mongodb_test.go

139 lines
3.6 KiB
Go
Raw Permalink Normal View History

2025-06-07 08:44:10 +08:00
package protocol
import (
"context"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func TestTC001_Connection_001_Connect(t *testing.T) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
t.Fatal(err)
}
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
t.Fatal(err)
}
}
func TestTC001_UPDATE_004_IncOperator(t *testing.T) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
t.Fatal(err)
}
coll := client.Database("testdb").Collection("testcoll")
// 初始化文档
_, err = coll.InsertOne(context.TODO(), bson.D{{"name", "counter"}, {"value", 0}})
if err != nil {
t.Fatal(err)
}
// 执行$inc更新
update := bson.D{{"$inc", bson.D{{"value", 1}}}}
result := coll.FindOneAndUpdate(context.TODO(), bson.D{{"name", "counter"}}, update)
if result.Err() != nil {
t.Fatal(result.Err())
}
// 验证更新结果
var updated bson.D
if err := result.Decode(&updated); err != nil {
t.Fatal(err)
}
value := updated.Map()["value"]
if value.(int32) != 1 {
t.Errorf("Expected value 1, got %v", value)
}
}
func TestTC001_DELETE_003_DeleteNonExistent(t *testing.T) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
t.Fatal(err)
}
coll := client.Database("testdb").Collection("testcoll")
// 尝试删除不存在的文档
res, err := coll.DeleteMany(context.TODO(), bson.D{{"name", "nonexistent"}})
if err != nil {
t.Fatal(err)
}
// 验证删除计数
if res.DeletedCount != 0 {
t.Errorf("Expected 0 deleted docs, got %d", res.DeletedCount)
}
}
func TestTC001_COMMAND_003_DropCollection(t *testing.T) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
t.Fatal(err)
}
db := client.Database("testdb")
coll := db.Collection("testcoll")
// 创建集合
if err := db.CreateCollection(context.TODO(), "testcoll"); err != nil {
t.Fatal(err)
}
// 删除集合
if err := coll.Drop(context.TODO()); err != nil {
t.Fatal(err)
}
// 验证集合不存在
collections, err := db.ListCollectionNames(context.TODO(), bson.D{{"name", "testcoll"}})
if err != nil {
t.Fatal(err)
}
if len(collections) > 0 {
t.Error("Collection was not dropped")
}
}
func TestTC001_GETMORE_003_InvalidCursor(t *testing.T) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
t.Fatal(err)
}
coll := client.Database("testdb").Collection("testcoll")
// 插入测试数据
for i := 0; i < 100; i++ {
coll.InsertOne(context.TODO(), bson.D{{"i", i}})
}
// 创建游标并获取ID
cur, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetLimit(10))
if err != nil {
t.Fatal(err)
}
cursorID := cur.ID()
cur.Close(context.TODO())
// 使用无效游标ID
invalidCursor := cursorID + 1
opt := options.Database().SetReadPreference(readpref.PrimaryPreferred())
cmd := bson.D{
primitive.E{Key: "getMore", Value: invalidCursor},
primitive.E{Key: "collection", Value: "testcoll"},
}
err = coll.Database().Client().Database("testdb", opt).RunCommand(context.TODO(), cmd).Err()
if err == nil {
t.Error("Expected command error for invalid cursor")
} else {
_, isCmdErr := err.(mongo.CommandError)
if !isCmdErr {
t.Errorf("Expected command error but got %v", err)
}
}
}