feat: opensearch客户端初始化 feat: 索引模板接口 ai: 开发准则 chore: TDD流水线脚本
This commit is contained in:
113
client/client_test.go
Normal file
113
client/client_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config *Config
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "valid config",
|
||||
config: &Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "nil config",
|
||||
config: nil,
|
||||
wantErr: ErrInvalidConfig,
|
||||
},
|
||||
{
|
||||
name: "missing endpoint",
|
||||
config: &Config{
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
},
|
||||
wantErr: ErrInvalidConfig,
|
||||
},
|
||||
{
|
||||
name: "missing region",
|
||||
config: &Config{
|
||||
Endpoint: "https://example.com",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
},
|
||||
wantErr: ErrInvalidConfig,
|
||||
},
|
||||
{
|
||||
name: "missing access key",
|
||||
config: &Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
SecretKey: "test-secret",
|
||||
},
|
||||
wantErr: ErrInvalidConfig,
|
||||
},
|
||||
{
|
||||
name: "missing secret key",
|
||||
config: &Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
},
|
||||
wantErr: ErrInvalidConfig,
|
||||
},
|
||||
{
|
||||
name: "with custom timeout",
|
||||
config: &Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
Timeout: 60 * time.Second,
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := NewClient(tt.config)
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if tt.wantErr == nil && got == nil {
|
||||
t.Error("NewClient() returned nil client with no error")
|
||||
}
|
||||
if tt.wantErr != nil && got != nil {
|
||||
t.Error("NewClient() returned client with error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_GetClient(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
}
|
||||
|
||||
client, err := NewClient(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
osClient := client.GetClient()
|
||||
if osClient == nil {
|
||||
t.Error("GetClient() returned nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user