godocdb/document/document_test.go

119 lines
2.6 KiB
Go
Raw Normal View History

package document
import (
2025-06-07 18:13:29 +08:00
"fmt"
"os"
2025-06-07 18:13:29 +08:00
"sync"
"testing"
)
// 测试用结构体
type TestDoc struct {
ID string `bson:"_id"`
Name string `bson:"name"`
Age int `bson:"age"`
}
func TestDocumentStore(t *testing.T) {
// 测试目录
dir := "./testdb"
defer os.RemoveAll(dir)
// 初始化文档存储
ds, err := NewDocumentStore(dir)
if err != nil {
t.Fatalf("Failed to create document store: %v", err)
}
defer ds.storage.Close()
2025-06-07 22:28:29 +08:00
// 测试文档ID和数据
docID := "doc123"
doc := map[string]interface{}{
"_id": docID,
"name": "Test Document",
}
2025-06-07 22:28:29 +08:00
// 测试基本操作使用默认collection
collection := "test_collection"
if err := ds.StoreDocument(collection, docID, doc); err != nil {
t.Errorf("StoreDocument failed: %v", err)
}
2025-06-07 22:28:29 +08:00
// 验证存储结果
var result map[string]interface{}
if err := ds.GetDocument(collection, docID, &result); err != nil {
t.Errorf("GetDocument failed: %v", err)
}
2025-06-07 22:28:29 +08:00
// 验证删除功能
if err := ds.DeleteDocument(collection, docID); err != nil {
t.Errorf("DeleteDocument failed: %v", err)
}
2025-06-07 22:28:29 +08:00
// 删除后验证
if err := ds.GetDocument(collection, docID, &result); err == nil {
t.Error("Expected error after DeleteDocument")
}
2025-06-07 18:13:29 +08:00
}
func TestErrorHandling(t *testing.T) {
// 测试无效文档(包含不支持的类型)
dir := "./testdb_error"
defer os.RemoveAll(dir)
ds, _ := NewDocumentStore(dir)
2025-06-07 22:28:29 +08:00
// 测试存储非map文档
2025-06-07 18:13:29 +08:00
invalidDoc := struct {
F func()
}{}
2025-06-07 22:28:29 +08:00
if err := ds.StoreDocument("invalid", "doc1", invalidDoc); err == nil {
2025-06-07 18:13:29 +08:00
t.Error("Expected error for invalid document")
}
2025-06-07 22:28:29 +08:00
// 测试存储nil文档
if err := ds.StoreDocument("invalid", "doc2", nil); err == nil {
t.Error("Expected error for nil document")
}
2025-06-07 18:13:29 +08:00
}
func TestConcurrentAccess(t *testing.T) {
// 测试并发写入同一ID
dir := "./testdb_concurrent"
defer os.RemoveAll(dir)
numGoroutines := 10
var wg sync.WaitGroup
key := "concurrent_test"
2025-06-07 22:28:29 +08:00
ds, err := NewDocumentStore(dir)
if err != nil {
t.Fatalf("Failed to create document store: %v", err)
}
defer ds.storage.Close()
2025-06-07 18:13:29 +08:00
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(i int) {
defer wg.Done()
2025-06-07 22:28:29 +08:00
testDoc := map[string]interface{}{
"_id": key,
"name": fmt.Sprintf("Test%d", i),
2025-06-07 18:13:29 +08:00
}
2025-06-07 22:28:29 +08:00
err := ds.StoreDocument(key, fmt.Sprintf("doc%d", i), testDoc)
2025-06-07 18:13:29 +08:00
if err != nil {
t.Errorf("StoreDocument failed: %v", err)
}
}(i)
}
wg.Wait()
// 验证最终值是否为最后一个写入
2025-06-07 22:28:29 +08:00
var result map[string]interface{}
if err := ds.GetDocument(key, "doc9", &result); err != nil {
2025-06-07 18:13:29 +08:00
t.Errorf("GetDocument failed: %v", err)
}
}