Skip to content

Commit

Permalink
Refact tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 29, 2014
1 parent 6a00344 commit cb7d545
Show file tree
Hide file tree
Showing 9 changed files with 2,109 additions and 2,114 deletions.
10 changes: 5 additions & 5 deletions callback_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func SaveBeforeAssociations(scope *Scope) {
newDB := scope.NewDB()

if value.CanAddr() {
newDB.Save(value.Addr().Interface())
scope.Err(newDB.Save(value.Addr().Interface()).Error)
} else {
// If can't take address, then clone the value and set it back
value = reflect.New(reflect.ValueOf(field.Value).Type()).Elem()
for _, f := range newDB.NewScope(field.Value).Fields() {
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
}
newDB.Save(value.Addr().Interface())
scope.Err(newDB.Save(value.Addr().Interface()).Error)
scope.SetColumn(field.Name, value.Interface())
}

Expand All @@ -50,13 +50,13 @@ func SaveAfterAssociations(scope *Scope) {
newDB.NewScope(elem).SetColumn(field.ForeignKey, scope.PrimaryKeyValue())
}

newDB.Save(elem)
scope.Err(newDB.Save(elem).Error)
}
default:
newDB := scope.NewDB()
if value.CanAddr() {
newDB.NewScope(field.Value).SetColumn(field.ForeignKey, scope.PrimaryKeyValue())
newDB.Save(field.Value)
scope.Err(newDB.Save(field.Value).Error)
} else {
destValue := reflect.New(reflect.TypeOf(field.Value)).Elem()

Expand All @@ -66,7 +66,7 @@ func SaveAfterAssociations(scope *Scope) {

elem := destValue.Addr().Interface()
newDB.NewScope(elem).SetColumn(field.ForeignKey, scope.PrimaryKeyValue())
newDB.Save(elem)
scope.Err(newDB.Save(elem).Error)
scope.SetColumn(field.Name, destValue.Interface())
}
}
Expand Down
38 changes: 38 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gorm_test

import (
"reflect"
"testing"
"time"
)

func TestCreate(t *testing.T) {
user := User{Name: "1", Age: 18, Birthday: time.Now(), UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}}

if !db.NewRecord(user) || !db.NewRecord(&user) {
t.Error("User should be new record before create")
}

if count := db.Save(&user).RowsAffected; count != 1 {
t.Error("There should be one record be affected when create record")
}

if db.NewRecord(user) || db.NewRecord(&user) {
t.Error("User should not new record after save")
}

var newUser User
db.First(&newUser, user.Id)

if !reflect.DeepEqual(newUser.PasswordHash, []byte{'f', 'a', 'k', '4'}) {
t.Errorf("User's PasswordHash should be saved ([]byte)")
}

if newUser.Age != 18 {
t.Errorf("User's Age should be saved (int)")
}

if newUser.UserNum != Num(111) {
t.Errorf("User's UserNum should be saved (custom type)")
}
}
68 changes: 68 additions & 0 deletions delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package gorm_test

import (
"testing"
"time"
)

func TestDelete(t *testing.T) {
user1, user2 := User{Name: "delete1"}, User{Name: "delete2"}
db.Save(&user1)
db.Save(&user2)

if db.Delete(&user1).Error != nil {
t.Errorf("No error should happen when delete a record")
}

if !db.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}

if db.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
t.Errorf("Other users that not deleted should be found-able")
}
}

func TestInlineDelete(t *testing.T) {
user1, user2 := User{Name: "inline_delete1"}, User{Name: "inline_delete2"}
db.Save(&user1)
db.Save(&user2)

if db.Delete(&User{}, user1.Id).Error != nil {
t.Errorf("No error should happen when delete a record")
} else if !db.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}

if db.Delete(&User{}, "name = ?", user2.Name).Error != nil {
t.Errorf("No error should happen when delete a record")
} else if !db.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}
}

func TestSoftDelete(t *testing.T) {
type User struct {
Id int64
Name string
DeletedAt time.Time
}
db.AutoMigrate(&User{})

user := User{Name: "soft_delete"}
db.Save(&user)
db.Delete(&user)

if db.First(&User{}, "name = ?", user.Name).Error == nil {
t.Errorf("Can't find a soft deleted record")
}

if db.Unscoped().First(&User{}, "name = ?", user.Name).Error != nil {
t.Errorf("Should be able to find soft deleted record with Unscoped")
}

db.Unscoped().Delete(&user)
if !db.Unscoped().First(&User{}, "name = ?", user.Name).RecordNotFound() {
t.Errorf("Can't find permanently deleted record")
}
}
Loading

0 comments on commit cb7d545

Please sign in to comment.