-
Notifications
You must be signed in to change notification settings - Fork 0
/
consistently_test.go
193 lines (136 loc) · 3.12 KB
/
consistently_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package consistently_test
import (
"sync"
"testing"
"time"
"github.com/ftomza/consistently"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
var db *gorm.DB
type User struct {
gorm.Model
Name string
consistently.Consistently
}
type UserCustom struct {
gorm.Model
Name string
Version2 string `consistently:"version"`
}
func TestGoConsistently(t *testing.T) {
user := User{Name: "FtomZa"}
db.Save(&user)
if user.Version == "" {
t.Errorf("Must contain a version of the data")
}
user.Name = "gopa"
beforeUpdateVesrion := user.Version
errs := db.Save(&user).GetErrors()
errFound := false
for _, v := range errs {
if v == consistently.ErrVersionNotValid {
errFound = true
}
}
if errFound {
t.Errorf("Must not contain ErrVersionNotValid")
}
if user.Name != "gopa" {
t.Errorf("Must equal user.Name")
}
userCheck := User{}
db.First(&userCheck, user.ID)
if beforeUpdateVesrion == userCheck.Version {
t.Errorf("Must not equal new version and old version")
}
user.Version = "bad"
errs = db.Save(&user).GetErrors()
errFound = false
for _, v := range errs {
if v == consistently.ErrVersionNotValid {
errFound = true
}
}
if !errFound {
t.Errorf("Must contain ErrVersionNotValid")
}
}
func TestGoConsistentlyCustomField(t *testing.T) {
user := UserCustom{Name: "FtomZa"}
db.Save(&user)
if user.Version2 == "" {
t.Errorf("Must contain a version of the data")
}
beforeUpdateVesrion := user.Version2
errs := db.Model(&user).Update(&user).GetErrors()
errFound := false
for _, v := range errs {
if v == consistently.ErrVersionNotValid {
errFound = true
}
}
if errFound {
t.Errorf("Must not contain ErrVersionNotValid")
}
userCheck := UserCustom{}
db.First(&userCheck, user.ID)
if beforeUpdateVesrion == userCheck.Version2 {
t.Errorf("Must not equal new version and old version")
}
user.Version2 = "bad"
errs = db.Model(&user).Update(&user).GetErrors()
errFound = false
for _, v := range errs {
if v == consistently.ErrVersionNotValid {
errFound = true
}
}
if !errFound {
t.Errorf("Must contain ErrVersionNotValid")
}
}
func TestGoConsistentlyAsync(t *testing.T) {
user := User{Name: "FtomZa"}
db.Save(&user)
var wg sync.WaitGroup
wg.Add(2)
handler := func(fail bool, dur time.Duration) {
defer wg.Done()
testUser := User{}
db.Find(&testUser, user.ID)
time.Sleep(dur * time.Second)
errs := db.Model(&testUser).Update(testUser).GetErrors()
errFound := false
for _, v := range errs {
if v == consistently.ErrVersionNotValid {
errFound = true
}
}
if errFound && !fail {
t.Errorf("Must not contain ErrVersionNotValid")
}
if !errFound && fail {
t.Errorf("Must contain ErrVersionNotValid")
}
}
go handler(false, 2)
go handler(true, 4)
wg.Wait()
}
func init() {
var err error
db, err = gorm.Open("sqlite3", "test.db")
if err != nil {
panic(err)
}
db.LogMode(true)
consistently.RegisterCallbacks(db)
tables := []interface{}{&User{}, &UserCustom{}}
for _, table := range tables {
if err := db.DropTableIfExists(table).Error; err != nil {
panic(err)
}
db.AutoMigrate(table)
}
}