Codebase list golang-github-jinzhu-gorm / fresh-releases/upstream update_test.go
fresh-releases/upstream

Tree @fresh-releases/upstream (Download .tar.gz)

update_test.go @fresh-releases/upstreamraw · history · blame

  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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
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

	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)
	updatedAt4 := product4.UpdatedAt
	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 product4.UpdatedAt.Format(time.RFC3339Nano) == updatedAt4.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 fields 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)
	}

	// When changing a field with a default value with blank value
	animal.Name = ""
	DB.Save(&animal)
	DB.First(&animal, animal.Counter)
	if animal.Name != "" {
		t.Errorf("Update a filed to blank 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)
	updatedAt2 := product2.UpdatedAt

	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")
	}

	updatedAt4 := product4.UpdatedAt
	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")
	}
	// product4's UpdatedAt will be reset when updating
	if product4.UpdatedAt.Format(time.RFC3339Nano) == updatedAt4.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")
	}
}

func TestSelectWithUpdate(t *testing.T) {
	user := getPreparedUser("select_user", "select_with_update")
	DB.Create(user)

	var reloadUser User
	DB.First(&reloadUser, user.Id)
	reloadUser.Name = "new_name"
	reloadUser.Age = 50
	reloadUser.BillingAddress = Address{Address1: "New Billing Address"}
	reloadUser.ShippingAddress = Address{Address1: "New ShippingAddress Address"}
	reloadUser.CreditCard = CreditCard{Number: "987654321"}
	reloadUser.Emails = []Email{
		{Email: "new_user_1@example1.com"}, {Email: "new_user_2@example2.com"}, {Email: "new_user_3@example2.com"},
	}
	reloadUser.Company = Company{Name: "new company"}

	DB.Select("Name", "BillingAddress", "CreditCard", "Company", "Emails").Save(&reloadUser)

	var queryUser User
	DB.Preload("BillingAddress").Preload("ShippingAddress").
		Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryUser, user.Id)

	if queryUser.Name == user.Name || queryUser.Age != user.Age {
		t.Errorf("Should only update users with name column")
	}

	if queryUser.BillingAddressID.Int64 == user.BillingAddressID.Int64 ||
		queryUser.ShippingAddressId != user.ShippingAddressId ||
		queryUser.CreditCard.ID == user.CreditCard.ID ||
		len(queryUser.Emails) == len(user.Emails) || queryUser.Company.Id == user.Company.Id {
		t.Errorf("Should only update selected relationships")
	}
}

func TestSelectWithUpdateWithMap(t *testing.T) {
	user := getPreparedUser("select_user", "select_with_update_map")
	DB.Create(user)

	updateValues := map[string]interface{}{
		"Name":            "new_name",
		"Age":             50,
		"BillingAddress":  Address{Address1: "New Billing Address"},
		"ShippingAddress": Address{Address1: "New ShippingAddress Address"},
		"CreditCard":      CreditCard{Number: "987654321"},
		"Emails": []Email{
			{Email: "new_user_1@example1.com"}, {Email: "new_user_2@example2.com"}, {Email: "new_user_3@example2.com"},
		},
		"Company": Company{Name: "new company"},
	}

	var reloadUser User
	DB.First(&reloadUser, user.Id)
	DB.Model(&reloadUser).Select("Name", "BillingAddress", "CreditCard", "Company", "Emails").Update(updateValues)

	var queryUser User
	DB.Preload("BillingAddress").Preload("ShippingAddress").
		Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryUser, user.Id)

	if queryUser.Name == user.Name || queryUser.Age != user.Age {
		t.Errorf("Should only update users with name column")
	}

	if queryUser.BillingAddressID.Int64 == user.BillingAddressID.Int64 ||
		queryUser.ShippingAddressId != user.ShippingAddressId ||
		queryUser.CreditCard.ID == user.CreditCard.ID ||
		len(queryUser.Emails) == len(user.Emails) || queryUser.Company.Id == user.Company.Id {
		t.Errorf("Should only update selected relationships")
	}
}

func TestOmitWithUpdate(t *testing.T) {
	user := getPreparedUser("omit_user", "omit_with_update")
	DB.Create(user)

	var reloadUser User
	DB.First(&reloadUser, user.Id)
	reloadUser.Name = "new_name"
	reloadUser.Age = 50
	reloadUser.BillingAddress = Address{Address1: "New Billing Address"}
	reloadUser.ShippingAddress = Address{Address1: "New ShippingAddress Address"}
	reloadUser.CreditCard = CreditCard{Number: "987654321"}
	reloadUser.Emails = []Email{
		{Email: "new_user_1@example1.com"}, {Email: "new_user_2@example2.com"}, {Email: "new_user_3@example2.com"},
	}
	reloadUser.Company = Company{Name: "new company"}

	DB.Omit("Name", "BillingAddress", "CreditCard", "Company", "Emails").Save(&reloadUser)

	var queryUser User
	DB.Preload("BillingAddress").Preload("ShippingAddress").
		Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryUser, user.Id)

	if queryUser.Name != user.Name || queryUser.Age == user.Age {
		t.Errorf("Should only update users with name column")
	}

	if queryUser.BillingAddressID.Int64 != user.BillingAddressID.Int64 ||
		queryUser.ShippingAddressId == user.ShippingAddressId ||
		queryUser.CreditCard.ID != user.CreditCard.ID ||
		len(queryUser.Emails) != len(user.Emails) || queryUser.Company.Id != user.Company.Id {
		t.Errorf("Should only update relationships that not omitted")
	}
}

