-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathinsert_test.go
76 lines (67 loc) · 2.22 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
package dbr
import (
"testing"
"github.com/mailru/dbr/dialect"
"github.com/stretchr/testify/assert"
)
type insertTest struct {
v string
A int
C string `db:"b"`
}
func TestInsertStmt(t *testing.T) {
buf := NewBuffer()
builder := InsertInto("table").Columns("a", "b").Values(1, "one").Record(&insertTest{
A: 2,
C: "two",
})
err := builder.Build(dialect.MySQL, buf)
assert.NoError(t, err)
assert.Equal(t, "INSERT INTO `table` (`a`,`b`) VALUES (?,?), (?,?)", buf.String())
assert.Equal(t, []interface{}{1, "one", 2, "two"}, buf.Value())
}
func TestInsertRecordNoColumns(t *testing.T) {
buf := NewBuffer()
builder := InsertInto("table").Record(&insertTest{
A: 2,
C: "two",
}).Values(1, "one")
err := builder.Build(dialect.MySQL, buf)
assert.NoError(t, err)
assert.Equal(t, "INSERT INTO `table` (`a`,`b`) VALUES (?,?), (?,?)", buf.String())
assert.Equal(t, []interface{}{2, "two", 1, "one"}, buf.Value())
}
func TestInsertOnConflictStmt(t *testing.T) {
buf := NewBuffer()
exp := Expr("a + ?", 1)
builder := InsertInto("table").Columns("a", "b").Values(1, "one")
builder.OnConflict("").Action("a", exp).Action("b", "one")
err := builder.Build(dialect.MySQL, buf)
assert.NoError(t, err)
assert.Equal(t, "INSERT INTO `table` (`a`,`b`) VALUES (?,?) ON DUPLICATE KEY UPDATE `a`=?,`b`=?", buf.String())
assert.Equal(t, []interface{}{1, "one", exp, "one"}, buf.Value())
}
func TestInsertOnConflictMapStmt(t *testing.T) {
buf := NewBuffer()
exp := Expr("a + ?", 1)
builder := InsertInto("table").Columns("a", "b").Values(1, "one")
err := builder.OnConflictMap("", map[string]interface{}{"a": exp, "b": "one"}).Build(dialect.MySQL, buf)
assert.NoError(t, err)
assert.Equal(t, "INSERT INTO `table` (`a`,`b`) VALUES (?,?) ON DUPLICATE KEY UPDATE `a`=?,`b`=?", buf.String())
assert.Equal(t, []interface{}{1, "one", exp, "one"}, buf.Value())
}
func BenchmarkInsertValuesSQL(b *testing.B) {
buf := NewBuffer()
for i := 0; i < b.N; i++ {
InsertInto("table").Columns("a", "b").Values(1, "one").Build(dialect.MySQL, buf)
}
}
func BenchmarkInsertRecordSQL(b *testing.B) {
buf := NewBuffer()
for i := 0; i < b.N; i++ {
InsertInto("table").Columns("a", "b").Record(&insertTest{
A: 2,
C: "two",
}).Build(dialect.MySQL, buf)
}
}