forked from qor/publish2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_test.go
308 lines (251 loc) · 9.58 KB
/
version_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package publish2_test
import (
"testing"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/publish2"
)
type Wiki struct {
gorm.Model
Title string
Body string
publish2.Version
}
func TestVersions(t *testing.T) {
var wiki = Wiki{Title: "wiki 1", Body: "wiki 1"}
DB.Create(&wiki)
wiki.SetVersionName("v1")
wiki.Body = "wiki 1 - v1"
DB.Save(&wiki)
wiki.SetVersionName("v2")
wiki.Body = "wiki 1 - v2"
DB.Save(&wiki)
var count int
DB.Model(&Wiki{}).Where("id = ?", wiki.ID).Count(&count)
if count != 1 {
t.Errorf("Should only find one version for wiki, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Model(&Wiki{}).Where("id = ?", wiki.ID).Count(&count)
if count != 3 {
t.Errorf("Should find all versions for wiki when with multiple mode, but got %v", count)
}
DB.Set(publish2.VersionNameMode, "v2").Delete(&Wiki{}, "id = ?", wiki.ID)
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Model(&Wiki{}).Where("id = ?", wiki.ID).Count(&count)
if count != 2 {
t.Errorf("After delete version v2, should only have 2 records left, but got %v", count)
}
}
type Post struct {
gorm.Model
Title string
Body string
publish2.Version
publish2.Schedule
}
func TestVersionsWithSchedule(t *testing.T) {
now := time.Now()
oneDayLater := now.Add(24 * time.Hour)
post := Post{Title: "post 1", Body: "post 1"}
DB.Create(&post)
post.SetVersionName("v1")
post.Body = "post 1 - v1"
post.SetScheduledStartAt(&now)
post.SetScheduledEndAt(&oneDayLater)
DB.Save(&post)
post.SetVersionName("v2")
post.Body = "post 1 - v2"
post.SetScheduledStartAt(&oneDayLater)
post.SetScheduledEndAt(nil)
DB.Save(&post)
var count int
DB.Model(&Post{}).Where("id = ?", post.ID).Count(&count)
if count != 1 {
t.Errorf("Should have one available post")
}
var post1, post2, post3 Post
DB.Set(publish2.ScheduledTime, now.Add(-24*time.Hour)).Model(&Post{}).Where("id = ?", post.ID).First(&post1)
if post1.Body != "post 1" {
t.Errorf("should find default version, but got %v", post1.Body)
}
DB.Set(publish2.ScheduledTime, now.Add(6*time.Hour)).Model(&Post{}).Where("id = ?", post.ID).First(&post2)
if post2.Body != "post 1 - v1" {
t.Errorf("should find first version, but got %v", post2.Body)
}
DB.Set(publish2.ScheduledTime, now.Add(25*time.Hour)).Model(&Post{}).Where("id = ?", post.ID).First(&post3)
if post3.Body != "post 1 - v2" {
t.Errorf("should find second version, but got %v", post3.Body)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(6*time.Hour)).Model(&Post{}).Where("id = ?", post.ID).Count(&count)
if count != 2 {
t.Errorf("Should find two valid versions for posts that match current schedule, but got %v", count)
}
}
func TestVersionsWithOverlappedSchedule(t *testing.T) {
now := time.Now()
postV1 := prepareOverlappedPost("post 2 - 1")
postV2 := prepareOverlappedPost("post 3 - 2")
var post1, post2, post3 Post
DB.Set(publish2.ScheduledTime, now.Add(-36*time.Hour)).Model(&Post{}).Where("id = ?", postV1.ID).First(&post1)
if post1.Body != postV1.Title {
t.Errorf("should find default version, but got %v", post1.Body)
}
DB.Set(publish2.ScheduledTime, now.Add(6*time.Hour)).Model(&Post{}).Where("id = ?", postV1.ID).First(&post2)
if post2.Body != postV1.Title+" - v2" {
t.Errorf("should find first version, but got %v", post2.Body)
}
DB.Set(publish2.ScheduledTime, now.Add(25*time.Hour)).Model(&Post{}).Where("id = ?", postV1.ID).First(&post3)
if post3.Body != postV1.Title+" - v1" {
t.Errorf("should find second version, but got %v", post3.Body)
}
var count uint
var postIDs = []uint{postV1.ID, postV2.ID}
DB.Set(publish2.ScheduledTime, now.Add(-36*time.Hour)).Model(&Post{}).Where("id IN (?)", postIDs).Count(&count)
if count != 2 {
t.Errorf("should only find 2 valid versions, but got %v", count)
}
DB.Set(publish2.ScheduledTime, now.Add(6*time.Hour)).Model(&Post{}).Where("id IN (?)", postIDs).Count(&count)
if count != 2 {
t.Errorf("should only find 2 valid versions, but got %v", count)
}
DB.Set(publish2.ScheduledTime, now.Add(25*time.Hour)).Model(&Post{}).Where("id IN (?)", postIDs).Count(&count)
if count != 2 {
t.Errorf("should only find 2 valid versions, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(25*time.Hour)).Model(&Post{}).Where("id IN (?)", postIDs).Count(&count)
if count != 4 {
t.Errorf("Should find 4 valid versions for posts that match current schedule, but got %v", count)
}
}
func TestVersionsWithScheduleRange(t *testing.T) {
now := time.Now()
postV1 := prepareOverlappedPost("post 5 - 1")
postV2 := prepareOverlappedPost("post 5 - 2")
var count uint
var postIDs = []uint{postV1.ID, postV2.ID}
DB.Set(publish2.ScheduledStart, now.Add(-36*time.Hour)).Set(publish2.ScheduledEnd, now.Add(-6*time.Hour)).Model(&Post{}).Where("id IN (?)", postIDs).Count(&count)
if count != 2 {
t.Errorf("should only find 2 valid versions in scheduled range, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(-36*time.Hour)).Set(publish2.ScheduledEnd, now.Add(-6*time.Hour)).Model(&Post{}).Where("id IN (?)", postIDs).Count(&count)
if count != 4 {
t.Errorf("should only find 4 valid versions in scheduled range with multiple mode, but got %v", count)
}
}
func prepareOverlappedPost(name string) *Post {
now := time.Now()
oneDayAgo := now.Add(-24 * time.Hour)
oneDayLater := now.Add(24 * time.Hour)
post := Post{Title: name, Body: name}
DB.Create(&post)
post.SetVersionName("v1")
post.Body = name + " - v1"
post.SetScheduledStartAt(&oneDayAgo)
post.SetScheduledEndAt(nil)
DB.Save(&post)
post.SetVersionName("v2")
post.Body = name + " - v2"
post.SetScheduledStartAt(&now)
post.SetScheduledEndAt(&oneDayLater)
DB.Save(&post)
return &post
}
type Article struct {
gorm.Model
Title string
Body string
publish2.Version
publish2.Visible
}
func TestVersionsWithPublishReady(t *testing.T) {
articleV1 := Article{Title: "article 1", Body: "article 1"}
articleV1.PublishReady = true
DB.Create(&articleV1)
articleV1.SetVersionName("v1")
articleV1.PublishReady = false
DB.Save(&articleV1)
articleV1.SetVersionName("v2")
articleV1.PublishReady = true
DB.Save(&articleV1)
articleV2 := Article{Title: "article 2", Body: "article 2"}
articleV2.PublishReady = true
DB.Create(&articleV2)
articleV2.SetVersionName("v1")
articleV2.PublishReady = false
DB.Save(&articleV2)
articleV2.SetVersionName("v2")
articleV2.PublishReady = false
DB.Save(&articleV2)
var count int
DB.Model(&Article{}).Where("id IN (?)", []uint{articleV1.ID, articleV2.ID}).Count(&count)
if count != 2 {
t.Errorf("Should find two articles, but got %v", count)
}
var article1, article2 Article
DB.Model(&Article{}).Where("id = ?", articleV1.ID).First(&article1)
if article1.VersionName != "v2" {
t.Errorf("Should find article v2 as it is latest versible version")
}
DB.Model(&Article{}).Where("id = ?", articleV2.ID).First(&article2)
if article2.VersionName != publish2.DefaultVersionName {
t.Errorf("Should find article w/o version name as no other versions is visible")
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Model(&Article{}).Where("id IN (?)", []uint{articleV1.ID, articleV2.ID}).Count(&count)
if count != 3 {
t.Errorf("Should find 3 visible versions for article, but got %v", count)
}
}
type Product struct {
gorm.Model
Name string
Body string
publish2.Version
publish2.Schedule
publish2.Visible
}
func TestProductWithVersionAndScheduleAndPublishReady(t *testing.T) {
name := "product 1"
now := time.Now()
oneDayAgo := now.Add(-24 * time.Hour)
oneDayLater := now.Add(24 * time.Hour)
product := Product{Name: name}
product.SetPublishReady(true)
DB.Create(&product)
product.SetVersionName("v1")
product.Body = name + " - v1"
product.SetScheduledStartAt(&oneDayAgo)
product.SetScheduledEndAt(&now)
DB.Save(&product)
product.SetVersionName("v2")
product.Body = name + " - v2"
product.SetPublishReady(false)
product.SetScheduledStartAt(&oneDayAgo)
product.SetScheduledEndAt(&oneDayLater)
DB.Save(&product)
product.SetVersionName("v3")
product.Body = name + " - v3"
product.SetPublishReady(true)
product.SetScheduledStartAt(&now)
product.SetScheduledEndAt(&oneDayLater)
DB.Save(&product)
var count int
DB.Model(&Product{}).Where("id = ?", product.ID).Count(&count)
if count != 1 {
t.Errorf("Should only find one valid product, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(-time.Hour)).Model(&Product{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("Should only find two valid product when scheduled time, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(time.Hour)).Model(&Product{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("Should only find two valid product when scheduled time, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(time.Hour)).Set(publish2.ScheduledEnd, now.Add(24*time.Hour)).Model(&Product{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("Should only find two valid product when scheduled time, but got %v", count)
}
DB.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(-time.Hour)).Set(publish2.ScheduledEnd, now.Add(24*time.Hour)).Model(&Product{}).Where("id = ?", product.ID).Count(&count)
if count != 3 {
t.Errorf("Should only find two valid product when scheduled time, but got %v", count)
}
}