feat: 新增index/mapping/document/search接口
This commit is contained in:
204
operations/index/mapping_test.go
Normal file
204
operations/index/mapping_test.go
Normal file
@@ -0,0 +1,204 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestMappingIntegration tests mapping operations.
|
||||
func TestMappingIntegration(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-mapping-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)
|
||||
}()
|
||||
|
||||
// Test 1: Add field
|
||||
t.Run("AddField", func(t *testing.T) {
|
||||
mapping := FieldMapping{
|
||||
Type: "text",
|
||||
}
|
||||
|
||||
err := AddField(ctx, c, indexName, "new_field", mapping)
|
||||
if err != nil {
|
||||
t.Fatalf("AddField() error = %v", err)
|
||||
}
|
||||
|
||||
t.Log("Field added successfully")
|
||||
})
|
||||
|
||||
// Test 2: Get mapping
|
||||
t.Run("GetMapping", func(t *testing.T) {
|
||||
mappings, err := GetMapping(ctx, c, indexName)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMapping() error = %v", err)
|
||||
}
|
||||
|
||||
if mappings == nil {
|
||||
t.Error("GetMapping() returned nil")
|
||||
}
|
||||
|
||||
t.Logf("Retrieved mappings for index %s", indexName)
|
||||
})
|
||||
|
||||
// Test 3: Put mapping with multiple fields
|
||||
t.Run("PutMapping", func(t *testing.T) {
|
||||
properties := map[string]FieldMapping{
|
||||
"title": {
|
||||
Type: "text",
|
||||
},
|
||||
"count": {
|
||||
Type: "integer",
|
||||
},
|
||||
"timestamp": {
|
||||
Type: "date",
|
||||
Format: "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
|
||||
},
|
||||
}
|
||||
|
||||
err := PutMapping(ctx, c, indexName, properties)
|
||||
if err != nil {
|
||||
t.Fatalf("PutMapping() error = %v", err)
|
||||
}
|
||||
|
||||
t.Log("Mapping updated successfully")
|
||||
})
|
||||
}
|
||||
|
||||
func TestPutMapping(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-put-mapping"
|
||||
|
||||
// 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)
|
||||
}()
|
||||
|
||||
properties := map[string]FieldMapping{
|
||||
"name": {
|
||||
Type: "text",
|
||||
},
|
||||
"age": {
|
||||
Type: "integer",
|
||||
},
|
||||
}
|
||||
|
||||
err = PutMapping(ctx, c, indexName, properties)
|
||||
if err != nil {
|
||||
t.Fatalf("PutMapping() error = %v", err)
|
||||
}
|
||||
|
||||
t.Log("PutMapping succeeded")
|
||||
}
|
||||
|
||||
func TestGetMapping(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-mapping"
|
||||
|
||||
// Cleanup any existing index first
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
|
||||
// Setup with initial mapping
|
||||
config := &IndexConfig{
|
||||
Mappings: map[string]any{
|
||||
"properties": map[string]any{
|
||||
"field1": map[string]any{
|
||||
"type": "text",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := CreateIndex(ctx, c, indexName, config)
|
||||
if err != nil {
|
||||
t.Fatalf("setup: CreateIndex() error = %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = DeleteIndex(ctx, c, indexName)
|
||||
}()
|
||||
|
||||
mappings, err := GetMapping(ctx, c, indexName)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMapping() error = %v", err)
|
||||
}
|
||||
|
||||
if mappings == nil {
|
||||
t.Error("GetMapping() returned nil")
|
||||
}
|
||||
|
||||
t.Log("GetMapping succeeded")
|
||||
}
|
||||
|
||||
func TestAddField(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-add-field"
|
||||
|
||||
// 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)
|
||||
}()
|
||||
|
||||
mapping := FieldMapping{
|
||||
Type: "keyword",
|
||||
}
|
||||
|
||||
err = AddField(ctx, c, indexName, "status", mapping)
|
||||
if err != nil {
|
||||
t.Fatalf("AddField() error = %v", err)
|
||||
}
|
||||
|
||||
t.Log("AddField succeeded")
|
||||
}
|
||||
Reference in New Issue
Block a user