137 lines
4.4 KiB
Go
137 lines
4.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
)
|
|
|
|
func newTestEditor() *SchemaEditor {
|
|
db := models.InitDatabase("test")
|
|
schema := models.InitSchema("public")
|
|
table := models.InitTable("users", "public")
|
|
table.Columns["id"] = models.InitColumn("id", "users", "public")
|
|
table.Columns["email"] = models.InitColumn("email", "users", "public")
|
|
schema.Tables = append(schema.Tables, table)
|
|
db.Schemas = append(db.Schemas, schema)
|
|
return &SchemaEditor{db: db}
|
|
}
|
|
|
|
func TestSaveIndex(t *testing.T) {
|
|
se := newTestEditor()
|
|
table := se.db.Schemas[0].Tables[0]
|
|
|
|
tests := []struct {
|
|
name string
|
|
old string
|
|
idx *models.Index
|
|
wantErr bool
|
|
}{
|
|
{"valid", "", &models.Index{Name: "idx_email", Columns: []string{"email"}, Unique: true}, false},
|
|
{"duplicate", "", &models.Index{Name: "idx_email", Columns: []string{"email"}}, true},
|
|
{"missing name", "", &models.Index{Columns: []string{"email"}}, true},
|
|
{"no columns", "", &models.Index{Name: "idx_none"}, true},
|
|
{"unknown column", "", &models.Index{Name: "idx_bad", Columns: []string{"nope"}}, true},
|
|
{"rename", "idx_email", &models.Index{Name: "idx_email2", Columns: []string{"email", "id"}}, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := se.SaveIndex(0, 0, tt.old, tt.idx); (err != nil) != tt.wantErr {
|
|
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
if _, ok := table.Indexes["idx_email"]; ok {
|
|
t.Error("renamed index should be gone under old name")
|
|
}
|
|
if idx := table.Indexes["idx_email2"]; idx == nil || idx.Table != "users" || idx.Schema != "public" {
|
|
t.Errorf("unexpected renamed index: %+v", idx)
|
|
}
|
|
if !se.DeleteIndex(0, 0, "idx_email2") || se.DeleteIndex(0, 0, "idx_email2") {
|
|
t.Error("delete should succeed once")
|
|
}
|
|
}
|
|
|
|
func TestSaveViewSequenceScript(t *testing.T) {
|
|
se := newTestEditor()
|
|
schema := se.db.Schemas[0]
|
|
|
|
if err := se.SaveView(0, -1, &models.View{Name: "v", Definition: "select 1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := se.SaveView(0, -1, &models.View{Name: "v", Definition: "select 2"}); err == nil {
|
|
t.Error("duplicate view accepted")
|
|
}
|
|
if err := se.SaveView(0, 0, &models.View{Name: "v", Definition: "select 3"}); err != nil {
|
|
t.Errorf("editing in place should not conflict: %v", err)
|
|
}
|
|
if err := se.SaveView(0, -1, &models.View{Name: "w"}); err == nil {
|
|
t.Error("view without definition accepted")
|
|
}
|
|
if len(schema.Views) != 1 || schema.Views[0].Definition != "select 3" || schema.Views[0].Schema != "public" {
|
|
t.Errorf("unexpected views: %+v", schema.Views)
|
|
}
|
|
if !se.DeleteView(0, 0) || se.DeleteView(0, 0) {
|
|
t.Error("view delete mismatch")
|
|
}
|
|
|
|
if err := se.SaveSequence(0, -1, &models.Sequence{Name: "s", IncrementBy: 1, StartValue: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := se.SaveSequence(0, -1, &models.Sequence{Name: "z"}); err == nil {
|
|
t.Error("zero increment accepted")
|
|
}
|
|
if !se.DeleteSequence(0, 0) || len(schema.Sequences) != 0 {
|
|
t.Error("sequence delete failed")
|
|
}
|
|
|
|
if err := se.SaveScript(0, -1, &models.Script{Name: "init", SQL: "select 1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := se.SaveScript(0, -1, &models.Script{Name: "empty"}); err == nil {
|
|
t.Error("script without SQL accepted")
|
|
}
|
|
if err := se.SaveScript(5, -1, &models.Script{Name: "x", SQL: "y"}); err == nil {
|
|
t.Error("bad schema index accepted")
|
|
}
|
|
if !se.DeleteScript(0, 0) || len(schema.Scripts) != 0 {
|
|
t.Error("script delete failed")
|
|
}
|
|
}
|
|
|
|
func TestDomainTableAssignment(t *testing.T) {
|
|
se := newTestEditor()
|
|
se.createDomainNoUI("core")
|
|
|
|
if err := se.AssignTableToDomain(0, "public", "users"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := se.AssignTableToDomain(0, "public", "users"); err == nil {
|
|
t.Error("duplicate assignment accepted")
|
|
}
|
|
if err := se.AssignTableToDomain(0, "public", "missing"); err == nil {
|
|
t.Error("unknown table accepted")
|
|
}
|
|
if err := se.AssignTableToDomain(3, "public", "users"); err == nil {
|
|
t.Error("bad domain index accepted")
|
|
}
|
|
dt := se.db.Domains[0].Tables[0]
|
|
if dt.RefTable != se.db.Schemas[0].Tables[0] {
|
|
t.Error("RefTable not linked")
|
|
}
|
|
if !se.UnassignTableFromDomain(0, "public", "users") || se.UnassignTableFromDomain(0, "public", "users") {
|
|
t.Error("unassign mismatch")
|
|
}
|
|
}
|
|
|
|
func (se *SchemaEditor) createDomainNoUI(name string) {
|
|
se.db.Domains = append(se.db.Domains, models.InitDomain(name))
|
|
}
|
|
|
|
func TestSplitList(t *testing.T) {
|
|
got := splitList(" a, b,, c ,")
|
|
if len(got) != 3 || got[0] != "a" || got[2] != "c" {
|
|
t.Errorf("got %v", got)
|
|
}
|
|
}
|