feat: opensearch客户端初始化 feat: 索引模板接口 ai: 开发准则 chore: TDD流水线脚本
This commit is contained in:
180
operations/index/template_test.go
Normal file
180
operations/index/template_test.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"es-demo/client"
|
||||
)
|
||||
|
||||
func TestPutTemplate(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
cfg := &client.Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
}
|
||||
|
||||
c, err := client.NewClient(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
template := &Template{
|
||||
IndexPatterns: []string{"test-*"},
|
||||
Settings: map[string]any{
|
||||
"number_of_shards": 1,
|
||||
"number_of_replicas": 0,
|
||||
},
|
||||
Mappings: map[string]any{
|
||||
"properties": map[string]any{
|
||||
"timestamp": map[string]any{
|
||||
"type": "date",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// This will fail without a real cluster
|
||||
err = PutTemplate(ctx, c, "test-template", template)
|
||||
if err == nil {
|
||||
t.Log("PutTemplate succeeded (unexpected in unit test)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTemplate(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
cfg := &client.Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
}
|
||||
|
||||
c, err := client.NewClient(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// This will fail without a real cluster
|
||||
_, err = GetTemplate(ctx, c, "test-template")
|
||||
if err == nil {
|
||||
t.Log("GetTemplate succeeded (unexpected in unit test)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTemplate(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
cfg := &client.Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
}
|
||||
|
||||
c, err := client.NewClient(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// This will fail without a real cluster
|
||||
err = DeleteTemplate(ctx, c, "test-template")
|
||||
if err == nil {
|
||||
t.Log("DeleteTemplate succeeded (unexpected in unit test)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTemplates(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
cfg := &client.Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
}
|
||||
|
||||
c, err := client.NewClient(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// This will fail without a real cluster
|
||||
_, err = ListTemplates(ctx, c)
|
||||
if err == nil {
|
||||
t.Log("ListTemplates succeeded (unexpected in unit test)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplate_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
template *Template
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "valid template",
|
||||
template: &Template{
|
||||
IndexPatterns: []string{"logs-*"},
|
||||
Settings: map[string]any{
|
||||
"number_of_shards": 1,
|
||||
},
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "nil template",
|
||||
template: nil,
|
||||
wantErr: ErrInvalidTemplate,
|
||||
},
|
||||
{
|
||||
name: "empty index patterns",
|
||||
template: &Template{
|
||||
IndexPatterns: []string{},
|
||||
},
|
||||
wantErr: ErrInvalidTemplate,
|
||||
},
|
||||
{
|
||||
name: "nil index patterns",
|
||||
template: &Template{
|
||||
IndexPatterns: nil,
|
||||
},
|
||||
wantErr: ErrInvalidTemplate,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateTemplate(tt.template)
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Errorf("validateTemplate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user