create_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package gorm_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestCreate(t *testing.T) {
  8. float := 35.03554004971999
  9. user := User{Name: "CreateUser", Age: 18, Birthday: time.Now(), UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float}
  10. if !DB.NewRecord(user) || !DB.NewRecord(&user) {
  11. t.Error("User should be new record before create")
  12. }
  13. if count := DB.Save(&user).RowsAffected; count != 1 {
  14. t.Error("There should be one record be affected when create record")
  15. }
  16. if DB.NewRecord(user) || DB.NewRecord(&user) {
  17. t.Error("User should not new record after save")
  18. }
  19. var newUser User
  20. DB.First(&newUser, user.Id)
  21. if !reflect.DeepEqual(newUser.PasswordHash, []byte{'f', 'a', 'k', '4'}) {
  22. t.Errorf("User's PasswordHash should be saved ([]byte)")
  23. }
  24. if newUser.Age != 18 {
  25. t.Errorf("User's Age should be saved (int)")
  26. }
  27. if newUser.UserNum != Num(111) {
  28. t.Errorf("User's UserNum should be saved (custom type)")
  29. }
  30. if newUser.Latitude != float {
  31. t.Errorf("Float64 should not be changed after save")
  32. }
  33. if user.CreatedAt.IsZero() {
  34. t.Errorf("Should have created_at after create")
  35. }
  36. if newUser.CreatedAt.IsZero() {
  37. t.Errorf("Should have created_at after create")
  38. }
  39. DB.Model(user).Update("name", "create_user_new_name")
  40. DB.First(&user, user.Id)
  41. if user.CreatedAt != newUser.CreatedAt {
  42. t.Errorf("CreatedAt should not be changed after update")
  43. }
  44. }
  45. func TestCreateWithNoGORMPrimayKey(t *testing.T) {
  46. jt := JoinTable{From: 1, To: 2}
  47. err := DB.Create(&jt).Error
  48. if err != nil {
  49. t.Errorf("No error should happen when create a record without a GORM primary key. But in the database this primary key exists and is the union of 2 or more fields\n But got: %s", err)
  50. }
  51. }
  52. func TestCreateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
  53. animal := Animal{Name: "Ferdinand"}
  54. if DB.Save(&animal).Error != nil {
  55. t.Errorf("No error should happen when create a record without std primary key")
  56. }
  57. if animal.Counter == 0 {
  58. t.Errorf("No std primary key should be filled value after create")
  59. }
  60. if animal.Name != "Ferdinand" {
  61. t.Errorf("Default value should be overrided")
  62. }
  63. // Test create with default value not overrided
  64. an := Animal{From: "nerdz"}
  65. if DB.Save(&an).Error != nil {
  66. t.Errorf("No error should happen when create an record without std primary key")
  67. }
  68. // We must fetch the value again, to have the default fields updated
  69. // (We can't do this in the update statements, since sql default can be expressions
  70. // And be different from the fields' type (eg. a time.Time fiels has a default value of "now()"
  71. DB.Model(Animal{}).Where(&Animal{Counter: an.Counter}).First(&an)
  72. if an.Name != "galeone" {
  73. t.Errorf("Default value should fill the field. But got %v", an.Name)
  74. }
  75. }
  76. func TestAnonymousScanner(t *testing.T) {
  77. user := User{Name: "anonymous_scanner", Role: Role{Name: "admin"}}
  78. DB.Save(&user)
  79. var user2 User
  80. DB.First(&user2, "name = ?", "anonymous_scanner")
  81. if user2.Role.Name != "admin" {
  82. t.Errorf("Should be able to get anonymous scanner")
  83. }
  84. if !user2.IsAdmin() {
  85. t.Errorf("Should be able to get anonymous scanner")
  86. }
  87. }
  88. func TestAnonymousField(t *testing.T) {
  89. user := User{Name: "anonymous_field", Company: Company{Name: "company"}}
  90. DB.Save(&user)
  91. var user2 User
  92. DB.First(&user2, "name = ?", "anonymous_field")
  93. DB.Model(&user2).Related(&user2.Company)
  94. if user2.Company.Name != "company" {
  95. t.Errorf("Should be able to get anonymous field")
  96. }
  97. }