2019-04-07 09:58:12 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestInsertNoRow(t *testing.T) {
|
2019-05-01 17:25:10 +02:00
|
|
|
_, _, err := table1.INSERT(table1Col1).Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertColumnLengthMismatch(t *testing.T) {
|
2019-05-31 12:59:57 +02:00
|
|
|
_, _, err := table1.INSERT(table1Col1, table1ColFloat).VALUES(nil).Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-05-01 17:25:10 +02:00
|
|
|
//fmt.Println(err)
|
2019-04-07 09:58:12 +02:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertNilValue(t *testing.T) {
|
2019-05-01 17:25:10 +02:00
|
|
|
query, args, err := table1.INSERT(table1Col1).VALUES(nil).Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
assert.Equal(t, query, `
|
|
|
|
|
INSERT INTO db.table1 (col1) VALUES
|
|
|
|
|
($1);
|
|
|
|
|
`)
|
2019-05-01 17:25:10 +02:00
|
|
|
assert.Equal(t, len(args), 1)
|
|
|
|
|
assert.NilError(t, err)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertNilColumn(t *testing.T) {
|
2019-05-01 17:25:10 +02:00
|
|
|
_, _, err := table1.INSERT(nil).VALUES(1).Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertSingleValue(t *testing.T) {
|
2019-05-01 17:25:10 +02:00
|
|
|
sql, _, err := table1.INSERT(table1Col1).VALUES(1).Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
assert.Equal(t, sql, `
|
|
|
|
|
INSERT INTO db.table1 (col1) VALUES
|
|
|
|
|
($1);
|
|
|
|
|
`)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertDate(t *testing.T) {
|
|
|
|
|
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
|
|
|
|
|
|
2019-05-29 14:03:38 +02:00
|
|
|
sql, _, err := table1.INSERT(table1ColTime).VALUES(date).Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
assert.Equal(t, sql, `
|
2019-05-29 14:03:38 +02:00
|
|
|
INSERT INTO db.table1 (colTime) VALUES
|
2019-05-12 18:15:23 +02:00
|
|
|
($1);
|
|
|
|
|
`)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleValues(t *testing.T) {
|
2019-05-31 12:59:57 +02:00
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat, table1Col3)
|
2019-04-07 09:58:12 +02:00
|
|
|
stmt.VALUES(1, 2, 3)
|
|
|
|
|
|
2019-05-01 17:25:10 +02:00
|
|
|
sql, _, err := stmt.Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
2019-06-14 14:35:50 +02:00
|
|
|
INSERT INTO db.table1 (col1, colFloat, col3) VALUES
|
2019-05-12 18:15:23 +02:00
|
|
|
($1, $2, $3);
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, sql, expectedSql)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleRows(t *testing.T) {
|
2019-05-31 12:59:57 +02:00
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
2019-04-07 09:58:12 +02:00
|
|
|
VALUES(1, 2).
|
|
|
|
|
VALUES(11, 22).
|
|
|
|
|
VALUES(111, 222)
|
|
|
|
|
|
2019-05-01 17:25:10 +02:00
|
|
|
sql, _, err := stmt.Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
2019-06-14 14:35:50 +02:00
|
|
|
INSERT INTO db.table1 (col1, colFloat) VALUES
|
2019-05-12 18:15:23 +02:00
|
|
|
($1, $2),
|
|
|
|
|
($3, $4),
|
|
|
|
|
($5, $6);
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, sql, expectedSql)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertValuesFromModel(t *testing.T) {
|
|
|
|
|
type Table1Model struct {
|
2019-06-11 12:47:35 +02:00
|
|
|
Col1 *int
|
2019-05-31 12:59:57 +02:00
|
|
|
ColFloat float64
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
one := 1
|
|
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
toInsert := Table1Model{
|
2019-06-11 12:47:35 +02:00
|
|
|
Col1: &one,
|
2019-05-31 12:59:57 +02:00
|
|
|
ColFloat: 1.11,
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
2019-06-14 14:35:50 +02:00
|
|
|
USING(toInsert).
|
|
|
|
|
USING(&toInsert)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
expectedSql := `
|
2019-06-14 14:35:50 +02:00
|
|
|
INSERT INTO db.table1 (col1, colFloat) VALUES
|
2019-06-11 12:47:35 +02:00
|
|
|
($1, $2),
|
|
|
|
|
($3, $4);
|
|
|
|
|
`
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatement(t, stmt, expectedSql, int(1), float64(1.11), int(1), float64(1.11))
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertValuesFromModelColumnMismatch(t *testing.T) {
|
|
|
|
|
type Table1Model struct {
|
|
|
|
|
Col1Prim int
|
|
|
|
|
Col2 string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toInsert := Table1Model{
|
|
|
|
|
Col1Prim: 1,
|
|
|
|
|
Col2: "one",
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
2019-06-14 14:35:50 +02:00
|
|
|
USING(toInsert)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-05-01 17:25:10 +02:00
|
|
|
_, _, err := stmt.Sql()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
|
}
|
2019-04-07 16:54:06 +02:00
|
|
|
|
|
|
|
|
func TestInsertQuery(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
stmt := table1.INSERT(table1Col1).
|
|
|
|
|
QUERY(table1.SELECT(table1Col1))
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
var expectedSql = `
|
|
|
|
|
INSERT INTO db.table1 (col1) (
|
|
|
|
|
SELECT table1.col1 AS "table1.col1"
|
|
|
|
|
FROM db.table1
|
|
|
|
|
);
|
|
|
|
|
`
|
|
|
|
|
assertStatement(t, stmt, expectedSql)
|
2019-04-07 16:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertDefaultValue(t *testing.T) {
|
2019-05-31 12:59:57 +02:00
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
2019-04-07 16:54:06 +02:00
|
|
|
VALUES(DEFAULT, "two")
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
var expectedSql = `
|
|
|
|
|
INSERT INTO db.table1 (col1, colFloat) VALUES
|
|
|
|
|
(DEFAULT, $1);
|
|
|
|
|
`
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatement(t, stmt, expectedSql, "two")
|
2019-04-07 16:54:06 +02:00
|
|
|
}
|