测试:完善和优化测试用例
- 优化 schema 测试用例,增加边界条件测试 - 完善 table、index、database 等模块的测试 - 改进测试数据清理和错误处理 - 更新示例程序以使用最新 API - 增强测试覆盖率和可靠性
This commit is contained in:
33
schema.go
33
schema.go
@@ -50,11 +50,40 @@ type Schema struct {
|
||||
}
|
||||
|
||||
// NewSchema 创建 Schema
|
||||
func NewSchema(name string, fields []Field) *Schema {
|
||||
// 参数:
|
||||
// - name: Schema 名称,不能为空
|
||||
// - fields: 字段列表,至少需要 1 个字段
|
||||
//
|
||||
// 返回:
|
||||
// - *Schema: Schema 实例
|
||||
// - error: 错误信息
|
||||
func NewSchema(name string, fields []Field) (*Schema, error) {
|
||||
// 验证 name
|
||||
if name == "" {
|
||||
return nil, NewError(ErrCodeSchemaInvalid, fmt.Errorf("schema name cannot be empty"))
|
||||
}
|
||||
|
||||
// 验证 fields 数量
|
||||
if len(fields) == 0 {
|
||||
return nil, NewError(ErrCodeSchemaInvalid, fmt.Errorf("schema must have at least one field"))
|
||||
}
|
||||
|
||||
// 验证字段名不能为空且不能重复
|
||||
fieldNames := make(map[string]bool)
|
||||
for i, field := range fields {
|
||||
if field.Name == "" {
|
||||
return nil, NewError(ErrCodeSchemaInvalid, fmt.Errorf("field at index %d has empty name", i))
|
||||
}
|
||||
if fieldNames[field.Name] {
|
||||
return nil, NewError(ErrCodeSchemaInvalid, fmt.Errorf("duplicate field name: %s", field.Name))
|
||||
}
|
||||
fieldNames[field.Name] = true
|
||||
}
|
||||
|
||||
return &Schema{
|
||||
Name: name,
|
||||
Fields: fields,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// StructToFields 从 Go 结构体生成 Field 列表
|
||||
|
||||
Reference in New Issue
Block a user