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 script is testing the FTS3 module.
14 # $Id: fts3ao.test,v 1.1 2007/08/20 17:38:42 shess Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # If SQLITE_ENABLE_FTS3 is not defined, omit this file.
26 set ::testprefix fts3ao
28 #---------------------------------------------------------------------
29 # These tests, fts3ao-1.*, test that ticket #2429 is fixed.
32 CREATE VIRTUAL TABLE t1 USING fts3(a, b, c);
33 INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one four two');
37 SELECT rowid, snippet(t1) FROM t1 WHERE c MATCH 'four';
39 } {1 {one <b>four</b> two}}
42 SELECT rowid, snippet(t1) FROM t1 WHERE b MATCH 'four';
44 } {1 {one <b>four</b>}}
47 SELECT rowid, snippet(t1) FROM t1 WHERE a MATCH 'four';
49 } {1 {one three <b>four</b>}}
51 #---------------------------------------------------------------------
52 # Test that it is possible to rename an fts3 table.
55 execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
56 } {t1 t1_content t1_segments t1_segdir}
58 execsql { ALTER TABLE t1 RENAME to fts_t1; }
61 execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; }
62 } {1 {one three <b>four</b>}}
64 execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
65 } {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir}
67 # See what happens when renaming the fts3 table fails.
71 CREATE TABLE t1_segdir(a, b, c);
72 ALTER TABLE fts_t1 RENAME to t1;
74 } {1 {SQL logic error}}
76 execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; }
77 } {1 {one three <b>four</b>}}
79 execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
80 } {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir t1_segdir}
82 # See what happens when renaming the fts3 table fails inside a transaction.
87 INSERT INTO fts_t1(a, b, c) VALUES('one two three', 'one four', 'one two');
92 ALTER TABLE fts_t1 RENAME to t1;
94 } {1 {SQL logic error}}
96 execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; }
97 } {1 {one three <b>four</b>}}
99 execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
100 } {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir t1_segdir}
101 do_test fts3ao-2.12 {
103 execsql {SELECT a FROM fts_t1}
104 } {{one three four} {one two three}}
105 do_test fts3ao-2.12 {
106 execsql { SELECT a, b, c FROM fts_t1 WHERE c MATCH 'four'; }
107 } {{one three four} {one four} {one four two}}
109 #-------------------------------------------------------------------
110 # Close, delete and reopen the database. The following test should
111 # be run on an initially empty db.
114 forcedelete test.db test.db-journal
119 CREATE VIRTUAL TABLE t1 USING fts3(a, b, c);
120 INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one two');
121 SELECT a, b, c FROM t1 WHERE c MATCH 'two';
123 } {{one three four} {one four} {one two}}
125 # This test was crashing at one point.
129 SELECT a, b, c FROM t1 WHERE c MATCH 'two';
130 CREATE TABLE t3(a, b, c);
131 SELECT a, b, c FROM t1 WHERE c MATCH 'two';
133 } {{one three four} {one four} {one two} {one three four} {one four} {one two}}
135 #---------------------------------------------------------------------
136 # Test that it is possible to rename an fts3 table in an attached
139 forcedelete test2.db test2.db-journal
143 ATTACH 'test2.db' AS aux;
144 CREATE VIRTUAL TABLE aux.t1 USING fts3(a, b, c);
145 INSERT INTO aux.t1(a, b, c) VALUES(
146 'neung song sahm', 'neung see', 'neung see song'
152 execsql { SELECT a, b, c FROM aux.t1 WHERE a MATCH 'song'; }
153 } {{neung song sahm} {neung see} {neung see song}}
156 execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; }
157 } {{one three four} {one four} {one two}}
160 execsql { ALTER TABLE aux.t1 RENAME TO t2 }
164 execsql { SELECT a, b, c FROM t2 WHERE a MATCH 'song'; }
165 } {{neung song sahm} {neung see} {neung see song}}
168 execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; }
169 } {{one three four} {one four} {one two}}
171 #---------------------------------------------------------------------
172 # Test that it is possible to rename an fts3 table within a
177 CREATE VIRTUAL TABLE t4 USING fts3;
178 INSERT INTO t4 VALUES('the quick brown fox');
184 INSERT INTO t4 VALUES('jumped over the');
187 do_test fts3ao-4.3 { execsql { ALTER TABLE t4 RENAME TO t5; } } {}
188 do_test fts3ao-4.4 { execsql { INSERT INTO t5 VALUES('lazy dog'); } } {}
189 do_test fts3ao-4.5 { execsql COMMIT } {}
191 execsql { SELECT * FROM t5 }
192 } {{the quick brown fox} {jumped over the} {lazy dog}}
196 INSERT INTO t5 VALUES('Down came a jumbuck to drink at that billabong');
197 ALTER TABLE t5 RENAME TO t6;
198 INSERT INTO t6 VALUES('Down came the troopers, one, two, three');
202 } {{the quick brown fox} {jumped over the} {lazy dog}}
203 do_execsql_test fts3ao-4.8 {
204 SELECT snippet(t5, '[', ']') FROM t5 WHERE t5 MATCH 'the'
205 } {{[the] quick brown fox} {jumped over [the]}}
207 # Test that it is possible to rename an FTS4 table. Renaming an FTS4 table
208 # involves renaming the extra %_docsize and %_stat tables.
210 do_execsql_test 5.1 {
211 CREATE VIRTUAL TABLE t7 USING FTS4;
212 INSERT INTO t7 VALUES('coined by a German clinician');
213 SELECT count(*) FROM sqlite_master WHERE name LIKE 't7%';
214 SELECT count(*) FROM sqlite_master WHERE name LIKE 't8%';
216 do_execsql_test 5.2 {
217 ALTER TABLE t7 RENAME TO t8;
218 SELECT count(*) FROM sqlite_master WHERE name LIKE 't7%';
219 SELECT count(*) FROM sqlite_master WHERE name LIKE 't8%';
222 # At one point this was causing a memory leak.
226 2 { INSERT INTO ft(ft) VALUES('merge=2,2'); }
229 do_execsql_test 6.$tn.1 "
231 CREATE VIRTUAL TABLE ft USING fts3;
232 INSERT INTO ft VALUES('hello world');
238 do_execsql_test 6.$tn.2 { SELECT * FROM t1 } {}
242 db2 eval { DROP TABLE t1 }
244 set stmt [sqlite3_prepare db { SELECT * FROM ft } -1 dummy]
245 sqlite3_finalize $stmt