backend/pkg/library/cassandra/sai_test.go

268 lines
5.7 KiB
Go
Raw Normal View History

2025-11-19 05:33:06 +00:00
package cassandra
import (
2025-11-19 09:06:44 +00:00
"fmt"
2025-11-19 05:33:06 +00:00
"testing"
"github.com/stretchr/testify/assert"
)
2025-11-19 09:06:44 +00:00
func TestDefaultSAIIndexOptions(t *testing.T) {
opts := DefaultSAIIndexOptions()
assert.NotNil(t, opts)
assert.Equal(t, SAIIndexTypeStandard, opts.IndexType)
assert.False(t, opts.IsAsync)
assert.True(t, opts.CaseSensitive)
}
func TestCreateSAIIndex_Validation(t *testing.T) {
2025-11-19 05:33:06 +00:00
tests := []struct {
2025-11-19 09:06:44 +00:00
name string
keyspace string
table string
column string
indexName string
opts *SAIIndexOptions
wantErr bool
errMsg string
2025-11-19 05:33:06 +00:00
}{
{
2025-11-19 09:06:44 +00:00
name: "missing keyspace",
keyspace: "",
table: "test_table",
column: "test_column",
indexName: "test_idx",
opts: nil,
wantErr: true,
errMsg: "keyspace is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "missing table",
keyspace: "test_keyspace",
table: "",
column: "test_column",
indexName: "test_idx",
opts: nil,
wantErr: true,
errMsg: "table is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "missing column",
keyspace: "test_keyspace",
table: "test_table",
column: "",
indexName: "test_idx",
opts: nil,
wantErr: true,
errMsg: "column is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "valid parameters with default options",
keyspace: "test_keyspace",
table: "test_table",
column: "test_column",
indexName: "test_idx",
opts: nil,
wantErr: false,
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "valid parameters with custom options",
keyspace: "test_keyspace",
table: "test_table",
column: "test_column",
indexName: "test_idx",
opts: &SAIIndexOptions{
IndexType: SAIIndexTypeFullText,
IsAsync: true,
CaseSensitive: false,
},
wantErr: false,
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "auto-generate index name",
keyspace: "test_keyspace",
table: "test_table",
column: "test_column",
indexName: "",
opts: nil,
wantErr: false,
2025-11-19 05:33:06 +00:00
},
2025-11-19 09:06:44 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 注意:這需要一個有效的 DB 實例和 SAI 支援
// 在實際測試中,需要使用 mock 或 testcontainers
_ = tt
})
}
}
func TestDropSAIIndex_Validation(t *testing.T) {
tests := []struct {
name string
keyspace string
indexName string
wantErr bool
errMsg string
}{
2025-11-19 05:33:06 +00:00
{
2025-11-19 09:06:44 +00:00
name: "missing keyspace",
keyspace: "",
indexName: "test_idx",
wantErr: true,
errMsg: "keyspace is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "missing index name",
keyspace: "test_keyspace",
indexName: "",
wantErr: true,
errMsg: "index name is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "valid parameters",
keyspace: "test_keyspace",
indexName: "test_idx",
wantErr: false,
2025-11-19 05:33:06 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-11-19 09:06:44 +00:00
// 注意:這需要一個有效的 DB 實例
// 在實際測試中,需要使用 mock 或 testcontainers
2025-11-19 05:33:06 +00:00
_ = tt
})
}
}
2025-11-19 09:06:44 +00:00
func TestListSAIIndexes_Validation(t *testing.T) {
2025-11-19 05:33:06 +00:00
tests := []struct {
2025-11-19 09:06:44 +00:00
name string
keyspace string
table string
wantErr bool
errMsg string
2025-11-19 05:33:06 +00:00
}{
{
2025-11-19 09:06:44 +00:00
name: "missing keyspace",
keyspace: "",
table: "test_table",
wantErr: true,
errMsg: "keyspace is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "missing table",
keyspace: "test_keyspace",
table: "",
wantErr: true,
errMsg: "table is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "valid parameters",
keyspace: "test_keyspace",
table: "test_table",
wantErr: false,
2025-11-19 05:33:06 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-11-19 09:06:44 +00:00
// 注意:這需要一個有效的 DB 實例
// 在實際測試中,需要使用 mock 或 testcontainers
2025-11-19 05:33:06 +00:00
_ = tt
})
}
}
2025-11-19 09:06:44 +00:00
func TestCheckSAIIndexExists_Validation(t *testing.T) {
2025-11-19 05:33:06 +00:00
tests := []struct {
2025-11-19 09:06:44 +00:00
name string
keyspace string
indexName string
wantErr bool
errMsg string
2025-11-19 05:33:06 +00:00
}{
{
2025-11-19 09:06:44 +00:00
name: "missing keyspace",
keyspace: "",
indexName: "test_idx",
wantErr: true,
errMsg: "keyspace is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "missing index name",
keyspace: "test_keyspace",
indexName: "",
wantErr: true,
errMsg: "index name is required",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "valid parameters",
keyspace: "test_keyspace",
indexName: "test_idx",
wantErr: false,
2025-11-19 05:33:06 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-11-19 09:06:44 +00:00
// 注意:這需要一個有效的 DB 實例
// 在實際測試中,需要使用 mock 或 testcontainers
2025-11-19 05:33:06 +00:00
_ = tt
})
}
}
2025-11-19 09:06:44 +00:00
func TestSAIIndexType_Constants(t *testing.T) {
2025-11-19 05:33:06 +00:00
tests := []struct {
2025-11-19 09:06:44 +00:00
name string
indexType SAIIndexType
expected string
2025-11-19 05:33:06 +00:00
}{
{
2025-11-19 09:06:44 +00:00
name: "standard index type",
indexType: SAIIndexTypeStandard,
expected: "STANDARD",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "collection index type",
indexType: SAIIndexTypeCollection,
expected: "COLLECTION",
2025-11-19 05:33:06 +00:00
},
{
2025-11-19 09:06:44 +00:00
name: "full text index type",
indexType: SAIIndexTypeFullText,
expected: "FULL_TEXT",
2025-11-19 05:33:06 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-11-19 09:06:44 +00:00
assert.Equal(t, tt.expected, string(tt.indexType))
2025-11-19 05:33:06 +00:00
})
}
}
2025-11-19 09:06:44 +00:00
func TestCreateSAIIndex_NotSupported(t *testing.T) {
t.Run("should return error when SAI not supported", func(t *testing.T) {
// 注意:這需要一個不支援 SAI 的 DB 實例
// 在實際測試中,需要使用 mock 或 testcontainers
2025-11-19 05:33:06 +00:00
})
}
2025-11-19 09:06:44 +00:00
func TestCreateSAIIndex_IndexNameGeneration(t *testing.T) {
t.Run("should generate index name when not provided", func(t *testing.T) {
// 測試自動生成索引名稱的邏輯
// 格式應該是: {table}_{column}_sai_idx
table := "users"
column := "email"
expected := "users_email_sai_idx"
2025-11-19 05:33:06 +00:00
2025-11-19 09:06:44 +00:00
// 這裡只是測試命名邏輯,實際建立需要 DB 實例
generated := fmt.Sprintf("%s_%s_sai_idx", table, column)
assert.Equal(t, expected, generated)
2025-11-19 05:33:06 +00:00
})
}