-
Notifications
You must be signed in to change notification settings - Fork 28
/
insert_test.go
150 lines (127 loc) · 3.52 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package sqlingo
import (
"context"
"errors"
"testing"
)
type tTest struct {
Table
F1 fTestF1
F2 fTestF2
}
func (t tTest) GetFields() []Field {
return []Field{t.F1, t.F2}
}
type fTestF1 struct{ NumberField }
type fTestF2 struct{ StringField }
type TestModel struct {
F1 int64
F2 string
}
var tTestTable = NewTable("test")
var Test = tTest{
Table: NewTable("test"),
F1: fTestF1{NewNumberField(tTestTable, "f1")},
F2: fTestF2{NewStringField(tTestTable, "f2")},
}
func (m TestModel) GetTable() Table {
return Test
}
func (m TestModel) GetValues() []interface{} {
return []interface{}{m.F1, m.F2}
}
func TestInsert(t *testing.T) {
db := newMockDatabase()
if _, err := db.InsertInto(Table1).Fields(field1).
Values(1).
Values(2).
OnDuplicateKeyUpdate().Set(field1, 10).
Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `table1` (`field1`)"+
" VALUES (1), (2)"+
" ON DUPLICATE KEY UPDATE `field1` = 10")
if _, err := db.InsertInto(Table1).Fields(field1).
Values(1).
OnDuplicateKeyUpdate().
SetIf(false, field1, 2).
Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `table1` (`field1`)"+
" VALUES (1)")
if _, err := db.InsertInto(Table1).Fields(field1).
Values(0).
OnDuplicateKeyUpdate().
SetIf(true, field1, 1).
Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `table1` (`field1`)"+
" VALUES (0)"+
" ON DUPLICATE KEY UPDATE `field1` = 1")
if _, err := db.InsertInto(Table1).
Fields(field1, field2).
Values(1, 2).
OnDuplicateKeyUpdate().
SetIf(false, field1, 10).
SetIf(true, field2, 20).
Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `table1` (`field1`, `field2`)"+
" VALUES (1, 2)"+
" ON DUPLICATE KEY UPDATE `field2` = 20")
if _, err := db.InsertInto(Table1).Fields(field1).
Values(1).
Values(2).
OnDuplicateKeyIgnore().
Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `table1` (`field1`)"+
" VALUES (1), (2)"+
" ON DUPLICATE KEY UPDATE `field1` = `field1`")
model := &TestModel{
F1: 1,
F2: "test",
}
if _, err := db.InsertInto(Test).Values(1, 2).Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `test` (`f1`, `f2`) VALUES (1, 2)")
if _, err := db.InsertInto(Test).Models(model, &model, []Model{model}).Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `test` (`f1`, `f2`) VALUES (1, 'test'), (1, 'test'), (1, 'test')")
if _, err := db.InsertInto(Test).Models(model, &model, []interface{}{model, "invalid type"}).Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.InsertInto(Table1).Models(model).Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.ReplaceInto(Test).Values(1, 2).Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "REPLACE INTO `test` (`f1`, `f2`) VALUES (1, 2)")
errExpr := expression{
builder: func(scope scope) (string, error) {
return "", errors.New("error")
},
}
if _, err := db.InsertInto(Test).Fields(errExpr).Values(1).Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.InsertInto(Test).Fields(Test.F1).Values(errExpr).Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.InsertInto(Test).
Fields(Test.F1).Values(1).
OnDuplicateKeyUpdate().Set(Test.F1, errExpr).Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.InsertInto(Test).Fields(Test.F1).Values(1).WithContext(context.Background()).Execute(); err != nil {
t.Error(err)
}
}