2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-16 11:20:44 +02:00
|
|
|
func TestInvalidInsert(t *testing.T) {
|
2019-07-08 13:00:44 +02:00
|
|
|
assertStatementErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
|
|
|
|
assertStatementErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertNilValue(t *testing.T) {
|
2019-06-16 11:20:44 +02:00
|
|
|
assertStatement(t, table1.INSERT(table1Col1).VALUES(nil), `
|
2019-05-12 18:15:23 +02:00
|
|
|
INSERT INTO db.table1 (col1) VALUES
|
|
|
|
|
($1);
|
2019-06-16 11:20:44 +02:00
|
|
|
`, nil)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertSingleValue(t *testing.T) {
|
2019-06-16 11:20:44 +02:00
|
|
|
assertStatement(t, table1.INSERT(table1Col1).VALUES(1), `
|
2019-05-12 18:15:23 +02:00
|
|
|
INSERT INTO db.table1 (col1) VALUES
|
|
|
|
|
($1);
|
2019-06-16 11:20:44 +02:00
|
|
|
`, int(1))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertWithColumnList(t *testing.T) {
|
|
|
|
|
columnList := ColumnList{table3ColInt, table3StrCol}
|
|
|
|
|
|
|
|
|
|
assertStatement(t, table3.INSERT(columnList).VALUES(1, 3), `
|
2019-06-17 12:05:52 +02:00
|
|
|
INSERT INTO db.table3 (col_int, col2) VALUES
|
2019-06-16 11:20:44 +02:00
|
|
|
($1, $2);
|
|
|
|
|
`, 1, 3)
|
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-06-16 11:20:44 +02:00
|
|
|
assertStatement(t, table1.INSERT(table1ColTime).VALUES(date), `
|
2019-06-17 12:05:52 +02:00
|
|
|
INSERT INTO db.table1 (col_time) VALUES
|
2019-05-12 18:15:23 +02:00
|
|
|
($1);
|
2019-06-16 11:20:44 +02:00
|
|
|
`, date)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleValues(t *testing.T) {
|
2019-06-16 11:20:44 +02:00
|
|
|
assertStatement(t, table1.INSERT(table1Col1, table1ColFloat, table1Col3).VALUES(1, 2, 3), `
|
2019-06-17 12:05:52 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float, col3) VALUES
|
2019-05-12 18:15:23 +02:00
|
|
|
($1, $2, $3);
|
2019-06-16 11:20:44 +02:00
|
|
|
`, 1, 2, 3)
|
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-06-16 11:20:44 +02:00
|
|
|
assertStatement(t, stmt, `
|
2019-06-17 12:05:52 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float) VALUES
|
2019-05-12 18:15:23 +02:00
|
|
|
($1, $2),
|
|
|
|
|
($3, $4),
|
|
|
|
|
($5, $6);
|
2019-06-16 11:20:44 +02:00
|
|
|
`, 1, 2, 11, 22, 111, 222)
|
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-30 11:53:35 +02:00
|
|
|
MODEL(toInsert).
|
|
|
|
|
MODEL(&toInsert)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
expectedSql := `
|
2019-06-17 12:05:52 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float) 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) {
|
2019-06-15 13:58:45 +02:00
|
|
|
defer func() {
|
|
|
|
|
r := recover()
|
|
|
|
|
assert.Equal(t, r, "missing struct field for column : col1")
|
|
|
|
|
}()
|
2019-04-07 09:58:12 +02:00
|
|
|
type Table1Model struct {
|
|
|
|
|
Col1Prim int
|
|
|
|
|
Col2 string
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
newData := Table1Model{
|
2019-04-07 09:58:12 +02:00
|
|
|
Col1Prim: 1,
|
|
|
|
|
Col2: "one",
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 11:20:44 +02:00
|
|
|
table1.
|
2019-06-15 13:58:45 +02:00
|
|
|
INSERT(table1Col1, table1ColFloat).
|
2019-06-30 11:53:35 +02:00
|
|
|
MODEL(newData)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func TestInsertFromNonStructModel(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
r := recover()
|
|
|
|
|
assert.Equal(t, r, "argument mismatch: expected struct, got []int")
|
|
|
|
|
}()
|
|
|
|
|
|
2019-06-30 11:53:35 +02:00
|
|
|
table2.INSERT(table2ColInt).MODEL([]int{})
|
2019-06-15 13:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
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 = `
|
2019-06-17 12:05:52 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float) VALUES
|
2019-06-14 14:35:50 +02:00
|
|
|
(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
|
|
|
}
|