forked from qor/publish2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_with_l10n_test.go
127 lines (102 loc) · 4.41 KB
/
version_with_l10n_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
package publish2_test
import (
"testing"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/l10n"
"github.com/qor/publish2"
)
var dbGlobal, dbCN, dbEN *gorm.DB
func init() {
dbGlobal = DB
dbCN = dbGlobal.Set("l10n:locale", "zh")
dbEN = dbGlobal.Set("l10n:locale", "en")
}
type L10nProduct struct {
gorm.Model
Name string
Body string
publish2.Version
publish2.Schedule
publish2.Visible
l10n.Locale
}
func TestL10nWithVersions(t *testing.T) {
name := "product 1"
now := time.Now()
oneDayAgo := now.Add(-24 * time.Hour)
oneDayLater := now.Add(24 * time.Hour)
product := L10nProduct{Name: name}
product.SetPublishReady(true)
dbGlobal.Create(&product)
dbEN.Create(&product)
dbCN.Create(&product)
var productCN, productEN L10nProduct
dbEN.First(&productEN, "id = ?", product.ID)
dbCN.First(&productCN, "id = ?", product.ID)
productEN.SetVersionName("v1-en")
productEN.Body = name + " - v1-EN"
productEN.SetScheduledStartAt(&oneDayAgo)
productEN.SetScheduledEndAt(&now)
dbEN.Save(&productEN)
productEN.SetVersionName("v2-en")
productEN.Body = name + " - v2-EN"
productEN.SetPublishReady(false)
productEN.SetScheduledStartAt(&oneDayAgo)
productEN.SetScheduledEndAt(&oneDayLater)
dbEN.Save(&productEN)
productEN.SetVersionName("v3-en")
productEN.Body = name + " - v3-EN"
productEN.SetPublishReady(true)
productEN.SetScheduledStartAt(&now)
productEN.SetScheduledEndAt(&oneDayLater)
dbEN.Save(&productEN)
productCN.SetVersionName("v1-cn")
productCN.Body = name + " - v1-CN"
productCN.SetScheduledStartAt(&oneDayAgo)
productCN.SetScheduledEndAt(&now)
dbCN.Save(&productCN)
productCN.SetVersionName("v2-cn")
productCN.Body = name + " - v2-CN"
productCN.SetPublishReady(false)
productCN.SetScheduledStartAt(&oneDayAgo)
productCN.SetScheduledEndAt(&oneDayLater)
dbCN.Save(&productCN)
var count int
dbEN.Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 1 {
t.Errorf("Should only find one valid product, but got %v", count)
}
dbEN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(-time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("EN: Should only find two valid product when scheduled time, but got %v", count)
}
dbCN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(-time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("CN: Should only find two valid product when scheduled time, but got %v", count)
}
dbEN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("EN: Should only find two valid product when scheduled time, but got %v", count)
}
dbCN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledTime, now.Add(time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 1 {
t.Errorf("CN: Should only find one valid product when scheduled time, but got %v", count)
}
dbEN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(time.Hour)).Set(publish2.ScheduledEnd, now.Add(24*time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("EN: Should only find two valid product when scheduled time, but got %v", count)
}
dbCN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(time.Hour)).Set(publish2.ScheduledEnd, now.Add(24*time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 1 {
t.Errorf("CN: Should only find two valid product when scheduled time, but got %v", count)
}
dbEN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(-time.Hour)).Set(publish2.ScheduledEnd, now.Add(24*time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 3 {
t.Errorf("EN: Should only find two valid product when scheduled time, but got %v", count)
}
dbCN.Set(publish2.VersionMode, publish2.VersionMultipleMode).Set(publish2.ScheduledStart, now.Add(-time.Hour)).Set(publish2.ScheduledEnd, now.Add(24*time.Hour)).Model(&L10nProduct{}).Where("id = ?", product.ID).Count(&count)
if count != 2 {
t.Errorf("CN: Should only find two valid product when scheduled time, but got %v", count)
}
}