First public contribution.
4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #*************************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this file is testing DISTINCT, UNION, INTERSECT and EXCEPT
14 # SELECT statements that use user-defined collation sequences. Also
15 # GROUP BY clauses that use user-defined collation sequences.
17 # $Id: collate5.test,v 1.6 2007/11/12 15:29:19 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
24 # Tests are organised as follows:
25 # collate5-1.* - DISTINCT
26 # collate5-2.* - Compound SELECT
27 # collate5-3.* - ORDER BY on compound SELECT
28 # collate5-4.* - GROUP BY
30 # Create the collation sequence 'TEXT', purely for asthetic reasons. The
31 # test cases in this script could just as easily use BINARY.
32 db collate TEXT [list string compare]
34 # Mimic the SQLite 2 collation type NUMERIC.
35 db collate numeric numeric_collate
36 proc numeric_collate {lhs rhs} {
37 if {$lhs == $rhs} {return 0}
38 return [expr ($lhs>$rhs)?1:-1]
42 # These tests - collate5-1.* - focus on the DISTINCT keyword.
44 do_test collate5-1.0 {
46 CREATE TABLE collate5t1(a COLLATE nocase, b COLLATE text);
48 INSERT INTO collate5t1 VALUES('a', 'apple');
49 INSERT INTO collate5t1 VALUES('A', 'Apple');
50 INSERT INTO collate5t1 VALUES('b', 'banana');
51 INSERT INTO collate5t1 VALUES('B', 'banana');
52 INSERT INTO collate5t1 VALUES('n', NULL);
53 INSERT INTO collate5t1 VALUES('N', NULL);
56 do_test collate5-1.1 {
58 SELECT DISTINCT a FROM collate5t1;
61 do_test collate5-1.2 {
63 SELECT DISTINCT b FROM collate5t1;
65 } {{} Apple apple banana}
66 do_test collate5-1.3 {
68 SELECT DISTINCT a, b FROM collate5t1;
70 } {A Apple a apple B banana N {}}
72 # The remainder of this file tests compound SELECT statements.
73 # Omit it if the library is compiled such that they are omitted.
81 # Tests named collate5-2.* focus on UNION, EXCEPT and INTERSECT
82 # queries that use user-defined collation sequences.
84 # collate5-2.1.* - UNION
85 # collate5-2.2.* - INTERSECT
86 # collate5-2.3.* - EXCEPT
88 do_test collate5-2.0 {
90 CREATE TABLE collate5t2(a COLLATE text, b COLLATE nocase);
92 INSERT INTO collate5t2 VALUES('a', 'apple');
93 INSERT INTO collate5t2 VALUES('A', 'apple');
94 INSERT INTO collate5t2 VALUES('b', 'banana');
95 INSERT INTO collate5t2 VALUES('B', 'Banana');
99 do_test collate5-2.1.1 {
101 SELECT a FROM collate5t1 UNION select a FROM collate5t2;
104 do_test collate5-2.1.2 {
106 SELECT a FROM collate5t2 UNION select a FROM collate5t1;
109 do_test collate5-2.1.3 {
111 SELECT a, b FROM collate5t1 UNION select a, b FROM collate5t2;
113 } {A Apple A apple B Banana b banana N {}}
114 do_test collate5-2.1.4 {
116 SELECT a, b FROM collate5t2 UNION select a, b FROM collate5t1;
118 } {A Apple B banana N {} a apple b banana n {}}
120 do_test collate5-2.2.1 {
122 SELECT a FROM collate5t1 EXCEPT select a FROM collate5t2;
125 do_test collate5-2.2.2 {
127 SELECT a FROM collate5t2 EXCEPT select a FROM collate5t1 WHERE a != 'a';
130 do_test collate5-2.2.3 {
132 SELECT a, b FROM collate5t1 EXCEPT select a, b FROM collate5t2;
135 do_test collate5-2.2.4 {
137 SELECT a, b FROM collate5t2 EXCEPT select a, b FROM collate5t1
142 do_test collate5-2.3.1 {
144 SELECT a FROM collate5t1 INTERSECT select a FROM collate5t2;
147 do_test collate5-2.3.2 {
149 SELECT a FROM collate5t2 INTERSECT select a FROM collate5t1 WHERE a != 'a';
152 do_test collate5-2.3.3 {
154 SELECT a, b FROM collate5t1 INTERSECT select a, b FROM collate5t2;
157 do_test collate5-2.3.4 {
159 SELECT a, b FROM collate5t2 INTERSECT select a, b FROM collate5t1;
161 } {A apple B Banana a apple b banana}
164 # This test ensures performs a UNION operation with a bunch of different
165 # length records. The goal is to test that the logic that compares records
166 # for the compound SELECT operators works with record lengths that lie
167 # either side of the troublesome 256 and 65536 byte marks.
170 0 1 2 3 4 5 6 7 8 9 \
171 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 \
172 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 \
173 65520 65521 65522 65523 65524 65525 65526 65527 65528 65529 65530 \
174 65531 65532 65533 65534 65535 65536 65537 65538 65539 65540 65541 \
175 65542 65543 65544 65545 65546 65547 65548 65549 65550 65551 ]
176 do_test collate5-2.4.0 {
179 CREATE TABLE collate5t3(a, b);
182 execsql "INSERT INTO collate5t3 VALUES($ii, '[string repeat a $ii]');"
184 expr [llength [execsql {
186 SELECT * FROM collate5t3 UNION SELECT * FROM collate5t3;
189 do_test collate5-2.4.1 {
190 execsql {DROP TABLE collate5t3;}
195 # These tests - collate5-3.* - focus on compound SELECT queries that
196 # feature ORDER BY clauses.
198 do_test collate5-3.0 {
200 SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2 ORDER BY 1;
202 } {a A a A b B b B n N}
203 do_test collate5-3.1 {
205 SELECT a FROM collate5t2 UNION ALL SELECT a FROM collate5t1 ORDER BY 1;
207 } {A A B B N a a b b n}
208 do_test collate5-3.2 {
210 SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2
211 ORDER BY 1 COLLATE TEXT;
213 } {A A B B N a a b b n}
215 do_test collate5-3.3 {
217 CREATE TABLE collate5t_cn(a COLLATE NUMERIC);
218 CREATE TABLE collate5t_ct(a COLLATE TEXT);
219 INSERT INTO collate5t_cn VALUES('1');
220 INSERT INTO collate5t_cn VALUES('11');
221 INSERT INTO collate5t_cn VALUES('101');
222 INSERT INTO collate5t_ct SELECT * FROM collate5t_cn;
225 do_test collate5-3.4 {
227 SELECT a FROM collate5t_cn INTERSECT SELECT a FROM collate5t_ct ORDER BY 1;
230 do_test collate5-3.5 {
232 SELECT a FROM collate5t_ct INTERSECT SELECT a FROM collate5t_cn ORDER BY 1;
236 do_test collate5-3.20 {
238 DROP TABLE collate5t_cn;
239 DROP TABLE collate5t_ct;
240 DROP TABLE collate5t1;
241 DROP TABLE collate5t2;
245 do_test collate5-4.0 {
247 CREATE TABLE collate5t1(a COLLATE NOCASE, b COLLATE NUMERIC);
248 INSERT INTO collate5t1 VALUES('a', '1');
249 INSERT INTO collate5t1 VALUES('A', '1.0');
250 INSERT INTO collate5t1 VALUES('b', '2');
251 INSERT INTO collate5t1 VALUES('B', '3');
254 do_test collate5-4.1 {
255 string tolower [execsql {
256 SELECT a, count(*) FROM collate5t1 GROUP BY a;
259 do_test collate5-4.2 {
261 SELECT a, b, count(*) FROM collate5t1 GROUP BY a, b ORDER BY a, b;
263 } {A 1.0 2 b 2 1 B 3 1}
264 do_test collate5-4.3 {
266 DROP TABLE collate5t1;