2019-03-31 09:17:28 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-29 14:03:38 +02:00
|
|
|
func TestBoolExpressionEQ(t *testing.T) {
|
2019-06-15 13:58:45 +02:00
|
|
|
assertClauseSerializeErr(t, table1ColBool.EQ(nil), "nil rhs")
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.col_bool = table2.col_bool)")
|
|
|
|
|
assertClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.col_bool = $1)", true)
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionNOT_EQ(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.col_bool != table2.col_bool)")
|
|
|
|
|
assertClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.col_bool != $1)", true)
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionIS_TRUE(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.col_bool IS TRUE")
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(),
|
2019-06-17 12:05:52 +02:00
|
|
|
`($1 = table1.col_int) IS TRUE`, int64(2))
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)),
|
2019-06-17 12:05:52 +02:00
|
|
|
`(($1 = table1.col_int) IS TRUE AND ($2 = table2.col_int))`, int64(2), int64(4))
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionIS_NOT_TRUE(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.col_bool IS NOT TRUE")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionIS_FALSE(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.col_bool IS FALSE")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionIS_NOT_FALSE(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.IS_NOT_FALSE(), "table1.col_bool IS NOT FALSE")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionIS_UNKNOWN(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.col_bool IS UNKNOWN")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBoolExpressionIS_NOT_UNKNOWN(t *testing.T) {
|
2019-06-17 12:05:52 +02:00
|
|
|
assertClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.col_bool IS NOT UNKNOWN")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
func TestBinaryBoolExpression(t *testing.T) {
|
|
|
|
|
boolExpression := Int(2).EQ(Int(3))
|
2019-03-31 09:17:28 +02:00
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, boolExpression, "($1 = $2)", int64(2), int64(3))
|
2019-06-09 11:06:08 +02:00
|
|
|
assertProjectionSerialize(t, boolExpression, "$1 = $2", int64(2), int64(3))
|
2019-06-04 11:52:37 +02:00
|
|
|
assertProjectionSerialize(t, boolExpression.AS("alias_eq_expression"),
|
2019-06-09 11:06:08 +02:00
|
|
|
`($1 = $2) AS "alias_eq_expression"`, int64(2), int64(3))
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, boolExpression.AND(Int(4).EQ(Int(5))),
|
2019-06-04 11:52:37 +02:00
|
|
|
"(($1 = $2) AND ($3 = $4))", int64(2), int64(3), int64(4), int64(5))
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, boolExpression.OR(Int(4).EQ(Int(5))),
|
2019-06-04 11:52:37 +02:00
|
|
|
"(($1 = $2) OR ($3 = $4))", int64(2), int64(3), int64(4), int64(5))
|
2019-03-31 09:17:28 +02:00
|
|
|
}
|
2019-06-07 14:23:14 +02:00
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
func TestBoolLiteral(t *testing.T) {
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, Bool(true), "$1", true)
|
|
|
|
|
assertClauseSerialize(t, Bool(false), "$1", false)
|
2019-03-31 09:17:28 +02:00
|
|
|
}
|
2019-05-05 18:03:30 +02:00
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
2019-06-04 11:52:37 +02:00
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
assertClauseSerialize(t, EXISTS(
|
2019-05-05 18:03:30 +02:00
|
|
|
table2.
|
2019-06-04 11:52:37 +02:00
|
|
|
SELECT(Int(1)).
|
2019-05-29 14:03:38 +02:00
|
|
|
WHERE(table1Col1.EQ(table2Col3)),
|
2019-06-04 11:52:37 +02:00
|
|
|
),
|
2019-05-12 18:15:23 +02:00
|
|
|
`EXISTS (
|
|
|
|
|
SELECT $1
|
|
|
|
|
FROM db.table2
|
|
|
|
|
WHERE table1.col1 = table2.col3
|
2019-06-04 11:52:37 +02:00
|
|
|
)`, int64(1))
|
2019-05-05 18:03:30 +02:00
|
|
|
}
|
2019-06-07 14:23:14 +02:00
|
|
|
|
|
|
|
|
func TestBoolExp(t *testing.T) {
|
|
|
|
|
assertClauseSerialize(t, BoolExp(String("true")), "$1", "true")
|
|
|
|
|
assertClauseSerialize(t, BoolExp(String("true")).IS_TRUE(), "$1 IS TRUE", "true")
|
|
|
|
|
}
|