forked from jostyee/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_test.go
67 lines (48 loc) · 1.57 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
package dbr
import (
"github.com/stretchr/testify/assert"
"testing"
)
func BenchmarkDeleteSql(b *testing.B) {
s := createFakeSession()
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.DeleteFrom("alpha").Where("a", "b").Limit(1).OrderDir("id", true).ToSql()
}
}
func TestDeleteAllToSql(t *testing.T) {
s := createFakeSession()
sql, _ := s.DeleteFrom("a").ToSql()
assert.Equal(t, sql, "DELETE FROM a")
}
func TestDeleteSingleToSql(t *testing.T) {
s := createFakeSession()
sql, args := s.DeleteFrom("a").Where("id = ?", 1).ToSql()
assert.Equal(t, sql, "DELETE FROM a WHERE (id = ?)")
assert.Equal(t, args, []interface{}{1})
}
func TestDeleteTenStaringFromTwentyToSql(t *testing.T) {
s := createFakeSession()
sql, _ := s.DeleteFrom("a").Limit(10).Offset(20).OrderBy("id").ToSql()
assert.Equal(t, sql, "DELETE FROM a ORDER BY id LIMIT 10 OFFSET 20")
}
func TestDeleteReal(t *testing.T) {
s := createRealSessionWithFixtures()
// Insert a Barack
res, err := s.InsertInto("dbr_people").Columns("name", "email").Values("Barack", "barack@whitehouse.gov").Exec()
assert.NoError(t, err)
// Get Barack's ID
id, err := res.LastInsertId()
assert.NoError(t, err)
// Delete Barack
res, err = s.DeleteFrom("dbr_people").Where("id = ?", id).Exec()
assert.NoError(t, err)
// Ensure we only reflected one row and that the id no longer exists
rowsAff, err := res.RowsAffected()
assert.NoError(t, err)
assert.Equal(t, rowsAff, 1)
var count int64
err = s.Select("count(*)").From("dbr_people").Where("id = ?", id).LoadValue(&count)
assert.NoError(t, err)
assert.Equal(t, count, 0)
}