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.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix fts3conf
19 # If SQLITE_ENABLE_FTS3 is defined, omit this file.
26 proc fts3_integrity {tn db tbl} {
28 if {[sqlite3_get_autocommit $db]==0} {
29 error "fts3_integrity does not work with an open transaction"
32 set sql [db one {SELECT sql FROM sqlite_master WHERE name = $tbl}]
33 regexp -nocase {[^(]* using (.*)} $sql -> tail
35 $db eval "PRAGMA table_info($tbl)" {
38 set cols [join [concat docid $cols] ,]
41 CREATE VIRTUAL TABLE fts3check USING fts4term($tbl);
42 CREATE VIRTUAL TABLE temp.fts3check2 USING $tail;
43 INSERT INTO temp.fts3check2($cols) SELECT docid, * FROM $tbl;
44 CREATE VIRTUAL TABLE temp.fts3check3 USING fts4term(fts3check2);
47 set m1 [$db one {SELECT md5sum(term, docid, col, pos) FROM fts3check}]
48 set m2 [$db one {SELECT md5sum(term, docid, col, pos) FROM fts3check3}]
52 DROP TABLE temp.fts3check2;
53 DROP TABLE temp.fts3check3;
56 uplevel [list do_test $tn [list set {} $m1] $m2]
59 do_execsql_test 1.0.1 {
60 CREATE VIRTUAL TABLE t1 USING fts3(x);
61 INSERT INTO t1(rowid, x) VALUES(1, 'a b c d');
62 INSERT INTO t1(rowid, x) VALUES(2, 'e f g h');
64 CREATE TABLE source(a, b);
65 INSERT INTO source VALUES(4, 'z');
66 INSERT INTO source VALUES(2, 'y');
70 set T1 "INTO t1(rowid, x) VALUES(1, 'x')"
71 set T2 "INTO t1(rowid, x) SELECT * FROM source"
73 set T3 "t1 SET docid = 2 WHERE docid = 1"
74 set T4 "t1 SET docid = CASE WHEN docid = 1 THEN 4 ELSE 3 END WHERE docid <=2"
76 foreach {tn sql uses constraint data} [subst {
77 1 "INSERT OR ROLLBACK $T1" 0 1 {{a b c d} {e f g h}}
78 2 "INSERT OR ABORT $T1" 0 1 {{a b c d} {e f g h} {i j k l}}
79 3 "INSERT OR FAIL $T1" 0 1 {{a b c d} {e f g h} {i j k l}}
80 4 "INSERT OR IGNORE $T1" 0 0 {{a b c d} {e f g h} {i j k l}}
81 5 "INSERT OR REPLACE $T1" 0 0 {x {e f g h} {i j k l}}
83 6 "INSERT OR ROLLBACK $T2" 1 1 {{a b c d} {e f g h}}
84 7 "INSERT OR ABORT $T2" 1 1 {{a b c d} {e f g h} {i j k l}}
85 8 "INSERT OR FAIL $T2" 1 1 {{a b c d} {e f g h} {i j k l} z}
86 9 "INSERT OR IGNORE $T2" 1 0 {{a b c d} {e f g h} {i j k l} z}
87 10 "INSERT OR REPLACE $T2" 1 0 {{a b c d} y {i j k l} z}
89 11 "UPDATE OR ROLLBACK $T3" 1 1 {{a b c d} {e f g h}}
90 12 "UPDATE OR ABORT $T3" 1 1 {{a b c d} {e f g h} {i j k l}}
91 13 "UPDATE OR FAIL $T3" 1 1 {{a b c d} {e f g h} {i j k l}}
92 14 "UPDATE OR IGNORE $T3" 1 0 {{a b c d} {e f g h} {i j k l}}
93 15 "UPDATE OR REPLACE $T3" 1 0 {{a b c d} {i j k l}}
95 16 "UPDATE OR ROLLBACK $T4" 1 1 {{a b c d} {e f g h}}
96 17 "UPDATE OR ABORT $T4" 1 1 {{a b c d} {e f g h} {i j k l}}
97 18 "UPDATE OR FAIL $T4" 1 1 {{e f g h} {i j k l} {a b c d}}
98 19 "UPDATE OR IGNORE $T4" 1 0 {{e f g h} {i j k l} {a b c d}}
99 20 "UPDATE OR REPLACE $T4" 1 0 {{e f g h} {a b c d}}
101 db_restore_and_reopen
104 INSERT INTO t1(rowid, x) VALUES(3, 'i j k l');
107 set R(1) {1 {constraint failed}}
108 do_catchsql_test 1.$tn.1 $sql $R($constraint)
109 do_catchsql_test 1.$tn.2 { SELECT * FROM t1 } [list 0 $data]
112 fts3_integrity 1.$tn.3 db t1
114 do_test 1.$tn.4 [list sql_uses_stmt db $sql] $uses
117 do_execsql_test 2.1.1 {
120 INSERT INTO t1 VALUES('a b c');
122 INSERT INTO t1 VALUES('x y z');
126 fts3_integrity 2.1.2 db t1
128 do_catchsql_test 2.2.1 {
131 INSERT INTO t1(docid, x) VALUES(0, 'a b c');
132 INSERT INTO t1(docid, x) VALUES(1, 'a b c');
133 REPLACE INTO t1(docid, x) VALUES('zero', 'd e f');
134 } {1 {datatype mismatch}}
135 do_execsql_test 2.2.2 { COMMIT }
136 do_execsql_test 2.2.3 { SELECT * FROM t1 } {{a b c} {a b c}}
137 fts3_integrity 2.2.4 db t1
139 do_execsql_test 3.1 {
140 CREATE VIRTUAL TABLE t3 USING fts4;
141 REPLACE INTO t3(docid, content) VALUES (1, 'one two');
142 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
143 } {X'0100000002000000'}
145 do_execsql_test 3.2 {
146 REPLACE INTO t3(docid, content) VALUES (2, 'one two three four');
147 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'four'
148 } {X'0200000003000000'}
150 do_execsql_test 3.3 {
151 REPLACE INTO t3(docid, content) VALUES (1, 'one two three four five six');
152 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
153 } {X'0200000005000000'}
155 do_execsql_test 3.4 {
156 UPDATE OR REPLACE t3 SET docid = 2 WHERE docid=1;
157 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
158 } {X'0100000006000000'}
160 do_execsql_test 3.5 {
161 UPDATE OR REPLACE t3 SET docid = 3 WHERE docid=2;
162 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
163 } {X'0100000006000000'}
165 do_execsql_test 3.6 {
166 REPLACE INTO t3(docid, content) VALUES (3, 'one two');
167 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
168 } {X'0100000002000000'}
170 do_execsql_test 3.7 {
171 REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four');
172 REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four five six');
173 SELECT docid FROM t3;
176 do_execsql_test 3.8 {
177 UPDATE OR REPLACE t3 SET docid = 5, content='three four' WHERE docid = 4;
178 SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
179 } {X'0200000002000000'}