feat: 新增index/mapping/document/search接口
This commit is contained in:
368
operations/index/document_test.go
Normal file
368
operations/index/document_test.go
Normal file
@@ -0,0 +1,368 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestDocumentIntegration tests complete document lifecycle.
|
||||
func TestDocumentIntegration(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
c := setupIntegrationTest(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
indexName := "test-doc-integration"
|
||||
|
||||
// Setup index
|
||||
err := CreateIndex(ctx, c, indexName, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
var docID string
|
||||
|
||||
// Test 1: Index document
|
||||
t.Run("IndexDocument", func(t *testing.T) {
|
||||
doc := &Document{
|
||||
Source: map[string]any{
|
||||
"title": "Test Document",
|
||||
"content": "This is a test",
|
||||
"count": 42,
|
||||
},
|
||||
}
|
||||
|
||||
id, err := IndexDocument(ctx, c, indexName, doc)
|
||||
if err != nil {
|
||||
t.Fatalf("IndexDocument() error = %v", err)
|
||||
}
|
||||
if id == "" {
|
||||
t.Error("IndexDocument() returned empty ID")
|
||||
}
|
||||
docID = id
|
||||
t.Logf("Document indexed with ID: %s", docID)
|
||||
})
|
||||
|
||||
// Test 2: Get document
|
||||
t.Run("GetDocument", func(t *testing.T) {
|
||||
doc, err := GetDocument(ctx, c, indexName, docID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDocument() error = %v", err)
|
||||
}
|
||||
|
||||
if doc.ID != docID {
|
||||
t.Errorf("GetDocument() ID = %q, want %q", doc.ID, docID)
|
||||
}
|
||||
|
||||
if doc.Source["title"] != "Test Document" {
|
||||
t.Errorf("GetDocument() title = %v, want %v", doc.Source["title"], "Test Document")
|
||||
}
|
||||
|
||||
t.Logf("Retrieved document: %s", doc.ID)
|
||||
})
|
||||
|
||||
// Test 3: Update document
|
||||
t.Run("UpdateDocument", func(t *testing.T) {
|
||||
updates := map[string]any{
|
||||
"title": "Updated Document",
|
||||
"count": 100,
|
||||
}
|
||||
|
||||
err := UpdateDocument(ctx, c, indexName, docID, updates)
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateDocument() error = %v", err)
|
||||
}
|
||||
|
||||
// Verify update
|
||||
doc, err := GetDocument(ctx, c, indexName, docID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDocument() error = %v", err)
|
||||
}
|
||||
|
||||
if doc.Source["title"] != "Updated Document" {
|
||||
t.Errorf("Updated title = %v, want %v", doc.Source["title"], "Updated Document")
|
||||
}
|
||||
|
||||
t.Log("Document updated successfully")
|
||||
})
|
||||
|
||||
// Test 4: Delete document
|
||||
t.Run("DeleteDocument", func(t *testing.T) {
|
||||
err := DeleteDocument(ctx, c, indexName, docID)
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteDocument() error = %v", err)
|
||||
}
|
||||
|
||||
// Verify deletion
|
||||
_, err = GetDocument(ctx, c, indexName, docID)
|
||||
if err != ErrDocumentNotFound {
|
||||
t.Errorf("GetDocument() error = %v, want %v", err, ErrDocumentNotFound)
|
||||
}
|
||||
|
||||
t.Log("Document deleted successfully")
|
||||
})
|
||||
}
|
||||
|
||||
func TestBulkIndexDocuments(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
c := setupIntegrationTest(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
indexName := "test-bulk-index"
|
||||
|
||||
// Setup index
|
||||
err := CreateIndex(ctx, c, indexName, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
// Create multiple documents
|
||||
docs := []*Document{
|
||||
{
|
||||
ID: "doc1",
|
||||
Source: map[string]any{
|
||||
"title": "Document 1",
|
||||
"value": 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "doc2",
|
||||
Source: map[string]any{
|
||||
"title": "Document 2",
|
||||
"value": 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "doc3",
|
||||
Source: map[string]any{
|
||||
"title": "Document 3",
|
||||
"value": 3,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = BulkIndexDocuments(ctx, c, indexName, docs)
|
||||
if err != nil {
|
||||
t.Fatalf("BulkIndexDocuments() error = %v", err)
|
||||
}
|
||||
|
||||
// Verify all documents were indexed
|
||||
for _, doc := range docs {
|
||||
retrieved, err := GetDocument(ctx, c, indexName, doc.ID)
|
||||
if err != nil {
|
||||
t.Errorf("GetDocument(%s) error = %v", doc.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if retrieved.Source["title"] != doc.Source["title"] {
|
||||
t.Errorf("Document %s title = %v, want %v", doc.ID, retrieved.Source["title"], doc.Source["title"])
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("BulkIndexDocuments succeeded: indexed %d documents", len(docs))
|
||||
}
|
||||
|
||||
func TestIndexDocument(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
c := setupIntegrationTest(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
indexName := "test-index-document"
|
||||
|
||||
// Cleanup any existing index first
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
|
||||
// Setup
|
||||
err := CreateIndex(ctx, c, indexName, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
doc := &Document{
|
||||
Source: map[string]any{
|
||||
"field1": "value1",
|
||||
"field2": 123,
|
||||
},
|
||||
}
|
||||
|
||||
id, err := IndexDocument(ctx, c, indexName, doc)
|
||||
if err != nil {
|
||||
t.Fatalf("IndexDocument() error = %v", err)
|
||||
}
|
||||
|
||||
if id == "" {
|
||||
t.Error("IndexDocument() returned empty ID")
|
||||
}
|
||||
|
||||
t.Logf("IndexDocument succeeded: %s", id)
|
||||
}
|
||||
|
||||
func TestGetDocument(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
c := setupIntegrationTest(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
indexName := "test-get-document"
|
||||
|
||||
// Cleanup any existing index first
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
|
||||
// Setup
|
||||
err := CreateIndex(ctx, c, indexName, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
// Index a document
|
||||
doc := &Document{
|
||||
ID: "test-doc",
|
||||
Source: map[string]any{
|
||||
"test": "data",
|
||||
},
|
||||
}
|
||||
|
||||
_, err = IndexDocument(ctx, c, indexName, doc)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: IndexDocument() error = %v", err)
|
||||
}
|
||||
|
||||
// Test getting the document
|
||||
retrieved, err := GetDocument(ctx, c, indexName, "test-doc")
|
||||
if err != nil {
|
||||
t.Fatalf("GetDocument() error = %v", err)
|
||||
}
|
||||
|
||||
if retrieved.ID != "test-doc" {
|
||||
t.Errorf("GetDocument() ID = %q, want %q", retrieved.ID, "test-doc")
|
||||
}
|
||||
|
||||
t.Logf("GetDocument succeeded: %s", retrieved.ID)
|
||||
}
|
||||
|
||||
func TestUpdateDocument(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
c := setupIntegrationTest(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
indexName := "test-update-document"
|
||||
|
||||
// Cleanup any existing index first
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
|
||||
// Setup
|
||||
err := CreateIndex(ctx, c, indexName, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
// Index a document
|
||||
doc := &Document{
|
||||
ID: "test-doc",
|
||||
Source: map[string]any{
|
||||
"value": 1,
|
||||
},
|
||||
}
|
||||
|
||||
_, err = IndexDocument(ctx, c, indexName, doc)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: IndexDocument() error = %v", err)
|
||||
}
|
||||
|
||||
// Test update
|
||||
updates := map[string]any{
|
||||
"value": 2,
|
||||
}
|
||||
|
||||
err = UpdateDocument(ctx, c, indexName, "test-doc", updates)
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateDocument() error = %v", err)
|
||||
}
|
||||
|
||||
t.Log("UpdateDocument succeeded")
|
||||
}
|
||||
|
||||
func TestDeleteDocument(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
c := setupIntegrationTest(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
indexName := "test-delete-document"
|
||||
|
||||
// Cleanup any existing index first
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
|
||||
// Setup
|
||||
err := CreateIndex(ctx, c, indexName, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
// Index a document
|
||||
doc := &Document{
|
||||
ID: "test-doc",
|
||||
Source: map[string]any{
|
||||
"test": "data",
|
||||
},
|
||||
}
|
||||
|
||||
_, err = IndexDocument(ctx, c, indexName, doc)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: IndexDocument() error = %v", err)
|
||||
}
|
||||
|
||||
// Test deletion
|
||||
err = DeleteDocument(ctx, c, indexName, "test-doc")
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteDocument() error = %v", err)
|
||||
}
|
||||
|
||||
t.Log("DeleteDocument succeeded")
|
||||
}
|
||||
Reference in New Issue
Block a user