Merge 1f896a6e82edf08f648738bca3f4fe3714a651d3 into 38f96c65140f00f0b15efc495a487cfd5db510b8
This commit is contained in:
		
						commit
						28f850f85c
					
				
							
								
								
									
										34
									
								
								main_test.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								main_test.go
									
									
									
									
									
								
							@ -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{})
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										8
									
								
								scope.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								scope.go
									
									
									
									
									
								
							@ -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))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user