test(drawdb): add test for converting column types with modifiers
* Implement tests to ensure explicit type modifiers are preserved during conversion. * Validate behavior for varchar, numeric, and custom vector types.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/pgsql"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/writers/drawdb"
|
||||
)
|
||||
@@ -231,30 +232,35 @@ func (r *Reader) convertToColumn(field *drawdb.DrawDBField, tableName, schemaNam
|
||||
|
||||
// Parse type and dimensions
|
||||
typeStr := field.Type
|
||||
typeStr = strings.TrimSpace(typeStr)
|
||||
column.Type = typeStr
|
||||
|
||||
// Try to extract length/precision from type string like "varchar(255)" or "decimal(10,2)"
|
||||
if strings.Contains(typeStr, "(") {
|
||||
parts := strings.Split(typeStr, "(")
|
||||
column.Type = parts[0]
|
||||
baseType := strings.TrimSpace(parts[0])
|
||||
|
||||
if len(parts) > 1 {
|
||||
dimensions := strings.TrimSuffix(parts[1], ")")
|
||||
if strings.Contains(dimensions, ",") {
|
||||
// Precision and scale (e.g., decimal(10,2))
|
||||
dims := strings.Split(dimensions, ",")
|
||||
if precision, err := strconv.Atoi(strings.TrimSpace(dims[0])); err == nil {
|
||||
column.Precision = precision
|
||||
}
|
||||
if len(dims) > 1 {
|
||||
if scale, err := strconv.Atoi(strings.TrimSpace(dims[1])); err == nil {
|
||||
column.Scale = scale
|
||||
// Precision and scale (e.g., decimal(10,2), numeric(10,2))
|
||||
if pgsql.SupportsPrecision(baseType) {
|
||||
dims := strings.Split(dimensions, ",")
|
||||
if precision, err := strconv.Atoi(strings.TrimSpace(dims[0])); err == nil {
|
||||
column.Precision = precision
|
||||
}
|
||||
if len(dims) > 1 {
|
||||
if scale, err := strconv.Atoi(strings.TrimSpace(dims[1])); err == nil {
|
||||
column.Scale = scale
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Just length (e.g., varchar(255))
|
||||
if length, err := strconv.Atoi(dimensions); err == nil {
|
||||
column.Length = length
|
||||
if pgsql.SupportsLength(baseType) {
|
||||
if length, err := strconv.Atoi(dimensions); err == nil {
|
||||
column.Length = length
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/writers/drawdb"
|
||||
)
|
||||
|
||||
func TestReader_ReadDatabase_Simple(t *testing.T) {
|
||||
@@ -288,6 +289,61 @@ func TestReader_ReadDatabase_Complex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertToColumn_PreservesExplicitTypeModifiers(t *testing.T) {
|
||||
reader := &Reader{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
fieldType string
|
||||
wantType string
|
||||
wantLength int
|
||||
wantPrecision int
|
||||
wantScale int
|
||||
}{
|
||||
{
|
||||
name: "varchar with length",
|
||||
fieldType: "varchar(255)",
|
||||
wantType: "varchar(255)",
|
||||
wantLength: 255,
|
||||
},
|
||||
{
|
||||
name: "numeric precision/scale",
|
||||
fieldType: "numeric(10,2)",
|
||||
wantType: "numeric(10,2)",
|
||||
wantPrecision: 10,
|
||||
wantScale: 2,
|
||||
},
|
||||
{
|
||||
name: "custom vector modifier",
|
||||
fieldType: "vector(1536)",
|
||||
wantType: "vector(1536)",
|
||||
wantLength: 1536,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
field := &drawdb.DrawDBField{
|
||||
Name: tt.name,
|
||||
Type: tt.fieldType,
|
||||
}
|
||||
col := reader.convertToColumn(field, "events", "public")
|
||||
if col.Type != tt.wantType {
|
||||
t.Fatalf("column type = %q, want %q", col.Type, tt.wantType)
|
||||
}
|
||||
if col.Length != tt.wantLength {
|
||||
t.Fatalf("column length = %d, want %d", col.Length, tt.wantLength)
|
||||
}
|
||||
if col.Precision != tt.wantPrecision {
|
||||
t.Fatalf("column precision = %d, want %d", col.Precision, tt.wantPrecision)
|
||||
}
|
||||
if col.Scale != tt.wantScale {
|
||||
t.Fatalf("column scale = %d, want %d", col.Scale, tt.wantScale)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReader_ReadSchema(t *testing.T) {
|
||||
opts := &readers.ReaderOptions{
|
||||
FilePath: filepath.Join("..", "..", "..", "tests", "assets", "drawdb", "simple.json"),
|
||||
|
||||
Reference in New Issue
Block a user