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) {
|
||||
db := db.Model("")
|
||||
if db.NewScope(Order{}).TableName() != "orders" {
|
||||
|
@ -439,6 +449,11 @@ func TestTimeWithZone(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHstore(t *testing.T) {
|
||||
type Details struct {
|
||||
Id int64
|
||||
Bulk gorm.Hstore
|
||||
}
|
||||
|
||||
if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" {
|
||||
t.Skip()
|
||||
}
|
||||
|
|
|
@ -51,10 +51,6 @@ func runMigration() {
|
|||
if err := db.AutoMigrate(Role{}).Error; err != nil {
|
||||
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) {
|
||||
|
|
|
@ -127,7 +127,7 @@ func TestRelated(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
|
||||
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
||||
// db.Model(&User{}).Many2Many("Languages").Add(&Language{})
|
||||
|
|
137
structs_test.go
137
structs_test.go
|
@ -5,56 +5,10 @@ import (
|
|||
"database/sql/driver"
|
||||
"errors"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"reflect"
|
||||
"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 {
|
||||
Id int64
|
||||
Age int64
|
||||
|
@ -64,7 +18,6 @@ type User struct {
|
|||
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
|
||||
Emails []Email // Embedded structs
|
||||
Ignored Ignored `sql:"-"`
|
||||
BillingAddress Address // Embedded struct
|
||||
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
||||
ShippingAddress Address // Embedded struct
|
||||
|
@ -75,18 +28,9 @@ type User struct {
|
|||
Company
|
||||
Role
|
||||
PasswordHash []byte
|
||||
IgnoreMe int64 `sql:"-"`
|
||||
IgnoreStringSlice []string `sql:"-"`
|
||||
}
|
||||
|
||||
type UserCompany struct {
|
||||
Id int64
|
||||
UserId int64
|
||||
CompanyId int64
|
||||
}
|
||||
|
||||
func (t UserCompany) TableName() string {
|
||||
return "user_companies"
|
||||
IgnoreMe int64 `sql:"-"`
|
||||
IgnoreStringSlice []string `sql:"-"`
|
||||
Ignored struct{ Name string } `sql:"-"`
|
||||
}
|
||||
|
||||
type CreditCard struct {
|
||||
|
@ -133,6 +77,41 @@ type Product struct {
|
|||
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 {
|
||||
Counter int64 `primaryKey:"yes"`
|
||||
Name string
|
||||
|
@ -141,16 +120,6 @@ type Animal struct {
|
|||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type Details struct {
|
||||
Id int64
|
||||
Bulk gorm.Hstore
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type Post struct {
|
||||
Id int64
|
||||
CategoryId sql.NullInt64
|
||||
|
@ -162,6 +131,11 @@ type Post struct {
|
|||
MainCategory Category
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
Id int64
|
||||
PostId int64
|
||||
|
@ -169,14 +143,14 @@ type Comment struct {
|
|||
Post Post
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
}
|
||||
|
||||
type Cart struct {
|
||||
}
|
||||
|
||||
func (c Cart) TableName() string {
|
||||
return "shopping_cart"
|
||||
// Scanner
|
||||
type NullValue struct {
|
||||
Id int64
|
||||
Name sql.NullString `sql:"not null"`
|
||||
Age sql.NullInt64
|
||||
Male sql.NullBool
|
||||
Height sql.NullFloat64
|
||||
AddedAt NullTime
|
||||
}
|
||||
|
||||
type NullTime struct {
|
||||
|
@ -199,12 +173,3 @@ func (nt NullTime) Value() (driver.Value, error) {
|
|||
}
|
||||
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