2019-03-02 12:34:08 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-05 17:15:20 +02:00
|
|
|
"testing"
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func TestJoinNilInputs(t *testing.T) {
|
|
|
|
|
assertClauseSerializeErr(t, table2.INNER_JOIN(nil, table1ColBool.EQ(table2ColBool)),
|
|
|
|
|
"right hand side of join operation is nil table")
|
|
|
|
|
assertClauseSerializeErr(t, table2.INNER_JOIN(table1, nil),
|
|
|
|
|
"join condition is nil")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func TestINNER_JOIN(t *testing.T) {
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
INNER JOIN db.table2 ON (table1.colInt = table2.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
|
|
|
|
INNER_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
INNER JOIN db.table2 ON (table1.colInt = table2.colInt)
|
|
|
|
|
INNER JOIN db.table3 ON (table1.colInt = table3.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
INNER_JOIN(table2, table1ColInt.EQ(Int(1))).
|
|
|
|
|
INNER_JOIN(table3, table1ColInt.EQ(Int(2))),
|
|
|
|
|
`db.table1
|
|
|
|
|
INNER JOIN db.table2 ON (table1.colInt = $1)
|
|
|
|
|
INNER JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2))
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func TestLEFT_JOIN(t *testing.T) {
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
LEFT JOIN db.table2 ON (table1.colInt = table2.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
|
|
|
|
LEFT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
LEFT JOIN db.table2 ON (table1.colInt = table2.colInt)
|
|
|
|
|
LEFT JOIN db.table3 ON (table1.colInt = table3.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
LEFT_JOIN(table2, table1ColInt.EQ(Int(1))).
|
|
|
|
|
LEFT_JOIN(table3, table1ColInt.EQ(Int(2))),
|
|
|
|
|
`db.table1
|
|
|
|
|
LEFT JOIN db.table2 ON (table1.colInt = $1)
|
|
|
|
|
LEFT JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2))
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func TestRIGHT_JOIN(t *testing.T) {
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
RIGHT JOIN db.table2 ON (table1.colInt = table2.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
|
|
|
|
RIGHT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
RIGHT JOIN db.table2 ON (table1.colInt = table2.colInt)
|
|
|
|
|
RIGHT JOIN db.table3 ON (table1.colInt = table3.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
RIGHT_JOIN(table2, table1ColInt.EQ(Int(1))).
|
|
|
|
|
RIGHT_JOIN(table3, table1ColInt.EQ(Int(2))),
|
|
|
|
|
`db.table1
|
|
|
|
|
RIGHT JOIN db.table2 ON (table1.colInt = $1)
|
|
|
|
|
RIGHT JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2))
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func TestFULL_JOIN(t *testing.T) {
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
FULL JOIN db.table2 ON (table1.colInt = table2.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
|
|
|
|
FULL_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
|
|
|
|
`db.table1
|
|
|
|
|
FULL JOIN db.table2 ON (table1.colInt = table2.colInt)
|
|
|
|
|
FULL JOIN db.table3 ON (table1.colInt = table3.colInt)`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
FULL_JOIN(table2, table1ColInt.EQ(Int(1))).
|
|
|
|
|
FULL_JOIN(table3, table1ColInt.EQ(Int(2))),
|
|
|
|
|
`db.table1
|
|
|
|
|
FULL JOIN db.table2 ON (table1.colInt = $1)
|
|
|
|
|
FULL JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2))
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func TestCROSS_JOIN(t *testing.T) {
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
CROSS_JOIN(table2),
|
|
|
|
|
`db.table1
|
|
|
|
|
CROSS JOIN db.table2`)
|
|
|
|
|
assertClauseSerialize(t, table1.
|
|
|
|
|
CROSS_JOIN(table2).
|
|
|
|
|
CROSS_JOIN(table3),
|
|
|
|
|
`db.table1
|
|
|
|
|
CROSS JOIN db.table2
|
|
|
|
|
CROSS JOIN db.table3`)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|