forked from jinzhu/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
116 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,9 @@ | ||
package gorm_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func getPreloadUser(name string) User { | ||
var company Company | ||
DB.Where(Company{Name: "preload"}).FirstOrCreate(&company) | ||
|
||
return User{ | ||
Name: name, | ||
Role: Role{"Preload"}, | ||
BillingAddress: Address{Address1: fmt.Sprintf("Billing Address %v", name)}, | ||
ShippingAddress: Address{Address1: fmt.Sprintf("Shipping Address %v", name)}, | ||
CreditCard: CreditCard{Number: fmt.Sprintf("123456%v", name)}, | ||
Emails: []Email{ | ||
{Email: fmt.Sprintf("user_%[email protected]", name)}, {Email: fmt.Sprintf("user_%[email protected]", name)}, | ||
}, | ||
Company: company, | ||
Languages: []Language{ | ||
{Name: fmt.Sprintf("lang_1_%v", name)}, | ||
{Name: fmt.Sprintf("lang_2_%v", name)}, | ||
}, | ||
} | ||
import "testing" | ||
|
||
func getPreloadUser(name string) *User { | ||
return getPreparedUser(name, "Preload") | ||
} | ||
|
||
func checkUserHasPreloadData(user User, t *testing.T) { | ||
|
@@ -64,7 +44,7 @@ func checkUserHasPreloadData(user User, t *testing.T) { | |
|
||
func TestPreload(t *testing.T) { | ||
user1 := getPreloadUser("user1") | ||
DB.Save(&user1) | ||
DB.Save(user1) | ||
|
||
preloadDB := DB.Where("role = ?", "Preload").Preload("BillingAddress").Preload("ShippingAddress"). | ||
Preload("CreditCard").Preload("Emails").Preload("Company") | ||
|
@@ -73,10 +53,10 @@ func TestPreload(t *testing.T) { | |
checkUserHasPreloadData(user, t) | ||
|
||
user2 := getPreloadUser("user2") | ||
DB.Save(&user2) | ||
DB.Save(user2) | ||
|
||
user3 := getPreloadUser("user3") | ||
DB.Save(&user3) | ||
DB.Save(user3) | ||
|
||
var users []User | ||
preloadDB.Find(&users) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"database/sql" | ||
"database/sql/driver" | ||
"errors" | ||
"fmt" | ||
|
||
"reflect" | ||
"time" | ||
|
@@ -194,3 +195,25 @@ func (nt NullTime) Value() (driver.Value, error) { | |
} | ||
return nt.Time, nil | ||
} | ||
|
||
func getPreparedUser(name string, role string) *User { | ||
var company Company | ||
DB.Where(Company{Name: role}).FirstOrCreate(&company) | ||
|
||
return &User{ | ||
Name: name, | ||
Age: 20, | ||
Role: Role{role}, | ||
BillingAddress: Address{Address1: fmt.Sprintf("Billing Address %v", name)}, | ||
ShippingAddress: Address{Address1: fmt.Sprintf("Shipping Address %v", name)}, | ||
CreditCard: CreditCard{Number: fmt.Sprintf("123456%v", name)}, | ||
Emails: []Email{ | ||
{Email: fmt.Sprintf("user_%[email protected]", name)}, {Email: fmt.Sprintf("user_%[email protected]", name)}, | ||
}, | ||
Company: company, | ||
Languages: []Language{ | ||
{Name: fmt.Sprintf("lang_1_%v", name)}, | ||
{Name: fmt.Sprintf("lang_2_%v", name)}, | ||
}, | ||
} | ||
} |