9 "github.com/jinzhu/now"
12 func TestCreate(t *testing.T) {
13 float := 35.03554004971999
15 user := User{Name: "CreateUser", Age: 18, Birthday: &now, UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float}
17 if !DB.NewRecord(user) || !DB.NewRecord(&user) {
18 t.Error("User should be new record before create")
21 if count := DB.Save(&user).RowsAffected; count != 1 {
22 t.Error("There should be one record be affected when create record")
25 if DB.NewRecord(user) || DB.NewRecord(&user) {
26 t.Error("User should not new record after save")
30 if err := DB.First(&newUser, user.Id).Error; err != nil {
31 t.Errorf("No error should happen, but got %v", err)
34 if !reflect.DeepEqual(newUser.PasswordHash, []byte{'f', 'a', 'k', '4'}) {
35 t.Errorf("User's PasswordHash should be saved ([]byte)")
38 if newUser.Age != 18 {
39 t.Errorf("User's Age should be saved (int)")
42 if newUser.UserNum != Num(111) {
43 t.Errorf("User's UserNum should be saved (custom type), but got %v", newUser.UserNum)
46 if newUser.Latitude != float {
47 t.Errorf("Float64 should not be changed after save")
50 if user.CreatedAt.IsZero() {
51 t.Errorf("Should have created_at after create")
54 if newUser.CreatedAt.IsZero() {
55 t.Errorf("Should have created_at after create")
58 DB.Model(user).Update("name", "create_user_new_name")
59 DB.First(&user, user.Id)
60 if user.CreatedAt.Format(time.RFC3339Nano) != newUser.CreatedAt.Format(time.RFC3339Nano) {
61 t.Errorf("CreatedAt should not be changed after update")
65 func TestCreateEmptyStrut(t *testing.T) {
66 type EmptyStruct struct {
69 DB.AutoMigrate(&EmptyStruct{})
71 if err := DB.Create(&EmptyStruct{}).Error; err != nil {
72 t.Errorf("No error should happen when creating user, but got %v", err)
76 func TestCreateWithExistingTimestamp(t *testing.T) {
77 user := User{Name: "CreateUserExistingTimestamp"}
79 timeA := now.MustParse("2016-01-01")
80 user.CreatedAt = timeA
81 user.UpdatedAt = timeA
84 if user.CreatedAt.UTC().Format(time.RFC3339) != timeA.UTC().Format(time.RFC3339) {
85 t.Errorf("CreatedAt should not be changed")
88 if user.UpdatedAt.UTC().Format(time.RFC3339) != timeA.UTC().Format(time.RFC3339) {
89 t.Errorf("UpdatedAt should not be changed")
93 DB.First(&newUser, user.Id)
95 if newUser.CreatedAt.UTC().Format(time.RFC3339) != timeA.UTC().Format(time.RFC3339) {
96 t.Errorf("CreatedAt should not be changed")
99 if newUser.UpdatedAt.UTC().Format(time.RFC3339) != timeA.UTC().Format(time.RFC3339) {
100 t.Errorf("UpdatedAt should not be changed")
104 type AutoIncrementUser struct {
106 Sequence uint `gorm:"AUTO_INCREMENT"`
109 func TestCreateWithAutoIncrement(t *testing.T) {
110 if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" {
111 t.Skip("Skipping this because only postgres properly support auto_increment on a non-primary_key column")
114 DB.AutoMigrate(&AutoIncrementUser{})
116 user1 := AutoIncrementUser{}
117 user2 := AutoIncrementUser{}
122 if user2.Sequence-user1.Sequence != 1 {
123 t.Errorf("Auto increment should apply on Sequence")
127 func TestCreateWithNoGORMPrimayKey(t *testing.T) {
128 if dialect := os.Getenv("GORM_DIALECT"); dialect == "mssql" {
129 t.Skip("Skipping this because MSSQL will return identity only if the table has an Id column")
132 jt := JoinTable{From: 1, To: 2}
133 err := DB.Create(&jt).Error
135 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)
139 func TestCreateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
140 animal := Animal{Name: "Ferdinand"}
141 if DB.Save(&animal).Error != nil {
142 t.Errorf("No error should happen when create a record without std primary key")
145 if animal.Counter == 0 {
146 t.Errorf("No std primary key should be filled value after create")
149 if animal.Name != "Ferdinand" {
150 t.Errorf("Default value should be overrided")
153 // Test create with default value not overrided
154 an := Animal{From: "nerdz"}
156 if DB.Save(&an).Error != nil {
157 t.Errorf("No error should happen when create an record without std primary key")
160 // We must fetch the value again, to have the default fields updated
161 // (We can't do this in the update statements, since sql default can be expressions
162 // And be different from the fields' type (eg. a time.Time fields has a default value of "now()"
163 DB.Model(Animal{}).Where(&Animal{Counter: an.Counter}).First(&an)
165 if an.Name != "galeone" {
166 t.Errorf("Default value should fill the field. But got %v", an.Name)
170 func TestAnonymousScanner(t *testing.T) {
171 user := User{Name: "anonymous_scanner", Role: Role{Name: "admin"}}
175 DB.First(&user2, "name = ?", "anonymous_scanner")
176 if user2.Role.Name != "admin" {
177 t.Errorf("Should be able to get anonymous scanner")
180 if !user2.Role.IsAdmin() {
181 t.Errorf("Should be able to get anonymous scanner")
185 func TestAnonymousField(t *testing.T) {
186 user := User{Name: "anonymous_field", Company: Company{Name: "company"}}
190 DB.First(&user2, "name = ?", "anonymous_field")
191 DB.Model(&user2).Related(&user2.Company)
192 if user2.Company.Name != "company" {
193 t.Errorf("Should be able to get anonymous field")
197 func TestSelectWithCreate(t *testing.T) {
198 user := getPreparedUser("select_user", "select_with_create")
199 DB.Select("Name", "BillingAddress", "CreditCard", "Company", "Emails").Create(user)
202 DB.Preload("BillingAddress").Preload("ShippingAddress").
203 Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryuser, user.Id)
205 if queryuser.Name != user.Name || queryuser.Age == user.Age {
206 t.Errorf("Should only create users with name column")
209 if queryuser.BillingAddressID.Int64 == 0 || queryuser.ShippingAddressId != 0 ||
210 queryuser.CreditCard.ID == 0 || len(queryuser.Emails) == 0 {
211 t.Errorf("Should only create selected relationships")
215 func TestOmitWithCreate(t *testing.T) {
216 user := getPreparedUser("omit_user", "omit_with_create")
217 DB.Omit("Name", "BillingAddress", "CreditCard", "Company", "Emails").Create(user)
220 DB.Preload("BillingAddress").Preload("ShippingAddress").
221 Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryuser, user.Id)
223 if queryuser.Name == user.Name || queryuser.Age != user.Age {
224 t.Errorf("Should only create users with age column")
227 if queryuser.BillingAddressID.Int64 != 0 || queryuser.ShippingAddressId == 0 ||
228 queryuser.CreditCard.ID != 0 || len(queryuser.Emails) != 0 {
229 t.Errorf("Should not create omitted relationships")