forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_test.go
89 lines (70 loc) · 1.97 KB
/
delete_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
package squirrel
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDeleteBuilderToSql(t *testing.T) {
b := Delete("").
Prefix("WITH prefix AS ?", 0).
From("a").
JoinClause("CROSS JOIN j1").
Join("j2").
LeftJoin("j3").
RightJoin("j4").
InnerJoin("j5").
CrossJoin("j6").
Where("b = ?", 1).
OrderBy("c").
Limit(2).
Offset(3).
Suffix("RETURNING ?", 4)
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql :=
"WITH prefix AS ? " +
"DELETE FROM a " +
"CROSS JOIN j1 JOIN j2 LEFT JOIN j3 RIGHT JOIN j4 INNER JOIN j5 CROSS JOIN j6 " +
"WHERE b = ? ORDER BY c LIMIT 2 OFFSET 3 " +
"RETURNING ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{0, 1, 4}
assert.Equal(t, expectedArgs, args)
}
func TestDeleteBuilderToSqlErr(t *testing.T) {
_, _, err := Delete("").ToSql()
assert.Error(t, err)
}
func TestDeleteBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestDeleteBuilderMustSql should have panicked!")
}
}()
Delete("").MustSql()
}
func TestDeleteBuilderPlaceholders(t *testing.T) {
b := Delete("test").Where("x = ? AND y = ?", 1, 2)
sql, _, _ := b.PlaceholderFormat(Question).ToSql()
assert.Equal(t, "DELETE FROM test WHERE x = ? AND y = ?", sql)
sql, _, _ = b.PlaceholderFormat(Dollar).ToSql()
assert.Equal(t, "DELETE FROM test WHERE x = $1 AND y = $2", sql)
}
func TestDeleteBuilderRunners(t *testing.T) {
db := &DBStub{}
b := Delete("test").Where("x = ?", 1).RunWith(db)
expectedSql := "DELETE FROM test WHERE x = ?"
b.Exec()
assert.Equal(t, expectedSql, db.LastExecSql)
}
func TestDeleteBuilderNoRunner(t *testing.T) {
b := Delete("test")
_, err := b.Exec()
assert.Equal(t, RunnerNotSet, err)
}
func TestDeleteWithQuery(t *testing.T) {
db := &DBStub{}
b := Delete("test").Where("id=55").Suffix("RETURNING path").RunWith(db)
expectedSql := "DELETE FROM test WHERE id=55 RETURNING path"
b.Query()
assert.Equal(t, expectedSql, db.LastQuerySql)
}