mirror of https://github.com/go-gorm/gorm.git
Add name checker test, close #4007
This commit is contained in:
parent
cc61202fe2
commit
8500380e60
|
@ -15,6 +15,7 @@ func TestPostgres(t *testing.T) {
|
||||||
|
|
||||||
type Harumph struct {
|
type Harumph struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
Name string `gorm:"check:name_checker,name <> ''"`
|
||||||
Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
|
Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
|
||||||
Things pq.StringArray `gorm:"type:text[]"`
|
Things pq.StringArray `gorm:"type:text[]"`
|
||||||
}
|
}
|
||||||
|
@ -30,10 +31,17 @@ func TestPostgres(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
harumph := Harumph{}
|
harumph := Harumph{}
|
||||||
DB.Create(&harumph)
|
if err := DB.Create(&harumph).Error; err == nil {
|
||||||
|
t.Fatalf("should failed to create data, name can't be blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
harumph = Harumph{Name: "jinzhu"}
|
||||||
|
if err := DB.Create(&harumph).Error; err != nil {
|
||||||
|
t.Fatalf("should be able to create data, but got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
var result Harumph
|
var result Harumph
|
||||||
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil {
|
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu" {
|
||||||
t.Errorf("No error should happen, but got %v", err)
|
t.Errorf("No error should happen, but got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue