update_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package gorm_test
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestUpdate(t *testing.T) {
  7. product1 := Product{Code: "product1code"}
  8. product2 := Product{Code: "product2code"}
  9. DB.Save(&product1).Save(&product2).Update("code", "product2newcode")
  10. if product2.Code != "product2newcode" {
  11. t.Errorf("Record should be updated")
  12. }
  13. DB.First(&product1, product1.Id)
  14. DB.First(&product2, product2.Id)
  15. updatedAt1 := product1.UpdatedAt
  16. updatedAt2 := product2.UpdatedAt
  17. var product3 Product
  18. DB.First(&product3, product2.Id).Update("code", "product2newcode")
  19. if updatedAt2.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
  20. t.Errorf("updatedAt should not be updated if nothing changed")
  21. }
  22. if DB.First(&Product{}, "code = ?", product1.Code).RecordNotFound() {
  23. t.Errorf("Product1 should not be updated")
  24. }
  25. if !DB.First(&Product{}, "code = ?", "product2code").RecordNotFound() {
  26. t.Errorf("Product2's code should be updated")
  27. }
  28. if DB.First(&Product{}, "code = ?", "product2newcode").RecordNotFound() {
  29. t.Errorf("Product2's code should be updated")
  30. }
  31. DB.Table("products").Where("code in (?)", []string{"product1code"}).Update("code", "product1newcode")
  32. var product4 Product
  33. DB.First(&product4, product1.Id)
  34. if updatedAt1.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
  35. t.Errorf("updatedAt should be updated if something changed")
  36. }
  37. if !DB.First(&Product{}, "code = 'product1code'").RecordNotFound() {
  38. t.Errorf("Product1's code should be updated")
  39. }
  40. if DB.First(&Product{}, "code = 'product1newcode'").RecordNotFound() {
  41. t.Errorf("Product should not be changed to 789")
  42. }
  43. if DB.Model(product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
  44. t.Error("No error should raise when update with CamelCase")
  45. }
  46. if DB.Model(&product2).UpdateColumn("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
  47. t.Error("No error should raise when update_column with CamelCase")
  48. }
  49. var products []Product
  50. DB.Find(&products)
  51. if count := DB.Model(Product{}).Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(products)) {
  52. t.Error("RowsAffected should be correct when do batch update")
  53. }
  54. }
  55. func TestUpdateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
  56. animal := Animal{Name: "Ferdinand"}
  57. DB.Save(&animal)
  58. updatedAt1 := animal.UpdatedAt
  59. DB.Save(&animal).Update("name", "Francis")
  60. if updatedAt1.Format(time.RFC3339Nano) == animal.UpdatedAt.Format(time.RFC3339Nano) {
  61. t.Errorf("updatedAt should not be updated if nothing changed")
  62. }
  63. var animals []Animal
  64. DB.Find(&animals)
  65. if count := DB.Model(Animal{}).Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(animals)) {
  66. t.Error("RowsAffected should be correct when do batch update")
  67. }
  68. animal = Animal{From: "somewhere"} // No name fields, should be filled with the default value (galeone)
  69. DB.Save(&animal).Update("From", "a nice place") // The name field shoul be untouched
  70. DB.First(&animal, animal.Counter)
  71. if animal.Name != "galeone" {
  72. t.Errorf("Name fiels shouldn't be changed if untouched, but got %v", animal.Name)
  73. }
  74. // When changing a field with a default value, the change must occur
  75. animal.Name = "amazing horse"
  76. DB.Save(&animal)
  77. DB.First(&animal, animal.Counter)
  78. if animal.Name != "amazing horse" {
  79. t.Errorf("Update a filed with a default value should occur. But got %v\n", animal.Name)
  80. }
  81. }
  82. func TestUpdates(t *testing.T) {
  83. product1 := Product{Code: "product1code", Price: 10}
  84. product2 := Product{Code: "product2code", Price: 10}
  85. DB.Save(&product1).Save(&product2)
  86. DB.Model(&product1).Updates(map[string]interface{}{"code": "product1newcode", "price": 100})
  87. if product1.Code != "product1newcode" || product1.Price != 100 {
  88. t.Errorf("Record should be updated also with map")
  89. }
  90. DB.First(&product1, product1.Id)
  91. DB.First(&product2, product2.Id)
  92. updatedAt1 := product1.UpdatedAt
  93. updatedAt2 := product2.UpdatedAt
  94. var product3 Product
  95. DB.First(&product3, product1.Id).Updates(Product{Code: "product1newcode", Price: 100})
  96. if product3.Code != "product1newcode" || product3.Price != 100 {
  97. t.Errorf("Record should be updated with struct")
  98. }
  99. if updatedAt1.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
  100. t.Errorf("updatedAt should not be updated if nothing changed")
  101. }
  102. if DB.First(&Product{}, "code = ? and price = ?", product2.Code, product2.Price).RecordNotFound() {
  103. t.Errorf("Product2 should not be updated")
  104. }
  105. if DB.First(&Product{}, "code = ?", "product1newcode").RecordNotFound() {
  106. t.Errorf("Product1 should be updated")
  107. }
  108. DB.Table("products").Where("code in (?)", []string{"product2code"}).Updates(Product{Code: "product2newcode"})
  109. if !DB.First(&Product{}, "code = 'product2code'").RecordNotFound() {
  110. t.Errorf("Product2's code should be updated")
  111. }
  112. var product4 Product
  113. DB.First(&product4, product2.Id)
  114. if updatedAt2.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
  115. t.Errorf("updatedAt should be updated if something changed")
  116. }
  117. if DB.First(&Product{}, "code = ?", "product2newcode").RecordNotFound() {
  118. t.Errorf("product2's code should be updated")
  119. }
  120. }
  121. func TestUpdateColumn(t *testing.T) {
  122. product1 := Product{Code: "product1code", Price: 10}
  123. product2 := Product{Code: "product2code", Price: 20}
  124. DB.Save(&product1).Save(&product2).UpdateColumn(map[string]interface{}{"code": "product2newcode", "price": 100})
  125. if product2.Code != "product2newcode" || product2.Price != 100 {
  126. t.Errorf("product 2 should be updated with update column")
  127. }
  128. var product3 Product
  129. DB.First(&product3, product1.Id)
  130. if product3.Code != "product1code" || product3.Price != 10 {
  131. t.Errorf("product 1 should not be updated")
  132. }
  133. DB.First(&product2, product2.Id)
  134. updatedAt2 := product2.UpdatedAt
  135. DB.Model(product2).UpdateColumn("code", "update_column_new")
  136. var product4 Product
  137. DB.First(&product4, product2.Id)
  138. if updatedAt2.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
  139. t.Errorf("updatedAt should not be updated with update column")
  140. }
  141. }