-
Notifications
You must be signed in to change notification settings - Fork 27
/
struct_update_test.go
109 lines (93 loc) · 3.34 KB
/
struct_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
package godb
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestUpdateDo(t *testing.T) {
Convey("Given a test database", t, func() {
db := fixturesSetup(t)
defer db.Close()
Convey("Update update a record", func() {
dummy := &Dummy{}
err := db.Select(dummy).Where("an_integer = ?", 11).Do()
So(err, ShouldBeNil)
dummy.AText = "New text"
dummy.AnotherText = "Replacement text"
dummy.AnInteger = 123
err = db.Update(dummy).Do()
So(err, ShouldBeNil)
Convey("The data are in the database", func() {
retrieveddummy := Dummy{}
db.Select(&retrieveddummy).Where("id = ?", dummy.ID).Do()
So(retrieveddummy.ID, ShouldEqual, dummy.ID)
So(retrieveddummy.AText, ShouldEqual, dummy.AText)
So(retrieveddummy.AnotherText, ShouldEqual, dummy.AnotherText)
So(retrieveddummy.AnInteger, ShouldEqual, dummy.AnInteger)
})
})
Convey("Update returns error if optimistic locking fails", func() {
Convey("With non auto oplock field", 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)
err = db.Update(dummy).Do()
So(err, ShouldEqual, ErrOpLock)
})
Convey("With auto oplock field", func() {
dummy := &DummyAutoOplock{}
err := db.Select(dummy).Where("an_integer = ?", 11).Do()
So(err, ShouldBeNil)
// Simulate another update of the record.
_, err = db.UpdateTable("dummiesautooplock").Set("version", 1).Where("id = ?", dummy.ID).Do()
So(err, ShouldBeNil)
err = db.Update(dummy).Do()
So(err, ShouldEqual, ErrOpLock)
})
})
Convey("Update update a record whitelisted after reset", func() {
dummy := &Dummy{}
err := db.Select(dummy).Where("an_integer = ?", 11).Do()
So(err, ShouldBeNil)
dummy.AText = "New text"
dummy.AnotherText = "Replacement text"
dummy.AnInteger = 1453
updQ := db.Update(dummy)
updQ.Whitelist("an_integer")
updQ.WhitelistReset()
err = updQ.Whitelist("another_text", "a_text").Do()
So(err, ShouldBeNil)
Convey("The data are in the database", func() {
retrieveddummy := Dummy{}
db.Select(&retrieveddummy).Where("id = ?", dummy.ID).Do()
So(retrieveddummy.ID, ShouldEqual, dummy.ID)
So(retrieveddummy.AText, ShouldEqual, dummy.AText)
So(retrieveddummy.AnotherText, ShouldEqual, dummy.AnotherText)
So(retrieveddummy.AnInteger, ShouldNotEqual, dummy.AnInteger)
})
})
Convey("Update a record blacklisted after reset", func() {
dummy := &Dummy{}
err := db.Select(dummy).Where("an_integer = ?", 11).Do()
So(err, ShouldBeNil)
dummy.AText = "New text"
dummy.AnotherText = "Replacement text blacklisted"
dummy.AnInteger = 1453
updQ := db.Update(dummy)
updQ.Blacklist("a_text")
updQ.BlacklistReset()
err = updQ.Blacklist("another_text").Do()
So(err, ShouldBeNil)
Convey("The data are in the database", func() {
retrieveddummy := Dummy{}
db.Select(&retrieveddummy).Where("id = ?", dummy.ID).Do()
So(retrieveddummy.ID, ShouldEqual, dummy.ID)
So(retrieveddummy.AText, ShouldEqual, dummy.AText)
So(retrieveddummy.AnotherText, ShouldNotEqual, dummy.AnotherText)
So(retrieveddummy.AnInteger, ShouldEqual, dummy.AnInteger)
})
})
})
}