func TestOmitWithUpdateWithMap(t *testing.T) {
	user := getPreparedUser("select_user", "select_with_update_map")
	DB.Create(user)

	updateValues := map[string]interface{}{
		"Name":            "new_name",
		"Age":             50,
		"BillingAddress":  Address{Address1: "New Billing Address"},
		"ShippingAddress": Address{Address1: "New ShippingAddress Address"},
		"CreditCard":      CreditCard{Number: "987654321"},
		"Emails": []Email{
			{Email: "new_user_1@example1.com"}, {Email: "new_user_2@example2.com"}, {Email: "new_user_3@example2.com"},
		},
		"Company": Company{Name: "new company"},
	}

	var reloadUser User
	DB.First(&reloadUser, user.Id)
	DB.Model(&reloadUser).Omit("Name", "BillingAddress", "CreditCard", "Company", "Emails").Update(updateValues)

	var queryUser User
	DB.Preload("BillingAddress").Preload("ShippingAddress").
		Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryUser, user.Id)

	if queryUser.Name != user.Name || queryUser.Age == user.Age {
		t.Errorf("Should only update users with name column")
	}

	if queryUser.BillingAddressID.Int64 != user.BillingAddressID.Int64 ||
		queryUser.ShippingAddressId == user.ShippingAddressId ||
		queryUser.CreditCard.ID != user.CreditCard.ID ||
		len(queryUser.Emails) != len(user.Emails) || queryUser.Company.Id != user.Company.Id {
		t.Errorf("Should only update relationships not omitted")
	}
}

func TestSelectWithUpdateColumn(t *testing.T) {
	user := getPreparedUser("select_user", "select_with_update_map")
	DB.Create(user)

	updateValues := map[string]interface{}{"Name": "new_name", "Age": 50}

	var reloadUser User
	DB.First(&reloadUser, user.Id)
	DB.Model(&reloadUser).Select("Name").UpdateColumn(updateValues)

	var queryUser User
	DB.First(&queryUser, user.Id)

	if queryUser.Name == user.Name || queryUser.Age != user.Age {
		t.Errorf("Should only update users with name column")
	}
}

func TestOmitWithUpdateColumn(t *testing.T) {
	user := getPreparedUser("select_user", "select_with_update_map")
	DB.Create(user)

	updateValues := map[string]interface{}{"Name": "new_name", "Age": 50}

	var reloadUser User
	DB.First(&reloadUser, user.Id)
	DB.Model(&reloadUser).Omit("Name").UpdateColumn(updateValues)

	var queryUser User
	DB.First(&queryUser, user.Id)

	if queryUser.Name != user.Name || queryUser.Age == user.Age {
		t.Errorf("Should omit name column when update user")
	}
}

func TestUpdateColumnsSkipsAssociations(t *testing.T) {
	user := getPreparedUser("update_columns_user", "special_role")
	user.Age = 99
	address1 := "first street"
	user.BillingAddress = Address{Address1: address1}
	DB.Save(user)

	// Update a single field of the user and verify that the changed address is not stored.
	newAge := int64(100)
	user.BillingAddress.Address1 = "second street"
	db := DB.Model(user).UpdateColumns(User{Age: newAge})
	if db.RowsAffected != 1 {
		t.Errorf("Expected RowsAffected=1 but instead RowsAffected=%v", DB.RowsAffected)
	}

	// Verify that Age now=`newAge`.
	freshUser := &User{Id: user.Id}
	DB.First(freshUser)
	if freshUser.Age != newAge {
		t.Errorf("Expected freshly queried user to have Age=%v but instead found Age=%v", newAge, freshUser.Age)
	}

	// Verify that user's BillingAddress.Address1 is not changed and is still "first street".
	DB.First(&freshUser.BillingAddress, freshUser.BillingAddressID)
	if freshUser.BillingAddress.Address1 != address1 {
		t.Errorf("Expected user's BillingAddress.Address1=%s to remain unchanged after UpdateColumns invocation, but BillingAddress.Address1=%s", address1, freshUser.BillingAddress.Address1)
	}
}

func TestUpdatesWithBlankValues(t *testing.T) {
	product := Product{Code: "product1", Price: 10}
	DB.Save(&product)

	DB.Model(&Product{Id: product.Id}).Updates(&Product{Price: 100})

	var product1 Product
	DB.First(&product1, product.Id)

	if product1.Code != "product1" || product1.Price != 100 {
		t.Errorf("product's code should not be updated")
	}
}

type ElementWithIgnoredField struct {
	Id           int64
	Value        string
	IgnoredField int64 `sql:"-"`
}

func (e ElementWithIgnoredField) TableName() string {
	return "element_with_ignored_field"
}

func TestUpdatesTableWithIgnoredValues(t *testing.T) {
	elem := ElementWithIgnoredField{Value: "foo", IgnoredField: 10}
	DB.Save(&elem)

	DB.Table(elem.TableName()).
		Where("id = ?", elem.Id).
		// DB.Model(&ElementWithIgnoredField{Id: elem.Id}).
		Updates(&ElementWithIgnoredField{Value: "bar", IgnoredField: 100})

	var elem1 ElementWithIgnoredField
	err := DB.First(&elem1, elem.Id).Error
	if err != nil {
		t.Errorf("error getting an element from database: %s", err.Error())
	}

	if elem1.IgnoredField != 0 {
		t.Errorf("element's ignored field should not be updated")
	}
}

func TestUpdateDecodeVirtualAttributes(t *testing.T) {
	var user = User{
		Name:     "jinzhu",
		IgnoreMe: 88,
	}

	DB.Save(&user)

	DB.Model(&user).Updates(User{Name: "jinzhu2", IgnoreMe: 100})

	if user.IgnoreMe != 100 {
		t.Errorf("should decode virtual attributes to struct, so it could be used in callbacks")
	}
}