fix tests
This commit is contained in:
parent
e4a96cc3a7
commit
0f191f3e28
12
engine.go
12
engine.go
|
@ -37,9 +37,7 @@ type Engine struct {
|
||||||
db *core.DB
|
db *core.DB
|
||||||
dialect dialects.Dialect
|
dialect dialects.Dialect
|
||||||
|
|
||||||
ColumnMapper names.Mapper
|
Tables map[reflect.Type]*schemas.Table
|
||||||
TableMapper names.Mapper
|
|
||||||
Tables map[reflect.Type]*schemas.Table
|
|
||||||
|
|
||||||
mutex *sync.RWMutex
|
mutex *sync.RWMutex
|
||||||
|
|
||||||
|
@ -151,12 +149,12 @@ func (engine *Engine) SetMapper(mapper names.Mapper) {
|
||||||
|
|
||||||
// SetTableMapper set the table name mapping rule
|
// SetTableMapper set the table name mapping rule
|
||||||
func (engine *Engine) SetTableMapper(mapper names.Mapper) {
|
func (engine *Engine) SetTableMapper(mapper names.Mapper) {
|
||||||
engine.TableMapper = mapper
|
engine.tagParser.TableMapper = mapper
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetColumnMapper set the column name mapping rule
|
// SetColumnMapper set the column name mapping rule
|
||||||
func (engine *Engine) SetColumnMapper(mapper names.Mapper) {
|
func (engine *Engine) SetColumnMapper(mapper names.Mapper) {
|
||||||
engine.ColumnMapper = mapper
|
engine.tagParser.ColumnMapper = mapper
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportInsertMany If engine's database support batch insert records like
|
// SupportInsertMany If engine's database support batch insert records like
|
||||||
|
@ -1333,12 +1331,12 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}
|
||||||
|
|
||||||
// GetColumnMapper returns the column name mapper
|
// GetColumnMapper returns the column name mapper
|
||||||
func (engine *Engine) GetColumnMapper() names.Mapper {
|
func (engine *Engine) GetColumnMapper() names.Mapper {
|
||||||
return engine.ColumnMapper
|
return engine.tagParser.ColumnMapper
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTableMapper returns the table name mapper
|
// GetTableMapper returns the table name mapper
|
||||||
func (engine *Engine) GetTableMapper() names.Mapper {
|
func (engine *Engine) GetTableMapper() names.Mapper {
|
||||||
return engine.TableMapper
|
return engine.tagParser.TableMapper
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTZLocation returns time zone of the application
|
// GetTZLocation returns time zone of the application
|
||||||
|
|
|
@ -112,9 +112,9 @@ func (eg *EngineGroup) Ping() error {
|
||||||
|
|
||||||
// SetColumnMapper set the column name mapping rule
|
// SetColumnMapper set the column name mapping rule
|
||||||
func (eg *EngineGroup) SetColumnMapper(mapper names.Mapper) {
|
func (eg *EngineGroup) SetColumnMapper(mapper names.Mapper) {
|
||||||
eg.Engine.ColumnMapper = mapper
|
eg.Engine.SetColumnMapper(mapper)
|
||||||
for i := 0; i < len(eg.slaves); i++ {
|
for i := 0; i < len(eg.slaves); i++ {
|
||||||
eg.slaves[i].ColumnMapper = mapper
|
eg.slaves[i].SetColumnMapper(mapper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,9 +182,9 @@ func (eg *EngineGroup) SetPolicy(policy GroupPolicy) *EngineGroup {
|
||||||
|
|
||||||
// SetTableMapper set the table name mapping rule
|
// SetTableMapper set the table name mapping rule
|
||||||
func (eg *EngineGroup) SetTableMapper(mapper names.Mapper) {
|
func (eg *EngineGroup) SetTableMapper(mapper names.Mapper) {
|
||||||
eg.Engine.TableMapper = mapper
|
eg.Engine.SetTableMapper(mapper)
|
||||||
for i := 0; i < len(eg.slaves); i++ {
|
for i := 0; i < len(eg.slaves); i++ {
|
||||||
eg.slaves[i].TableMapper = mapper
|
eg.slaves[i].SetTableMapper(mapper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
||||||
v := rValue(f)
|
v := rValue(f)
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
if t.Kind() == reflect.Struct {
|
if t.Kind() == reflect.Struct {
|
||||||
table = names.GetTableName(engine.TableMapper, v)
|
table = names.GetTableName(engine.GetTableMapper(), v)
|
||||||
} else {
|
} else {
|
||||||
table = engine.Quote(fmt.Sprintf("%v", f))
|
table = engine.Quote(fmt.Sprintf("%v", f))
|
||||||
}
|
}
|
||||||
|
@ -96,12 +96,12 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
||||||
return tablename.(string)
|
return tablename.(string)
|
||||||
case reflect.Value:
|
case reflect.Value:
|
||||||
v := tablename.(reflect.Value)
|
v := tablename.(reflect.Value)
|
||||||
return names.GetTableName(engine.TableMapper, v)
|
return names.GetTableName(engine.GetTableMapper(), v)
|
||||||
default:
|
default:
|
||||||
v := rValue(tablename)
|
v := rValue(tablename)
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
if t.Kind() == reflect.Struct {
|
if t.Kind() == reflect.Struct {
|
||||||
return names.GetTableName(engine.TableMapper, v)
|
return names.GetTableName(engine.GetTableMapper(), v)
|
||||||
}
|
}
|
||||||
return engine.Quote(fmt.Sprintf("%v", tablename))
|
return engine.Quote(fmt.Sprintf("%v", tablename))
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,19 +19,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
identifier string
|
identifier string
|
||||||
dialect dialects.Dialect
|
dialect dialects.Dialect
|
||||||
tableMapper, columnMapper names.Mapper
|
ColumnMapper names.Mapper
|
||||||
handlers map[string]Handler
|
TableMapper names.Mapper
|
||||||
cacherMgr *caches.Manager
|
handlers map[string]Handler
|
||||||
|
cacherMgr *caches.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnMapper names.Mapper, cacherMgr *caches.Manager) *Parser {
|
func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnMapper names.Mapper, cacherMgr *caches.Manager) *Parser {
|
||||||
return &Parser{
|
return &Parser{
|
||||||
identifier: identifier,
|
identifier: identifier,
|
||||||
dialect: dialect,
|
dialect: dialect,
|
||||||
tableMapper: tableMapper,
|
TableMapper: tableMapper,
|
||||||
columnMapper: columnMapper,
|
ColumnMapper: columnMapper,
|
||||||
handlers: defaultTagHandlers,
|
handlers: defaultTagHandlers,
|
||||||
cacherMgr: cacherMgr,
|
cacherMgr: cacherMgr,
|
||||||
}
|
}
|
||||||
|
@ -53,7 +54,7 @@ func (parser *Parser) MapType(v reflect.Value) (*schemas.Table, error) {
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
table := schemas.NewEmptyTable()
|
table := schemas.NewEmptyTable()
|
||||||
table.Type = t
|
table.Type = t
|
||||||
table.Name = names.GetTableName(parser.tableMapper, v)
|
table.Name = names.GetTableName(parser.TableMapper, v)
|
||||||
|
|
||||||
var idFieldColName string
|
var idFieldColName string
|
||||||
var hasCacheTag, hasNoCacheTag bool
|
var hasCacheTag, hasNoCacheTag bool
|
||||||
|
@ -170,7 +171,7 @@ func (parser *Parser) MapType(v reflect.Value) (*schemas.Table, error) {
|
||||||
col.Length2 = col.SQLType.DefaultLength2
|
col.Length2 = col.SQLType.DefaultLength2
|
||||||
}
|
}
|
||||||
if col.Name == "" {
|
if col.Name == "" {
|
||||||
col.Name = parser.columnMapper.Obj2Table(t.Field(i).Name)
|
col.Name = parser.ColumnMapper.Obj2Table(t.Field(i).Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.isUnique {
|
if ctx.isUnique {
|
||||||
|
@ -195,7 +196,7 @@ func (parser *Parser) MapType(v reflect.Value) (*schemas.Table, error) {
|
||||||
} else {
|
} else {
|
||||||
sqlType = schemas.Type2SQLType(fieldType)
|
sqlType = schemas.Type2SQLType(fieldType)
|
||||||
}
|
}
|
||||||
col = schemas.NewColumn(parser.columnMapper.Obj2Table(t.Field(i).Name),
|
col = schemas.NewColumn(parser.ColumnMapper.Obj2Table(t.Field(i).Name),
|
||||||
t.Field(i).Name, sqlType, sqlType.DefaultLength,
|
t.Field(i).Name, sqlType, sqlType.DefaultLength,
|
||||||
sqlType.DefaultLength2, true)
|
sqlType.DefaultLength2, true)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
// Copyright 2017 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tags
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSplitTag(t *testing.T) {
|
||||||
|
var cases = []struct {
|
||||||
|
tag string
|
||||||
|
tags []string
|
||||||
|
}{
|
||||||
|
{"not null default '2000-01-01 00:00:00' TIMESTAMP", []string{"not", "null", "default", "'2000-01-01 00:00:00'", "TIMESTAMP"}},
|
||||||
|
{"TEXT", []string{"TEXT"}},
|
||||||
|
{"default('2000-01-01 00:00:00')", []string{"default('2000-01-01 00:00:00')"}},
|
||||||
|
{"json binary", []string{"json", "binary"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, kase := range cases {
|
||||||
|
tags := splitTag(kase.tag)
|
||||||
|
if !sliceEq(tags, kase.tags) {
|
||||||
|
t.Fatalf("[%d]%v is not equal [%d]%v", len(tags), tags, len(kase.tags), kase.tags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
tags_test.go
19
tags_test.go
|
@ -1223,25 +1223,6 @@ func TestTagTime(t *testing.T) {
|
||||||
strings.Replace(strings.Replace(tm, "T", " ", -1), "Z", "", -1))
|
strings.Replace(strings.Replace(tm, "T", " ", -1), "Z", "", -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSplitTag(t *testing.T) {
|
|
||||||
var cases = []struct {
|
|
||||||
tag string
|
|
||||||
tags []string
|
|
||||||
}{
|
|
||||||
{"not null default '2000-01-01 00:00:00' TIMESTAMP", []string{"not", "null", "default", "'2000-01-01 00:00:00'", "TIMESTAMP"}},
|
|
||||||
{"TEXT", []string{"TEXT"}},
|
|
||||||
{"default('2000-01-01 00:00:00')", []string{"default('2000-01-01 00:00:00')"}},
|
|
||||||
{"json binary", []string{"json", "binary"}},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, kase := range cases {
|
|
||||||
tags := splitTag(kase.tag)
|
|
||||||
if !sliceEq(tags, kase.tags) {
|
|
||||||
t.Fatalf("[%d]%v is not equal [%d]%v", len(tags), tags, len(kase.tags), kase.tags)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTagAutoIncr(t *testing.T) {
|
func TestTagAutoIncr(t *testing.T) {
|
||||||
assert.NoError(t, prepareEngine())
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestArrayField(t *testing.T) {
|
func TestArrayField(t *testing.T) {
|
||||||
|
@ -137,8 +139,8 @@ type ConvStruct struct {
|
||||||
Conv ConvString
|
Conv ConvString
|
||||||
Conv2 *ConvString
|
Conv2 *ConvString
|
||||||
Cfg1 ConvConfig
|
Cfg1 ConvConfig
|
||||||
Cfg2 *ConvConfig `xorm:"TEXT"`
|
Cfg2 *ConvConfig `xorm:"TEXT"`
|
||||||
Cfg3 Conversion `xorm:"BLOB"`
|
Cfg3 convert.Conversion `xorm:"BLOB"`
|
||||||
Slice SliceType
|
Slice SliceType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,11 +269,11 @@ type Status struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ Conversion = &Status{}
|
_ convert.Conversion = &Status{}
|
||||||
Registered Status = Status{"Registered", "white"}
|
Registered Status = Status{"Registered", "white"}
|
||||||
Approved Status = Status{"Approved", "green"}
|
Approved Status = Status{"Approved", "green"}
|
||||||
Removed Status = Status{"Removed", "red"}
|
Removed Status = Status{"Removed", "red"}
|
||||||
Statuses map[string]Status = map[string]Status{
|
Statuses map[string]Status = map[string]Status{
|
||||||
Registered.Name: Registered,
|
Registered.Name: Registered,
|
||||||
Approved.Name: Approved,
|
Approved.Name: Approved,
|
||||||
Removed.Name: Removed,
|
Removed.Name: Removed,
|
||||||
|
|
4
xorm.go
4
xorm.go
|
@ -80,8 +80,8 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
logger := log.NewSimpleLogger(os.Stdout)
|
logger := log.NewSimpleLogger(os.Stdout)
|
||||||
logger.SetLevel(log.LOG_INFO)
|
logger.SetLevel(log.LOG_INFO)
|
||||||
engine.SetLogger(logger)
|
engine.SetLogger(logger)
|
||||||
engine.SetMapper(names.NewCacheMapper(new(names.SnakeMapper)))
|
mapper := names.NewCacheMapper(new(names.SnakeMapper))
|
||||||
engine.tagParser = tags.NewParser("xorm", dialect, engine.TableMapper, engine.ColumnMapper, engine.cacherMgr)
|
engine.tagParser = tags.NewParser("xorm", dialect, mapper, mapper, engine.cacherMgr)
|
||||||
|
|
||||||
runtime.SetFinalizer(engine, close)
|
runtime.SetFinalizer(engine, close)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue