forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
208 lines (170 loc) · 7.17 KB
/
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
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
package gorm_test
import (
"testing"
"time"
"github.com/jinzhu/gorm"
)
func TestUpdate(t *testing.T) {
product1 := Product{Code: "product1code"}
product2 := Product{Code: "product2code"}
DB.Save(&product1).Save(&product2).Update("code", "product2newcode")
if product2.Code != "product2newcode" {
t.Errorf("Record should be updated")
}
DB.First(&product1, product1.Id)
DB.First(&product2, product2.Id)
updatedAt1 := product1.UpdatedAt
updatedAt2 := product2.UpdatedAt
var product3 Product
DB.First(&product3, product2.Id).Update("code", "product2newcode")
if updatedAt2.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should not be updated if nothing changed")
}
if DB.First(&Product{}, "code = ?", product1.Code).RecordNotFound() {
t.Errorf("Product1 should not be updated")
}
if !DB.First(&Product{}, "code = ?", "product2code").RecordNotFound() {
t.Errorf("Product2's code should be updated")
}
if DB.First(&Product{}, "code = ?", "product2newcode").RecordNotFound() {
t.Errorf("Product2's code should be updated")
}
DB.Table("products").Where("code in (?)", []string{"product1code"}).Update("code", "product1newcode")
var product4 Product
DB.First(&product4, product1.Id)
if updatedAt1.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should be updated if something changed")
}
if !DB.First(&Product{}, "code = 'product1code'").RecordNotFound() {
t.Errorf("Product1's code should be updated")
}
if DB.First(&Product{}, "code = 'product1newcode'").RecordNotFound() {
t.Errorf("Product should not be changed to 789")
}
if DB.Model(product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
t.Error("No error should raise when update with CamelCase")
}
if DB.Model(&product2).UpdateColumn("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
t.Error("No error should raise when update_column with CamelCase")
}
var products []Product
DB.Find(&products)
if count := DB.Model(Product{}).Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(products)) {
t.Error("RowsAffected should be correct when do batch update")
}
DB.First(&product4, product4.Id)
DB.Model(&product4).Update("price", gorm.Expr("price + ? - ?", 100, 50))
var product5 Product
DB.First(&product5, product4.Id)
if product5.Price != product4.Price+100-50 {
t.Errorf("Update with expression")
}
if product5.UpdatedAt.Format(time.RFC3339Nano) == product4.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("Update with expression should update UpdatedAt")
}
}
func TestUpdateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
animal := Animal{Name: "Ferdinand"}
DB.Save(&animal)
updatedAt1 := animal.UpdatedAt
DB.Save(&animal).Update("name", "Francis")
if updatedAt1.Format(time.RFC3339Nano) == animal.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should not be updated if nothing changed")
}
var animals []Animal
DB.Find(&animals)
if count := DB.Model(Animal{}).Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(animals)) {
t.Error("RowsAffected should be correct when do batch update")
}
animal = Animal{From: "somewhere"} // No name fields, should be filled with the default value (galeone)
DB.Save(&animal).Update("From", "a nice place") // The name field shoul be untouched
DB.First(&animal, animal.Counter)
if animal.Name != "galeone" {
t.Errorf("Name fiels shouldn't be changed if untouched, but got %v", animal.Name)
}
// When changing a field with a default value, the change must occur
animal.Name = "amazing horse"
DB.Save(&animal)
DB.First(&animal, animal.Counter)
if animal.Name != "amazing horse" {
t.Errorf("Update a filed with a default value should occur. But got %v\n", animal.Name)
}
}
func TestUpdates(t *testing.T) {
product1 := Product{Code: "product1code", Price: 10}
product2 := Product{Code: "product2code", Price: 10}
DB.Save(&product1).Save(&product2)
DB.Model(&product1).Updates(map[string]interface{}{"code": "product1newcode", "price": 100})
if product1.Code != "product1newcode" || product1.Price != 100 {
t.Errorf("Record should be updated also with map")
}
DB.First(&product1, product1.Id)
DB.First(&product2, product2.Id)
updatedAt1 := product1.UpdatedAt
updatedAt2 := product2.UpdatedAt
var product3 Product
DB.First(&product3, product1.Id).Updates(Product{Code: "product1newcode", Price: 100})
if product3.Code != "product1newcode" || product3.Price != 100 {
t.Errorf("Record should be updated with struct")
}
if updatedAt1.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should not be updated if nothing changed")
}
if DB.First(&Product{}, "code = ? and price = ?", product2.Code, product2.Price).RecordNotFound() {
t.Errorf("Product2 should not be updated")
}
if DB.First(&Product{}, "code = ?", "product1newcode").RecordNotFound() {
t.Errorf("Product1 should be updated")
}
DB.Table("products").Where("code in (?)", []string{"product2code"}).Updates(Product{Code: "product2newcode"})
if !DB.First(&Product{}, "code = 'product2code'").RecordNotFound() {
t.Errorf("Product2's code should be updated")
}
var product4 Product
DB.First(&product4, product2.Id)
if updatedAt2.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should be updated if something changed")
}
if DB.First(&Product{}, "code = ?", "product2newcode").RecordNotFound() {
t.Errorf("product2's code should be updated")
}
DB.Model(&product4).Updates(map[string]interface{}{"price": gorm.Expr("price + ?", 100)})
var product5 Product
DB.First(&product5, product4.Id)
if product5.Price != product4.Price+100 {
t.Errorf("Updates with expression")
}
if product5.UpdatedAt.Format(time.RFC3339Nano) == product4.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("Updates with expression should update UpdatedAt")
}
}
func TestUpdateColumn(t *testing.T) {
product1 := Product{Code: "product1code", Price: 10}
product2 := Product{Code: "product2code", Price: 20}
DB.Save(&product1).Save(&product2).UpdateColumn(map[string]interface{}{"code": "product2newcode", "price": 100})
if product2.Code != "product2newcode" || product2.Price != 100 {
t.Errorf("product 2 should be updated with update column")
}
var product3 Product
DB.First(&product3, product1.Id)
if product3.Code != "product1code" || product3.Price != 10 {
t.Errorf("product 1 should not be updated")
}
DB.First(&product2, product2.Id)
updatedAt2 := product2.UpdatedAt
DB.Model(product2).UpdateColumn("code", "update_column_new")
var product4 Product
DB.First(&product4, product2.Id)
if updatedAt2.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("updatedAt should not be updated with update column")
}
DB.Model(&product4).UpdateColumn("price", gorm.Expr("price + 100 - 50"))
var product5 Product
DB.First(&product5, product4.Id)
if product5.Price != product4.Price+100-50 {
t.Errorf("UpdateColumn with expression")
}
if product5.UpdatedAt.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("UpdateColumn with expression should not update UpdatedAt")
}
}