From 189e2727740e872839026d302253bd0ff1724563 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 20 Feb 2020 07:18:44 +0000 Subject: [PATCH] Add tests for table name (#1517) add tests for table name Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 --- engine.go | 4 +-- engine_table.go | 20 ++----------- session_get_test.go | 2 +- table_name.go | 31 +++++++++++++++++++ table_name_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 table_name.go create mode 100644 table_name_test.go diff --git a/engine.go b/engine.go index a7e52ea4..286ce766 100644 --- a/engine.go +++ b/engine.go @@ -215,7 +215,7 @@ func quoteTo(buf *strings.Builder, quotePair string, value string) { _, _ = buf.WriteString(value) return } - + prefix, suffix := quotePair[0], quotePair[1] i := 0 @@ -921,7 +921,7 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) { t := v.Type() table := core.NewEmptyTable() table.Type = t - table.Name = engine.tbNameForMap(v) + table.Name = getTableName(engine.TableMapper, v) var idFieldColName string var hasCacheTag, hasNoCacheTag bool diff --git a/engine_table.go b/engine_table.go index eb5aa850..87388a35 100644 --- a/engine_table.go +++ b/engine_table.go @@ -44,20 +44,6 @@ func (session *Session) tbNameNoSchema(table *core.Table) string { return table.Name } -func (engine *Engine) tbNameForMap(v reflect.Value) string { - if v.Type().Implements(tpTableName) { - return v.Interface().(TableName).TableName() - } - if v.Kind() == reflect.Ptr { - v = v.Elem() - if v.Type().Implements(tpTableName) { - return v.Interface().(TableName).TableName() - } - } - - return engine.TableMapper.Obj2Table(v.Type().Name()) -} - func (engine *Engine) tbNameNoSchema(tablename interface{}) string { switch tablename.(type) { case []string: @@ -82,7 +68,7 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string { v := rValue(f) t := v.Type() if t.Kind() == reflect.Struct { - table = engine.tbNameForMap(v) + table = getTableName(engine.TableMapper, v) } else { table = engine.Quote(fmt.Sprintf("%v", f)) } @@ -100,12 +86,12 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string { return tablename.(string) case reflect.Value: v := tablename.(reflect.Value) - return engine.tbNameForMap(v) + return getTableName(engine.TableMapper, v) default: v := rValue(tablename) t := v.Type() if t.Kind() == reflect.Struct { - return engine.tbNameForMap(v) + return getTableName(engine.TableMapper, v) } return engine.Quote(fmt.Sprintf("%v", tablename)) } diff --git a/session_get_test.go b/session_get_test.go index fcef992e..54ba8916 100644 --- a/session_get_test.go +++ b/session_get_test.go @@ -480,7 +480,7 @@ type MyGetCustomTableImpletation struct { const getCustomTableName = "GetCustomTableInterface" -func (m *MyGetCustomTableImpletation) TableName() string { +func (MyGetCustomTableImpletation) TableName() string { return getCustomTableName } diff --git a/table_name.go b/table_name.go new file mode 100644 index 00000000..632c2879 --- /dev/null +++ b/table_name.go @@ -0,0 +1,31 @@ +// Copyright 2020 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 xorm + +import ( + "reflect" + + "xorm.io/core" +) + +func getTableName(mapper core.IMapper, v reflect.Value) string { + if t, ok := v.Interface().(TableName); ok { + return t.TableName() + } + if v.Type().Implements(tpTableName) { + return v.Interface().(TableName).TableName() + } + if v.Kind() == reflect.Ptr { + v = v.Elem() + if t, ok := v.Interface().(TableName); ok { + return t.TableName() + } + if v.Type().Implements(tpTableName) { + return v.Interface().(TableName).TableName() + } + } + + return mapper.Obj2Table(v.Type().Name()) +} diff --git a/table_name_test.go b/table_name_test.go new file mode 100644 index 00000000..6cb0ceaa --- /dev/null +++ b/table_name_test.go @@ -0,0 +1,73 @@ +// Copyright 2020 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 xorm + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + + "xorm.io/core" +) + +type TestTableNameStruct struct{} + +func (t *TestTableNameStruct) TableName() string { + return "my_test_table_name_struct" +} + +func TestGetTableName(t *testing.T) { + var kases = []struct { + mapper core.IMapper + v reflect.Value + expectedTableName string + }{ + { + core.SnakeMapper{}, + reflect.ValueOf(new(Userinfo)), + "userinfo", + }, + { + core.SnakeMapper{}, + reflect.ValueOf(Userinfo{}), + "userinfo", + }, + { + core.SameMapper{}, + reflect.ValueOf(new(Userinfo)), + "Userinfo", + }, + { + core.SameMapper{}, + reflect.ValueOf(Userinfo{}), + "Userinfo", + }, + { + core.SnakeMapper{}, + reflect.ValueOf(new(MyGetCustomTableImpletation)), + getCustomTableName, + }, + { + core.SnakeMapper{}, + reflect.ValueOf(MyGetCustomTableImpletation{}), + getCustomTableName, + }, + { + core.SnakeMapper{}, + reflect.ValueOf(MyGetCustomTableImpletation{}), + getCustomTableName, + }, + { + core.SnakeMapper{}, + reflect.ValueOf(new(TestTableNameStruct)), + new(TestTableNameStruct).TableName(), + }, + } + + for _, kase := range kases { + assert.EqualValues(t, kase.expectedTableName, getTableName(kase.mapper, kase.v)) + } +}