-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsert_test.go
More file actions
62 lines (54 loc) · 1.74 KB
/
insert_test.go
File metadata and controls
62 lines (54 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sqlparser_test
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/viant/sqlparser"
"testing"
)
func TestParseInsert(t *testing.T) {
var testCases = []struct {
description string
SQL string
expect string
}{
{
description: "basic insert",
SQL: "INSERT INTO t (c1, c2) VALUES(1, 2)",
expect: "INSERT INTO t (c1, c2) VALUES(1, 2)",
},
{
description: "basic insert",
SQL: "INSERT INTO CI_AD_ORDER(ID, NAME) VALUES(0, $Name);",
expect: "INSERT INTO CI_AD_ORDER (ID, NAME) VALUES(0, $Name)",
},
{
description: "batch insert insert",
SQL: "INSERT INTO CI_AD_ORDER(ID, NAME) VALUES(?, ?), (?, ?);",
expect: "INSERT INTO CI_AD_ORDER (ID, NAME) VALUES(?, ?), (?, ?)",
},
{
description: "batch insert insert on duplicate key update",
SQL: "INSERT INTO CI_AD_ORDER(ID, VAL, SVAL) VALUES(?, ?, ?), (?, ?, ?) AS new ON DUPLICATE KEY UPDATE VAL = VAL + new.VAL, SVAL = SAVL + new.VAL",
expect: "INSERT INTO CI_AD_ORDER (ID, VAL, SVAL) VALUES(?, ?, ?), (?, ?, ?) AS new ON DUPLICATE KEY UPDATE VAL = VAL + new.VAL, SVAL = SAVL + new.VAL",
},
{
description: "",
SQL: `INSERT INTO table1[id=?].tt(ID, VAL, SVAL) VALUES(?, ?, ?) `,
expect: `INSERT INTO table1[id=?].tt (ID, VAL, SVAL) VALUES(?, ?, ?)`,
},
}
//for _, testCase := range testCases[len(testCases)-1:] {
for _, testCase := range testCases {
update, err := sqlparser.ParseInsert(testCase.SQL)
if !assert.Nil(t, err) {
fmt.Printf("%v\n", testCase.SQL)
continue
}
actual := sqlparser.Stringify(update)
if !assert.EqualValues(t, testCase.expect, actual) {
data, _ := json.Marshal(update)
fmt.Printf("%s\n", data)
}
}
}