test: expand coverage across readers, writers, cmd, ui, diff and merge
Implements tests/_plans and previously deferred packages; updates plan README with new coverage numbers.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package pgsql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/writers"
|
||||
)
|
||||
|
||||
func TestCurrentColumnHasDescription(t *testing.T) {
|
||||
table := models.InitTable("users", "public")
|
||||
c := models.InitColumn("Email", "users", "public")
|
||||
c.Description = " the email "
|
||||
table.Columns["Email"] = c
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
table *models.Table
|
||||
col *models.Column
|
||||
want bool
|
||||
}{
|
||||
{"nil table", nil, &models.Column{Name: "email", Description: "x"}, false},
|
||||
{"match ignoring case and whitespace", table, &models.Column{Name: "email", Description: "the email"}, true},
|
||||
{"different description", table, &models.Column{Name: "email", Description: "other"}, false},
|
||||
{"column missing", table, &models.Column{Name: "age", Description: "x"}, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := currentColumnHasDescription(tt.table, tt.col); got != tt.want {
|
||||
t.Errorf("got %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteCommentColumn(t *testing.T) {
|
||||
te, err := NewTemplateExecutor(false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := te.ExecuteCommentColumn(CommentColumnData{
|
||||
SchemaName: "public", TableName: "users", ColumnName: "email", Comment: "it''s",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(got, "COMMENT ON COLUMN") || !strings.Contains(got, "public.users") ||
|
||||
!strings.Contains(got, "email") || !strings.Contains(got, "IS 'it''s';") {
|
||||
t.Errorf("unexpected output: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func migrationWithColumnDescription(t *testing.T, currentDesc string, withCurrentCol bool) string {
|
||||
t.Helper()
|
||||
newDB := func(desc string, include bool) *models.Database {
|
||||
db := models.InitDatabase("testdb")
|
||||
s := models.InitSchema("public")
|
||||
tbl := models.InitTable("users", "public")
|
||||
id := models.InitColumn("id", "users", "public")
|
||||
id.Type = "integer"
|
||||
tbl.Columns["id"] = id
|
||||
if include {
|
||||
col := models.InitColumn("email", "users", "public")
|
||||
col.Type = "text"
|
||||
col.Description = desc
|
||||
tbl.Columns["email"] = col
|
||||
}
|
||||
s.Tables = append(s.Tables, tbl)
|
||||
db.Schemas = append(db.Schemas, s)
|
||||
return db
|
||||
}
|
||||
model := newDB("it's the email", true)
|
||||
current := newDB(currentDesc, withCurrentCol)
|
||||
|
||||
var buf bytes.Buffer
|
||||
w, err := NewMigrationWriter(&writers.WriterOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.writer = &buf
|
||||
if err := w.WriteMigration(model, current); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestWriteMigration_ColumnComments(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
currentDesc string
|
||||
withCol bool
|
||||
wantComment bool
|
||||
}{
|
||||
{"added", "", true, true},
|
||||
{"changed", "old text", true, true},
|
||||
{"unchanged", "it's the email", true, false},
|
||||
{"new column", "", false, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
out := migrationWithColumnDescription(t, tt.currentDesc, tt.withCol)
|
||||
has := strings.Contains(out, "COMMENT ON COLUMN") && strings.Contains(out, "it''s the email")
|
||||
if has != tt.wantComment {
|
||||
t.Errorf("comment emitted = %v, want %v\n%s", has, tt.wantComment, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user