jet/sqlbuilder/update_statement_test.go

125 lines
2.9 KiB
Go
Raw Normal View History

2019-04-14 17:55:10 +02:00
package sqlbuilder
import (
"fmt"
"gotest.tools/assert"
"testing"
)
//
2019-05-12 18:15:23 +02:00
// UPDATE Statement tests =====================================================
2019-04-14 17:55:10 +02:00
//
func TestUpdate(t *testing.T) {
stmt := table1.UPDATE(table1Col1, table1Col2).
2019-05-12 18:15:23 +02:00
SET(table1.SELECT(table1Col2, table2Col3)).
WHERE(table1Col1.EqL(2)).
RETURNING(table1Col1)
2019-04-14 17:55:10 +02:00
2019-05-01 17:25:10 +02:00
stmtStr, _, err := stmt.Sql()
2019-04-14 17:55:10 +02:00
assert.NilError(t, err)
fmt.Println(stmtStr)
2019-05-12 18:15:23 +02:00
assert.Equal(t, stmtStr, `
UPDATE db.table1 SET (col1,col2) = (
SELECT table1.col2 AS "table1.col2",
table2.col3 AS "table2.col3"
FROM db.table1
)
WHERE table1.col1 = $1
RETURNING table1.col1 AS "table1.col1";
`)
2019-04-14 17:55:10 +02:00
}
//func (s *StmtSuite) TestUpdateNilColumn(c *gc.C) {
// stmt := table1.UPDATE().SET(nil, Literal(1))
// _, err := stmt.String()
// c.Assert(err, gc.NotNil)
//}
//
//func (s *StmtSuite) TestUpdateNilExpr(c *gc.C) {
// stmt := table1.UPDATE().SET(table1Col1, nil)
// _, err := stmt.String()
// c.Assert(err, gc.NotNil)
//}
//
//func (s *StmtSuite) TestUpdateUnconditionally(c *gc.C) {
// stmt := table1.UPDATE().SET(table1Col1, Literal(1))
// _, err := stmt.String()
// c.Assert(err, gc.NotNil)
//}
//
//func (s *StmtSuite) TestUpdateSingleValue(c *gc.C) {
// stmt := table1.UPDATE().SET(table1Col1, Literal(1))
2019-05-06 12:42:15 +02:00
// stmt.WHERE(EqString(table1Col2, 2))
2019-04-14 17:55:10 +02:00
// sql, err := stmt.String()
// c.Assert(err, gc.IsNil)
//
// c.Assert(
// sql,
// gc.Equals,
// "UPDATE db.table1 SET table1.col1=1 WHERE table1.col2=2")
//}
//
//func (s *StmtSuite) TestUpdateUsingDeferredLookupColumns(c *gc.C) {
// stmt := table1.UPDATE().SET(table1.C("col1"), Literal(1))
2019-05-06 12:42:15 +02:00
// stmt.WHERE(EqString(table1Col2, 2))
2019-04-14 17:55:10 +02:00
// sql, err := stmt.String()
// c.Assert(err, gc.IsNil)
//
// c.Assert(
// sql,
// gc.Equals,
// "UPDATE db.table1 SET table1.col1=1 WHERE table1.col2=2")
//}
//
//func (s *StmtSuite) TestUpdateMultiValues(c *gc.C) {
// stmt := table1.UPDATE()
// stmt.SET(table1Col1, Literal(1))
// stmt.SET(table1Col2, Literal(2))
2019-05-06 12:42:15 +02:00
// stmt.WHERE(EqString(table1Col2, 3))
2019-04-14 17:55:10 +02:00
// sql, err := stmt.String()
// c.Assert(err, gc.IsNil)
//
// c.Assert(
// sql,
// gc.Equals,
// "UPDATE db.table1 "+
// "SET table1.col1=1, table1.col2=2 "+
// "WHERE table1.col2=3")
//}
//
//func (s *StmtSuite) TestUpdateWithOrderBy(c *gc.C) {
// stmt := table1.UPDATE().SET(table1Col1, Literal(1))
2019-05-06 12:42:15 +02:00
// stmt.WHERE(EqString(table1Col2, 2))
// stmt.ORDER_BY(table1Col2)
2019-04-14 17:55:10 +02:00
// sql, err := stmt.String()
// c.Assert(err, gc.IsNil)
//
// c.Assert(
// sql,
// gc.Equals,
// "UPDATE db.table1 "+
// "SET table1.col1=1 "+
// "WHERE table1.col2=2 "+
// "ORDER BY table1.col2")
//}
//
//func (s *StmtSuite) TestUpdateWithLimit(c *gc.C) {
// stmt := table1.UPDATE().SET(table1Col1, Literal(1))
2019-05-06 12:42:15 +02:00
// stmt.WHERE(EqString(table1Col2, 2))
// stmt.LIMIT(5)
2019-04-14 17:55:10 +02:00
// sql, err := stmt.String()
// c.Assert(err, gc.IsNil)
//
// c.Assert(
// sql,
// gc.Equals,
// "UPDATE db.table1 "+
// "SET table1.col1=1 "+
// "WHERE table1.col2=2 "+
// "LIMIT 5")
//}