2016-04-13 15:51:58 +02:00
|
|
|
package boil
|
2016-04-19 07:31:07 +02:00
|
|
|
|
|
|
|
import (
|
2016-04-20 14:51:04 +02:00
|
|
|
"database/sql"
|
2016-04-19 07:31:07 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-04-20 14:51:04 +02:00
|
|
|
func TestSetLimit(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetLimit(q, 10)
|
|
|
|
|
|
|
|
expect := 10
|
|
|
|
if q.limit != expect {
|
|
|
|
t.Errorf("Expected %d, got %d", expect, q.limit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 04:21:06 +02:00
|
|
|
func TestSetOffset(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetOffset(q, 10)
|
|
|
|
|
|
|
|
expect := 10
|
|
|
|
if q.offset != expect {
|
|
|
|
t.Errorf("Expected %d, got %d", expect, q.offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
func TestSetSQL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetSQL(q, "select * from thing", 5, 3)
|
|
|
|
|
|
|
|
if len(q.plainSQL.args) != 2 {
|
|
|
|
t.Errorf("Expected len 2, got %d", len(q.plainSQL.args))
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.plainSQL.sql != "select * from thing" {
|
|
|
|
t.Errorf("Was not expected string, got %s", q.plainSQL.sql)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 19:56:00 +02:00
|
|
|
func TestSetLoad(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetLoad(q, "one", "two")
|
|
|
|
|
|
|
|
if len(q.load) != 2 {
|
|
|
|
t.Errorf("Expected len 2, got %d", len(q.load))
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.load[0] != "one" || q.load[1] != "two" {
|
|
|
|
t.Errorf("Was not expected string, got %s", q.load)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func TestAppendWhere(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
2016-08-04 07:22:17 +02:00
|
|
|
expect := "x > $1 AND y > $2"
|
|
|
|
AppendWhere(q, expect, 5, 3)
|
|
|
|
AppendWhere(q, expect, 5, 3)
|
2016-04-20 14:51:04 +02:00
|
|
|
|
2016-08-04 07:22:17 +02:00
|
|
|
if len(q.where) != 2 {
|
|
|
|
t.Errorf("%#v", q.where)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 07:22:17 +02:00
|
|
|
if q.where[0].clause != expect || q.where[1].clause != expect {
|
|
|
|
t.Errorf("Expected %s, got %#v", expect, q.where)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.where[0].args) != 2 || len(q.where[0].args) != 2 {
|
|
|
|
t.Errorf("arg length wrong: %#v", q.where)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 {
|
|
|
|
t.Errorf("args wrong: %#v", q.where)
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
q.where = []where{{clause: expect, args: []interface{}{5, 3}}}
|
2016-04-20 14:51:04 +02:00
|
|
|
if q.where[0].clause != expect {
|
2016-07-08 18:39:36 +02:00
|
|
|
t.Errorf("Expected %s, got %v", expect, q.where)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.where[0].args) != 2 {
|
|
|
|
t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args))
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 {
|
|
|
|
t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args)
|
|
|
|
}
|
2016-08-04 07:22:17 +02:00
|
|
|
|
|
|
|
if len(q.where) != 1 {
|
|
|
|
t.Errorf("%#v", q.where)
|
|
|
|
}
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 08:44:15 +02:00
|
|
|
func TestSetLastWhereAsOr(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
q := &Query{}
|
|
|
|
|
|
|
|
AppendWhere(q, "")
|
|
|
|
|
|
|
|
if q.where[0].orSeparator {
|
|
|
|
t.Errorf("Do not want or separator")
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastWhereAsOr(q)
|
|
|
|
|
|
|
|
if len(q.where) != 1 {
|
|
|
|
t.Errorf("Want len 1")
|
|
|
|
}
|
|
|
|
if !q.where[0].orSeparator {
|
|
|
|
t.Errorf("Want or separator")
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendWhere(q, "")
|
|
|
|
SetLastWhereAsOr(q)
|
|
|
|
|
|
|
|
if len(q.where) != 2 {
|
|
|
|
t.Errorf("Want len 2")
|
|
|
|
}
|
|
|
|
if q.where[0].orSeparator != true {
|
|
|
|
t.Errorf("Expected true")
|
|
|
|
}
|
|
|
|
if q.where[1].orSeparator != true {
|
|
|
|
t.Errorf("Expected true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func TestAppendIn(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
expect := "col IN ?"
|
|
|
|
AppendIn(q, expect, 5, 3)
|
|
|
|
AppendIn(q, expect, 5, 3)
|
|
|
|
|
|
|
|
if len(q.in) != 2 {
|
|
|
|
t.Errorf("%#v", q.in)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.in[0].clause != expect || q.in[1].clause != expect {
|
|
|
|
t.Errorf("Expected %s, got %#v", expect, q.in)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.in[0].args) != 2 || len(q.in[0].args) != 2 {
|
|
|
|
t.Errorf("arg length wrong: %#v", q.in)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.in[0].args[0].(int) != 5 || q.in[0].args[1].(int) != 3 {
|
|
|
|
t.Errorf("args wrong: %#v", q.in)
|
|
|
|
}
|
|
|
|
|
|
|
|
q.in = []in{{clause: expect, args: []interface{}{5, 3}}}
|
|
|
|
if q.in[0].clause != expect {
|
|
|
|
t.Errorf("Expected %s, got %v", expect, q.in)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.in[0].args) != 2 {
|
|
|
|
t.Errorf("Expected %d args, got %d", 2, len(q.in[0].args))
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.in[0].args[0].(int) != 5 || q.in[0].args[1].(int) != 3 {
|
|
|
|
t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.in[0].args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.in) != 1 {
|
|
|
|
t.Errorf("%#v", q.in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLastInAsOr(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
q := &Query{}
|
|
|
|
|
|
|
|
AppendIn(q, "")
|
|
|
|
|
|
|
|
if q.in[0].orSeparator {
|
|
|
|
t.Errorf("Do not want or separator")
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastInAsOr(q)
|
|
|
|
|
|
|
|
if len(q.in) != 1 {
|
|
|
|
t.Errorf("Want len 1")
|
|
|
|
}
|
|
|
|
if !q.in[0].orSeparator {
|
|
|
|
t.Errorf("Want or separator")
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendIn(q, "")
|
|
|
|
SetLastInAsOr(q)
|
|
|
|
|
|
|
|
if len(q.in) != 2 {
|
|
|
|
t.Errorf("Want len 2")
|
|
|
|
}
|
|
|
|
if q.in[0].orSeparator != true {
|
|
|
|
t.Errorf("Expected true")
|
|
|
|
}
|
|
|
|
if q.in[1].orSeparator != true {
|
|
|
|
t.Errorf("Expected true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendGroupBy(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
expect := "col1, col2"
|
2016-08-17 07:19:23 +02:00
|
|
|
AppendGroupBy(q, expect)
|
|
|
|
AppendGroupBy(q, expect)
|
2016-08-04 07:22:17 +02:00
|
|
|
|
|
|
|
if len(q.groupBy) != 2 && (q.groupBy[0] != expect || q.groupBy[1] != expect) {
|
|
|
|
t.Errorf("Expected %s, got %s %s", expect, q.groupBy[0], q.groupBy[1])
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
q.groupBy = []string{expect}
|
2016-04-20 14:51:04 +02:00
|
|
|
if len(q.groupBy) != 1 && q.groupBy[0] != expect {
|
|
|
|
t.Errorf("Expected %s, got %s", expect, q.groupBy[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func TestAppendOrderBy(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
expect := "col1 desc, col2 asc"
|
2016-08-17 07:19:23 +02:00
|
|
|
AppendOrderBy(q, expect)
|
|
|
|
AppendOrderBy(q, expect)
|
2016-08-04 07:22:17 +02:00
|
|
|
|
|
|
|
if len(q.orderBy) != 2 && (q.orderBy[0] != expect || q.orderBy[1] != expect) {
|
|
|
|
t.Errorf("Expected %s, got %s %s", expect, q.orderBy[0], q.orderBy[1])
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
q.orderBy = []string{"col1 desc, col2 asc"}
|
2016-04-20 14:51:04 +02:00
|
|
|
if len(q.orderBy) != 1 && q.orderBy[0] != expect {
|
|
|
|
t.Errorf("Expected %s, got %s", expect, q.orderBy[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func TestAppendHaving(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
2016-08-12 19:08:09 +02:00
|
|
|
expect := "count(orders.order_id) > ?"
|
2016-08-17 07:19:23 +02:00
|
|
|
AppendHaving(q, expect, 10)
|
|
|
|
AppendHaving(q, expect, 10)
|
2016-08-04 07:22:17 +02:00
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
if len(q.having) != 2 {
|
|
|
|
t.Errorf("Expected 2, got %d", len(q.having))
|
2016-08-04 07:22:17 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
if q.having[0].clause != expect || q.having[1].clause != expect {
|
|
|
|
t.Errorf("Expected %s, got %s %s", expect, q.having[0].clause, q.having[1].clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.having[0].args[0] != 10 || q.having[1].args[0] != 10 {
|
|
|
|
t.Errorf("Expected %v, got %v %v", 10, q.having[0].args[0], q.having[1].args[0])
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
q.having = []having{{clause: expect, args: []interface{}{10}}}
|
2016-08-12 19:08:09 +02:00
|
|
|
if len(q.having) != 1 && (q.having[0].clause != expect || q.having[0].args[0] != 10) {
|
|
|
|
t.Errorf("Expected %s, got %s %v", expect, q.having[0], q.having[0].args[0])
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:22:17 +02:00
|
|
|
func TestFrom(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
2016-08-04 07:22:17 +02:00
|
|
|
AppendFrom(q, "videos a", "orders b")
|
|
|
|
AppendFrom(q, "videos a", "orders b")
|
2016-04-20 14:51:04 +02:00
|
|
|
|
2016-08-04 07:22:17 +02:00
|
|
|
expect := []string{"videos a", "orders b", "videos a", "orders b"}
|
2016-08-04 06:50:26 +02:00
|
|
|
if !reflect.DeepEqual(q.from, expect) {
|
2016-07-19 06:07:12 +02:00
|
|
|
t.Errorf("Expected %s, got %s", expect, q.from)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
2016-08-04 07:22:17 +02:00
|
|
|
|
|
|
|
SetFrom(q, "videos a", "orders b")
|
|
|
|
if !reflect.DeepEqual(q.from, expect[:2]) {
|
|
|
|
t.Errorf("Expected %s, got %s", expect, q.from)
|
|
|
|
}
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 09:48:14 +02:00
|
|
|
func TestSetSelect(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{selectCols: []string{"hello"}}
|
|
|
|
SetSelect(q, nil)
|
|
|
|
|
|
|
|
if q.selectCols != nil {
|
|
|
|
t.Errorf("want nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:46:41 +02:00
|
|
|
func TestSetCount(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetCount(q)
|
|
|
|
|
2016-08-26 14:08:27 +02:00
|
|
|
if q.count != true {
|
|
|
|
t.Errorf("got false")
|
2016-08-15 17:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetUpdate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetUpdate(q, map[string]interface{}{"test": 5})
|
|
|
|
|
|
|
|
if q.update["test"] != 5 {
|
|
|
|
t.Errorf("Wrong update, got %v", q.update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 14:51:04 +02:00
|
|
|
func TestSetDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
SetDelete(q)
|
|
|
|
|
|
|
|
if q.delete != true {
|
|
|
|
t.Errorf("Expected %t, got %t", true, q.delete)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetExecutor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
|
|
|
d := &sql.DB{}
|
|
|
|
SetExecutor(q, d)
|
|
|
|
|
|
|
|
if q.executor != d {
|
|
|
|
t.Errorf("Expected executor to get set to d, but was: %#v", q.executor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:46:41 +02:00
|
|
|
func TestAppendSelect(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
2016-08-04 07:22:17 +02:00
|
|
|
AppendSelect(q, "col1", "col2")
|
|
|
|
AppendSelect(q, "col1", "col2")
|
2016-04-20 14:51:04 +02:00
|
|
|
|
2016-08-04 07:22:17 +02:00
|
|
|
if len(q.selectCols) != 4 {
|
|
|
|
t.Errorf("Expected selectCols len 4, got %d", len(q.selectCols))
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.selectCols[0] != `col1` && q.selectCols[1] != `col2` {
|
|
|
|
t.Errorf("select cols value mismatch: %#v", q.selectCols)
|
|
|
|
}
|
|
|
|
if q.selectCols[2] != `col1` && q.selectCols[3] != `col2` {
|
|
|
|
t.Errorf("select cols value mismatch: %#v", q.selectCols)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
q.selectCols = []string{"col1", "col2"}
|
2016-08-03 02:53:34 +02:00
|
|
|
if q.selectCols[0] != `col1` && q.selectCols[1] != `col2` {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Errorf("select cols value mismatch: %#v", q.selectCols)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:46:41 +02:00
|
|
|
func TestSQL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-28 06:51:03 +02:00
|
|
|
q := SQL(&sql.DB{}, "thing", 5)
|
|
|
|
if q.plainSQL.sql != "thing" {
|
|
|
|
t.Errorf("Expected %q, got %s", "thing", q.plainSQL.sql)
|
|
|
|
}
|
|
|
|
if q.plainSQL.args[0].(int) != 5 {
|
|
|
|
t.Errorf("Expected 5, got %v", q.plainSQL.args[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLG(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := SQLG("thing", 5)
|
2016-08-15 17:46:41 +02:00
|
|
|
if q.plainSQL.sql != "thing" {
|
|
|
|
t.Errorf("Expected %q, got %s", "thing", q.plainSQL.sql)
|
|
|
|
}
|
|
|
|
if q.plainSQL.args[0].(int) != 5 {
|
|
|
|
t.Errorf("Expected 5, got %v", q.plainSQL.args[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func TestAppendInnerJoin(t *testing.T) {
|
2016-04-20 14:51:04 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
q := &Query{}
|
2016-08-04 07:22:17 +02:00
|
|
|
AppendInnerJoin(q, "thing=$1 AND stuff=$2", 2, 5)
|
|
|
|
AppendInnerJoin(q, "thing=$1 AND stuff=$2", 2, 5)
|
2016-04-20 14:51:04 +02:00
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
if len(q.joins) != 2 {
|
|
|
|
t.Errorf("Expected len 1, got %d", len(q.joins))
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
if q.joins[0].clause != "thing=$1 AND stuff=$2" {
|
|
|
|
t.Errorf("Got invalid innerJoin on string: %#v", q.joins)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
2016-08-07 22:37:51 +02:00
|
|
|
if q.joins[1].clause != "thing=$1 AND stuff=$2" {
|
|
|
|
t.Errorf("Got invalid innerJoin on string: %#v", q.joins)
|
2016-08-04 07:22:17 +02:00
|
|
|
}
|
2016-04-20 14:51:04 +02:00
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
if len(q.joins[0].args) != 2 {
|
|
|
|
t.Errorf("Expected len 2, got %d", len(q.joins[0].args))
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
2016-08-07 22:37:51 +02:00
|
|
|
if len(q.joins[1].args) != 2 {
|
|
|
|
t.Errorf("Expected len 2, got %d", len(q.joins[1].args))
|
2016-08-04 07:22:17 +02:00
|
|
|
}
|
2016-04-20 14:51:04 +02:00
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
if q.joins[0].args[0] != 2 && q.joins[0].args[1] != 5 {
|
|
|
|
t.Errorf("Invalid args values, got %#v", q.joins[0].args)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
q.joins = []join{{kind: JoinInner,
|
|
|
|
clause: "thing=$1 AND stuff=$2",
|
|
|
|
args: []interface{}{2, 5},
|
|
|
|
}}
|
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
if len(q.joins) != 1 {
|
|
|
|
t.Errorf("Expected len 1, got %d", len(q.joins))
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
if q.joins[0].clause != "thing=$1 AND stuff=$2" {
|
|
|
|
t.Errorf("Got invalid innerJoin on string: %#v", q.joins)
|
2016-04-20 14:51:04 +02:00
|
|
|
}
|
|
|
|
}
|