package index import ( "context" "testing" "time" ) // TestIndexIntegration tests complete index lifecycle. func TestIndexIntegration(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-index-integration" // Cleanup before and after test _ = DeleteIndex(ctx, c, indexName) defer func() { _ = DeleteIndex(ctx, c, indexName) }() // Test 1: Create index t.Run("CreateIndex", func(t *testing.T) { config := &IndexConfig{ Settings: &IndexSettings{ NumberOfShards: 1, NumberOfReplicas: 0, }, Mappings: map[string]any{ "properties": map[string]any{ "title": map[string]any{ "type": "text", }, "count": map[string]any{ "type": "integer", }, }, }, } err := CreateIndex(ctx, c, indexName, config) if err != nil { t.Fatalf("CreateIndex() error = %v", err) } t.Log("Index created successfully") }) // Test 2: Index exists t.Run("IndexExists", func(t *testing.T) { exists, err := IndexExists(ctx, c, indexName) if err != nil { t.Fatalf("IndexExists() error = %v", err) } if !exists { t.Error("IndexExists() = false, want true") } t.Log("Index exists check passed") }) // Test 3: Get index t.Run("GetIndex", func(t *testing.T) { info, err := GetIndex(ctx, c, indexName) if err != nil { t.Fatalf("GetIndex() error = %v", err) } if info.Name != indexName { t.Errorf("GetIndex() name = %q, want %q", info.Name, indexName) } t.Logf("Retrieved index: %s", info.Name) }) // Test 4: List indices t.Run("ListIndices", func(t *testing.T) { indices, err := ListIndices(ctx, c, "test-*") if err != nil { t.Fatalf("ListIndices() error = %v", err) } found := false for _, idx := range indices { if idx == indexName { found = true break } } if !found { t.Errorf("Index %q not found in list", indexName) } t.Logf("Found %d indices", len(indices)) }) // Test 5: Delete index t.Run("DeleteIndex", func(t *testing.T) { err := DeleteIndex(ctx, c, indexName) if err != nil { t.Fatalf("DeleteIndex() error = %v", err) } // Verify deletion exists, err := IndexExists(ctx, c, indexName) if err != nil { t.Fatalf("IndexExists() error = %v", err) } if exists { t.Error("Index still exists after deletion") } t.Log("Index deleted successfully") }) } func TestCreateIndex(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-create-index" defer func() { _ = DeleteIndex(ctx, c, indexName) }() config := &IndexConfig{ Settings: &IndexSettings{ NumberOfShards: 1, NumberOfReplicas: 0, }, } err := CreateIndex(ctx, c, indexName, config) if err != nil { t.Fatalf("CreateIndex() error = %v", err) } t.Log("CreateIndex succeeded") } func TestGetIndex(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-index" // 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) }() // Test getting the index info, err := GetIndex(ctx, c, indexName) if err != nil { t.Fatalf("GetIndex() error = %v", err) } if info.Name != indexName { t.Errorf("GetIndex() name = %q, want %q", info.Name, indexName) } t.Logf("GetIndex succeeded: %s", info.Name) } func TestDeleteIndex(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-index" // Create index err := CreateIndex(ctx, c, indexName, nil) if err != nil { t.Fatalf("setup: CreateIndex() error = %v", err) } // Test deletion err = DeleteIndex(ctx, c, indexName) if err != nil { t.Fatalf("DeleteIndex() error = %v", err) } t.Log("DeleteIndex succeeded") } func TestIndexExists(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-exists-index" // Test non-existent index exists, err := IndexExists(ctx, c, indexName) if err != nil { t.Fatalf("IndexExists() error = %v", err) } if exists { t.Error("IndexExists() = true for non-existent index") } // Create index err = CreateIndex(ctx, c, indexName, nil) if err != nil { t.Fatalf("setup: CreateIndex() error = %v", err) } defer func() { _ = DeleteIndex(ctx, c, indexName) }() // Test existing index exists, err = IndexExists(ctx, c, indexName) if err != nil { t.Fatalf("IndexExists() error = %v", err) } if !exists { t.Error("IndexExists() = false for existing index") } t.Log("IndexExists succeeded") } func TestListIndices(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() indices, err := ListIndices(ctx, c, "*") if err != nil { t.Fatalf("ListIndices() error = %v", err) } if indices == nil { t.Error("ListIndices() returned nil") } t.Logf("ListIndices succeeded: found %d indices", len(indices)) } // Unit tests for validation func TestCreateIndex_Validation(t *testing.T) { ctx := context.Background() tests := []struct { name string indexName string wantErr error }{ { name: "empty index name", indexName: "", wantErr: ErrInvalidIndexName, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Pass nil client to only test validation logic err := CreateIndex(ctx, nil, tt.indexName, nil) if err != tt.wantErr { t.Errorf("CreateIndex() error = %v, want %v", err, tt.wantErr) } }) } }