75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package cassandra
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/scylladb/gocqlx/v3/table"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestGenerateTableMetadata 測試 GenerateTableMetadata 函式
|
|
func TestGenerateTableMetadata(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
document any
|
|
expected table.Metadata
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "MonkeyEntity with TableName",
|
|
document: &MonkeyEntity{},
|
|
expected: table.Metadata{
|
|
Name: "test.monkey_entity",
|
|
Columns: []string{"id", "name", "update_at", "create_at"},
|
|
PartKey: []string{"id"},
|
|
SortKey: []string{"name"},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "Animal without TableName, type name converted to snake_case",
|
|
document: &Animal{},
|
|
expected: table.Metadata{
|
|
Name: "test.animal",
|
|
Columns: []string{"id", "type"},
|
|
PartKey: []string{"id"},
|
|
SortKey: []string{},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidEntity without partition key",
|
|
document: &InvalidEntity{},
|
|
expected: table.Metadata{},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "CatEntity with TableName",
|
|
document: &CatEntity{},
|
|
expected: table.Metadata{
|
|
Name: "test.cat_entity",
|
|
Columns: []string{"id", "name", "update_at", "create_at"},
|
|
PartKey: []string{"id", "name"},
|
|
SortKey: []string{"create_at"},
|
|
},
|
|
expectError: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
meta, err := GenerateTableMetadata(tc.document, "test")
|
|
if tc.expectError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
// 比較 Metadata 的各個欄位
|
|
assert.Equal(t, tc.expected.Name, meta.Name, "table name mismatch")
|
|
assert.Equal(t, tc.expected.Columns, meta.Columns, "columns mismatch")
|
|
assert.Equal(t, tc.expected.PartKey, meta.PartKey, "partition keys mismatch")
|
|
assert.Equal(t, tc.expected.SortKey, meta.SortKey, "sort keys mismatch")
|
|
}
|
|
})
|
|
}
|
|
}
|