From 5d52826b3dc2957e8da1b59d7a874efb1980b440 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 12 Mar 2015 18:01:27 +0800 Subject: [PATCH] Create with Omit --- create_test.go | 36 ++++++++++++++++++++++++++---------- main.go | 4 ++++ query_test.go | 28 ++++++++++++++++++---------- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/create_test.go b/create_test.go index f8f4d642..e3c09d05 100644 --- a/create_test.go +++ b/create_test.go @@ -1,7 +1,6 @@ package gorm_test import ( - "fmt" "reflect" "testing" "time" @@ -123,21 +122,38 @@ func TestAnonymousField(t *testing.T) { } } -func TestSelectCreate(t *testing.T) { - user := getPreparedUser("user1", "select_create") +func TestSelectWithCreate(t *testing.T) { + user := getPreparedUser("select_user", "select_with_create") DB.Select("Name", "BillingAddress", "CreditCard", "Company", "Emails").Create(&user) - var user2 User + var queryuser User DB.Preload("BillingAddress").Preload("ShippingAddress"). - Preload("CreditCard").Preload("Emails").Preload("Company").First(&user2, user.Id) + Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryuser, user.Id) - if user2.Name != user.Name || user2.Age == user.Age { + if queryuser.Name != user.Name || queryuser.Age == user.Age { t.Errorf("Should only create users with name column") } - fmt.Println(user2.CreditCard.ID) - if user2.BillingAddressID.Int64 == 0 || user2.ShippingAddressId != 0 || - user2.CreditCard.ID == 0 || len(user2.Emails) == 0 { - t.Errorf("Should only create users with name column") + if queryuser.BillingAddressID.Int64 == 0 || queryuser.ShippingAddressId != 0 || + queryuser.CreditCard.ID == 0 || len(queryuser.Emails) == 0 { + t.Errorf("Should only create selected relationships") + } +} + +func TestOmitWithCreate(t *testing.T) { + user := getPreparedUser("omit_user", "omit_with_create") + DB.Omit("Name", "BillingAddress", "CreditCard", "Company", "Emails").Create(&user) + + var queryuser User + DB.Preload("BillingAddress").Preload("ShippingAddress"). + Preload("CreditCard").Preload("Emails").Preload("Company").First(&queryuser, user.Id) + + if queryuser.Name == user.Name || queryuser.Age != user.Age { + t.Errorf("Should only create users with age column") + } + + if queryuser.BillingAddressID.Int64 != 0 || queryuser.ShippingAddressId == 0 || + queryuser.CreditCard.ID != 0 || len(queryuser.Emails) != 0 { + t.Errorf("Should not create omited relationships") } } diff --git a/main.go b/main.go index b142e275..87fecd59 100644 --- a/main.go +++ b/main.go @@ -155,6 +155,10 @@ func (s *DB) Select(query interface{}, args ...interface{}) *DB { return s.clone().search.Select(query, args...).db } +func (s *DB) Omit(columns ...string) *DB { + return s.clone().search.Omit(columns...).db +} + func (s *DB) Group(query string) *DB { return s.clone().search.Group(query).db } diff --git a/query_test.go b/query_test.go index 684c6f1e..d84fae93 100644 --- a/query_test.go +++ b/query_test.go @@ -367,8 +367,16 @@ func TestCount(t *testing.T) { } func TestNot(t *testing.T) { + DB.Create(getPreparedUser("user1", "not")) + DB.Create(getPreparedUser("user2", "not")) + DB.Create(getPreparedUser("user3", "not")) + DB.Create(getPreparedUser("user4", "not")) + DB := DB.Where("role = ?", "not") + var users1, users2, users3, users4, users5, users6, users7, users8 []User - DB.Find(&users1) + if DB.Find(&users1).RowsAffected != 4 { + t.Errorf("should find 4 not users") + } DB.Not(users1[0].Id).Find(&users2) if len(users1)-len(users2) != 1 { @@ -381,43 +389,43 @@ func TestNot(t *testing.T) { } var name3Count int64 - DB.Table("users").Where("name = ?", "3").Count(&name3Count) - DB.Not("name", "3").Find(&users4) + DB.Table("users").Where("name = ?", "user3").Count(&name3Count) + DB.Not("name", "user3").Find(&users4) if len(users1)-len(users4) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } users4 = []User{} - DB.Not("name = ?", "3").Find(&users4) + DB.Not("name = ?", "user3").Find(&users4) if len(users1)-len(users4) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } users4 = []User{} - DB.Not("name <> ?", "3").Find(&users4) + DB.Not("name <> ?", "user3").Find(&users4) if len(users4) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } - DB.Not(User{Name: "3"}).Find(&users5) + DB.Not(User{Name: "user3"}).Find(&users5) if len(users1)-len(users5) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } - DB.Not(map[string]interface{}{"name": "3"}).Find(&users6) + DB.Not(map[string]interface{}{"name": "user3"}).Find(&users6) if len(users1)-len(users6) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } - DB.Not("name", []string{"3"}).Find(&users7) + DB.Not("name", []string{"user3"}).Find(&users7) if len(users1)-len(users7) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } var name2Count int64 - DB.Table("users").Where("name = ?", "2").Count(&name2Count) - DB.Not("name", []string{"3", "2"}).Find(&users8) + DB.Table("users").Where("name = ?", "user2").Count(&name2Count) + DB.Not("name", []string{"user3", "user2"}).Find(&users8) if len(users1)-len(users8) != (int(name3Count) + int(name2Count)) { t.Errorf("Should find all users's name not equal 3") }