2016-04-25 03:43:09 +02:00
|
|
|
package strmangle
|
2016-03-02 04:11:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pobri19/sqlboiler/dbdrivers"
|
|
|
|
)
|
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
var testColumns = []dbdrivers.Column{
|
2016-04-04 12:28:58 +02:00
|
|
|
{Name: "friend_column", Type: "int", IsNullable: false},
|
|
|
|
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
2016-03-02 04:11:47 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 15:42:25 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 11:31:51 +02:00
|
|
|
func TestTitleCaseCommaList(t *testing.T) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 11:01:24 +02:00
|
|
|
func TestCamelCaseCommaList(t *testing.T) {
|
|
|
|
cols := []string{
|
|
|
|
"test_id",
|
|
|
|
"test_thing",
|
|
|
|
"test_stuff_thing",
|
|
|
|
"test",
|
|
|
|
}
|
|
|
|
|
2016-05-06 11:31:51 +02:00
|
|
|
x := CamelCaseCommaList("", cols)
|
2016-05-05 11:01:24 +02:00
|
|
|
expected := `testID, testThing, testStuffThing, test`
|
|
|
|
if x != expected {
|
|
|
|
t.Errorf("Expected %s, got %s", expected, x)
|
|
|
|
}
|
2016-05-06 11:31:51 +02:00
|
|
|
|
|
|
|
x = CamelCaseCommaList("o.", cols)
|
|
|
|
expected = `o.testID, o.testThing, o.testStuffThing, o.test`
|
|
|
|
if x != expected {
|
|
|
|
t.Errorf("Expected %s, got %s", expected, x)
|
|
|
|
}
|
2016-05-05 11:01:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
func TestAutoIncPrimaryKey(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var pkey *dbdrivers.PrimaryKey
|
|
|
|
var cols []dbdrivers.Column
|
|
|
|
|
|
|
|
r := AutoIncPrimaryKey(cols, pkey)
|
|
|
|
if r != "" {
|
|
|
|
t.Errorf("Expected empty string, got %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
pkey = &dbdrivers.PrimaryKey{
|
|
|
|
Columns: []string{
|
|
|
|
"col1", "auto",
|
|
|
|
},
|
|
|
|
Name: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
cols = []dbdrivers.Column{
|
|
|
|
{
|
|
|
|
Name: "thing",
|
|
|
|
IsNullable: true,
|
|
|
|
Type: "int64",
|
|
|
|
Default: "nextval('abc'::regclass)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "stuff",
|
|
|
|
IsNullable: false,
|
|
|
|
Type: "string",
|
|
|
|
Default: "nextval('abc'::regclass)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "other",
|
|
|
|
IsNullable: false,
|
|
|
|
Type: "int64",
|
|
|
|
Default: "nextval",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r = AutoIncPrimaryKey(cols, pkey)
|
|
|
|
if r != "" {
|
|
|
|
t.Errorf("Expected empty string, got %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
cols = append(cols, dbdrivers.Column{
|
|
|
|
Name: "auto",
|
|
|
|
IsNullable: false,
|
|
|
|
Type: "int64",
|
|
|
|
Default: "nextval('abc'::regclass)",
|
|
|
|
})
|
|
|
|
|
|
|
|
r = AutoIncPrimaryKey(cols, pkey)
|
|
|
|
if r != "auto" {
|
|
|
|
t.Errorf("Expected empty string, got %s", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 12:26:48 +01:00
|
|
|
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 {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := Singular(test.In); out != test.Out {
|
2016-03-18 12:26:48 +01:00
|
|
|
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 {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := Plural(test.In); out != test.Out {
|
2016-03-18 12:26:48 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
func TestTitleCase(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"hello_there", "HelloThere"},
|
|
|
|
{"", ""},
|
|
|
|
{"fun_id", "FunID"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := TitleCase(test.In); out != test.Out {
|
2016-03-02 04:11:47 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCamelCase(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"hello_there_sunny", "helloThereSunny"},
|
|
|
|
{"", ""},
|
|
|
|
{"fun_id_times", "funIDTimes"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := CamelCase(test.In); out != test.Out {
|
2016-03-02 04:11:47 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeDBName(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := MakeDBName("a", "b"); out != "a_b" {
|
2016-03-02 04:11:47 +01:00
|
|
|
t.Error("Out was wrong:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 07:22:10 +01:00
|
|
|
func TestUpdateParamNames(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
var testCols = []dbdrivers.Column{
|
2016-04-04 12:28:58 +02:00
|
|
|
{Name: "id", Type: "int", IsNullable: false},
|
|
|
|
{Name: "friend_column", Type: "int", IsNullable: false},
|
|
|
|
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
out := UpdateParamNames(testCols, []string{"id"})
|
2016-03-19 07:22:10 +01:00
|
|
|
if out != "friend_column=$1,enemy_column_thing=$2" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateParamVariables(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
var testCols = []dbdrivers.Column{
|
2016-04-04 12:28:58 +02:00
|
|
|
{Name: "id", Type: "int", IsNullable: false},
|
|
|
|
{Name: "friend_column", Type: "int", IsNullable: false},
|
|
|
|
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
out := UpdateParamVariables("o.", testCols, []string{"id"})
|
2016-03-19 07:22:10 +01:00
|
|
|
if out != "o.FriendColumn, o.EnemyColumnThing" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
func TestInsertParamNames(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
out := InsertParamNames(testColumns)
|
2016-03-02 04:11:47 +01:00
|
|
|
if out != "friend_column, enemy_column_thing" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertParamFlags(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
out := InsertParamFlags(testColumns)
|
2016-03-02 04:11:47 +01:00
|
|
|
if out != "$1, $2" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 12:26:48 +01:00
|
|
|
func TestInsertParamVariables(t *testing.T) {
|
2016-04-25 03:43:09 +02:00
|
|
|
out := InsertParamVariables("o.", testColumns)
|
2016-03-18 12:26:48 +01:00
|
|
|
if out != "o.FriendColumn, o.EnemyColumnThing" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
func TestSelectParamFlags(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
out := SelectParamNames("table", testColumns)
|
2016-03-02 04:11:47 +01:00
|
|
|
if out != "friend_column AS table_friend_column, enemy_column_thing AS table_enemy_column_thing" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 05:05:25 +01:00
|
|
|
|
|
|
|
func TestScanParams(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
out := ScanParamNames("object", testColumns)
|
2016-03-02 05:05:25 +01:00
|
|
|
if out != "&object.FriendColumn, &object.EnemyColumnThing" {
|
|
|
|
t.Error("Wrong output:", out)
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:28:58 +02:00
|
|
|
|
|
|
|
func TestHasPrimaryKey(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var pkey *dbdrivers.PrimaryKey
|
2016-04-25 03:43:09 +02:00
|
|
|
if HasPrimaryKey(pkey) {
|
2016-04-04 12:28:58 +02:00
|
|
|
t.Errorf("1) Expected false, got true")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkey = &dbdrivers.PrimaryKey{}
|
2016-04-25 03:43:09 +02:00
|
|
|
if HasPrimaryKey(pkey) {
|
2016-04-04 12:28:58 +02:00
|
|
|
t.Errorf("2) Expected false, got true")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkey.Columns = append(pkey.Columns, "test")
|
2016-04-25 03:43:09 +02:00
|
|
|
if !HasPrimaryKey(pkey) {
|
2016-04-04 12:28:58 +02:00
|
|
|
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 {
|
2016-04-25 03:43:09 +02:00
|
|
|
r := ParamsPrimaryKey(test.Prefix, test.Pkey.Columns, true)
|
2016-04-04 12:28:58 +02:00
|
|
|
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 {
|
2016-04-25 03:43:09 +02:00
|
|
|
r := ParamsPrimaryKey(test.Prefix, test.Pkey.Columns, false)
|
2016-04-04 12:28:58 +02:00
|
|
|
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 {
|
2016-04-25 03:43:09 +02:00
|
|
|
r := WherePrimaryKey(test.Pkey.Columns, test.Start)
|
2016-04-04 12:28:58 +02:00
|
|
|
if r != test.Should {
|
|
|
|
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 08:34:25 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 11:01:24 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|