/
curd_test.go
191 lines (153 loc) · 5.93 KB
/
curd_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
package l10n_test
import (
"testing"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/qor/l10n"
_ "github.com/go-sql-driver/mysql"
)
func checkHasErr(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}
func checkHasProductInLocale(db *gorm.DB, locale string, t *testing.T) {
var count int
if db.Set("l10n:locale", locale).Count(&count); count != 1 {
t.Errorf("should has only one product for locale %v, but found %v", locale, count)
}
}
func checkHasProductInAllLocales(db *gorm.DB, t *testing.T) {
checkHasProductInLocale(db, l10n.Global, t)
checkHasProductInLocale(db, "zh", t)
checkHasProductInLocale(db, "en", t)
}
func TestCreateWithCreate(t *testing.T) {
product := Product{Code: "CreateWithCreate"}
checkHasErr(t, dbGlobal.Create(&product).Error)
checkHasErr(t, dbCN.Create(&product).Error)
checkHasErr(t, dbEN.Create(&product).Error)
checkHasProductInAllLocales(dbGlobal.Model(&Product{}).Where("id = ? AND code = ?", product.ID, "CreateWithCreate"), t)
}
func TestCreateWithSave(t *testing.T) {
product := Product{Code: "CreateWithSave"}
checkHasErr(t, dbGlobal.Create(&product).Error)
checkHasErr(t, dbCN.Create(&product).Error)
checkHasErr(t, dbEN.Create(&product).Error)
checkHasProductInAllLocales(dbGlobal.Model(&Product{}).Where("id = ? AND code = ?", product.ID, "CreateWithSave"), t)
}
func TestUpdate(t *testing.T) {
product := Product{Code: "Update", Name: "global"}
checkHasErr(t, dbGlobal.Create(&product).Error)
sharedDB := dbGlobal.Model(&Product{}).Where("id = ? AND code = ?", product.ID, "Update")
product.Name = "中文名"
checkHasErr(t, dbCN.Create(&product).Error)
checkHasProductInLocale(sharedDB.Where("name = ?", "中文名"), "zh", t)
product.Name = "English Name"
checkHasErr(t, dbEN.Create(&product).Error)
checkHasProductInLocale(sharedDB.Where("name = ?", "English Name"), "en", t)
product.Name = "新的中文名"
product.Code = "NewCode // should be ignored when update"
dbCN.Save(&product)
checkHasProductInLocale(sharedDB.Where("name = ?", "新的中文名"), "zh", t)
product.Name = "New English Name"
product.Code = "NewCode // should be ignored when update"
dbEN.Save(&product)
checkHasProductInLocale(sharedDB.Where("name = ?", "New English Name"), "en", t)
// Check sync columns with UpdateColumn
dbGlobal.Model(&Product{}).Where("id = ?", product.ID).UpdateColumns(map[string]interface{}{"quantity": gorm.Expr("quantity + ?", 2)})
var newGlobalProduct Product
var newENProduct Product
dbGlobal.Find(&newGlobalProduct, product.ID)
dbEN.Find(&newENProduct, product.ID)
if newGlobalProduct.Quantity != product.Quantity+2 || newENProduct.Quantity != product.Quantity+2 {
t.Errorf("should sync update columns results correctly")
}
// Check sync columns with Save
newGlobalProduct.Quantity = 5
dbGlobal.Save(&newGlobalProduct)
var newGlobalProduct2 Product
var newENProduct2 Product
dbGlobal.Find(&newGlobalProduct2, product.ID)
dbEN.Find(&newENProduct2, product.ID)
if newGlobalProduct2.Quantity != 5 || newENProduct2.Quantity != 5 {
t.Errorf("should sync update columns results correctly")
}
}
func TestQuery(t *testing.T) {
product := Product{Code: "Query", Name: "global"}
dbGlobal.Create(&product)
dbCN.Create(&product)
var productCN Product
dbCN.First(&productCN, product.ID)
if productCN.LanguageCode != "zh" {
t.Error("Should find localized zh product with unscoped mode")
}
var newProduct Product
if dbCN.Set("l10n:mode", "locale").First(&newProduct, product.ID).RecordNotFound() {
t.Error("Should find localized zh product with locale mode")
}
var newProduct2 Product
if dbCN.Set("l10n:mode", "global").First(&newProduct2); newProduct2.LanguageCode != l10n.Global {
t.Error("Should find global product with global mode")
}
var productEN Product
dbEN.First(&productEN, product.ID)
if productEN.LanguageCode != l10n.Global {
t.Error("Should find global product for en with unscoped mode")
}
if !dbEN.Set("l10n:mode", "locale").First(&productEN, product.ID).RecordNotFound() {
t.Error("Should find no record with locale mode")
}
if dbEN.Set("l10n:mode", "global").First(&productEN); productEN.LanguageCode != l10n.Global {
t.Error("Should find global product with global mode")
}
if dbEN.Joins("LEFT JOIN brands ON products.brand_id = brands.id").First(&productEN).Error != nil {
t.Error("Should handle queries with extra joins")
}
}
func TestQueryWithPreload(t *testing.T) {
product := Product{
Code: "Query",
Name: "global",
Brand: Brand{Name: "Brand"},
Tags: []Tag{{Name: "tag0"}, {Name: "tag2"}},
Categories: []Category{{Name: "category1"}, {Name: "category2"}},
}
dbGlobal.Create(&product)
dbCN.Create(&product)
var productCN Product
dbCN.Preload("Brand").Preload("Tags").Preload("Categories").First(&productCN, product.ID)
if (productCN.Brand.LanguageCode != "zh") || len(productCN.Tags) != 2 || len(productCN.Categories) != 2 {
t.Error("Failed to preload data relations")
}
}
func TestDelete(t *testing.T) {
product := Product{Code: "Delete", Name: "global"}
dbGlobal.Create(&product)
dbCN.Create(&product)
if dbCN.Delete(&product).RowsAffected != 1 {
t.Errorf("Should delete localized record")
}
if dbEN.Delete(&product).RowsAffected != 0 {
t.Errorf("Should delete none record in unlocalized locale")
}
// relocalize deleted record
dbCN.Save(&product)
var count uint
if dbCN.Model(&Product{}).Where("code = ? AND name = ?", "Delete", "global").Count(&count); count != 1 {
t.Errorf("Should be able to relocalize deleted records, get record %v", count)
}
}
func TestResetLanguageCodeWithGlobalDB(t *testing.T) {
product := Product{Code: "Query", Name: "global"}
product.LanguageCode = "test"
dbGlobal.Save(&product)
if product.LanguageCode != l10n.Global {
t.Error("Should reset language code in global mode")
}
}
func TestManyToManyRelations(t *testing.T) {
product := Product{Code: "Delete", Name: "global", Tags: []Tag{{Name: "tag1"}, {Name: "tag2"}}}
dbGlobal.Save(&product)
}