forked from elgris/sqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
150 lines (114 loc) · 3.44 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
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 sqrl
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpdateBuilderToSql(t *testing.T) {
b := Update("").
Prefix("WITH prefix AS ?", 0).
Table("a").
Set("b", Expr("? + 1", 1)).
SetMap(Eq{"c": 2}).
Where("d = ?", 3).
OrderBy("e").
Limit(4).
Offset(5).
Suffix("RETURNING ?", 6)
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql :=
"WITH prefix AS ? " +
"UPDATE a SET b = ? + 1, c = ? WHERE d = ? " +
"ORDER BY e LIMIT 4 OFFSET 5 " +
"RETURNING ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{0, 1, 2, 3, 6}
assert.Equal(t, expectedArgs, args)
}
func TestUpdateBuilderFrom(t *testing.T) {
b := Update("a").
Set("foo", 1).
From("b").
Where("id = b.id").
Where(Eq{"b.id": 42})
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Equal(t, "UPDATE a SET foo = ? FROM b WHERE id = b.id AND b.id = ?", sql)
assert.Equal(t, []interface{}{1, 42}, args)
}
func TestUpdateBuilderReturning(t *testing.T) {
b := Update("a").
Set("foo", 1).
Where("id = ?", 42).
Returning("bar")
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Equal(t, "UPDATE a SET foo = ? WHERE id = ? RETURNING bar", sql)
assert.Equal(t, []interface{}{1, 42}, args)
b = Update("a").
Set("foo", 1).
Where("id = ?", 42).
ReturningSelect(Select("bar").From("b").Where("b.id = a.id"), "bar")
sql, args, err = b.ToSql()
assert.NoError(t, err)
assert.Equal(t, "UPDATE a SET foo = ? WHERE id = ? RETURNING (SELECT bar FROM b WHERE b.id = a.id) AS bar", sql)
assert.Equal(t, []interface{}{1, 42}, args)
}
func TestUpdateBuilderZeroOffsetLimit(t *testing.T) {
qb := Update("a").
Set("b", true).
Limit(0).
Offset(0)
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "UPDATE a SET b = ? LIMIT 0 OFFSET 0"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{true}
assert.Equal(t, expectedArgs, args)
}
func TestUpdateBuilderToSqlErr(t *testing.T) {
_, _, err := Update("").Set("x", 1).ToSql()
assert.Error(t, err)
_, _, err = Update("x").ToSql()
assert.Error(t, err)
}
func TestUpdateBuilderPlaceholders(t *testing.T) {
b := Update("test").SetMap(Eq{"x": 1, "y": 2})
sql, _, _ := b.PlaceholderFormat(Question).ToSql()
assert.Equal(t, "UPDATE test SET x = ?, y = ?", sql)
sql, _, _ = b.PlaceholderFormat(Dollar).ToSql()
assert.Equal(t, "UPDATE test SET x = $1, y = $2", sql)
}
func TestUpdateBuilderRunners(t *testing.T) {
db := &DBStub{}
b := Update("test").Set("x", 1).Suffix("RETURNING y").RunWith(db)
expectedSql := "UPDATE test SET x = ? RETURNING y"
b.Exec()
assert.Equal(t, expectedSql, db.LastExecSql)
b.Query()
assert.Equal(t, expectedSql, db.LastQuerySql)
b.QueryRow()
assert.Equal(t, expectedSql, db.LastQueryRowSql)
b.ExecContext(context.TODO())
assert.Equal(t, expectedSql, db.LastExecSql)
b.QueryContext(context.TODO())
assert.Equal(t, expectedSql, db.LastQuerySql)
b.QueryRowContext(context.TODO())
assert.Equal(t, expectedSql, db.LastQueryRowSql)
err := b.Scan()
assert.NoError(t, err)
}
func TestUpdateBuilderNoRunner(t *testing.T) {
b := Update("test").Set("x", 1)
_, err := b.Exec()
assert.Equal(t, ErrRunnerNotSet, err)
_, err = b.Query()
assert.Equal(t, ErrRunnerNotSet, err)
_, err = b.ExecContext(context.TODO())
assert.Equal(t, ErrRunnerNotSet, err)
_, err = b.QueryContext(context.TODO())
assert.Equal(t, ErrRunnerNotSet, err)
err = b.Scan()
assert.Equal(t, ErrRunnerNotSet, err)
}