Merge 1f896a6e82edf08f648738bca3f4fe3714a651d3 into 38f96c65140f00f0b15efc495a487cfd5db510b8

This commit is contained in:
Masaki Yoshida 2018-02-09 14:13:44 +00:00 committed by GitHub
commit 28f850f85c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"reflect"
"strconv"
"sync"
"testing"
"time"
@ -23,6 +24,7 @@ import (
var (
DB *gorm.DB
t1, t2, t3, t4, t5 time.Time
mu sync.RWMutex
)
func init() {
@ -262,6 +264,38 @@ func TestTableName(t *testing.T) {
DB.SingularTable(false)
}
func TestTableNameWithTableNameHandler(t *testing.T) {
mu.Lock()
orig := gorm.DefaultTableNameHandler
defer func() {
mu.Unlock()
gorm.DefaultTableNameHandler = orig
}()
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return "renamed_" + defaultTableName
}
if DB.NewScope(Cart{}).TableName() != "renamed_shopping_cart" {
t.Errorf("Cart's table name should be renamed_shopping_cart")
}
if DB.NewScope(&Cart{}).TableName() != "renamed_shopping_cart" {
t.Errorf("&Cart's table name should be renamed_shopping_cart")
}
var cart interface{}
cart = Cart{}
if DB.NewScope(cart).TableName() != "renamed_shopping_cart" {
t.Errorf("interface{}'s table name should be renamed_shopping_cart")
}
cart = &Cart{}
if DB.NewScope(cart).TableName() != "renamed_shopping_cart" {
t.Errorf("interface{}'s table name should be renamed_shopping_cart")
}
}
func TestNullValues(t *testing.T) {
DB.DropTable(&NullValue{})
DB.AutoMigrate(&NullValue{})

View File

@ -314,14 +314,6 @@ func (scope *Scope) TableName() string {
return scope.Search.tableName
}
if tabler, ok := scope.Value.(tabler); ok {
return tabler.TableName()
}
if tabler, ok := scope.Value.(dbTabler); ok {
return tabler.TableName(scope.db)
}
return scope.GetModelStruct().TableName(scope.db.Model(scope.Value))
}