forked from deoxxa/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_test.go
89 lines (85 loc) · 2.65 KB
/
insert_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package sqlbuilder
import (
"testing"
"time"
)
func TestInsert(t *testing.T) {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
StringColumn("str", &ColumnOption{
Size: 255,
}),
BoolColumn("bool", nil),
FloatColumn("float", nil),
DateColumn("date", nil),
BytesColumn("bytes", nil),
)
table2 := NewTable(
"TABLE_B",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
)
tableJoined := table1.InnerJoin(table2, table1.C("test1").Eq(table2.C("id")))
var cases = []statementTestCase{{
stmt: Insert(table1).
Columns(table1.C("str"), table1.C("bool"), table1.C("float"), table1.C("date"), table1.C("bytes")).
Values("hoge", true, 0.1, time.Unix(0, 0).UTC(), []byte{0x01}),
query: `INSERT INTO "TABLE_A" ( "str", "bool", "float", "date", "bytes" ) VALUES ( ?, ?, ?, ?, ? );`,
args: []interface{}{"hoge", true, 0.1, time.Unix(0, 0).UTC(), []byte{0x01}},
errmsg: "",
}, {
stmt: Insert(table1).
Set(table1.C("str"), "hoge").
Set(table1.C("bool"), true).
Set(table1.C("float"), 0.1).
Set(table1.C("date"), time.Unix(0, 0).UTC()).
Set(table1.C("bytes"), []byte{0x01}),
query: `INSERT INTO "TABLE_A" ( "str", "bool", "float", "date", "bytes" ) VALUES ( ?, ?, ?, ?, ? );`,
args: []interface{}{"hoge", true, 0.1, time.Unix(0, 0).UTC(), []byte{0x01}},
errmsg: "",
}, {
stmt: Insert(table1).
Set(table1.C("str"), "x").
Returning(table1.C("id")),
query: `INSERT INTO "TABLE_A" ( "str" ) VALUES ( ? ) RETURNING "TABLE_A"."id";`,
args: []interface{}{"x"},
errmsg: "",
}, {
stmt: Insert(table1).Values(1, "hoge", true, 0.1, time.Unix(0, 0).UTC(), []byte{0x01}),
query: `INSERT INTO "TABLE_A" ( "id", "str", "bool", "float", "date", "bytes" ) VALUES ( ?, ?, ?, ?, ?, ? );`,
args: []interface{}{int64(1), "hoge", true, 0.1, time.Unix(0, 0).UTC(), []byte{0x01}},
errmsg: "",
}, {
stmt: Insert(table1).Columns(table1.C("id")).Values(1, 2, 3),
query: "",
args: []interface{}{},
errmsg: "sqlbuilder: 1 values needed, but got 3.",
}, {
stmt: Insert(nil).Columns(table1.C("id")).Values(1),
query: "",
args: []interface{}{},
errmsg: "sqlbuilder: table is nil.",
}, {
stmt: Insert(table1).Columns(table1.C("str")).Values(1),
query: "",
args: []interface{}{},
errmsg: "sqlbuilder: string column not accept int.",
}, {
stmt: Insert(tableJoined).Columns(table1.C("str")).Values(1),
query: "",
args: []interface{}{},
errmsg: "sqlbuilder: table is not natural table.",
}}
for num, c := range cases {
mes, args, ok := c.Run()
if !ok {
t.Errorf(mes+" (case no.%d)", append(args, num)...)
}
}
}