xref: /sqlite-3.40.0/test/swarmvtab.test (revision 7d44b22d)
163331b1aSdan# 2017-07-15
263331b1aSdan#
363331b1aSdan# The author disclaims copyright to this source code.  In place of
463331b1aSdan# a legal notice, here is a blessing:
563331b1aSdan#
663331b1aSdan#    May you do good and not evil.
763331b1aSdan#    May you find forgiveness for yourself and forgive others.
863331b1aSdan#    May you share freely, never taking more than you give.
963331b1aSdan#
1063331b1aSdan#***********************************************************************
1163331b1aSdan# This file implements regression tests for SQLite library.  The
1263331b1aSdan# focus of this file is the "swarmvtab" extension
1363331b1aSdan#
1463331b1aSdan
1563331b1aSdanset testdir [file dirname $argv0]
1663331b1aSdansource $testdir/tester.tcl
1763331b1aSdanset testprefix swarmvtab
18efeaec36Sdrhdo_not_use_codec
1963331b1aSdan
2063331b1aSdanifcapable !vtab {
2163331b1aSdan  finish_test
2263331b1aSdan  return
2363331b1aSdan}
2463331b1aSdan
2563331b1aSdanload_static_extension db unionvtab
2663331b1aSdan
27a64d1fbcSdanset nFile $sqlite_open_file_count
28a64d1fbcSdan
2963331b1aSdando_execsql_test 1.0 {
3063331b1aSdan  CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
3163331b1aSdan  WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400)
3263331b1aSdan  INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
3363331b1aSdan
3463331b1aSdan  CREATE TABLE dir(f, t, imin, imax);
3563331b1aSdan}
3663331b1aSdan
3763331b1aSdando_test 1.1 {
3863331b1aSdan  for {set i 0} {$i < 40} {incr i} {
3963331b1aSdan    set iMin [expr $i*10 + 1]
4063331b1aSdan    set iMax [expr $iMin+9]
4163331b1aSdan
4263331b1aSdan    forcedelete "test.db$i"
4363331b1aSdan    execsql [subst {
4463331b1aSdan      ATTACH 'test.db$i' AS aux;
4563331b1aSdan      CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
4663331b1aSdan      INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
4763331b1aSdan      DETACH aux;
4863331b1aSdan      INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
4963331b1aSdan    }]
5063331b1aSdan  }
5163331b1aSdan
5263331b1aSdan  execsql {
5363331b1aSdan    CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
5463331b1aSdan  }
55a64d1fbcSdan} {}
5663331b1aSdan
5763331b1aSdando_execsql_test 1.2 {
5863331b1aSdan  DROP TABLE s1;
59a64d1fbcSdan} {}
6063331b1aSdan
6163331b1aSdando_execsql_test 1.3 {
6263331b1aSdan  CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
6363331b1aSdan  SELECT count(*) FROM s1 WHERE rowid<50;
6463331b1aSdan} {49}
6563331b1aSdan
6663331b1aSdanproc do_compare_test {tn where} {
6763331b1aSdan  set sql [subst {
680ff22170Sdan    SELECT (SELECT group_concat(a || ',' || b, ',') FROM t0 WHERE $where)
690ff22170Sdan           IS
700ff22170Sdan           (SELECT group_concat(a || ',' || b, ',') FROM s1 WHERE $where)
7163331b1aSdan  }]
7263331b1aSdan
7363331b1aSdan  uplevel [list do_execsql_test $tn $sql 1]
7463331b1aSdan}
7563331b1aSdan
760ff22170Sdando_compare_test 1.4.1 "rowid = 700"
770ff22170Sdando_compare_test 1.4.2 "rowid = -1"
780ff22170Sdando_compare_test 1.4.3 "rowid = 0"
790ff22170Sdando_compare_test 1.4.4 "rowid = 55"
800ff22170Sdando_compare_test 1.4.5 "rowid BETWEEN 20 AND 100"
810ff22170Sdando_compare_test 1.4.6 "rowid > 350"
820ff22170Sdando_compare_test 1.4.7 "rowid >= 350"
830ff22170Sdando_compare_test 1.4.8 "rowid >= 200"
840ff22170Sdando_compare_test 1.4.9 "1"
850ff22170Sdan
860ff22170Sdan# Multiple simultaneous cursors.
870ff22170Sdan#
880ff22170Sdando_execsql_test 1.5.1.(5-seconds-or-so) {
890ff22170Sdan  SELECT count(*) FROM s1 a, s1 b WHERE b.rowid<=200;
900ff22170Sdan} {80000}
910ff22170Sdando_execsql_test 1.5.2 {
920ff22170Sdan  SELECT count(*) FROM s1 a, s1 b, s1 c
930ff22170Sdan  WHERE a.rowid=b.rowid AND b.rowid=c.rowid;
940ff22170Sdan} {400}
950ff22170Sdan
960ff22170Sdan# Empty source tables.
970ff22170Sdan#
980ff22170Sdando_test 1.6.0 {
990ff22170Sdan  for {set i 0} {$i < 20} {incr i} {
1000ff22170Sdan    sqlite3 db2 test.db$i
1010ff22170Sdan    db2 eval " DELETE FROM t$i "
1020ff22170Sdan    db2 close
1030ff22170Sdan  }
1040ff22170Sdan  db eval { DELETE FROM t0 WHERE rowid<=200 }
1050ff22170Sdan} {}
1060ff22170Sdan
1070ff22170Sdando_compare_test 1.6.1 "rowid = 700"
1080ff22170Sdando_compare_test 1.6.2 "rowid = -1"
1090ff22170Sdando_compare_test 1.6.3 "rowid = 0"
1100ff22170Sdando_compare_test 1.6.4 "rowid = 55"
1110ff22170Sdando_compare_test 1.6.5 "rowid BETWEEN 20 AND 100"
1120ff22170Sdando_compare_test 1.6.6 "rowid > 350"
1130ff22170Sdando_compare_test 1.6.7 "rowid >= 350"
1140ff22170Sdando_compare_test 1.6.8 "rowid >= 200"
1150ff22170Sdando_compare_test 1.6.9 "1"
1160ff22170Sdando_compare_test 1.6.10 "rowid >= 5"
11763331b1aSdan
118a64d1fbcSdando_test 1.x {
119a64d1fbcSdan  set sqlite_open_file_count
120a64d1fbcSdan} [expr $nFile+9]
121a64d1fbcSdan
122a64d1fbcSdando_test 1.y { db close } {}
12363331b1aSdan
1240ff22170Sdan# Delete all the database files created above.
1250ff22170Sdan#
1260ff22170Sdanfor {set i 0} {$i < 40} {incr i} { forcedelete "test.db$i" }
1270ff22170Sdan
1280ff22170Sdan#-------------------------------------------------------------------------
1290ff22170Sdan# Test some error conditions:
1300ff22170Sdan#
1310ff22170Sdan#   2.1: Database file does not exist.
1320ff22170Sdan#   2.2: Table does not exist.
1330ff22170Sdan#   2.3: Table schema does not match.
134226744d1Sdan#   2.4: Syntax error in SELECT statement.
1350ff22170Sdan#
1360ff22170Sdanreset_db
1370ff22170Sdanload_static_extension db unionvtab
1380ff22170Sdando_test 2.0.1 {
1390ff22170Sdan  db eval {
1400ff22170Sdan    CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
1410ff22170Sdan    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400)
1420ff22170Sdan      INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
1430ff22170Sdan    CREATE TABLE dir(f, t, imin, imax);
1440ff22170Sdan  }
1450ff22170Sdan
1460ff22170Sdan  for {set i 0} {$i < 40} {incr i} {
1470ff22170Sdan    set iMin [expr $i*10 + 1]
1480ff22170Sdan    set iMax [expr $iMin+9]
1490ff22170Sdan
1500ff22170Sdan    forcedelete "test.db$i"
1510ff22170Sdan    db eval [subst {
1520ff22170Sdan      ATTACH 'test.db$i' AS aux;
1530ff22170Sdan      CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
1540ff22170Sdan      INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
1550ff22170Sdan      DETACH aux;
1560ff22170Sdan      INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
1570ff22170Sdan    }]
1580ff22170Sdan  }
1590ff22170Sdan  execsql {
1600ff22170Sdan    CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
1610ff22170Sdan  }
1620ff22170Sdan} {}
1630ff22170Sdan
1640ff22170Sdando_test 2.0.2 {
1650ff22170Sdan  forcedelete test.db5
1660ff22170Sdan
1670ff22170Sdan  sqlite3 db2 test.db15
1680ff22170Sdan  db2 eval { DROP TABLE t15 }
1690ff22170Sdan  db2 close
1700ff22170Sdan
1710ff22170Sdan  sqlite3 db2 test.db25
1720ff22170Sdan  db2 eval {
1730ff22170Sdan    DROP TABLE t25;
1740ff22170Sdan    CREATE TABLE t25(x, y, z PRIMARY KEY);
1750ff22170Sdan  }
1760ff22170Sdan  db2 close
1770ff22170Sdan} {}
1780ff22170Sdan
1790ff22170Sdando_catchsql_test 2.1 {
1800ff22170Sdan  SELECT * FROM s1 WHERE rowid BETWEEN 1 AND 100;
1810ff22170Sdan} {1 {unable to open database file}}
1820ff22170Sdando_catchsql_test 2.2 {
1830ff22170Sdan  SELECT * FROM s1 WHERE rowid BETWEEN 101 AND 200;
1840ff22170Sdan} {1 {no such rowid table: t15}}
1850ff22170Sdando_catchsql_test 2.3 {
1860ff22170Sdan  SELECT * FROM s1 WHERE rowid BETWEEN 201 AND 300;
1870ff22170Sdan} {1 {source table schema mismatch}}
18863331b1aSdan
189226744d1Sdando_catchsql_test 2.4 {
190226744d1Sdan  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir');
191226744d1Sdan} {1 {sql error: near "FROMdir": syntax error}}
192226744d1Sdando_catchsql_test 2.5 {
193226744d1Sdan  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir', 'fetchdb');
194226744d1Sdan} {1 {sql error: near "FROMdir": syntax error}}
195226744d1Sdan
196226744d1Sdanfor {set i 0} {$i < 40} {incr i} {
197226744d1Sdan  forcedelete "test.db$i"
198226744d1Sdan}
199226744d1Sdan
200226744d1Sdan#-------------------------------------------------------------------------
201226744d1Sdan# Test the outcome of the fetch function throwing an exception.
202226744d1Sdan#
203226744d1Sdanproc fetch_db {file} {
204226744d1Sdan  error "fetch_db error!"
205226744d1Sdan}
206226744d1Sdan
207226744d1Sdandb func fetch_db fetch_db
208226744d1Sdan
209226744d1Sdando_catchsql_test 3.1 {
210226744d1Sdan  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
211226744d1Sdan    'VALUES
212*7d44b22dSdrh        (''test.db1'', ''t1'', 1, 10),
213*7d44b22dSdrh        (''test.db2'', ''t1'', 11, 20)
214226744d1Sdan    ', 'fetch_db_no_such_function'
215226744d1Sdan  );
2162c44e370Sdan} {1 {sql error: no such function: fetch_db_no_such_function}}
217226744d1Sdan
218226744d1Sdando_catchsql_test 3.2 {
219226744d1Sdan  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
220226744d1Sdan    'VALUES
221*7d44b22dSdrh        (''test.db1'', ''t1'', 1, 10),
222*7d44b22dSdrh        (''test.db2'', ''t1'', 11, 20)
223226744d1Sdan    ', 'fetch_db'
224226744d1Sdan  );
225226744d1Sdan} {1 {fetch_db error!}}
226226744d1Sdan
227226744d1Sdando_execsql_test 3.3.1 {
228226744d1Sdan  ATTACH 'test.db1' AS aux;
229226744d1Sdan  CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);
230226744d1Sdan  INSERT INTO aux.t1 VALUES(1, NULL);
231226744d1Sdan  INSERT INTO aux.t1 VALUES(2, NULL);
232226744d1Sdan  INSERT INTO aux.t1 VALUES(9, NULL);
233226744d1Sdan  DETACH aux;
234226744d1Sdan  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
235226744d1Sdan    'VALUES
236*7d44b22dSdrh        (''test.db1'', ''t1'', 1, 10),
237*7d44b22dSdrh        (''test.db2'', ''t1'', 11, 20)
238226744d1Sdan    ', 'fetch_db'
239226744d1Sdan  );
240226744d1Sdan} {}
241226744d1Sdan
24254640de1Smistachkindo_catchsql_test 3.3.2 { SELECT * FROM xyz } {1 {fetch_db error!}}
243226744d1Sdan
244226744d1Sdan
245226744d1Sdan
24663331b1aSdanfinish_test
247