Implements tests/_plans and previously deferred packages; updates plan README with new coverage numbers.
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
package pgsql
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
)
|
|
|
|
func TestNormalizePostgresDefault(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{"empty", "", ""},
|
|
{"function", "now()", "now()"},
|
|
{"nextval passthrough", "nextval('seq'::regclass)", "nextval('seq'::regclass)"},
|
|
{"number", "42", "42"},
|
|
{"null cast", "NULL::text", "NULL::text"},
|
|
{"quoted literal", "'abc'", "abc"},
|
|
{"quoted with cast", "'abc'::character varying", "abc"},
|
|
{"escaped quote", "'it''s'::text", "it's"},
|
|
{"empty literal", "''::text", ""},
|
|
{"only escaped quotes", "''''", "'"},
|
|
{"unterminated", "'abc", "abc"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := normalizePostgresDefault(tt.in); got != tt.want {
|
|
t.Errorf("normalizePostgresDefault(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCountHelpers(t *testing.T) {
|
|
cols := map[string]map[string]*models.Column{
|
|
"a": {"x": {}, "y": {}},
|
|
"b": {"z": {}},
|
|
"c": {},
|
|
}
|
|
if got := countColumns(cols); got != 3 {
|
|
t.Errorf("countColumns = %d, want 3", got)
|
|
}
|
|
if got := countColumns(nil); got != 0 {
|
|
t.Errorf("countColumns(nil) = %d, want 0", got)
|
|
}
|
|
|
|
cons := map[string][]*models.Constraint{"a": {{}, {}}, "b": {{}}}
|
|
if got := countConstraints(cons); got != 3 {
|
|
t.Errorf("countConstraints = %d, want 3", got)
|
|
}
|
|
if got := countConstraints(nil); got != 0 {
|
|
t.Errorf("countConstraints(nil) = %d, want 0", got)
|
|
}
|
|
|
|
idx := map[string][]*models.Index{"a": {{}}, "b": {{}, {}, {}}}
|
|
if got := countIndexes(idx); got != 4 {
|
|
t.Errorf("countIndexes = %d, want 4", got)
|
|
}
|
|
if got := countIndexes(nil); got != 0 {
|
|
t.Errorf("countIndexes(nil) = %d, want 0", got)
|
|
}
|
|
}
|
|
|
|
func TestExtractIndexOperatorClass(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in []string
|
|
want string
|
|
}{
|
|
{"none", nil, ""},
|
|
{"sort modifiers only", []string{"DESC", "NULLS", "LAST"}, ""},
|
|
{"opclass", []string{"", " Vector_Cosine_Ops "}, "vector_cosine_ops"},
|
|
{"opclass after ordering", []string{"desc", "gin_trgm_ops"}, "gin_trgm_ops"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := extractIndexOperatorClass(tt.in); got != tt.want {
|
|
t.Errorf("got %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildIndexHint(t *testing.T) {
|
|
tests := []struct {
|
|
opClass, params, want string
|
|
}{
|
|
{"", "", ""},
|
|
{"vector_cosine_ops", "", "opclass=vector_cosine_ops"},
|
|
{"", "m=16", "with (m=16)"},
|
|
{"vector_cosine_ops", "m=16", "opclass=vector_cosine_ops; with (m=16)"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := buildIndexHint(tt.opClass, tt.params); got != tt.want {
|
|
t.Errorf("buildIndexHint(%q,%q) = %q, want %q", tt.opClass, tt.params, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalizeIndexStorageParams(t *testing.T) {
|
|
tests := []struct{ in, want string }{
|
|
{"", ""},
|
|
{"m='16', ef_construction='64'", "m=16, ef_construction=64"},
|
|
{"key_field='id'", "key_field='id'"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := normalizeIndexStorageParams(tt.in); got != tt.want {
|
|
t.Errorf("normalizeIndexStorageParams(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|