sl@0
|
1 |
#
|
sl@0
|
2 |
# 2001 September 15
|
sl@0
|
3 |
#
|
sl@0
|
4 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
5 |
# a legal notice, here is a blessing:
|
sl@0
|
6 |
#
|
sl@0
|
7 |
# May you do good and not evil.
|
sl@0
|
8 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
9 |
# May you share freely, never taking more than you give.
|
sl@0
|
10 |
#
|
sl@0
|
11 |
#*************************************************************************
|
sl@0
|
12 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
13 |
# focus of this file is testing DISTINCT, UNION, INTERSECT and EXCEPT
|
sl@0
|
14 |
# SELECT statements that use user-defined collation sequences. Also
|
sl@0
|
15 |
# GROUP BY clauses that use user-defined collation sequences.
|
sl@0
|
16 |
#
|
sl@0
|
17 |
# $Id: collate5.test,v 1.6 2007/11/12 15:29:19 danielk1977 Exp $
|
sl@0
|
18 |
|
sl@0
|
19 |
set testdir [file dirname $argv0]
|
sl@0
|
20 |
source $testdir/tester.tcl
|
sl@0
|
21 |
|
sl@0
|
22 |
|
sl@0
|
23 |
#
|
sl@0
|
24 |
# Tests are organised as follows:
|
sl@0
|
25 |
# collate5-1.* - DISTINCT
|
sl@0
|
26 |
# collate5-2.* - Compound SELECT
|
sl@0
|
27 |
# collate5-3.* - ORDER BY on compound SELECT
|
sl@0
|
28 |
# collate5-4.* - GROUP BY
|
sl@0
|
29 |
|
sl@0
|
30 |
# Create the collation sequence 'TEXT', purely for asthetic reasons. The
|
sl@0
|
31 |
# test cases in this script could just as easily use BINARY.
|
sl@0
|
32 |
db collate TEXT [list string compare]
|
sl@0
|
33 |
|
sl@0
|
34 |
# Mimic the SQLite 2 collation type NUMERIC.
|
sl@0
|
35 |
db collate numeric numeric_collate
|
sl@0
|
36 |
proc numeric_collate {lhs rhs} {
|
sl@0
|
37 |
if {$lhs == $rhs} {return 0}
|
sl@0
|
38 |
return [expr ($lhs>$rhs)?1:-1]
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
#
|
sl@0
|
42 |
# These tests - collate5-1.* - focus on the DISTINCT keyword.
|
sl@0
|
43 |
#
|
sl@0
|
44 |
do_test collate5-1.0 {
|
sl@0
|
45 |
execsql {
|
sl@0
|
46 |
CREATE TABLE collate5t1(a COLLATE nocase, b COLLATE text);
|
sl@0
|
47 |
|
sl@0
|
48 |
INSERT INTO collate5t1 VALUES('a', 'apple');
|
sl@0
|
49 |
INSERT INTO collate5t1 VALUES('A', 'Apple');
|
sl@0
|
50 |
INSERT INTO collate5t1 VALUES('b', 'banana');
|
sl@0
|
51 |
INSERT INTO collate5t1 VALUES('B', 'banana');
|
sl@0
|
52 |
INSERT INTO collate5t1 VALUES('n', NULL);
|
sl@0
|
53 |
INSERT INTO collate5t1 VALUES('N', NULL);
|
sl@0
|
54 |
}
|
sl@0
|
55 |
} {}
|
sl@0
|
56 |
do_test collate5-1.1 {
|
sl@0
|
57 |
execsql {
|
sl@0
|
58 |
SELECT DISTINCT a FROM collate5t1;
|
sl@0
|
59 |
}
|
sl@0
|
60 |
} {A B N}
|
sl@0
|
61 |
do_test collate5-1.2 {
|
sl@0
|
62 |
execsql {
|
sl@0
|
63 |
SELECT DISTINCT b FROM collate5t1;
|
sl@0
|
64 |
}
|
sl@0
|
65 |
} {{} Apple apple banana}
|
sl@0
|
66 |
do_test collate5-1.3 {
|
sl@0
|
67 |
execsql {
|
sl@0
|
68 |
SELECT DISTINCT a, b FROM collate5t1;
|
sl@0
|
69 |
}
|
sl@0
|
70 |
} {A Apple a apple B banana N {}}
|
sl@0
|
71 |
|
sl@0
|
72 |
# The remainder of this file tests compound SELECT statements.
|
sl@0
|
73 |
# Omit it if the library is compiled such that they are omitted.
|
sl@0
|
74 |
#
|
sl@0
|
75 |
ifcapable !compound {
|
sl@0
|
76 |
finish_test
|
sl@0
|
77 |
return
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
#
|
sl@0
|
81 |
# Tests named collate5-2.* focus on UNION, EXCEPT and INTERSECT
|
sl@0
|
82 |
# queries that use user-defined collation sequences.
|
sl@0
|
83 |
#
|
sl@0
|
84 |
# collate5-2.1.* - UNION
|
sl@0
|
85 |
# collate5-2.2.* - INTERSECT
|
sl@0
|
86 |
# collate5-2.3.* - EXCEPT
|
sl@0
|
87 |
#
|
sl@0
|
88 |
do_test collate5-2.0 {
|
sl@0
|
89 |
execsql {
|
sl@0
|
90 |
CREATE TABLE collate5t2(a COLLATE text, b COLLATE nocase);
|
sl@0
|
91 |
|
sl@0
|
92 |
INSERT INTO collate5t2 VALUES('a', 'apple');
|
sl@0
|
93 |
INSERT INTO collate5t2 VALUES('A', 'apple');
|
sl@0
|
94 |
INSERT INTO collate5t2 VALUES('b', 'banana');
|
sl@0
|
95 |
INSERT INTO collate5t2 VALUES('B', 'Banana');
|
sl@0
|
96 |
}
|
sl@0
|
97 |
} {}
|
sl@0
|
98 |
|
sl@0
|
99 |
do_test collate5-2.1.1 {
|
sl@0
|
100 |
execsql {
|
sl@0
|
101 |
SELECT a FROM collate5t1 UNION select a FROM collate5t2;
|
sl@0
|
102 |
}
|
sl@0
|
103 |
} {A B N}
|
sl@0
|
104 |
do_test collate5-2.1.2 {
|
sl@0
|
105 |
execsql {
|
sl@0
|
106 |
SELECT a FROM collate5t2 UNION select a FROM collate5t1;
|
sl@0
|
107 |
}
|
sl@0
|
108 |
} {A B N a b n}
|
sl@0
|
109 |
do_test collate5-2.1.3 {
|
sl@0
|
110 |
execsql {
|
sl@0
|
111 |
SELECT a, b FROM collate5t1 UNION select a, b FROM collate5t2;
|
sl@0
|
112 |
}
|
sl@0
|
113 |
} {A Apple A apple B Banana b banana N {}}
|
sl@0
|
114 |
do_test collate5-2.1.4 {
|
sl@0
|
115 |
execsql {
|
sl@0
|
116 |
SELECT a, b FROM collate5t2 UNION select a, b FROM collate5t1;
|
sl@0
|
117 |
}
|
sl@0
|
118 |
} {A Apple B banana N {} a apple b banana n {}}
|
sl@0
|
119 |
|
sl@0
|
120 |
do_test collate5-2.2.1 {
|
sl@0
|
121 |
execsql {
|
sl@0
|
122 |
SELECT a FROM collate5t1 EXCEPT select a FROM collate5t2;
|
sl@0
|
123 |
}
|
sl@0
|
124 |
} {N}
|
sl@0
|
125 |
do_test collate5-2.2.2 {
|
sl@0
|
126 |
execsql {
|
sl@0
|
127 |
SELECT a FROM collate5t2 EXCEPT select a FROM collate5t1 WHERE a != 'a';
|
sl@0
|
128 |
}
|
sl@0
|
129 |
} {A a}
|
sl@0
|
130 |
do_test collate5-2.2.3 {
|
sl@0
|
131 |
execsql {
|
sl@0
|
132 |
SELECT a, b FROM collate5t1 EXCEPT select a, b FROM collate5t2;
|
sl@0
|
133 |
}
|
sl@0
|
134 |
} {A Apple N {}}
|
sl@0
|
135 |
do_test collate5-2.2.4 {
|
sl@0
|
136 |
execsql {
|
sl@0
|
137 |
SELECT a, b FROM collate5t2 EXCEPT select a, b FROM collate5t1
|
sl@0
|
138 |
where a != 'a';
|
sl@0
|
139 |
}
|
sl@0
|
140 |
} {A apple a apple}
|
sl@0
|
141 |
|
sl@0
|
142 |
do_test collate5-2.3.1 {
|
sl@0
|
143 |
execsql {
|
sl@0
|
144 |
SELECT a FROM collate5t1 INTERSECT select a FROM collate5t2;
|
sl@0
|
145 |
}
|
sl@0
|
146 |
} {A B}
|
sl@0
|
147 |
do_test collate5-2.3.2 {
|
sl@0
|
148 |
execsql {
|
sl@0
|
149 |
SELECT a FROM collate5t2 INTERSECT select a FROM collate5t1 WHERE a != 'a';
|
sl@0
|
150 |
}
|
sl@0
|
151 |
} {B b}
|
sl@0
|
152 |
do_test collate5-2.3.3 {
|
sl@0
|
153 |
execsql {
|
sl@0
|
154 |
SELECT a, b FROM collate5t1 INTERSECT select a, b FROM collate5t2;
|
sl@0
|
155 |
}
|
sl@0
|
156 |
} {a apple B banana}
|
sl@0
|
157 |
do_test collate5-2.3.4 {
|
sl@0
|
158 |
execsql {
|
sl@0
|
159 |
SELECT a, b FROM collate5t2 INTERSECT select a, b FROM collate5t1;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
} {A apple B Banana a apple b banana}
|
sl@0
|
162 |
|
sl@0
|
163 |
#
|
sl@0
|
164 |
# This test ensures performs a UNION operation with a bunch of different
|
sl@0
|
165 |
# length records. The goal is to test that the logic that compares records
|
sl@0
|
166 |
# for the compound SELECT operators works with record lengths that lie
|
sl@0
|
167 |
# either side of the troublesome 256 and 65536 byte marks.
|
sl@0
|
168 |
#
|
sl@0
|
169 |
set ::lens [list \
|
sl@0
|
170 |
0 1 2 3 4 5 6 7 8 9 \
|
sl@0
|
171 |
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 \
|
sl@0
|
172 |
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 \
|
sl@0
|
173 |
65520 65521 65522 65523 65524 65525 65526 65527 65528 65529 65530 \
|
sl@0
|
174 |
65531 65532 65533 65534 65535 65536 65537 65538 65539 65540 65541 \
|
sl@0
|
175 |
65542 65543 65544 65545 65546 65547 65548 65549 65550 65551 ]
|
sl@0
|
176 |
do_test collate5-2.4.0 {
|
sl@0
|
177 |
execsql {
|
sl@0
|
178 |
BEGIN;
|
sl@0
|
179 |
CREATE TABLE collate5t3(a, b);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
foreach ii $::lens {
|
sl@0
|
182 |
execsql "INSERT INTO collate5t3 VALUES($ii, '[string repeat a $ii]');"
|
sl@0
|
183 |
}
|
sl@0
|
184 |
expr [llength [execsql {
|
sl@0
|
185 |
COMMIT;
|
sl@0
|
186 |
SELECT * FROM collate5t3 UNION SELECT * FROM collate5t3;
|
sl@0
|
187 |
}]] / 2
|
sl@0
|
188 |
} [llength $::lens]
|
sl@0
|
189 |
do_test collate5-2.4.1 {
|
sl@0
|
190 |
execsql {DROP TABLE collate5t3;}
|
sl@0
|
191 |
} {}
|
sl@0
|
192 |
unset ::lens
|
sl@0
|
193 |
|
sl@0
|
194 |
#
|
sl@0
|
195 |
# These tests - collate5-3.* - focus on compound SELECT queries that
|
sl@0
|
196 |
# feature ORDER BY clauses.
|
sl@0
|
197 |
#
|
sl@0
|
198 |
do_test collate5-3.0 {
|
sl@0
|
199 |
execsql {
|
sl@0
|
200 |
SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2 ORDER BY 1;
|
sl@0
|
201 |
}
|
sl@0
|
202 |
} {a A a A b B b B n N}
|
sl@0
|
203 |
do_test collate5-3.1 {
|
sl@0
|
204 |
execsql {
|
sl@0
|
205 |
SELECT a FROM collate5t2 UNION ALL SELECT a FROM collate5t1 ORDER BY 1;
|
sl@0
|
206 |
}
|
sl@0
|
207 |
} {A A B B N a a b b n}
|
sl@0
|
208 |
do_test collate5-3.2 {
|
sl@0
|
209 |
execsql {
|
sl@0
|
210 |
SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2
|
sl@0
|
211 |
ORDER BY 1 COLLATE TEXT;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
} {A A B B N a a b b n}
|
sl@0
|
214 |
|
sl@0
|
215 |
do_test collate5-3.3 {
|
sl@0
|
216 |
execsql {
|
sl@0
|
217 |
CREATE TABLE collate5t_cn(a COLLATE NUMERIC);
|
sl@0
|
218 |
CREATE TABLE collate5t_ct(a COLLATE TEXT);
|
sl@0
|
219 |
INSERT INTO collate5t_cn VALUES('1');
|
sl@0
|
220 |
INSERT INTO collate5t_cn VALUES('11');
|
sl@0
|
221 |
INSERT INTO collate5t_cn VALUES('101');
|
sl@0
|
222 |
INSERT INTO collate5t_ct SELECT * FROM collate5t_cn;
|
sl@0
|
223 |
}
|
sl@0
|
224 |
} {}
|
sl@0
|
225 |
do_test collate5-3.4 {
|
sl@0
|
226 |
execsql {
|
sl@0
|
227 |
SELECT a FROM collate5t_cn INTERSECT SELECT a FROM collate5t_ct ORDER BY 1;
|
sl@0
|
228 |
}
|
sl@0
|
229 |
} {1 11 101}
|
sl@0
|
230 |
do_test collate5-3.5 {
|
sl@0
|
231 |
execsql {
|
sl@0
|
232 |
SELECT a FROM collate5t_ct INTERSECT SELECT a FROM collate5t_cn ORDER BY 1;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
} {1 101 11}
|
sl@0
|
235 |
|
sl@0
|
236 |
do_test collate5-3.20 {
|
sl@0
|
237 |
execsql {
|
sl@0
|
238 |
DROP TABLE collate5t_cn;
|
sl@0
|
239 |
DROP TABLE collate5t_ct;
|
sl@0
|
240 |
DROP TABLE collate5t1;
|
sl@0
|
241 |
DROP TABLE collate5t2;
|
sl@0
|
242 |
}
|
sl@0
|
243 |
} {}
|
sl@0
|
244 |
|
sl@0
|
245 |
do_test collate5-4.0 {
|
sl@0
|
246 |
execsql {
|
sl@0
|
247 |
CREATE TABLE collate5t1(a COLLATE NOCASE, b COLLATE NUMERIC);
|
sl@0
|
248 |
INSERT INTO collate5t1 VALUES('a', '1');
|
sl@0
|
249 |
INSERT INTO collate5t1 VALUES('A', '1.0');
|
sl@0
|
250 |
INSERT INTO collate5t1 VALUES('b', '2');
|
sl@0
|
251 |
INSERT INTO collate5t1 VALUES('B', '3');
|
sl@0
|
252 |
}
|
sl@0
|
253 |
} {}
|
sl@0
|
254 |
do_test collate5-4.1 {
|
sl@0
|
255 |
string tolower [execsql {
|
sl@0
|
256 |
SELECT a, count(*) FROM collate5t1 GROUP BY a;
|
sl@0
|
257 |
}]
|
sl@0
|
258 |
} {a 2 b 2}
|
sl@0
|
259 |
do_test collate5-4.2 {
|
sl@0
|
260 |
execsql {
|
sl@0
|
261 |
SELECT a, b, count(*) FROM collate5t1 GROUP BY a, b ORDER BY a, b;
|
sl@0
|
262 |
}
|
sl@0
|
263 |
} {A 1.0 2 b 2 1 B 3 1}
|
sl@0
|
264 |
do_test collate5-4.3 {
|
sl@0
|
265 |
execsql {
|
sl@0
|
266 |
DROP TABLE collate5t1;
|
sl@0
|
267 |
}
|
sl@0
|
268 |
} {}
|
sl@0
|
269 |
|
sl@0
|
270 |
finish_test
|