xref: /sqlite-3.40.0/test/swarmvtab.test (revision 226744d1)
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
1863331b1aSdan
1963331b1aSdanifcapable !vtab {
2063331b1aSdan  finish_test
2163331b1aSdan  return
2263331b1aSdan}
2363331b1aSdan
2463331b1aSdanload_static_extension db unionvtab
2563331b1aSdan
26a64d1fbcSdanset nFile $sqlite_open_file_count
27a64d1fbcSdan
2863331b1aSdando_execsql_test 1.0 {
2963331b1aSdan  CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
3063331b1aSdan  WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400)
3163331b1aSdan  INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
3263331b1aSdan
3363331b1aSdan  CREATE TABLE dir(f, t, imin, imax);
3463331b1aSdan}
3563331b1aSdan
3663331b1aSdando_test 1.1 {
3763331b1aSdan  for {set i 0} {$i < 40} {incr i} {
3863331b1aSdan    set iMin [expr $i*10 + 1]
3963331b1aSdan    set iMax [expr $iMin+9]
4063331b1aSdan
4163331b1aSdan    forcedelete "test.db$i"
4263331b1aSdan    execsql [subst {
4363331b1aSdan      ATTACH 'test.db$i' AS aux;
4463331b1aSdan      CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
4563331b1aSdan      INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
4663331b1aSdan      DETACH aux;
4763331b1aSdan      INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
4863331b1aSdan    }]
4963331b1aSdan  }
5063331b1aSdan
5163331b1aSdan  execsql {
5263331b1aSdan    CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
5363331b1aSdan  }
54a64d1fbcSdan} {}
5563331b1aSdan
5663331b1aSdando_execsql_test 1.2 {
5763331b1aSdan  DROP TABLE s1;
58a64d1fbcSdan} {}
5963331b1aSdan
6063331b1aSdando_execsql_test 1.3 {
6163331b1aSdan  CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
6263331b1aSdan  SELECT count(*) FROM s1 WHERE rowid<50;
6363331b1aSdan} {49}
6463331b1aSdan
6563331b1aSdanproc do_compare_test {tn where} {
6663331b1aSdan  set sql [subst {
670ff22170Sdan    SELECT (SELECT group_concat(a || ',' || b, ',') FROM t0 WHERE $where)
680ff22170Sdan           IS
690ff22170Sdan           (SELECT group_concat(a || ',' || b, ',') FROM s1 WHERE $where)
7063331b1aSdan  }]
7163331b1aSdan
7263331b1aSdan  uplevel [list do_execsql_test $tn $sql 1]
7363331b1aSdan}
7463331b1aSdan
750ff22170Sdando_compare_test 1.4.1 "rowid = 700"
760ff22170Sdando_compare_test 1.4.2 "rowid = -1"
770ff22170Sdando_compare_test 1.4.3 "rowid = 0"
780ff22170Sdando_compare_test 1.4.4 "rowid = 55"
790ff22170Sdando_compare_test 1.4.5 "rowid BETWEEN 20 AND 100"
800ff22170Sdando_compare_test 1.4.6 "rowid > 350"
810ff22170Sdando_compare_test 1.4.7 "rowid >= 350"
820ff22170Sdando_compare_test 1.4.8 "rowid >= 200"
830ff22170Sdando_compare_test 1.4.9 "1"
840ff22170Sdan
850ff22170Sdan# Multiple simultaneous cursors.
860ff22170Sdan#
870ff22170Sdando_execsql_test 1.5.1.(5-seconds-or-so) {
880ff22170Sdan  SELECT count(*) FROM s1 a, s1 b WHERE b.rowid<=200;
890ff22170Sdan} {80000}
900ff22170Sdando_execsql_test 1.5.2 {
910ff22170Sdan  SELECT count(*) FROM s1 a, s1 b, s1 c
920ff22170Sdan  WHERE a.rowid=b.rowid AND b.rowid=c.rowid;
930ff22170Sdan} {400}
940ff22170Sdan
950ff22170Sdan# Empty source tables.
960ff22170Sdan#
970ff22170Sdando_test 1.6.0 {
980ff22170Sdan  for {set i 0} {$i < 20} {incr i} {
990ff22170Sdan    sqlite3 db2 test.db$i
1000ff22170Sdan    db2 eval " DELETE FROM t$i "
1010ff22170Sdan    db2 close
1020ff22170Sdan  }
1030ff22170Sdan  db eval { DELETE FROM t0 WHERE rowid<=200 }
1040ff22170Sdan} {}
1050ff22170Sdan
1060ff22170Sdando_compare_test 1.6.1 "rowid = 700"
1070ff22170Sdando_compare_test 1.6.2 "rowid = -1"
1080ff22170Sdando_compare_test 1.6.3 "rowid = 0"
1090ff22170Sdando_compare_test 1.6.4 "rowid = 55"
1100ff22170Sdando_compare_test 1.6.5 "rowid BETWEEN 20 AND 100"
1110ff22170Sdando_compare_test 1.6.6 "rowid > 350"
1120ff22170Sdando_compare_test 1.6.7 "rowid >= 350"
1130ff22170Sdando_compare_test 1.6.8 "rowid >= 200"
1140ff22170Sdando_compare_test 1.6.9 "1"
1150ff22170Sdando_compare_test 1.6.10 "rowid >= 5"
11663331b1aSdan
117a64d1fbcSdando_test 1.x {
118a64d1fbcSdan  set sqlite_open_file_count
119a64d1fbcSdan} [expr $nFile+9]
120a64d1fbcSdan
121a64d1fbcSdando_test 1.y { db close } {}
12263331b1aSdan
1230ff22170Sdan# Delete all the database files created above.
1240ff22170Sdan#
1250ff22170Sdanfor {set i 0} {$i < 40} {incr i} { forcedelete "test.db$i" }
1260ff22170Sdan
1270ff22170Sdan#-------------------------------------------------------------------------
1280ff22170Sdan# Test some error conditions:
1290ff22170Sdan#
1300ff22170Sdan#   2.1: Database file does not exist.
1310ff22170Sdan#   2.2: Table does not exist.
1320ff22170Sdan#   2.3: Table schema does not match.
133*226744d1Sdan#   2.4: Syntax error in SELECT statement.
1340ff22170Sdan#
1350ff22170Sdanreset_db
1360ff22170Sdanload_static_extension db unionvtab
1370ff22170Sdando_test 2.0.1 {
1380ff22170Sdan  db eval {
1390ff22170Sdan    CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
1400ff22170Sdan    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400)
1410ff22170Sdan      INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
1420ff22170Sdan    CREATE TABLE dir(f, t, imin, imax);
1430ff22170Sdan  }
1440ff22170Sdan
1450ff22170Sdan  for {set i 0} {$i < 40} {incr i} {
1460ff22170Sdan    set iMin [expr $i*10 + 1]
1470ff22170Sdan    set iMax [expr $iMin+9]
1480ff22170Sdan
1490ff22170Sdan    forcedelete "test.db$i"
1500ff22170Sdan    db eval [subst {
1510ff22170Sdan      ATTACH 'test.db$i' AS aux;
1520ff22170Sdan      CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
1530ff22170Sdan      INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
1540ff22170Sdan      DETACH aux;
1550ff22170Sdan      INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
1560ff22170Sdan    }]
1570ff22170Sdan  }
1580ff22170Sdan  execsql {
1590ff22170Sdan    CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
1600ff22170Sdan  }
1610ff22170Sdan} {}
1620ff22170Sdan
1630ff22170Sdando_test 2.0.2 {
1640ff22170Sdan  forcedelete test.db5
1650ff22170Sdan
1660ff22170Sdan  sqlite3 db2 test.db15
1670ff22170Sdan  db2 eval { DROP TABLE t15 }
1680ff22170Sdan  db2 close
1690ff22170Sdan
1700ff22170Sdan  sqlite3 db2 test.db25
1710ff22170Sdan  db2 eval {
1720ff22170Sdan    DROP TABLE t25;
1730ff22170Sdan    CREATE TABLE t25(x, y, z PRIMARY KEY);
1740ff22170Sdan  }
1750ff22170Sdan  db2 close
1760ff22170Sdan} {}
1770ff22170Sdan
1780ff22170Sdando_catchsql_test 2.1 {
1790ff22170Sdan  SELECT * FROM s1 WHERE rowid BETWEEN 1 AND 100;
1800ff22170Sdan} {1 {unable to open database file}}
1810ff22170Sdando_catchsql_test 2.2 {
1820ff22170Sdan  SELECT * FROM s1 WHERE rowid BETWEEN 101 AND 200;
1830ff22170Sdan} {1 {no such rowid table: t15}}
1840ff22170Sdando_catchsql_test 2.3 {
1850ff22170Sdan  SELECT * FROM s1 WHERE rowid BETWEEN 201 AND 300;
1860ff22170Sdan} {1 {source table schema mismatch}}
18763331b1aSdan
188*226744d1Sdando_catchsql_test 2.4 {
189*226744d1Sdan  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir');
190*226744d1Sdan} {1 {sql error: near "FROMdir": syntax error}}
191*226744d1Sdando_catchsql_test 2.5 {
192*226744d1Sdan  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir', 'fetchdb');
193*226744d1Sdan} {1 {sql error: near "FROMdir": syntax error}}
194*226744d1Sdan
195*226744d1Sdanfor {set i 0} {$i < 40} {incr i} {
196*226744d1Sdan  forcedelete "test.db$i"
197*226744d1Sdan}
198*226744d1Sdan
199*226744d1Sdan#-------------------------------------------------------------------------
200*226744d1Sdan# Test the outcome of the fetch function throwing an exception.
201*226744d1Sdan#
202*226744d1Sdanproc fetch_db {file} {
203*226744d1Sdan  error "fetch_db error!"
204*226744d1Sdan}
205*226744d1Sdan
206*226744d1Sdandb func fetch_db fetch_db
207*226744d1Sdan
208*226744d1Sdando_catchsql_test 3.1 {
209*226744d1Sdan  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
210*226744d1Sdan    'VALUES
211*226744d1Sdan        ("test.db1", "t1", 1, 10),
212*226744d1Sdan        ("test.db2", "t1", 11, 20)
213*226744d1Sdan    ', 'fetch_db_no_such_function'
214*226744d1Sdan  );
215*226744d1Sdan} {1 {no such function: fetch_db_no_such_function}}
216*226744d1Sdan
217*226744d1Sdando_catchsql_test 3.2 {
218*226744d1Sdan  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
219*226744d1Sdan    'VALUES
220*226744d1Sdan        ("test.db1", "t1", 1, 10),
221*226744d1Sdan        ("test.db2", "t1", 11, 20)
222*226744d1Sdan    ', 'fetch_db'
223*226744d1Sdan  );
224*226744d1Sdan} {1 {fetch_db error!}}
225*226744d1Sdan
226*226744d1Sdando_execsql_test 3.3.1 {
227*226744d1Sdan  ATTACH 'test.db1' AS aux;
228*226744d1Sdan  CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);
229*226744d1Sdan  INSERT INTO aux.t1 VALUES(1, NULL);
230*226744d1Sdan  INSERT INTO aux.t1 VALUES(2, NULL);
231*226744d1Sdan  INSERT INTO aux.t1 VALUES(9, NULL);
232*226744d1Sdan  DETACH aux;
233*226744d1Sdan  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
234*226744d1Sdan    'VALUES
235*226744d1Sdan        ("test.db1", "t1", 1, 10),
236*226744d1Sdan        ("test.db2", "t1", 11, 20)
237*226744d1Sdan    ', 'fetch_db'
238*226744d1Sdan  );
239*226744d1Sdan} {}
240*226744d1Sdan
241*226744d1Sdando_catchsql_test 3.3 { SELECT * FROM xyz } {1 {fetch_db error!}}
242*226744d1Sdan
243*226744d1Sdan
244*226744d1Sdan
24563331b1aSdanfinish_test
24663331b1aSdan
247