-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_test.go
More file actions
177 lines (160 loc) · 5.48 KB
/
Copy pathselect_test.go
File metadata and controls
177 lines (160 loc) · 5.48 KB
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
package sql_test
import (
"database/sql"
"reflect"
"testing"
)
// seedNumbers populates a small `n` table with rowid + value pairs.
// Reused by the SELECT tests so each one stays focused on the clause
// under test rather than fixture boilerplate.
func seedNumbers(t *testing.T, db *sql.DB) {
t.Helper()
mustExec(t, db, `create table n (id integer primary key, v integer, label text)`)
mustExec(t, db, `insert into n(id, v, label) values
(1, 10, 'a'),
(2, 20, 'b'),
(3, 20, 'a'),
(4, 30, 'c'),
(5, NULL, 'a')`)
}
func TestSelect_WhereComparisons(t *testing.T) {
db := openDB(t)
seedNumbers(t, db)
cases := []struct {
name string
sql string
want []int64
}{
{"eq", `select id from n where v = 20`, []int64{2, 3}},
{"ne", `select id from n where v != 20`, []int64{1, 4}},
{"lt", `select id from n where v < 20`, []int64{1}},
{"gt", `select id from n where v > 20`, []int64{4}},
{"between", `select id from n where v between 15 and 25`, []int64{2, 3}},
{"in", `select id from n where v in (10, 30)`, []int64{1, 4}},
{"notin", `select id from n where v not in (20)`, []int64{1, 4}},
{"isnull", `select id from n where v is null`, []int64{5}},
{"isnotnull", `select id from n where v is not null`, []int64{1, 2, 3, 4}},
{"like", `select id from n where label like 'a%'`, []int64{1, 3, 5}},
{"glob", `select id from n where label glob 'a*'`, []int64{1, 3, 5}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rows := scanAll(t, db, c.sql+` order by id`)
got := make([]int64, len(rows))
for i, r := range rows {
got[i] = r[0].(int64)
}
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}
func TestSelect_OrderBy(t *testing.T) {
db := openDB(t)
seedNumbers(t, db)
// Single column, ASC default + explicit DESC.
asc := scanAll(t, db, `select id from n where v is not null order by v`)
if asc[0][0].(int64) != 1 || asc[len(asc)-1][0].(int64) != 4 {
t.Errorf("ASC: head=%v tail=%v", asc[0], asc[len(asc)-1])
}
desc := scanAll(t, db, `select id from n where v is not null order by v desc`)
if desc[0][0].(int64) != 4 || desc[len(desc)-1][0].(int64) != 1 {
t.Errorf("DESC: head=%v tail=%v", desc[0], desc[len(desc)-1])
}
// Multi-column: by v ASC, then label DESC tie-breaks rowid 2 (b) before 3 (a).
multi := scanAll(t, db, `select id from n where v = 20 order by v asc, label desc`)
if len(multi) != 2 || multi[0][0].(int64) != 2 || multi[1][0].(int64) != 3 {
t.Errorf("multi-col: %+v, want [2, 3]", multi)
}
}
func TestSelect_GroupByHaving(t *testing.T) {
db := openDB(t)
seedNumbers(t, db)
// label → count(*). 'a' appears 3 times, 'b' once, 'c' once.
rows := scanAll(t, db, `select label, count(*) from n group by label order by label`)
want := [][]any{
{"a", int64(3)},
{"b", int64(1)},
{"c", int64(1)},
}
if !reflect.DeepEqual(rows, want) {
t.Errorf("group by: %+v, want %+v", rows, want)
}
// HAVING filters post-aggregation: only 'a' has > 1.
having := scanAll(t, db, `select label from n group by label having count(*) > 1`)
if len(having) != 1 || having[0][0].(string) != "a" {
t.Errorf("having: %+v, want [a]", having)
}
}
func TestSelect_Distinct(t *testing.T) {
db := openDB(t)
seedNumbers(t, db)
rows := scanAll(t, db, `select distinct v from n where v is not null order by v`)
want := [][]any{{int64(10)}, {int64(20)}, {int64(30)}}
if !reflect.DeepEqual(rows, want) {
t.Errorf("distinct: %+v, want %+v", rows, want)
}
}
func TestSelect_SetOperators(t *testing.T) {
db := openDB(t)
mustExec(t, db, `create table a (v int); insert into a values (1), (2), (3)`)
mustExec(t, db, `create table b (v int); insert into b values (2), (3), (4)`)
cases := []struct {
name string
sql string
want []int64
}{
{"union", `select v from a union select v from b order by v`, []int64{1, 2, 3, 4}},
{"unionall", `select v from a union all select v from b order by v`, []int64{1, 2, 2, 3, 3, 4}},
{"intersect", `select v from a intersect select v from b order by v`, []int64{2, 3}},
{"except", `select v from a except select v from b order by v`, []int64{1}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rows := scanAll(t, db, c.sql)
got := make([]int64, len(rows))
for i, r := range rows {
got[i] = r[0].(int64)
}
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}
func TestSelect_LimitOffset(t *testing.T) {
db := openDB(t)
seedNumbers(t, db)
// LIMIT N
rows := scanAll(t, db, `select id from n order by id limit 2`)
if len(rows) != 2 || rows[0][0].(int64) != 1 || rows[1][0].(int64) != 2 {
t.Errorf("LIMIT 2: %+v, want [1 2]", rows)
}
// LIMIT N OFFSET M
rows = scanAll(t, db, `select id from n order by id limit 2 offset 2`)
if len(rows) != 2 || rows[0][0].(int64) != 3 || rows[1][0].(int64) != 4 {
t.Errorf("LIMIT 2 OFFSET 2: %+v, want [3 4]", rows)
}
// LIMIT M, N — alternate SQLite syntax meaning OFFSET M LIMIT N.
rows = scanAll(t, db, `select id from n order by id limit 1, 2`)
if len(rows) != 2 || rows[0][0].(int64) != 2 || rows[1][0].(int64) != 3 {
t.Errorf("LIMIT 1, 2: %+v, want [2 3]", rows)
}
}
func TestSelect_ColumnAliases(t *testing.T) {
db := openDB(t)
seedNumbers(t, db)
rows, err := db.Query(`select v as value, label as name from n where id = 1`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
t.Fatal(err)
}
if cols[0] != "value" || cols[1] != "name" {
t.Errorf("aliases: %v, want [value name]", cols)
}
}