Add RecordNotFound tests for method Related

This commit is contained in:
Jinzhu 2013-12-23 21:02:55 +08:00
parent b3d2f62f40
commit e2f8d29d7f
2 changed files with 10 additions and 1 deletions

View File

@ -830,12 +830,17 @@ if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
// If no record found, gorm will return RecordNotFound error, you could check it with // If no record found, gorm will return RecordNotFound error, you could check it with
db.Where("name = ?", "hello world").First(&User{}).Error == gorm.RecordNotFound db.Where("name = ?", "hello world").First(&User{}).Error == gorm.RecordNotFound
// or use shortcut
// Or use shortcut method
if db.Where("name = ?", "hello world").First(&user).RecordNotFound() { if db.Where("name = ?", "hello world").First(&user).RecordNotFound() {
panic("no record found") panic("no record found")
} else { } else {
user.Blalala() user.Blalala()
} }
if db.Model(&user).Related(&credit_card).RecordNotFound() {
panic("no credit card found")
}
``` ```
## Advanced Usage With Query Chain ## Advanced Usage With Query Chain

View File

@ -1282,6 +1282,10 @@ func TestRelated(t *testing.T) {
if user3.Id != user.Id || user3.Name != user.Name { if user3.Id != user.Id || user3.Name != user.Name {
t.Errorf("Should get user from credit card correctly") t.Errorf("Should get user from credit card correctly")
} }
if !db.Model(&CreditCard{}).Related(&User{}).RecordNotFound() {
t.Errorf("RecordNotFound for Related")
}
} }
type Order struct { type Order struct {