forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_test.go
60 lines (52 loc) · 1.81 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
package dbr
import (
"testing"
"github.com/gocraft/dbr/v2/dialect"
"github.com/stretchr/testify/require"
)
type insertTest struct {
A int
C string `db:"b"`
}
func TestInsertStmt(t *testing.T) {
buf := NewBuffer()
builder := InsertInto("table").Ignore().Columns("a", "b").Values(1, "one").Record(&insertTest{
A: 2,
C: "two",
}).Comment("INSERT TEST")
err := builder.Build(dialect.MySQL, buf)
require.NoError(t, err)
require.Equal(t, "/* INSERT TEST */\nINSERT IGNORE INTO `table` (`a`,`b`) VALUES (?,?), (?,?)", buf.String())
require.Equal(t, []interface{}{1, "one", 2, "two"}, buf.Value())
}
func TestPostgresReturning(t *testing.T) {
sess := postgresSession
reset(t, sess)
var person dbrPerson
err := sess.InsertInto("dbr_people").Columns("name").Record(&person).
Returning("id").Load(&person.Id)
require.NoError(t, err)
require.True(t, person.Id > 0)
require.Len(t, sess.EventReceiver.(*testTraceReceiver).started, 1)
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].eventName, "dbr.select")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "INSERT")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "dbr_people")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "name")
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).finished)
require.Equal(t, 0, sess.EventReceiver.(*testTraceReceiver).errored)
}
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)
}
}