From 8500380e609be83dd7db46e1b29ee7ab69b6b2e2 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 27 Jan 2021 17:45:48 +0800 Subject: [PATCH] Add name checker test, close #4007 --- tests/postgres_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/postgres_test.go b/tests/postgres_test.go index 85cd34d4..94077d1d 100644 --- a/tests/postgres_test.go +++ b/tests/postgres_test.go @@ -15,6 +15,7 @@ func TestPostgres(t *testing.T) { type Harumph struct { gorm.Model + Name string `gorm:"check:name_checker,name <> ''"` Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"` Things pq.StringArray `gorm:"type:text[]"` } @@ -30,10 +31,17 @@ func TestPostgres(t *testing.T) { } 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 - 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) } }