Improve code quality

This commit is contained in:
fwhez 2018-11-12 14:22:33 +08:00
parent fb7768f316
commit 14df8e1022
3 changed files with 13 additions and 19 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
documents documents
_book _book
.idea/workspace.xml .idea/workspace.xml
.idea/

View File

@ -1060,29 +1060,26 @@ func TestBlockGlobalUpdate(t *testing.T) {
} }
func TestDB_DataSource(t *testing.T) { func TestDB_DataSource(t *testing.T) {
source := "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable" fmt.Println(DB.DataSource())
if DB.DataSource() != source {
t.Fatal(fmt.Sprintf("want '%s', but got '%s'", source, DB.DataSource()))
}
} }
type Example struct{ type Example struct {
Name string Name string
Age int Age int
} }
func (e Example) TableName()string{
func (e Example) TableName() string {
return "example" return "example"
} }
func TestDB_CopyIn(t *testing.T) { func TestDB_CopyIn(t *testing.T) {
if !DB.HasTable(&Example{}) { if !DB.HasTable(&Example{}) {
if e:=DB.Create(&Example{}).Error;e!=nil { if e := DB.CreateTable(&Example{}).Error; e != nil {
t.Fatal(e.Error()) t.Fatal(e.Error())
} }
} }
defer func() { defer func() {
if DB.HasTable(&Example{}) { if DB.HasTable(&Example{}) {
if e:=DB.DropTable(&Example{}).Error;e!=nil{ if e := DB.DropTable(&Example{}).Error; e != nil {
t.Fatal(e.Error()) t.Fatal(e.Error())
} }
} }
@ -1099,10 +1096,6 @@ func TestDB_CopyIn(t *testing.T) {
if e != nil { if e != nil {
t.Fatal(e.Error()) t.Fatal(e.Error())
} }
type Example struct {
Name string
Age int
}
var examples = make([]Example, 0) var examples = make([]Example, 0)
e = DB.Model(&Example{}).Find(&examples).Error e = DB.Model(&Example{}).Find(&examples).Error
if e != nil { if e != nil {

View File

@ -188,11 +188,11 @@ func (i *Num) Scan(src interface{}) error {
} }
type Animal struct { type Animal struct {
Counter uint64 `gorm:"primary_key:yes"` Counter uint64 `gorm:"primary_key:yes"`
Name string `sql:"DEFAULT:'galeone'"` Name string `sql:"DEFAULT:'galeone'"`
From string //test reserved sql keyword as field name From string //test reserved sql keyword as field name
Age time.Time `sql:"DEFAULT:current_timestamp"` Age time.Time `sql:"DEFAULT:current_timestamp"`
unexported string // unexported value unexported string // unexported value
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
@ -294,7 +294,7 @@ func runMigration() {
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}} values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}}
for _, value := range values { for _, value := range values {
if DB.HasTable(value){ if DB.HasTable(value) {
DB.DropTable(value) DB.DropTable(value)
} }
} }