forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_test.go
201 lines (168 loc) · 5.25 KB
/
select_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
package dbr
import (
"testing"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"github.com/valkuere/dbr/v2/dialect"
)
func TestSelectStmt(t *testing.T) {
buf := NewBuffer()
builder := Select("a", "b").
From(Select("a").From("table")).
LeftJoin("table2", "table.a1 = table.a2").
Distinct().
Where(Eq("c", 1)).
GroupBy("d").
Having(Eq("e", 2)).
OrderAsc("f").
Limit(3).
Offset(4).
Suffix("FOR UPDATE").
Comment("SELECT TEST")
err := builder.Build(dialect.MySQL, buf)
require.NoError(t, err)
require.Equal(t, "/* SELECT TEST */\nSELECT DISTINCT a, b FROM ? LEFT JOIN `table2` ON table.a1 = table.a2 WHERE (`c` = ?) GROUP BY d HAVING (`e` = ?) ORDER BY f ASC LIMIT 3 OFFSET 4 FOR UPDATE", buf.String())
// two functions cannot be compared
require.Equal(t, 3, len(buf.Value()))
}
func BenchmarkSelectSQL(b *testing.B) {
buf := NewBuffer()
for i := 0; i < b.N; i++ {
Select("a", "b").From("table").Where(Eq("c", 1)).OrderAsc("d").Build(dialect.MySQL, buf)
}
}
type stringSliceWithSQLScanner []string
func (ss *stringSliceWithSQLScanner) Scan(src interface{}) error {
*ss = append(*ss, "called")
return nil
}
func TestSliceWithSQLScannerSelect(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
_, err := sess.InsertInto("dbr_people").
Columns("name", "email").
Values("test1", "test1@test.com").
Values("test2", "test2@test.com").
Values("test3", "test3@test.com").
Exec()
//plain string slice (original behavior)
var stringSlice []string
cnt, err := sess.Select("name").From("dbr_people").Load(&stringSlice)
require.NoError(t, err)
require.Equal(t, 3, cnt)
require.Len(t, stringSlice, 3)
//string slice with sql.Scanner implemented, should act as a single record
var sliceScanner stringSliceWithSQLScanner
cnt, err = sess.Select("name").From("dbr_people").Load(&sliceScanner)
require.NoError(t, err)
require.Equal(t, 1, cnt)
require.Len(t, sliceScanner, 1)
}
}
func TestMaps(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
_, err := sess.InsertInto("dbr_people").
Columns("name", "email").
Values("test1", "test1@test.com").
Values("test2", "test2@test.com").
Values("test2", "test3@test.com").
Exec()
var m map[string]string
cnt, err := sess.Select("email, name").From("dbr_people").Load(&m)
require.NoError(t, err)
require.Equal(t, 3, cnt)
require.Len(t, m, 3)
require.Equal(t, "test1", m["test1@test.com"])
var m2 map[int64]*dbrPerson
cnt, err = sess.Select("id, name, email").From("dbr_people").Load(&m2)
require.NoError(t, err)
require.Equal(t, 3, cnt)
require.Len(t, m2, 3)
require.Equal(t, "test1@test.com", m2[1].Email)
require.Equal(t, "test1", m2[1].Name)
// the id value is used as the map key, so it is not hydrated in the struct
require.Equal(t, int64(0), m2[1].Id)
var m3 map[string][]string
cnt, err = sess.Select("name, email").From("dbr_people").OrderAsc("id").Load(&m3)
require.NoError(t, err)
require.Equal(t, 3, cnt)
require.Len(t, m3, 2)
require.Equal(t, []string{"test1@test.com"}, m3["test1"])
require.Equal(t, []string{"test2@test.com", "test3@test.com"}, m3["test2"])
var set map[string]struct{}
cnt, err = sess.Select("name").From("dbr_people").Load(&set)
require.NoError(t, err)
require.Equal(t, 3, cnt)
require.Len(t, set, 2)
_, ok := set["test1"]
require.True(t, ok)
}
}
func TestSelectRows(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
_, err := sess.InsertInto("dbr_people").
Columns("name", "email").
Values("test1", "test1@test.com").
Values("test2", "test2@test.com").
Values("test3", "test3@test.com").
Exec()
rows, err := sess.Select("*").From("dbr_people").OrderAsc("id").Rows()
require.NoError(t, err)
defer rows.Close()
want := []dbrPerson{
{Id: 1, Name: "test1", Email: "test1@test.com"},
{Id: 2, Name: "test2", Email: "test2@test.com"},
{Id: 3, Name: "test3", Email: "test3@test.com"},
}
count := 0
for rows.Next() {
var p dbrPerson
require.NoError(t, rows.Scan(&p.Id, &p.Name, &p.Email))
require.Equal(t, want[count], p)
count++
}
require.Equal(t, len(want), count)
}
}
func TestInterfaceLoader(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
_, err := sess.InsertInto("dbr_people").
Columns("name", "email").
Values("test1", "test1@test.com").
Values("test2", "test2@test.com").
Values("test2", "test3@test.com").
Exec()
var m []interface{}
cnt, err := sess.Select("*").From("dbr_people").Load(InterfaceLoader(&m, dbrPerson{}))
require.NoError(t, err)
require.Equal(t, 3, cnt)
require.Len(t, m, 3)
person, ok := m[0].(dbrPerson)
require.True(t, ok)
require.Equal(t, "test1", person.Name)
}
}
func TestPostgresArray(t *testing.T) {
sess := postgresSession
for _, v := range []string{
`DROP TABLE IF EXISTS array_table`,
`CREATE TABLE array_table (
val integer[]
)`,
} {
_, err := sess.Exec(v)
require.NoError(t, err)
}
// INSERT INTO "array_table" ("val") VALUES ('{1,2,3}')
_, err := sess.InsertInto("array_table").
Pair("val", pq.Array([]int64{1, 2, 3})).
Exec()
require.NoError(t, err)
var ns []int64
err = sess.Select("val").From("array_table").LoadOne(pq.Array(&ns))
require.NoError(t, err)
require.Equal(t, []int64{1, 2, 3}, ns)
}