52 lines
2.2 KiB
Go
52 lines
2.2 KiB
Go
package transform
|
|
|
|
import (
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
"testing"
|
|
)
|
|
|
|
func TestLowercaseIdentifiersReferencesAndData(t *testing.T) {
|
|
db := models.InitDatabase("DB")
|
|
s := models.InitSchema("App")
|
|
tbl := models.InitTable("Users", "App")
|
|
col := models.InitColumn("ID", "Users", "App")
|
|
tbl.Tablespace = "FastStore"
|
|
col.Collation = "MyCollation"
|
|
col.Default = "Keep CASE"
|
|
col.GenerationExpression = `"ID" + 1`
|
|
tbl.Columns[col.Name] = col
|
|
tbl.Constraints["FK"] = &models.Constraint{Name: "FK", Columns: []string{"ID"}, ReferencedSchema: "Other", ReferencedTable: "Target", ReferencedColumns: []string{"Key"}}
|
|
tbl.Indexes["IDX"] = &models.Index{Name: "IDX", Columns: []string{"ID", "lower('VALUE')"}, Include: []string{"ID"}, Where: `"ID" > 0`}
|
|
tbl.RefSchema = s
|
|
s.Tables = append(s.Tables, tbl)
|
|
db.Schemas = append(db.Schemas, s)
|
|
got, err := LowercaseIdentifiers(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
table := got.Schemas[0].Tables[0]
|
|
if table.Tablespace != "faststore" || table.Columns["id"].Collation != "mycollation" || got.Name != "db" || table.Name != "users" || table.Schema != "app" || table.Columns["id"].Name != "id" || table.Constraints["fk"].ReferencedColumns[0] != "key" || table.Indexes["idx"].Include[0] != "id" || table.RefSchema != got.Schemas[0] {
|
|
t.Fatalf("bad normalized model: %+v", table)
|
|
}
|
|
if table.Columns["id"].Default != "Keep CASE" || table.Columns["id"].GenerationExpression != `"ID" + 1` || table.Indexes["idx"].Where != `"ID" > 0` || table.Indexes["idx"].Columns[1] != "lower('VALUE')" {
|
|
t.Fatal("data/expression changed")
|
|
}
|
|
if tbl.Name != "Users" || tbl.Columns["ID"] != col {
|
|
t.Fatal("input mutated")
|
|
}
|
|
}
|
|
func TestLowercaseIdentifierCollisions(t *testing.T) {
|
|
db := models.InitDatabase("db")
|
|
s := models.InitSchema("app")
|
|
db.Schemas = append(db.Schemas, s)
|
|
s.Tables = []*models.Table{models.InitTable("Users", "app"), models.InitTable("users", "app")}
|
|
if _, err := LowercaseIdentifiers(db); err == nil {
|
|
t.Fatal("expected table collision")
|
|
}
|
|
s.Tables = s.Tables[:1]
|
|
s.Tables[0].Columns = map[string]*models.Column{"ID": {Name: "ID"}, "id": {Name: "id"}}
|
|
if _, err := LowercaseIdentifiers(db); err == nil {
|
|
t.Fatal("expected column collision")
|
|
}
|
|
}
|