2019-08-11 12:13:59 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
2020-02-11 10:25:13 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-08-11 12:13:59 +02:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-08-12 12:27:33 +02:00
|
|
|
func TestInvalidInsert(t *testing.T) {
|
2019-08-13 13:57:26 +02:00
|
|
|
assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: VALUES or QUERY has to be specified for INSERT statement")
|
2019-08-12 12:27:33 +02:00
|
|
|
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
|
|
|
|
}
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
func TestInsertNilValue(t *testing.T) {
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1)
|
|
|
|
|
VALUES (?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertSingleValue(t *testing.T) {
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(1), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1)
|
|
|
|
|
VALUES (?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`, int(1))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertWithColumnList(t *testing.T) {
|
2019-09-26 12:31:03 +02:00
|
|
|
columnList := ColumnList{table3ColInt}
|
|
|
|
|
|
|
|
|
|
columnList = append(columnList, table3StrCol)
|
2019-08-11 12:13:59 +02:00
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table3.INSERT(columnList).VALUES(1, 3), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table3 (col_int, col2)
|
|
|
|
|
VALUES (?, ?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`, 1, 3)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertDate(t *testing.T) {
|
|
|
|
|
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1ColTimestamp).VALUES(date), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col_timestamp)
|
|
|
|
|
VALUES (?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`, date)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleValues(t *testing.T) {
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1Col1, table1ColFloat, table1Col3).VALUES(1, 2, 3), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float, col3)
|
|
|
|
|
VALUES (?, ?, ?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`, 1, 2, 3)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleRows(t *testing.T) {
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
VALUES(1, 2).
|
|
|
|
|
VALUES(11, 22).
|
|
|
|
|
VALUES(111, 222)
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, stmt, `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (?, ?),
|
|
|
|
|
(?, ?),
|
|
|
|
|
(?, ?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`, 1, 2, 11, 22, 111, 222)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertValuesFromModel(t *testing.T) {
|
|
|
|
|
type Table1Model struct {
|
|
|
|
|
Col1 *int
|
|
|
|
|
ColFloat float64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
one := 1
|
|
|
|
|
|
|
|
|
|
toInsert := Table1Model{
|
|
|
|
|
Col1: &one,
|
|
|
|
|
ColFloat: 1.11,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
MODEL(toInsert).
|
|
|
|
|
MODEL(&toInsert)
|
|
|
|
|
|
|
|
|
|
expectedSQL := `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (?, ?),
|
|
|
|
|
(?, ?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, stmt, expectedSQL, int(1), float64(1.11), int(1), float64(1.11))
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertValuesFromModelColumnMismatch(t *testing.T) {
|
|
|
|
|
defer func() {
|
|
|
|
|
r := recover()
|
|
|
|
|
assert.Equal(t, r, "missing struct field for column : col1")
|
|
|
|
|
}()
|
|
|
|
|
type Table1Model struct {
|
|
|
|
|
Col1Prim int
|
|
|
|
|
Col2 string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newData := Table1Model{
|
|
|
|
|
Col1Prim: 1,
|
|
|
|
|
Col2: "one",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table1.
|
|
|
|
|
INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
MODEL(newData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertFromNonStructModel(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
r := recover()
|
2019-08-13 13:57:26 +02:00
|
|
|
assert.Equal(t, r, "jet: data has to be a struct")
|
2019-08-11 12:13:59 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
table2.INSERT(table2ColInt).MODEL([]int{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertDefaultValue(t *testing.T) {
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
VALUES(DEFAULT, "two")
|
|
|
|
|
|
|
|
|
|
var expectedSQL = `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (DEFAULT, ?);
|
2019-08-11 12:13:59 +02:00
|
|
|
`
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, stmt, expectedSQL, "two")
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
2020-05-03 20:46:21 +02:00
|
|
|
|
|
|
|
|
func TestInsertOnDuplicateKeyUpdate(t *testing.T) {
|
|
|
|
|
stmt := func() InsertStatement {
|
|
|
|
|
return table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
VALUES(DEFAULT, "two")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Run("empty list", func(t *testing.T) {
|
|
|
|
|
stmt := stmt().ON_DUPLICATE_KEY_UPDATE()
|
|
|
|
|
assertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (DEFAULT, ?);
|
|
|
|
|
`, "two")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("one set", func(t *testing.T) {
|
|
|
|
|
stmt := stmt().ON_DUPLICATE_KEY_UPDATE(table1ColFloat.SET(Float(11.1)))
|
|
|
|
|
assertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (DEFAULT, ?)
|
|
|
|
|
ON DUPLICATE KEY UPDATE col_float = ?;
|
|
|
|
|
`, "two", 11.1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("all types set", func(t *testing.T) {
|
|
|
|
|
stmt := stmt().ON_DUPLICATE_KEY_UPDATE(
|
|
|
|
|
table1ColBool.SET(Bool(true)),
|
|
|
|
|
table1ColInt.SET(Int(11)),
|
|
|
|
|
table1ColFloat.SET(Float(11.1)),
|
|
|
|
|
table1ColString.SET(String("str")),
|
|
|
|
|
table1ColTime.SET(Time(11, 23, 11)),
|
|
|
|
|
table1ColTimestamp.SET(Timestamp(2020, 1, 22, 3, 4, 5)),
|
|
|
|
|
table1ColDate.SET(Date(2020, 12, 1)),
|
|
|
|
|
)
|
|
|
|
|
assertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (DEFAULT, ?)
|
|
|
|
|
ON DUPLICATE KEY UPDATE col_bool = ?,
|
|
|
|
|
col_int = ?,
|
|
|
|
|
col_float = ?,
|
|
|
|
|
col_string = ?,
|
|
|
|
|
col_time = CAST(? AS TIME),
|
|
|
|
|
col_timestamp = TIMESTAMP(?),
|
|
|
|
|
col_date = CAST(? AS DATE);
|
|
|
|
|
`, "two", true, int64(11), 11.1, "str", "11:23:11", "2020-01-22 03:04:05", "2020-12-01")
|
|
|
|
|
})
|
|
|
|
|
}
|