Fix integration tests.
This commit is contained in:
parent
eccc17dc8a
commit
5c05214ba1
5 changed files with 102 additions and 18 deletions
|
|
@ -60,6 +60,20 @@ func newBinaryExpression(lhs, rhs Expression, operator []byte, parent ...Express
|
|||
return binaryExpression
|
||||
}
|
||||
|
||||
func isSimpleOperand(expression Expression) bool {
|
||||
if _, ok := expression.(*literalExpression); ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := expression.(Column); ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := expression.(FuncExpression); ok {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *binaryExpression) Serialize(out *queryData, options ...serializeOption) error {
|
||||
if c.lhs == nil {
|
||||
return errors.Newf("nil lhs.")
|
||||
|
|
@ -68,10 +82,9 @@ func (c *binaryExpression) Serialize(out *queryData, options ...serializeOption)
|
|||
return errors.Newf("nil rhs.")
|
||||
}
|
||||
|
||||
_, literalLeft := c.lhs.(*literalExpression)
|
||||
_, literalRight := c.rhs.(*literalExpression)
|
||||
wrap := !isSimpleOperand(c.lhs) && !isSimpleOperand(c.rhs)
|
||||
|
||||
if !literalLeft && !literalRight {
|
||||
if wrap {
|
||||
out.WriteString("(")
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +98,7 @@ func (c *binaryExpression) Serialize(out *queryData, options ...serializeOption)
|
|||
return err
|
||||
}
|
||||
|
||||
if !literalLeft && !literalRight {
|
||||
if wrap {
|
||||
out.WriteString(")")
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -81,8 +81,8 @@ func (us *setStatementImpl) OFFSET(offset int64) SetStatement {
|
|||
}
|
||||
|
||||
func (us *setStatementImpl) Serialize(out *queryData, options ...serializeOption) error {
|
||||
if len(us.selects) == 0 {
|
||||
return errors.Newf("UNION statement must have at least one SELECT")
|
||||
if len(us.selects) < 2 {
|
||||
return errors.Newf("UNION statement must have at least two SELECT statements.")
|
||||
}
|
||||
|
||||
out.WriteString("(")
|
||||
|
|
|
|||
|
|
@ -1 +1,71 @@
|
|||
package sqlbuilder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gotest.tools/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnionNoSelect(t *testing.T) {
|
||||
query, args, err := UNION().Sql()
|
||||
|
||||
assert.Assert(t, err != nil)
|
||||
//fmt.Println(err.Error())
|
||||
fmt.Print(query, args)
|
||||
}
|
||||
|
||||
func TestUnionOneSelect(t *testing.T) {
|
||||
query, args, err := UNION(
|
||||
table1.SELECT(table1Col1),
|
||||
).Sql()
|
||||
|
||||
assert.Assert(t, err != nil)
|
||||
fmt.Println(err.Error())
|
||||
fmt.Println(query)
|
||||
fmt.Println(args)
|
||||
}
|
||||
|
||||
func TestUnionTwoSelect(t *testing.T) {
|
||||
query, args, err := UNION(
|
||||
table1.SELECT(table1Col1),
|
||||
table2.SELECT(table2Col3),
|
||||
).Sql()
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, query, `((SELECT table1.col1 AS "table1.col1" FROM db.table1) UNION (SELECT table2.col3 AS "table2.col3" FROM db.table2))`)
|
||||
assert.Equal(t, len(args), 0)
|
||||
}
|
||||
|
||||
func TestUnionThreeSelect(t *testing.T) {
|
||||
query, args, err := UNION(
|
||||
table1.SELECT(table1Col1),
|
||||
table2.SELECT(table2Col3),
|
||||
table3.SELECT(table3Col1),
|
||||
).Sql()
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, query, `((SELECT table1.col1 AS "table1.col1" FROM db.table1) UNION (SELECT table2.col3 AS "table2.col3" FROM db.table2) UNION (SELECT table3.col1 AS "table3.col1" FROM db.table3))`)
|
||||
assert.Equal(t, len(args), 0)
|
||||
}
|
||||
|
||||
func TestUnionWithOrderBy(t *testing.T) {
|
||||
query, args, err := UNION(
|
||||
table1.SELECT(table1Col1),
|
||||
table2.SELECT(table2Col3),
|
||||
).ORDER_BY(table1Col1.Asc()).Sql()
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, query, `((SELECT table1.col1 AS "table1.col1" FROM db.table1) UNION (SELECT table2.col3 AS "table2.col3" FROM db.table2)) ORDER BY table1.col1 ASC`)
|
||||
assert.Equal(t, len(args), 0)
|
||||
}
|
||||
|
||||
func TestUnionWithLimit(t *testing.T) {
|
||||
query, args, err := UNION(
|
||||
table1.SELECT(table1Col1),
|
||||
table2.SELECT(table2Col3),
|
||||
).LIMIT(10).OFFSET(11).Sql()
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, query, `((SELECT table1.col1 AS "table1.col1" FROM db.table1) UNION (SELECT table2.col3 AS "table2.col3" FROM db.table2)) LIMIT $1 OFFSET $2`)
|
||||
assert.Equal(t, len(args), 2)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/sub0zero/go-sqlbuilder/generator"
|
||||
"gotest.tools/assert"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
|
@ -75,3 +77,14 @@ CREATE TABLE IF NOT EXISTS test_sample.link (
|
|||
fmt.Println(result)
|
||||
|
||||
}
|
||||
|
||||
func TestGenerateModel(t *testing.T) {
|
||||
|
||||
err := generator.Generate(folderPath, connectString, dbname, schemaName)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
//err = generator.Generate(folderPath, connectString, dbname, "sport")
|
||||
//
|
||||
//assert.NilError(t, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package tests
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sub0zero/go-sqlbuilder/generator"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder"
|
||||
"github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/model"
|
||||
. "github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/table"
|
||||
|
|
@ -12,17 +11,6 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func TestGenerateModel(t *testing.T) {
|
||||
|
||||
err := generator.Generate(folderPath, connectString, dbname, schemaName)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
//err = generator.Generate(folderPath, connectString, dbname, "sport")
|
||||
//
|
||||
//assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func TestSelect_ScanToStruct(t *testing.T) {
|
||||
actor := model.Actor{}
|
||||
query := Actor.SELECT(Actor.AllColumns).ORDER_BY(Actor.ActorID.Asc())
|
||||
Loading…
Add table
Add a link
Reference in a new issue