|
| 1 | +package diff |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/meru143/dbdiff/pkg/types" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSQLGenerator_CreateTable(t *testing.T) { |
| 11 | + diffs := types.DiffList{ |
| 12 | + {Type: types.DiffAdd, Object: types.ObjectTable, Name: "users"}, |
| 13 | + } |
| 14 | + gen := NewSQLGenerator(diffs, "public") |
| 15 | + gen.SetTransaction(false) |
| 16 | + sql := gen.Generate() |
| 17 | + |
| 18 | + if !strings.Contains(sql, "CREATE TABLE") { |
| 19 | + t.Errorf("Expected CREATE TABLE, got: %s", sql) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestSQLGenerator_DropTable(t *testing.T) { |
| 24 | + diffs := types.DiffList{ |
| 25 | + {Type: types.DiffDrop, Object: types.ObjectTable, Name: "users"}, |
| 26 | + } |
| 27 | + gen := NewSQLGenerator(diffs, "public") |
| 28 | + gen.SetTransaction(false) |
| 29 | + sql := gen.Generate() |
| 30 | + |
| 31 | + if !strings.Contains(sql, "DROP TABLE") { |
| 32 | + t.Errorf("Expected DROP TABLE, got: %s", sql) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestSQLGenerator_AddColumn(t *testing.T) { |
| 37 | + diffs := types.DiffList{ |
| 38 | + {Type: types.DiffAdd, Object: types.ObjectColumn, Name: "email", TableName: "users", NewValue: "varchar"}, |
| 39 | + } |
| 40 | + gen := NewSQLGenerator(diffs, "public") |
| 41 | + gen.SetTransaction(false) |
| 42 | + sql := gen.Generate() |
| 43 | + |
| 44 | + if !strings.Contains(sql, "ADD COLUMN") { |
| 45 | + t.Errorf("Expected ADD COLUMN, got: %s", sql) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestSQLGenerator_DropColumn(t *testing.T) { |
| 50 | + diffs := types.DiffList{ |
| 51 | + {Type: types.DiffDrop, Object: types.ObjectColumn, Name: "email", TableName: "users"}, |
| 52 | + } |
| 53 | + gen := NewSQLGenerator(diffs, "public") |
| 54 | + gen.SetTransaction(false) |
| 55 | + sql := gen.Generate() |
| 56 | + |
| 57 | + if !strings.Contains(sql, "DROP COLUMN") { |
| 58 | + t.Errorf("Expected DROP COLUMN, got: %s", sql) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestSQLGenerator_AlterColumn(t *testing.T) { |
| 63 | + diffs := types.DiffList{ |
| 64 | + {Type: types.DiffAlter, Object: types.ObjectColumn, Name: "email", TableName: "users", OldValue: "varchar", NewValue: "text"}, |
| 65 | + } |
| 66 | + gen := NewSQLGenerator(diffs, "public") |
| 67 | + gen.SetTransaction(false) |
| 68 | + sql := gen.Generate() |
| 69 | + |
| 70 | + if !strings.Contains(sql, "ALTER COLUMN") { |
| 71 | + t.Errorf("Expected ALTER COLUMN, got: %s", sql) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestSQLGenerator_CreateIndex(t *testing.T) { |
| 76 | + diffs := types.DiffList{ |
| 77 | + {Type: types.DiffAdd, Object: types.ObjectIndex, Name: "users_email_idx", TableName: "users"}, |
| 78 | + } |
| 79 | + gen := NewSQLGenerator(diffs, "public") |
| 80 | + gen.SetTransaction(false) |
| 81 | + sql := gen.Generate() |
| 82 | + |
| 83 | + if !strings.Contains(sql, "CREATE INDEX") { |
| 84 | + t.Errorf("Expected CREATE INDEX, got: %s", sql) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestSQLGenerator_DropIndex(t *testing.T) { |
| 89 | + diffs := types.DiffList{ |
| 90 | + {Type: types.DiffDrop, Object: types.ObjectIndex, Name: "users_email_idx"}, |
| 91 | + } |
| 92 | + gen := NewSQLGenerator(diffs, "public") |
| 93 | + gen.SetTransaction(false) |
| 94 | + sql := gen.Generate() |
| 95 | + |
| 96 | + if !strings.Contains(sql, "DROP INDEX") { |
| 97 | + t.Errorf("Expected DROP INDEX, got: %s", sql) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestSQLGenerator_AddConstraint(t *testing.T) { |
| 102 | + diffs := types.DiffList{ |
| 103 | + {Type: types.DiffAdd, Object: types.ObjectConstraint, Name: "users_pkey", TableName: "users"}, |
| 104 | + } |
| 105 | + gen := NewSQLGenerator(diffs, "public") |
| 106 | + gen.SetTransaction(false) |
| 107 | + sql := gen.Generate() |
| 108 | + |
| 109 | + if !strings.Contains(sql, "ADD CONSTRAINT") { |
| 110 | + t.Errorf("Expected ADD CONSTRAINT, got: %s", sql) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestSQLGenerator_DropConstraint(t *testing.T) { |
| 115 | + diffs := types.DiffList{ |
| 116 | + {Type: types.DiffDrop, Object: types.ObjectConstraint, Name: "users_pkey", TableName: "users"}, |
| 117 | + } |
| 118 | + gen := NewSQLGenerator(diffs, "public") |
| 119 | + gen.SetTransaction(false) |
| 120 | + sql := gen.Generate() |
| 121 | + |
| 122 | + if !strings.Contains(sql, "DROP CONSTRAINT") { |
| 123 | + t.Errorf("Expected DROP CONSTRAINT, got: %s", sql) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestSQLGenerator_CreateSequence(t *testing.T) { |
| 128 | + diffs := types.DiffList{ |
| 129 | + {Type: types.DiffAdd, Object: types.ObjectSequence, Name: "users_id_seq"}, |
| 130 | + } |
| 131 | + gen := NewSQLGenerator(diffs, "public") |
| 132 | + gen.SetTransaction(false) |
| 133 | + sql := gen.Generate() |
| 134 | + |
| 135 | + if !strings.Contains(sql, "CREATE SEQUENCE") { |
| 136 | + t.Errorf("Expected CREATE SEQUENCE, got: %s", sql) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestSQLGenerator_TransactionWrapper(t *testing.T) { |
| 141 | + diffs := types.DiffList{ |
| 142 | + {Type: types.DiffAdd, Object: types.ObjectTable, Name: "users"}, |
| 143 | + } |
| 144 | + gen := NewSQLGenerator(diffs, "public") |
| 145 | + gen.SetTransaction(true) |
| 146 | + sql := gen.Generate() |
| 147 | + |
| 148 | + if !strings.Contains(sql, "DO $$") { |
| 149 | + t.Errorf("Expected transaction wrapper, got: %s", sql) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestSQLGenerator_EmptyDiffs(t *testing.T) { |
| 154 | + diffs := types.DiffList{} |
| 155 | + gen := NewSQLGenerator(diffs, "public") |
| 156 | + gen.SetTransaction(false) |
| 157 | + sql := gen.Generate() |
| 158 | + |
| 159 | + if strings.Contains(sql, "CREATE") || strings.Contains(sql, "DROP") { |
| 160 | + t.Errorf("Expected empty output for empty diffs, got: %s", sql) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func TestSQLGenerator_MultipleDiffs(t *testing.T) { |
| 165 | + diffs := types.DiffList{ |
| 166 | + {Type: types.DiffAdd, Object: types.ObjectTable, Name: "users"}, |
| 167 | + {Type: types.DiffAdd, Object: types.ObjectTable, Name: "posts"}, |
| 168 | + {Type: types.DiffDrop, Object: types.ObjectTable, Name: "old_table"}, |
| 169 | + } |
| 170 | + gen := NewSQLGenerator(diffs, "public") |
| 171 | + gen.SetTransaction(false) |
| 172 | + sql := gen.Generate() |
| 173 | + |
| 174 | + if !strings.Contains(sql, "users") || !strings.Contains(sql, "posts") || !strings.Contains(sql, "old_table") { |
| 175 | + t.Errorf("Expected all table names in output, got: %s", sql) |
| 176 | + } |
| 177 | +} |
0 commit comments