-
Notifications
You must be signed in to change notification settings - Fork 27
/
struct_delete_test.go
46 lines (37 loc) · 1.13 KB
/
struct_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
package godb
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDeleteDo(t *testing.T) {
Convey("Given a test database", t, func() {
db := fixturesSetup(t)
defer db.Close()
Convey("Delete delete a record", func() {
dummy := &Dummy{}
err := db.Select(dummy).Where("an_integer = ?", 11).Do()
So(err, ShouldBeNil)
count, err := db.Delete(dummy).Do()
So(err, ShouldBeNil)
Convey("The data isn't in database", func() {
found, err := db.SelectFrom("dummies").Where("id = ?", dummy.ID).Count()
So(err, ShouldBeNil)
So(found, ShouldEqual, 0)
})
Convey("Delete returns the count of affected rows", func() {
So(count, ShouldEqual, 1)
})
})
Convey("Delete returns error if optimistic locking fails", func() {
dummy := &Dummy{}
err := db.Select(dummy).Where("an_integer = ?", 11).Do()
So(err, ShouldBeNil)
// Simulate another update of the record.
_, err = db.UpdateTable("dummies").Set("version", 1).Where("id = ?", dummy.ID).Do()
So(err, ShouldBeNil)
count, err := db.Delete(dummy).Do()
So(count, ShouldEqual, 0)
So(err, ShouldEqual, ErrOpLock)
})
})
}