forked from xormplus/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows_test.go
150 lines (121 loc) · 2.96 KB
/
rows_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
// 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 (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRows(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserRows struct {
Id int64
IsMan bool
}
assert.NoError(t, testEngine.Sync2(new(UserRows)))
cnt, err := testEngine.Insert(&UserRows{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
rows, err := testEngine.Rows(new(UserRows))
assert.NoError(t, err)
defer rows.Close()
cnt = 0
user := new(UserRows)
for rows.Next() {
err = rows.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 1, cnt)
assert.False(t, rows.Next())
assert.NoError(t, rows.Close())
rows0, err := testEngine.Where("1>1").Rows(new(UserRows))
assert.NoError(t, err)
defer rows0.Close()
cnt = 0
user0 := new(UserRows)
for rows0.Next() {
err = rows0.Scan(user0)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 0, cnt)
assert.NoError(t, rows0.Close())
sess := testEngine.NewSession()
defer sess.Close()
rows1, err := sess.Prepare().Rows(new(UserRows))
assert.NoError(t, err)
defer rows1.Close()
cnt = 0
for rows1.Next() {
err = rows1.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 1, cnt)
var tbName = testEngine.Quote(testEngine.TableName(user, true))
rows2, err := testEngine.SQL("SELECT * FROM " + tbName).Rows(new(UserRows))
assert.NoError(t, err)
defer rows2.Close()
cnt = 0
for rows2.Next() {
err = rows2.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 1, cnt)
}
func TestRowsMyTableName(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserRowsMyTable struct {
Id int64
IsMan bool
}
var tableName = "user_rows_my_table_name"
assert.NoError(t, testEngine.Table(tableName).Sync2(new(UserRowsMyTable)))
cnt, err := testEngine.Table(tableName).Insert(&UserRowsMyTable{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
rows, err := testEngine.Table(tableName).Rows(new(UserRowsMyTable))
assert.NoError(t, err)
defer rows.Close()
cnt = 0
user := new(UserRowsMyTable)
for rows.Next() {
err = rows.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 1, cnt)
}
type UserRowsSpecTable struct {
Id int64
IsMan bool
}
func (UserRowsSpecTable) TableName() string {
return "user_rows_my_table_name"
}
func TestRowsSpecTableName(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Sync2(new(UserRowsSpecTable)))
cnt, err := testEngine.Insert(&UserRowsSpecTable{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
rows, err := testEngine.Rows(new(UserRowsSpecTable))
assert.NoError(t, err)
defer rows.Close()
cnt = 0
user := new(UserRowsSpecTable)
for rows.Next() {
err = rows.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 1, cnt)
}