feat(writer): enhance type conversion for PostgreSQL compatibility and add tests
Some checks failed
CI / Test (1.24) (push) Successful in -26m32s
CI / Test (1.25) (push) Successful in -26m27s
CI / Build (push) Successful in -26m48s
CI / Lint (push) Successful in -26m33s
Integration Tests / Integration Tests (push) Failing after -26m51s
Release / Build and Release (push) Successful in -26m41s
Some checks failed
CI / Test (1.24) (push) Successful in -26m32s
CI / Test (1.25) (push) Successful in -26m27s
CI / Build (push) Successful in -26m48s
CI / Lint (push) Successful in -26m33s
Integration Tests / Integration Tests (push) Failing after -26m51s
Release / Build and Release (push) Successful in -26m41s
This commit is contained in:
@@ -241,3 +241,67 @@ func TestIsIntegerType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypeConversion(t *testing.T) {
|
||||
// Test that invalid Go types are converted to valid PostgreSQL types
|
||||
db := models.InitDatabase("testdb")
|
||||
schema := models.InitSchema("public")
|
||||
|
||||
// Create a test table with Go types instead of SQL types
|
||||
table := models.InitTable("test_types", "public")
|
||||
|
||||
// Add columns with Go types (invalid for PostgreSQL)
|
||||
stringCol := models.InitColumn("name", "test_types", "public")
|
||||
stringCol.Type = "string" // Should be converted to "text"
|
||||
table.Columns["name"] = stringCol
|
||||
|
||||
int64Col := models.InitColumn("big_id", "test_types", "public")
|
||||
int64Col.Type = "int64" // Should be converted to "bigint"
|
||||
table.Columns["big_id"] = int64Col
|
||||
|
||||
int16Col := models.InitColumn("small_id", "test_types", "public")
|
||||
int16Col.Type = "int16" // Should be converted to "smallint"
|
||||
table.Columns["small_id"] = int16Col
|
||||
|
||||
schema.Tables = append(schema.Tables, table)
|
||||
db.Schemas = append(db.Schemas, schema)
|
||||
|
||||
// Create writer with output to buffer
|
||||
var buf bytes.Buffer
|
||||
options := &writers.WriterOptions{}
|
||||
writer := NewWriter(options)
|
||||
writer.writer = &buf
|
||||
|
||||
// Write the database
|
||||
err := writer.WriteDatabase(db)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteDatabase failed: %v", err)
|
||||
}
|
||||
|
||||
output := buf.String()
|
||||
|
||||
// Print output for debugging
|
||||
t.Logf("Generated SQL:\n%s", output)
|
||||
|
||||
// Verify that Go types were converted to PostgreSQL types
|
||||
if strings.Contains(output, "string") {
|
||||
t.Errorf("Output contains 'string' type - should be converted to 'text'\nFull output:\n%s", output)
|
||||
}
|
||||
if strings.Contains(output, "int64") {
|
||||
t.Errorf("Output contains 'int64' type - should be converted to 'bigint'\nFull output:\n%s", output)
|
||||
}
|
||||
if strings.Contains(output, "int16") {
|
||||
t.Errorf("Output contains 'int16' type - should be converted to 'smallint'\nFull output:\n%s", output)
|
||||
}
|
||||
|
||||
// Verify correct PostgreSQL types are present
|
||||
if !strings.Contains(output, "text") {
|
||||
t.Errorf("Output missing 'text' type (converted from 'string')\nFull output:\n%s", output)
|
||||
}
|
||||
if !strings.Contains(output, "bigint") {
|
||||
t.Errorf("Output missing 'bigint' type (converted from 'int64')\nFull output:\n%s", output)
|
||||
}
|
||||
if !strings.Contains(output, "smallint") {
|
||||
t.Errorf("Output missing 'smallint' type (converted from 'int16')\nFull output:\n%s", output)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user