summaryrefslogtreecommitdiffstats
path: root/test/swarmvtab.test
blob: b4b94487acc778eb21397819829306775b7d7957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# 2017-07-15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is the "swarmvtab" extension
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix swarmvtab
do_not_use_codec

ifcapable !vtab {
  finish_test
  return
}

load_static_extension db unionvtab

set nFile $sqlite_open_file_count

do_execsql_test 1.0 {
  CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
  WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400) 
  INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;

  CREATE TABLE dir(f, t, imin, imax);
}

do_test 1.1 {
  for {set i 0} {$i < 40} {incr i} {
    set iMin [expr $i*10 + 1]
    set iMax [expr $iMin+9]

    forcedelete "test.db$i"
    execsql [subst {
      ATTACH 'test.db$i' AS aux;
      CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
      INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
      DETACH aux;
      INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
    }]
  }

  execsql {
    CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
  }
} {}

do_execsql_test 1.2 { 
  DROP TABLE s1; 
} {}

do_execsql_test 1.3 {
  CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
  SELECT count(*) FROM s1 WHERE rowid<50;
} {49}

proc do_compare_test {tn where} {
  set sql [subst {
    SELECT (SELECT group_concat(a || ',' || b, ',') FROM t0 WHERE $where) 
           IS 
           (SELECT group_concat(a || ',' || b, ',') FROM s1 WHERE $where)
  }]

  uplevel [list do_execsql_test $tn $sql 1]
}

do_compare_test 1.4.1 "rowid = 700"
do_compare_test 1.4.2 "rowid = -1"
do_compare_test 1.4.3 "rowid = 0"
do_compare_test 1.4.4 "rowid = 55"
do_compare_test 1.4.5 "rowid BETWEEN 20 AND 100"
do_compare_test 1.4.6 "rowid > 350"
do_compare_test 1.4.7 "rowid >= 350"
do_compare_test 1.4.8 "rowid >= 200"
do_compare_test 1.4.9 "1"

# Multiple simultaneous cursors.
#
do_execsql_test 1.5.1.(5-seconds-or-so) {
  SELECT count(*) FROM s1 a, s1 b WHERE b.rowid<=200;
} {80000}
do_execsql_test 1.5.2 {
  SELECT count(*) FROM s1 a, s1 b, s1 c 
  WHERE a.rowid=b.rowid AND b.rowid=c.rowid;
} {400}

# Empty source tables.
#
do_test 1.6.0 {
  for {set i 0} {$i < 20} {incr i} {
    sqlite3 db2 test.db$i
    db2 eval " DELETE FROM t$i "
    db2 close
  }
  db eval { DELETE FROM t0 WHERE rowid<=200 }
} {}

do_compare_test 1.6.1 "rowid = 700"
do_compare_test 1.6.2 "rowid = -1"
do_compare_test 1.6.3 "rowid = 0"
do_compare_test 1.6.4 "rowid = 55"
do_compare_test 1.6.5 "rowid BETWEEN 20 AND 100"
do_compare_test 1.6.6 "rowid > 350"
do_compare_test 1.6.7 "rowid >= 350"
do_compare_test 1.6.8 "rowid >= 200"
do_compare_test 1.6.9 "1"
do_compare_test 1.6.10 "rowid >= 5"

do_test 1.x {
  set sqlite_open_file_count
} [expr $nFile+9]

do_test 1.y { db close } {}

# Delete all the database files created above.
#
for {set i 0} {$i < 40} {incr i} { forcedelete "test.db$i" }

#-------------------------------------------------------------------------
# Test some error conditions:
#
#   2.1: Database file does not exist.
#   2.2: Table does not exist.
#   2.3: Table schema does not match.
#   2.4: Syntax error in SELECT statement.
#
reset_db
load_static_extension db unionvtab
do_test 2.0.1 {
  db eval {
    CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT);
    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400) 
      INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s;
    CREATE TABLE dir(f, t, imin, imax);
  }

  for {set i 0} {$i < 40} {incr i} {
    set iMin [expr $i*10 + 1]
    set iMax [expr $iMin+9]

    forcedelete "test.db$i"
    db eval [subst {
      ATTACH 'test.db$i' AS aux;
      CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT);
      INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax;
      DETACH aux;
      INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax);
    }]
  }
  execsql {
    CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir');
  }
} {}

do_test 2.0.2 {
  forcedelete test.db5

  sqlite3 db2 test.db15
  db2 eval { DROP TABLE t15 }
  db2 close

  sqlite3 db2 test.db25
  db2 eval { 
    DROP TABLE t25;
    CREATE TABLE t25(x, y, z PRIMARY KEY);
  }
  db2 close
} {}

do_catchsql_test 2.1 {
  SELECT * FROM s1 WHERE rowid BETWEEN 1 AND 100;
} {1 {unable to open database file}}
do_catchsql_test 2.2 {
  SELECT * FROM s1 WHERE rowid BETWEEN 101 AND 200;
} {1 {no such rowid table: t15}}
do_catchsql_test 2.3 {
  SELECT * FROM s1 WHERE rowid BETWEEN 201 AND 300;
} {1 {source table schema mismatch}}

do_catchsql_test 2.4 {
  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir');
} {1 {sql error: near "FROMdir": syntax error}}
do_catchsql_test 2.5 {
  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir', 'fetchdb');
} {1 {sql error: near "FROMdir": syntax error}}

for {set i 0} {$i < 40} {incr i} {
  forcedelete "test.db$i"
}

#-------------------------------------------------------------------------
# Test the outcome of the fetch function throwing an exception.
#
proc fetch_db {file} {
  error "fetch_db error!"
}

db func fetch_db fetch_db

do_catchsql_test 3.1 {
  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
    'VALUES
        (''test.db1'', ''t1'', 1, 10),
        (''test.db2'', ''t1'', 11, 20)
    ', 'fetch_db_no_such_function'
  );
} {1 {sql error: no such function: fetch_db_no_such_function}}

do_catchsql_test 3.2 {
  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
    'VALUES
        (''test.db1'', ''t1'', 1, 10),
        (''test.db2'', ''t1'', 11, 20)
    ', 'fetch_db'
  );
} {1 {fetch_db error!}}

do_execsql_test 3.3.1 {
  ATTACH 'test.db1' AS aux;
  CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);
  INSERT INTO aux.t1 VALUES(1, NULL);
  INSERT INTO aux.t1 VALUES(2, NULL);
  INSERT INTO aux.t1 VALUES(9, NULL);
  DETACH aux;
  CREATE VIRTUAL TABLE temp.xyz USING swarmvtab(
    'VALUES
        (''test.db1'', ''t1'', 1, 10),
        (''test.db2'', ''t1'', 11, 20)
    ', 'fetch_db'
  );
} {}

do_catchsql_test 3.3.2 { SELECT * FROM xyz } {1 {fetch_db error!}}



finish_test