3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is the "swarmvtab" extension
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix swarmvtab
24 load_static_extension db unionvtab
26 set nFile $sqlite_open_file_count
29 CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
30 WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400)
31 INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
33 CREATE TABLE dir(f, t, imin, imax);
37 for {set i 0} {$i < 40} {incr i} {
38 set iMin [expr $i*10 + 1]
39 set iMax [expr $iMin+9]
41 forcedelete "test.db$i"
43 ATTACH 'test.db$i' AS aux;
44 CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
45 INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
47 INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
52 CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
61 CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
62 SELECT count(*) FROM s1 WHERE rowid<50;
65 proc do_compare_test {tn where} {
67 SELECT (SELECT group_concat(a || ',' || b, ',') FROM t0 WHERE $where)
69 (SELECT group_concat(a || ',' || b, ',') FROM s1 WHERE $where)
72 uplevel [list do_execsql_test $tn $sql 1]
75 do_compare_test 1.4.1 "rowid = 700"
76 do_compare_test 1.4.2 "rowid = -1"
77 do_compare_test 1.4.3 "rowid = 0"
78 do_compare_test 1.4.4 "rowid = 55"
79 do_compare_test 1.4.5 "rowid BETWEEN 20 AND 100"
80 do_compare_test 1.4.6 "rowid > 350"
81 do_compare_test 1.4.7 "rowid >= 350"
82 do_compare_test 1.4.8 "rowid >= 200"
83 do_compare_test 1.4.9 "1"
85 # Multiple simultaneous cursors.
87 do_execsql_test 1.5.1.(5-seconds-or-so) {
88 SELECT count(*) FROM s1 a, s1 b WHERE b.rowid<=200;
90 do_execsql_test 1.5.2 {
91 SELECT count(*) FROM s1 a, s1 b, s1 c
92 WHERE a.rowid=b.rowid AND b.rowid=c.rowid;
95 # Empty source tables.
98 for {set i 0} {$i < 20} {incr i} {
100 db2 eval " DELETE FROM t$i "
103 db eval { DELETE FROM t0 WHERE rowid<=200 }
106 do_compare_test 1.6.1 "rowid = 700"
107 do_compare_test 1.6.2 "rowid = -1"
108 do_compare_test 1.6.3 "rowid = 0"
109 do_compare_test 1.6.4 "rowid = 55"
110 do_compare_test 1.6.5 "rowid BETWEEN 20 AND 100"
111 do_compare_test 1.6.6 "rowid > 350"
112 do_compare_test 1.6.7 "rowid >= 350"
113 do_compare_test 1.6.8 "rowid >= 200"
114 do_compare_test 1.6.9 "1"
115 do_compare_test 1.6.10 "rowid >= 5"
118 set sqlite_open_file_count
121 do_test 1.y { db close } {}
123 # Delete all the database files created above.
125 for {set i 0} {$i < 40} {incr i} { forcedelete "test.db$i" }
127 #-------------------------------------------------------------------------
128 # Test some error conditions:
130 # 2.1: Database file does not exist.
131 # 2.2: Table does not exist.
132 # 2.3: Table schema does not match.
133 # 2.4: Syntax error in SELECT statement.
136 load_static_extension db unionvtab
139 CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
140 WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400)
141 INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
142 CREATE TABLE dir(f, t, imin, imax);
145 for {set i 0} {$i < 40} {incr i} {
146 set iMin [expr $i*10 + 1]
147 set iMax [expr $iMin+9]
149 forcedelete "test.db$i"
151 ATTACH 'test.db$i' AS aux;
152 CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
153 INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
155 INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
159 CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
166 sqlite3 db2 test.db15
167 db2 eval { DROP TABLE t15 }
170 sqlite3 db2 test.db25
173 CREATE TABLE t25(x, y, z PRIMARY KEY);
178 do_catchsql_test 2.1 {
179 SELECT * FROM s1 WHERE rowid BETWEEN 1 AND 100;
180 } {1 {unable to open database file}}
181 do_catchsql_test 2.2 {
182 SELECT * FROM s1 WHERE rowid BETWEEN 101 AND 200;
183 } {1 {no such rowid table: t15}}
184 do_catchsql_test 2.3 {
185 SELECT * FROM s1 WHERE rowid BETWEEN 201 AND 300;
186 } {1 {source table schema mismatch}}
188 do_catchsql_test 2.4 {
189 CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir');
190 } {1 {sql error: near "FROMdir": syntax error}}
191 do_catchsql_test 2.5 {
192 CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir', 'fetchdb');
193 } {1 {sql error: near "FROMdir": syntax error}}
195 for {set i 0} {$i < 40} {incr i} {
196 forcedelete "test.db$i"
199 #-------------------------------------------------------------------------
200 # Test the outcome of the fetch function throwing an exception.
202 proc fetch_db {file} {
203 error "fetch_db error!"
206 db func fetch_db fetch_db
208 do_catchsql_test 3.1 {
209 CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
211 ("test.db1", "t1", 1, 10),
212 ("test.db2", "t1", 11, 20)
213 ', 'fetch_db_no_such_function'
215 } {1 {no such function: fetch_db_no_such_function}}
217 do_catchsql_test 3.2 {
218 CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
220 ("test.db1", "t1", 1, 10),
221 ("test.db2", "t1", 11, 20)
224 } {1 {fetch_db error!}}
226 do_execsql_test 3.3.1 {
227 ATTACH 'test.db1' AS aux;
228 CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);
229 INSERT INTO aux.t1 VALUES(1, NULL);
230 INSERT INTO aux.t1 VALUES(2, NULL);
231 INSERT INTO aux.t1 VALUES(9, NULL);
233 CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
235 ("test.db1", "t1", 1, 10),
236 ("test.db2", "t1", 11, 20)
241 do_catchsql_test 3.3.2 { SELECT * FROM xyz } {1 {fetch_db error!}}