30ece150e7
- It's test was also a dead unicorn of unhappiness.
517 lines
11 KiB
Go
517 lines
11 KiB
Go
package strmangle
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/nullbio/sqlboiler/dbdrivers"
|
|
)
|
|
|
|
var testColumns = []dbdrivers.Column{
|
|
{Name: "friend_column", Type: "int", IsNullable: false},
|
|
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
|
}
|
|
|
|
func TestCommaList(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cols := []string{
|
|
"test1",
|
|
}
|
|
|
|
x := CommaList(cols)
|
|
|
|
if x != `"test1"` {
|
|
t.Errorf(`Expected "test1" - got %s`, x)
|
|
}
|
|
|
|
cols = append(cols, "test2")
|
|
|
|
x = CommaList(cols)
|
|
|
|
if x != `"test1", "test2"` {
|
|
t.Errorf(`Expected "test1", "test2" - got %s`, x)
|
|
}
|
|
|
|
cols = append(cols, "test3")
|
|
|
|
x = CommaList(cols)
|
|
|
|
if x != `"test1", "test2", "test3"` {
|
|
t.Errorf(`Expected "test1", "test2", "test3" - got %s`, x)
|
|
}
|
|
}
|
|
|
|
func TestTitleCaseCommaList(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cols := []string{
|
|
"test_id",
|
|
"test_thing",
|
|
"test_stuff_thing",
|
|
"test",
|
|
}
|
|
|
|
x := TitleCaseCommaList("", cols)
|
|
expected := `TestID, TestThing, TestStuffThing, Test`
|
|
if x != expected {
|
|
t.Errorf("Expected %s, got %s", expected, x)
|
|
}
|
|
|
|
x = TitleCaseCommaList("o.", cols)
|
|
expected = `o.TestID, o.TestThing, o.TestStuffThing, o.Test`
|
|
if x != expected {
|
|
t.Errorf("Expected %s, got %s", expected, x)
|
|
}
|
|
}
|
|
|
|
func TestCamelCaseCommaList(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cols := []string{
|
|
"test_id",
|
|
"test_thing",
|
|
"test_stuff_thing",
|
|
"test",
|
|
}
|
|
|
|
x := CamelCaseCommaList("", cols)
|
|
expected := `testID, testThing, testStuffThing, test`
|
|
if x != expected {
|
|
t.Errorf("Expected %s, got %s", expected, x)
|
|
}
|
|
|
|
x = CamelCaseCommaList("o.", cols)
|
|
expected = `o.testID, o.testThing, o.testStuffThing, o.test`
|
|
if x != expected {
|
|
t.Errorf("Expected %s, got %s", expected, x)
|
|
}
|
|
}
|
|
|
|
func TestAutoIncPrimaryKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := map[string]struct {
|
|
Expect string
|
|
Pkey *dbdrivers.PrimaryKey
|
|
Columns []dbdrivers.Column
|
|
}{
|
|
"easycase": {
|
|
Expect: "one",
|
|
Pkey: &dbdrivers.PrimaryKey{
|
|
Name: "pkey",
|
|
Columns: []string{"one"},
|
|
},
|
|
Columns: []dbdrivers.Column{
|
|
dbdrivers.Column{
|
|
Name: "one",
|
|
Type: "int32",
|
|
IsNullable: false,
|
|
Default: `nextval('abc'::regclass)`,
|
|
},
|
|
},
|
|
},
|
|
"missingcase": {
|
|
Expect: "",
|
|
Pkey: &dbdrivers.PrimaryKey{
|
|
Name: "pkey",
|
|
Columns: []string{"two"},
|
|
},
|
|
Columns: []dbdrivers.Column{
|
|
dbdrivers.Column{
|
|
Name: "one",
|
|
Type: "int32",
|
|
IsNullable: false,
|
|
Default: `nextval('abc'::regclass)`,
|
|
},
|
|
},
|
|
},
|
|
"wrongtype": {
|
|
Expect: "",
|
|
Pkey: &dbdrivers.PrimaryKey{
|
|
Name: "pkey",
|
|
Columns: []string{"one"},
|
|
},
|
|
Columns: []dbdrivers.Column{
|
|
dbdrivers.Column{
|
|
Name: "one",
|
|
Type: "string",
|
|
IsNullable: false,
|
|
Default: `nextval('abc'::regclass)`,
|
|
},
|
|
},
|
|
},
|
|
"nodefault": {
|
|
Expect: "",
|
|
Pkey: &dbdrivers.PrimaryKey{
|
|
Name: "pkey",
|
|
Columns: []string{"one"},
|
|
},
|
|
Columns: []dbdrivers.Column{
|
|
dbdrivers.Column{
|
|
Name: "one",
|
|
Type: "string",
|
|
IsNullable: false,
|
|
Default: ``,
|
|
},
|
|
},
|
|
},
|
|
"nullable": {
|
|
Expect: "",
|
|
Pkey: &dbdrivers.PrimaryKey{
|
|
Name: "pkey",
|
|
Columns: []string{"one"},
|
|
},
|
|
Columns: []dbdrivers.Column{
|
|
dbdrivers.Column{
|
|
Name: "one",
|
|
Type: "string",
|
|
IsNullable: true,
|
|
Default: `nextval('abc'::regclass)`,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for testName, test := range tests {
|
|
primaryKey := AutoIncPrimaryKey(test.Columns, test.Pkey)
|
|
if primaryKey != test.Expect {
|
|
t.Errorf("%s) wrong primary key, want: %q, got %q", testName, test.Expect, primaryKey)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGenerateParamFlags(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
x := GenerateParamFlags(5, 1)
|
|
want := "$1,$2,$3,$4,$5"
|
|
if want != x {
|
|
t.Errorf("want %s, got %s", want, x)
|
|
}
|
|
}
|
|
|
|
func TestSingular(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
In string
|
|
Out string
|
|
}{
|
|
{"hello_people", "hello_person"},
|
|
{"hello_person", "hello_person"},
|
|
{"friends", "friend"},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
if out := Singular(test.In); out != test.Out {
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPlural(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
In string
|
|
Out string
|
|
}{
|
|
{"hello_person", "hello_people"},
|
|
{"friend", "friends"},
|
|
{"friends", "friends"},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
if out := Plural(test.In); out != test.Out {
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTitleCase(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
In string
|
|
Out string
|
|
}{
|
|
{"hello_there", "HelloThere"},
|
|
{"", ""},
|
|
{"fun_id", "FunID"},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
if out := TitleCase(test.In); out != test.Out {
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCamelCase(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
In string
|
|
Out string
|
|
}{
|
|
{"hello_there_sunny", "helloThereSunny"},
|
|
{"", ""},
|
|
{"fun_id_times", "funIDTimes"},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
if out := CamelCase(test.In); out != test.Out {
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMakeDBName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if out := MakeDBName("a", "b"); out != "a_b" {
|
|
t.Error("Out was wrong:", out)
|
|
}
|
|
}
|
|
|
|
func TestUpdateParamNames(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var testCols = []dbdrivers.Column{
|
|
{Name: "id", Type: "int", IsNullable: false},
|
|
{Name: "friend_column", Type: "int", IsNullable: false},
|
|
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
|
}
|
|
|
|
out := UpdateParamNames(testCols, []string{"id"})
|
|
if out != "friend_column=$1,enemy_column_thing=$2" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestUpdateParamVariables(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var testCols = []dbdrivers.Column{
|
|
{Name: "id", Type: "int", IsNullable: false},
|
|
{Name: "friend_column", Type: "int", IsNullable: false},
|
|
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
|
}
|
|
|
|
out := UpdateParamVariables("o.", testCols, []string{"id"})
|
|
if out != "o.FriendColumn, o.EnemyColumnThing" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestInsertParamNames(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
out := InsertParamNames(testColumns)
|
|
if out != "friend_column, enemy_column_thing" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestInsertParamFlags(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
out := InsertParamFlags(testColumns)
|
|
if out != "$1, $2" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestInsertParamVariables(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
out := InsertParamVariables("o.", testColumns)
|
|
if out != "o.FriendColumn, o.EnemyColumnThing" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestSelectParamFlags(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
out := SelectParamNames("table", testColumns)
|
|
if out != "friend_column AS table_friend_column, enemy_column_thing AS table_enemy_column_thing" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestScanParams(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
out := ScanParamNames("object", testColumns)
|
|
if out != "&object.FriendColumn, &object.EnemyColumnThing" {
|
|
t.Error("Wrong output:", out)
|
|
}
|
|
}
|
|
|
|
func TestHasPrimaryKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var pkey *dbdrivers.PrimaryKey
|
|
if HasPrimaryKey(pkey) {
|
|
t.Errorf("1) Expected false, got true")
|
|
}
|
|
|
|
pkey = &dbdrivers.PrimaryKey{}
|
|
if HasPrimaryKey(pkey) {
|
|
t.Errorf("2) Expected false, got true")
|
|
}
|
|
|
|
pkey.Columns = append(pkey.Columns, "test")
|
|
if !HasPrimaryKey(pkey) {
|
|
t.Errorf("3) Expected true, got false")
|
|
}
|
|
}
|
|
|
|
func TestParamsPrimaryKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
Pkey dbdrivers.PrimaryKey
|
|
Prefix string
|
|
Should string
|
|
}{
|
|
{
|
|
Pkey: dbdrivers.PrimaryKey{Columns: []string{"col_one"}},
|
|
Prefix: "o.", Should: "o.ColOne",
|
|
},
|
|
{
|
|
Pkey: dbdrivers.PrimaryKey{Columns: []string{"col_one", "col_two"}},
|
|
Prefix: "o.", Should: "o.ColOne, o.ColTwo",
|
|
},
|
|
{
|
|
Pkey: dbdrivers.PrimaryKey{Columns: []string{"col_one", "col_two", "col_three"}},
|
|
Prefix: "o.", Should: "o.ColOne, o.ColTwo, o.ColThree",
|
|
},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
r := ParamsPrimaryKey(test.Prefix, test.Pkey.Columns, true)
|
|
if r != test.Should {
|
|
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
|
}
|
|
}
|
|
|
|
tests2 := []struct {
|
|
Pkey dbdrivers.PrimaryKey
|
|
Prefix string
|
|
Should string
|
|
}{
|
|
{
|
|
Pkey: dbdrivers.PrimaryKey{Columns: []string{"col_one"}},
|
|
Prefix: "o.", Should: "o.col_one",
|
|
},
|
|
{
|
|
Pkey: dbdrivers.PrimaryKey{Columns: []string{"col_one", "col_two"}},
|
|
Prefix: "o.", Should: "o.col_one, o.col_two",
|
|
},
|
|
{
|
|
Pkey: dbdrivers.PrimaryKey{Columns: []string{"col_one", "col_two", "col_three"}},
|
|
Prefix: "o.", Should: "o.col_one, o.col_two, o.col_three",
|
|
},
|
|
}
|
|
|
|
for i, test := range tests2 {
|
|
r := ParamsPrimaryKey(test.Prefix, test.Pkey.Columns, false)
|
|
if r != test.Should {
|
|
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWherePrimaryKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
Pkey dbdrivers.PrimaryKey
|
|
Start int
|
|
Should string
|
|
}{
|
|
{Pkey: dbdrivers.PrimaryKey{Columns: []string{"col1"}}, Start: 2, Should: "col1=$2"},
|
|
{Pkey: dbdrivers.PrimaryKey{Columns: []string{"col1", "col2"}}, Start: 4, Should: "col1=$4 AND col2=$5"},
|
|
{Pkey: dbdrivers.PrimaryKey{Columns: []string{"col1", "col2", "col3"}}, Start: 4, Should: "col1=$4 AND col2=$5 AND col3=$6"},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
r := WherePrimaryKey(test.Pkey.Columns, test.Start)
|
|
if r != test.Should {
|
|
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFilterColumnsByDefault(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cols := []dbdrivers.Column{
|
|
{
|
|
Name: "col1",
|
|
Default: "",
|
|
},
|
|
{
|
|
Name: "col2",
|
|
Default: "things",
|
|
},
|
|
{
|
|
Name: "col3",
|
|
Default: "",
|
|
},
|
|
{
|
|
Name: "col4",
|
|
Default: "things2",
|
|
},
|
|
}
|
|
|
|
res := FilterColumnsByDefault(cols, false)
|
|
if res != `"col1","col3"` {
|
|
t.Errorf("Invalid result: %s", res)
|
|
}
|
|
|
|
res = FilterColumnsByDefault(cols, true)
|
|
if res != `"col2","col4"` {
|
|
t.Errorf("Invalid result: %s", res)
|
|
}
|
|
|
|
res = FilterColumnsByDefault([]dbdrivers.Column{}, false)
|
|
if res != `` {
|
|
t.Errorf("Invalid result: %s", res)
|
|
}
|
|
}
|
|
|
|
func TestFilterColumnsByAutoIncrement(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cols := []dbdrivers.Column{
|
|
{
|
|
Name: "col1",
|
|
Default: `nextval("thing"::thing)`,
|
|
},
|
|
{
|
|
Name: "col2",
|
|
Default: "things",
|
|
},
|
|
{
|
|
Name: "col3",
|
|
Default: "",
|
|
},
|
|
{
|
|
Name: "col4",
|
|
Default: `nextval("thing"::thing)`,
|
|
},
|
|
}
|
|
|
|
res := FilterColumnsByAutoIncrement(cols)
|
|
if res != `"col1","col4"` {
|
|
t.Errorf("Invalid result: %s", res)
|
|
}
|
|
|
|
res = FilterColumnsByAutoIncrement([]dbdrivers.Column{})
|
|
if res != `` {
|
|
t.Errorf("Invalid result: %s", res)
|
|
}
|
|
}
|