-
Notifications
You must be signed in to change notification settings - Fork 209
/
update_test.go
63 lines (52 loc) · 2.1 KB
/
update_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
package dbr
import (
"testing"
"github.com/gocraft/dbr/v2/dialect"
"github.com/stretchr/testify/require"
)
func TestUpdateStmt(t *testing.T) {
buf := NewBuffer()
builder := Update("table").Set("a", 1).Where(Eq("b", 2)).Comment("UPDATE TEST").
IndexHint(ForceIndex("idx_a_b"))
err := builder.Build(dialect.MySQL, buf)
require.NoError(t, err)
require.Equal(t, "/* UPDATE TEST */\nUPDATE `table` FORCE INDEX(`idx_a_b`) SET `a` = ? WHERE (`b` = ?)", buf.String())
require.Equal(t, []interface{}{1, 2}, buf.Value())
}
func BenchmarkUpdateValuesSQL(b *testing.B) {
buf := NewBuffer()
for i := 0; i < b.N; i++ {
Update("table").Set("a", 1).Build(dialect.MySQL, buf)
}
}
func BenchmarkUpdateMapSQL(b *testing.B) {
buf := NewBuffer()
for i := 0; i < b.N; i++ {
Update("table").SetMap(map[string]interface{}{"a": 1, "b": 2}).Build(dialect.MySQL, buf)
}
}
func TestPostgresUpdateReturning(t *testing.T) {
sess := postgresSession
reset(t, sess)
var ids []int
err := sess.Update("dbr_people").Set("name", "Kordian").
Where(Eq("id", 1)).Returning("id").Load(&ids)
require.NoError(t, err)
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, "UPDATE")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "dbr_people")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "name")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "RETURNING")
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).finished)
require.Equal(t, 0, sess.EventReceiver.(*testTraceReceiver).errored)
}
func TestUpdateIncrBy(t *testing.T) {
buf := NewBuffer()
builder := Update("table").IncrBy("a", 1).Where(Eq("b", 2))
err := builder.Build(dialect.MySQL, buf)
require.NoError(t, err)
sqlstr, err := InterpolateForDialect(buf.String(), buf.Value(), dialect.MySQL)
require.NoError(t, err)
require.Equal(t, "UPDATE `table` SET `a` = `a` + 1 WHERE (`b` = 2)", sqlstr)
}