forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_test.go
248 lines (197 loc) · 5.87 KB
/
statement_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// 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 xorm
import (
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"xorm.io/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`"},
{"Longitude", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"},
{"", 8, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"},
}
func TestColumnsStringGeneration(t *testing.T) {
if dbType == "postgres" || dbType == "mssql" {
return
}
var statement *Statement
for ndx, testCase := range colStrTests {
statement = createTestStatement()
if testCase.omitColumn != "" {
statement.Omit(testCase.omitColumn)
}
columns := statement.RefTable.Columns()
if testCase.onlyToDBColumnNdx >= 0 {
columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB
}
actual := statement.genColumnStr()
if actual != testCase.expected {
t.Errorf("[test #%d] Unexpected columns string:\nwant:\t%s\nhave:\t%s", ndx, testCase.expected, actual)
}
if testCase.onlyToDBColumnNdx >= 0 {
columns[testCase.onlyToDBColumnNdx].MapType = core.TWOSIDES
}
}
}
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)
}
}
}
func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) {
b.StopTimer()
mapCols := make(map[string]bool)
cols := []*core.Column{
{Name: `ID`},
{Name: `IsDeleted`},
{Name: `Caption`},
{Name: `Code1`},
{Name: `Code2`},
{Name: `Code3`},
{Name: `ParentID`},
{Name: `Latitude`},
{Name: `Longitude`},
}
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{
{Name: `ID`},
{Name: `IsDeleted`},
{Name: `Caption`},
{Name: `Code1`},
{Name: `Code2`},
{Name: `Code3`},
{Name: `ParentID`},
{Name: `Latitude`},
{Name: `Longitude`},
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, col := range cols {
if _, ok := getFlagForColumn(mapCols, col); ok {
b.Fatal("Unexpected result")
}
}
}
}
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 {
if engine, ok := testEngine.(*Engine); ok {
statement := &Statement{}
statement.Init()
statement.Engine = engine
statement.setRefValue(reflect.ValueOf(TestType{}))
return statement
} else if eg, ok := testEngine.(*EngineGroup); ok {
statement := &Statement{}
statement.Init()
statement.Engine = eg.Engine
statement.setRefValue(reflect.ValueOf(TestType{}))
return statement
}
return nil
}
func TestDistinctAndCols(t *testing.T) {
type DistinctAndCols struct {
Id int64
Name string
}
assert.NoError(t, prepareEngine())
assertSync(t, new(DistinctAndCols))
cnt, err := testEngine.Insert(&DistinctAndCols{
Name: "test",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var names []string
err = testEngine.Table("distinct_and_cols").Cols("name").Distinct("name").Find(&names)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(names))
assert.EqualValues(t, "test", names[0])
}
func TestUpdateIgnoreOnlyFromDBFields(t *testing.T) {
type TestOnlyFromDBField struct {
Id int64 `xorm:"PK"`
OnlyFromDBField string `xorm:"<-"`
OnlyToDBField string `xorm:"->"`
IngoreField string `xorm:"-"`
}
assertGetRecord := func() *TestOnlyFromDBField {
var record TestOnlyFromDBField
has, err := testEngine.Where("id = ?", 1).Get(&record)
assert.NoError(t, err)
assert.EqualValues(t, true, has)
assert.EqualValues(t, "", record.OnlyFromDBField)
return &record
}
assert.NoError(t, prepareEngine())
assertSync(t, new(TestOnlyFromDBField))
_, err := testEngine.Insert(&TestOnlyFromDBField{
Id: 1,
OnlyFromDBField: "a",
OnlyToDBField: "b",
IngoreField: "c",
})
assert.NoError(t, err)
record := assertGetRecord()
record.OnlyFromDBField = "test"
testEngine.Update(record)
assertGetRecord()
}
func TestCol2NewColsWithQuote(t *testing.T) {
cols := []string{"f1", "f2", "t3.f3"}
statement := createTestStatement()
quotedCols := statement.col2NewColsWithQuote(cols...)
assert.EqualValues(t, []string{statement.Engine.Quote("f1"), statement.Engine.Quote("f2"), statement.Engine.Quote("t3.f3")}, quotedCols)
}