forked from doug-martin/goqu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_delete_test.go
134 lines (115 loc) · 3.95 KB
/
dataset_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
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
package goqu
import (
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"github.com/c2fo/testify/assert"
)
func (me *datasetTest) TestDeleteSqlNoReturning() {
t := me.T()
mDb, _, _ := sqlmock.New()
ds1 := New("no-return", mDb).From("items")
type item struct {
Address string `db:"address"`
Name string `db:"name"`
}
_, _, err := ds1.Returning("id").ToDeleteSql()
assert.EqualError(t, err, "goqu: Adapter does not support RETURNING clause")
}
func (me *datasetTest) TestDeleteSqlWithLimit() {
t := me.T()
mDb, _, _ := sqlmock.New()
ds1 := New("limit", mDb).From("items")
sql, _, err := ds1.Limit(10).ToDeleteSql()
assert.Nil(t, err)
assert.Equal(t, sql, `DELETE FROM "items" LIMIT 10`)
}
func (me *datasetTest) TestDeleteSqlWithOrder() {
t := me.T()
mDb, _, _ := sqlmock.New()
ds1 := New("order", mDb).From("items")
sql, _, err := ds1.Order(I("name").Desc()).ToDeleteSql()
assert.Nil(t, err)
assert.Equal(t, sql, `DELETE FROM "items" ORDER BY "name" DESC`)
}
func (me *datasetTest) TestToDeleteSql() {
t := me.T()
ds1 := From("items")
sql, _, err := ds1.ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, sql, `DELETE FROM "items"`)
}
func (me *datasetTest) TestDeleteSqlNoSources() {
t := me.T()
ds1 := From("items")
_, _, err := ds1.From().ToDeleteSql()
assert.EqualError(t, err, "goqu: No source found when generating delete sql")
}
func (me *datasetTest) TestDeleteSqlWithWhere() {
t := me.T()
ds1 := From("items")
sql, _, err := ds1.Where(I("id").IsNotNull()).ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, sql, `DELETE FROM "items" WHERE ("id" IS NOT NULL)`)
}
func (me *datasetTest) TestDeleteSqlWithReturning() {
t := me.T()
ds1 := From("items")
sql, _, err := ds1.Returning("id").ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, sql, `DELETE FROM "items" RETURNING "id"`)
sql, _, err = ds1.Returning("id").Where(I("id").IsNotNull()).ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, sql, `DELETE FROM "items" WHERE ("id" IS NOT NULL) RETURNING "id"`)
}
func (me *datasetTest) TestTruncateSql() {
t := me.T()
ds1 := From("items")
sql, _, err := ds1.ToTruncateSql()
assert.NoError(t, err)
assert.Equal(t, sql, `TRUNCATE "items"`)
}
func (me *datasetTest) TestTruncateSqlNoSources() {
t := me.T()
ds1 := From("items")
_, _, err := ds1.From().ToTruncateSql()
assert.EqualError(t, err, "goqu: No source found when generating truncate sql")
}
func (me *datasetTest) TestTruncateSqlWithOpts() {
t := me.T()
ds1 := From("items")
sql, _, err := ds1.ToTruncateWithOptsSql(TruncateOptions{Cascade: true})
assert.NoError(t, err)
assert.Equal(t, sql, `TRUNCATE "items" CASCADE`)
sql, _, err = ds1.ToTruncateWithOptsSql(TruncateOptions{Restrict: true})
assert.NoError(t, err)
assert.Equal(t, sql, `TRUNCATE "items" RESTRICT`)
sql, _, err = ds1.ToTruncateWithOptsSql(TruncateOptions{Identity: "restart"})
assert.NoError(t, err)
assert.Equal(t, sql, `TRUNCATE "items" RESTART IDENTITY`)
sql, _, err = ds1.ToTruncateWithOptsSql(TruncateOptions{Identity: "continue"})
assert.NoError(t, err)
assert.Equal(t, sql, `TRUNCATE "items" CONTINUE IDENTITY`)
}
func (me *datasetTest) TestPreparedToDeleteSql() {
t := me.T()
ds1 := From("items")
sql, args, err := ds1.Prepared(true).ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{})
assert.Equal(t, sql, `DELETE FROM "items"`)
sql, args, err = ds1.Where(I("id").Eq(1)).Prepared(true).ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{1})
assert.Equal(t, sql, `DELETE FROM "items" WHERE ("id" = ?)`)
sql, args, err = ds1.Returning("id").Where(I("id").Eq(1)).Prepared(true).ToDeleteSql()
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{1})
assert.Equal(t, sql, `DELETE FROM "items" WHERE ("id" = ?) RETURNING "id"`)
}
func (me *datasetTest) TestPreparedTruncateSql() {
t := me.T()
ds1 := From("items")
sql, args, err := ds1.ToTruncateSql()
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{})
assert.Equal(t, sql, `TRUNCATE "items"`)
}