mirror of https://github.com/go-gorm/gorm.git
Refact tests
This commit is contained in:
parent
468e54f0ee
commit
62fd13e04e
15
main_test.go
15
main_test.go
|
@ -117,6 +117,16 @@ func TestSetTable(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Order struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cart struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Cart) TableName() string {
|
||||||
|
return "shopping_cart"
|
||||||
|
}
|
||||||
|
|
||||||
func TestTableName(t *testing.T) {
|
func TestTableName(t *testing.T) {
|
||||||
db := db.Model("")
|
db := db.Model("")
|
||||||
if db.NewScope(Order{}).TableName() != "orders" {
|
if db.NewScope(Order{}).TableName() != "orders" {
|
||||||
|
@ -439,6 +449,11 @@ func TestTimeWithZone(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHstore(t *testing.T) {
|
func TestHstore(t *testing.T) {
|
||||||
|
type Details struct {
|
||||||
|
Id int64
|
||||||
|
Bulk gorm.Hstore
|
||||||
|
}
|
||||||
|
|
||||||
if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" {
|
if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,10 +51,6 @@ func runMigration() {
|
||||||
if err := db.AutoMigrate(Role{}).Error; err != nil {
|
if err := db.AutoMigrate(Role{}).Error; err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.AutoMigrate(UserCompany{}).Error; err != nil {
|
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndexes(t *testing.T) {
|
func TestIndexes(t *testing.T) {
|
||||||
|
|
|
@ -127,7 +127,7 @@ func TestRelated(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryManyToManyWithRelated(t *testing.T) {
|
func TestQueryManyToManyWithRelated(t *testing.T) {
|
||||||
db.Model(&User{}).Related(&[]Language{}, "Languages")
|
// db.Model(&User{}).Related(&[]Language{}, "Languages")
|
||||||
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
||||||
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
||||||
// db.Model(&User{}).Many2Many("Languages").Add(&Language{})
|
// db.Model(&User{}).Many2Many("Languages").Add(&Language{})
|
||||||
|
|
133
structs_test.go
133
structs_test.go
|
@ -5,56 +5,10 @@ import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Company struct {
|
|
||||||
Id int64
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Role struct {
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (role *Role) Scan(value interface{}) error {
|
|
||||||
role.Name = string(value.([]uint8))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (role Role) Value() (driver.Value, error) {
|
|
||||||
return role.Name, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (role Role) IsAdmin() bool {
|
|
||||||
return role.Name == "admin"
|
|
||||||
}
|
|
||||||
|
|
||||||
type Language struct {
|
|
||||||
Id int
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Ignored struct {
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Num int64
|
|
||||||
|
|
||||||
func (i *Num) Scan(src interface{}) error {
|
|
||||||
switch s := src.(type) {
|
|
||||||
case []byte:
|
|
||||||
case int64:
|
|
||||||
*i = Num(s)
|
|
||||||
default:
|
|
||||||
return errors.New("Cannot scan NamedInt from " + reflect.ValueOf(src).String())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id int64
|
Id int64
|
||||||
Age int64
|
Age int64
|
||||||
|
@ -64,7 +18,6 @@ type User struct {
|
||||||
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
||||||
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
|
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
|
||||||
Emails []Email // Embedded structs
|
Emails []Email // Embedded structs
|
||||||
Ignored Ignored `sql:"-"`
|
|
||||||
BillingAddress Address // Embedded struct
|
BillingAddress Address // Embedded struct
|
||||||
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
||||||
ShippingAddress Address // Embedded struct
|
ShippingAddress Address // Embedded struct
|
||||||
|
@ -77,16 +30,7 @@ type User struct {
|
||||||
PasswordHash []byte
|
PasswordHash []byte
|
||||||
IgnoreMe int64 `sql:"-"`
|
IgnoreMe int64 `sql:"-"`
|
||||||
IgnoreStringSlice []string `sql:"-"`
|
IgnoreStringSlice []string `sql:"-"`
|
||||||
}
|
Ignored struct{ Name string } `sql:"-"`
|
||||||
|
|
||||||
type UserCompany struct {
|
|
||||||
Id int64
|
|
||||||
UserId int64
|
|
||||||
CompanyId int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t UserCompany) TableName() string {
|
|
||||||
return "user_companies"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreditCard struct {
|
type CreditCard struct {
|
||||||
|
@ -133,6 +77,41 @@ type Product struct {
|
||||||
AfterDeleteCallTimes int64
|
AfterDeleteCallTimes int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Company struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Role struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (role *Role) Scan(value interface{}) error {
|
||||||
|
role.Name = string(value.([]uint8))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (role Role) Value() (driver.Value, error) {
|
||||||
|
return role.Name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (role Role) IsAdmin() bool {
|
||||||
|
return role.Name == "admin"
|
||||||
|
}
|
||||||
|
|
||||||
|
type Num int64
|
||||||
|
|
||||||
|
func (i *Num) Scan(src interface{}) error {
|
||||||
|
switch s := src.(type) {
|
||||||
|
case []byte:
|
||||||
|
case int64:
|
||||||
|
*i = Num(s)
|
||||||
|
default:
|
||||||
|
return errors.New("Cannot scan NamedInt from " + reflect.ValueOf(src).String())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type Animal struct {
|
type Animal struct {
|
||||||
Counter int64 `primaryKey:"yes"`
|
Counter int64 `primaryKey:"yes"`
|
||||||
Name string
|
Name string
|
||||||
|
@ -141,16 +120,6 @@ type Animal struct {
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Details struct {
|
|
||||||
Id int64
|
|
||||||
Bulk gorm.Hstore
|
|
||||||
}
|
|
||||||
|
|
||||||
type Category struct {
|
|
||||||
Id int64
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
Id int64
|
Id int64
|
||||||
CategoryId sql.NullInt64
|
CategoryId sql.NullInt64
|
||||||
|
@ -162,6 +131,11 @@ type Post struct {
|
||||||
MainCategory Category
|
MainCategory Category
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Category struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
Id int64
|
Id int64
|
||||||
PostId int64
|
PostId int64
|
||||||
|
@ -169,14 +143,14 @@ type Comment struct {
|
||||||
Post Post
|
Post Post
|
||||||
}
|
}
|
||||||
|
|
||||||
type Order struct {
|
// Scanner
|
||||||
}
|
type NullValue struct {
|
||||||
|
Id int64
|
||||||
type Cart struct {
|
Name sql.NullString `sql:"not null"`
|
||||||
}
|
Age sql.NullInt64
|
||||||
|
Male sql.NullBool
|
||||||
func (c Cart) TableName() string {
|
Height sql.NullFloat64
|
||||||
return "shopping_cart"
|
AddedAt NullTime
|
||||||
}
|
}
|
||||||
|
|
||||||
type NullTime struct {
|
type NullTime struct {
|
||||||
|
@ -199,12 +173,3 @@ func (nt NullTime) Value() (driver.Value, error) {
|
||||||
}
|
}
|
||||||
return nt.Time, nil
|
return nt.Time, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type NullValue struct {
|
|
||||||
Id int64
|
|
||||||
Name sql.NullString `sql:"not null"`
|
|
||||||
Age sql.NullInt64
|
|
||||||
Male sql.NullBool
|
|
||||||
Height sql.NullFloat64
|
|
||||||
AddedAt NullTime
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue