sqlboiler/cmds/template_funcs_test.go

266 lines
5.9 KiB
Go
Raw Normal View History

2016-03-02 04:11:47 +01:00
package cmds
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
}
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)
}
}
}
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 {
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) {
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 {
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) {
2016-03-03 05:14:21 +01:00
t.Parallel()
2016-03-02 04:11:47 +01:00
if out := makeDBName("a", "b"); out != "a_b" {
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-04 12:28:58 +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-04 12:28:58 +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-03-02 04:11:47 +01:00
out := insertParamNames(testColumns)
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-03-02 04:11:47 +01:00
out := insertParamFlags(testColumns)
if out != "$1, $2" {
t.Error("Wrong output:", out)
}
}
func TestInsertParamVariables(t *testing.T) {
out := insertParamVariables("o.", testColumns)
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-03-02 04:11:47 +01:00
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) {
2016-03-03 05:14:21 +01:00
t.Parallel()
out := scanParamNames("object", testColumns)
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
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)
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)
}
}
}