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,83 @@
|
||||
package bun
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSnakeCaseToCamelCase(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"", ""},
|
||||
{"user", "user"},
|
||||
{"User_Name", "userName"},
|
||||
{"user_id", "userID"},
|
||||
{"http_request", "httpRequest"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := SnakeCaseToCamelCase(tt.in); got != tt.want {
|
||||
t.Errorf("%q: got %q want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPascalCaseToSnakeCase(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"", ""},
|
||||
{"User", "user"},
|
||||
{"UserName", "user_name"},
|
||||
{"UserID", "user_id"},
|
||||
{"HTTPRequest", "http_request"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := PascalCaseToSnakeCase(tt.in); got != tt.want {
|
||||
t.Errorf("%q: got %q want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingularize(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"", ""},
|
||||
{"people", "person"},
|
||||
{"People", "person"},
|
||||
{"categories", "category"},
|
||||
{"wolves", "wolf"},
|
||||
{"boxes", "box"},
|
||||
{"churches", "church"},
|
||||
{"users", "user"},
|
||||
{"class", "class"},
|
||||
{"user", "user"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := Singularize(tt.in); got != tt.want {
|
||||
t.Errorf("%q: got %q want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluralize(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"", ""},
|
||||
{"person", "people"},
|
||||
{"category", "categories"},
|
||||
{"box", "boxes"},
|
||||
{"church", "churches"},
|
||||
{"user", "users"},
|
||||
{"day", "days"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := Pluralize(tt.in); got != tt.want {
|
||||
t.Errorf("%q: got %q want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsVowel(t *testing.T) {
|
||||
for _, c := range []byte("aeiouAEIOU") {
|
||||
if !isVowel(c) {
|
||||
t.Errorf("%c should be vowel", c)
|
||||
}
|
||||
}
|
||||
for _, c := range []byte("bcxyzBZ1_") {
|
||||
if isVowel(c) {
|
||||
t.Errorf("%c should not be vowel", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package bun
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/writers"
|
||||
)
|
||||
|
||||
func TestSQLTypeToGoType_Styles(t *testing.T) {
|
||||
tests := []struct {
|
||||
style string
|
||||
arrays string
|
||||
sqlType string
|
||||
notNull bool
|
||||
want string
|
||||
}{
|
||||
{writers.NullableTypeSqlTypes, "", "integer", true, "int32"},
|
||||
{writers.NullableTypeSqlTypes, "", "bigint", true, "int64"},
|
||||
{writers.NullableTypeSqlTypes, "", "text", true, "sql_types.SqlString"},
|
||||
{writers.NullableTypeSqlTypes, "", "boolean", true, "bool"},
|
||||
{writers.NullableTypeSqlTypes, "", "bigint", false, "sql_types.SqlInt64"},
|
||||
{writers.NullableTypeSqlTypes, "", "text", false, "sql_types.SqlString"},
|
||||
{writers.NullableTypeStdlib, "", "integer", true, "int32"},
|
||||
{writers.NullableTypeStdlib, "", "integer", false, "sql.NullInt32"},
|
||||
{writers.NullableTypeStdlib, "", "bigint", false, "sql.NullInt64"},
|
||||
{writers.NullableTypeStdlib, "", "boolean", false, "sql.NullBool"},
|
||||
{writers.NullableTypeStdlib, "", "text", false, "sql.NullString"},
|
||||
{writers.NullableTypeStdlib, "", "timestamptz", false, "sql.NullTime"},
|
||||
{writers.NullableTypeStdlib, "", "mystery", false, "sql.NullString"},
|
||||
{writers.NullableTypeBaselib, "", "integer", false, "*int32"},
|
||||
{"", "", "text", false, "*string"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.style+"/"+tt.sqlType, func(t *testing.T) {
|
||||
got := NewTypeMapper(tt.style, tt.arrays).SQLTypeToGoType(tt.sqlType, tt.notNull)
|
||||
if got != tt.want {
|
||||
t.Errorf("got %q want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLTypeToGoType_Arrays(t *testing.T) {
|
||||
slice := NewTypeMapper(writers.NullableTypeSqlTypes, writers.NullableArraysSlice)
|
||||
ptr := NewTypeMapper(writers.NullableTypeSqlTypes, writers.NullableArraysPointerSlice)
|
||||
for _, sqlType := range []string{"text[]", "integer[]", "bigint[]", "boolean[]", "uuid[]"} {
|
||||
s := slice.SQLTypeToGoType(sqlType, false)
|
||||
p := ptr.SQLTypeToGoType(sqlType, false)
|
||||
if s == "" || strings.HasPrefix(s, "*") {
|
||||
t.Errorf("%s slice mode: %q", sqlType, s)
|
||||
}
|
||||
if p != "*"+s {
|
||||
t.Errorf("%s pointer mode: %q want %q", sqlType, p, "*"+s)
|
||||
}
|
||||
if got := ptr.SQLTypeToGoType(sqlType, true); got != s {
|
||||
t.Errorf("%s not-null should stay a slice: %q", sqlType, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportHelpers(t *testing.T) {
|
||||
tests := []struct {
|
||||
style string
|
||||
want string
|
||||
}{
|
||||
{writers.NullableTypeStdlib, `"database/sql"`},
|
||||
{writers.NullableTypeBaselib, ""},
|
||||
{writers.NullableTypeSqlTypes, `sql_types "git.warky.dev/wdevs/relspecgo/pkg/sqltypes"`},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := NewTypeMapper(tt.style, "").GetNullableTypeImportLine(); got != tt.want {
|
||||
t.Errorf("%s: got %q want %q", tt.style, got, tt.want)
|
||||
}
|
||||
}
|
||||
tm := NewTypeMapper("", "")
|
||||
if !tm.NeedsFmtImport(true) || tm.NeedsFmtImport(false) {
|
||||
t.Error("NeedsFmtImport should echo its argument")
|
||||
}
|
||||
if tm.GetSQLTypesImport() == "" || tm.GetBunImport() != "github.com/uptrace/bun" {
|
||||
t.Error("unexpected imports")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user