2017-03-27 07:45:15 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-11-07 11:47:42 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2016-11-12 03:30:16 +00:00
|
|
|
"strings"
|
2017-03-23 06:05:32 +00:00
|
|
|
"testing"
|
2016-11-12 03:30:16 +00:00
|
|
|
|
2016-11-07 11:47:42 +00:00
|
|
|
"github.com/go-xorm/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
var colStrTests = []struct {
|
|
|
|
omitColumn string
|
|
|
|
onlyToDBColumnNdx int
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"", -1, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`, `Longitude`"},
|
|
|
|
{"Code2", -1, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code3`, `ParentID`, `Latitude`, `Longitude`"},
|
|
|
|
{"", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`, `Longitude`"},
|
|
|
|
{"Code3", 1, "`ID`, `Caption`, `Code1`, `Code2`, `ParentID`, `Latitude`, `Longitude`"},
|
2016-11-08 09:32:08 +00:00
|
|
|
{"Longitude", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"},
|
|
|
|
{"", 8, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"},
|
2016-11-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestColumnsStringGeneration(t *testing.T) {
|
2017-04-10 15:10:59 +00:00
|
|
|
if *db == "postgres" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-07 11:47:42 +00:00
|
|
|
var statement *Statement
|
|
|
|
|
|
|
|
for ndx, testCase := range colStrTests {
|
|
|
|
statement = createTestStatement()
|
|
|
|
|
|
|
|
if testCase.omitColumn != "" {
|
2017-03-23 06:05:32 +00:00
|
|
|
statement.Omit(testCase.omitColumn)
|
2016-11-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 06:05:32 +00:00
|
|
|
columns := statement.RefTable.Columns()
|
2016-11-07 11:47:42 +00:00
|
|
|
if testCase.onlyToDBColumnNdx >= 0 {
|
2017-03-23 06:05:32 +00:00
|
|
|
columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB
|
2016-11-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := statement.genColumnStr()
|
|
|
|
|
|
|
|
if actual != testCase.expected {
|
|
|
|
t.Errorf("[test #%d] Unexpected columns string:\nwant:\t%s\nhave:\t%s", ndx, testCase.expected, actual)
|
|
|
|
}
|
2017-03-23 06:05:32 +00:00
|
|
|
if testCase.onlyToDBColumnNdx >= 0 {
|
|
|
|
columns[testCase.onlyToDBColumnNdx].MapType = core.TWOSIDES
|
|
|
|
}
|
2016-11-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkColumnsStringGeneration(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
statement := createTestStatement()
|
|
|
|
|
|
|
|
testCase := colStrTests[0]
|
|
|
|
|
|
|
|
if testCase.omitColumn != "" {
|
|
|
|
statement.Omit(testCase.omitColumn) // !nemec784! Column must be skipped
|
|
|
|
}
|
|
|
|
|
|
|
|
if testCase.onlyToDBColumnNdx >= 0 {
|
|
|
|
columns := statement.RefTable.Columns()
|
|
|
|
columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB // !nemec784! Column must be skipped
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
actual := statement.genColumnStr()
|
|
|
|
|
|
|
|
if actual != testCase.expected {
|
|
|
|
b.Errorf("Unexpected columns string:\nwant:\t%s\nhave:\t%s", testCase.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 03:30:16 +00:00
|
|
|
func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) {
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
mapCols := make(map[string]bool)
|
|
|
|
cols := []*core.Column{
|
2017-01-09 01:52:23 +00:00
|
|
|
{Name: `ID`},
|
|
|
|
{Name: `IsDeleted`},
|
|
|
|
{Name: `Caption`},
|
|
|
|
{Name: `Code1`},
|
|
|
|
{Name: `Code2`},
|
|
|
|
{Name: `Code3`},
|
|
|
|
{Name: `ParentID`},
|
|
|
|
{Name: `Latitude`},
|
|
|
|
{Name: `Longitude`},
|
2016-11-12 03:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, col := range cols {
|
|
|
|
mapCols[strings.ToLower(col.Name)] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
|
|
|
for _, col := range cols {
|
|
|
|
|
|
|
|
if _, ok := getFlagForColumn(mapCols, col); !ok {
|
|
|
|
b.Fatal("Unexpected result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGetFlagForColumnWithICKey_EmptyMap(b *testing.B) {
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
mapCols := make(map[string]bool)
|
|
|
|
cols := []*core.Column{
|
2017-01-09 01:52:23 +00:00
|
|
|
{Name: `ID`},
|
|
|
|
{Name: `IsDeleted`},
|
|
|
|
{Name: `Caption`},
|
|
|
|
{Name: `Code1`},
|
|
|
|
{Name: `Code2`},
|
|
|
|
{Name: `Code3`},
|
|
|
|
{Name: `ParentID`},
|
|
|
|
{Name: `Latitude`},
|
|
|
|
{Name: `Longitude`},
|
2016-11-12 03:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
|
|
|
for _, col := range cols {
|
|
|
|
|
|
|
|
if _, ok := getFlagForColumn(mapCols, col); ok {
|
|
|
|
b.Fatal("Unexpected result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 11:47:42 +00:00
|
|
|
type TestType struct {
|
|
|
|
ID int64 `xorm:"ID PK"`
|
|
|
|
IsDeleted bool `xorm:"IsDeleted"`
|
|
|
|
Caption string `xorm:"Caption"`
|
|
|
|
Code1 string `xorm:"Code1"`
|
|
|
|
Code2 string `xorm:"Code2"`
|
|
|
|
Code3 string `xorm:"Code3"`
|
|
|
|
ParentID int64 `xorm:"ParentID"`
|
|
|
|
Latitude float64 `xorm:"Latitude"`
|
|
|
|
Longitude float64 `xorm:"Longitude"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TestType) TableName() string {
|
|
|
|
return "TestTable"
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestStatement() *Statement {
|
|
|
|
statement := &Statement{}
|
|
|
|
statement.Init()
|
2017-03-23 06:05:32 +00:00
|
|
|
statement.Engine = testEngine
|
2016-11-07 11:47:42 +00:00
|
|
|
statement.setRefValue(reflect.ValueOf(TestType{}))
|
|
|
|
|
|
|
|
return statement
|
|
|
|
}
|