|
6 | 6 | "testing" |
7 | 7 |
|
8 | 8 | "github.com/go-rel/rel" |
| 9 | + "github.com/go-rel/sql" |
9 | 10 | "github.com/go-rel/sql/specs" |
10 | 11 | _ "github.com/mattn/go-sqlite3" |
11 | 12 | "github.com/stretchr/testify/assert" |
@@ -123,3 +124,45 @@ func TestAdapter_Exec_error(t *testing.T) { |
123 | 124 | _, _, err = adapter.Exec(ctx, "error", nil) |
124 | 125 | assert.NotNil(t, err) |
125 | 126 | } |
| 127 | + |
| 128 | +func TestAdapter_TableBuilder(t *testing.T) { |
| 129 | + adapter, err := Open(dsn()) |
| 130 | + assert.Nil(t, err) |
| 131 | + defer adapter.Close() |
| 132 | + |
| 133 | + tests := []struct { |
| 134 | + result string |
| 135 | + table rel.Table |
| 136 | + }{ |
| 137 | + { |
| 138 | + result: `ALTER TABLE "columns" ADD COLUMN "verified" BOOL;ALTER TABLE "columns" RENAME COLUMN "string" TO "name";ALTER TABLE "columns" ;ALTER TABLE "columns" DROP COLUMN "blob";`, |
| 139 | + table: rel.Table{ |
| 140 | + Op: rel.SchemaAlter, |
| 141 | + Name: "columns", |
| 142 | + Definitions: []rel.TableDefinition{ |
| 143 | + rel.Column{Name: "verified", Type: rel.Bool, Op: rel.SchemaCreate}, |
| 144 | + rel.Column{Name: "string", Rename: "name", Op: rel.SchemaRename}, |
| 145 | + rel.Column{Name: "bool", Type: rel.Int, Op: rel.SchemaAlter}, |
| 146 | + rel.Column{Name: "blob", Op: rel.SchemaDrop}, |
| 147 | + |
| 148 | + // unsupported and will be skipped |
| 149 | + rel.Key{Op: rel.SchemaCreate, Columns: []string{"user_id"}, Type: rel.ForeignKey, Reference: rel.ForeignKeyReference{Table: "products", Columns: []string{"id", "name"}}}, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + { |
| 154 | + result: `ALTER TABLE "table" RENAME TO "table1";`, |
| 155 | + table: rel.Table{ |
| 156 | + Op: rel.SchemaRename, |
| 157 | + Name: "table", |
| 158 | + Rename: "table1", |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + for _, test := range tests { |
| 164 | + t.Run(test.result, func(t *testing.T) { |
| 165 | + assert.Equal(t, test.result, adapter.(*sql.SQL).TableBuilder.Build(test.table)) |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments