xref: /sqlite-3.40.0/test/fts4check.test (revision 38d69855)
1# 2012 March 26
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#*************************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this script is testing the FTS 'integrity-check' function,
13# used to check if the current FTS index accurately reflects the content
14# of the table.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19source $testdir/fts3_common.tcl
20set ::testprefix fts4check
21
22# If SQLITE_ENABLE_FTS3 is defined, omit this file.
23ifcapable !fts3 {
24  finish_test
25  return
26}
27
28# Run the integrity-check on FTS table $tbl using database handle $db. If
29# the integrity-check passes, return "ok". Otherwise, throw an exception.
30#
31proc fts_integrity {db tbl} {
32  $db eval "INSERT INTO $tbl ($tbl) VALUES('integrity-check')"
33  return "ok"
34}
35
36#-------------------------------------------------------------------------
37# Test cases 1.*
38#
39#   1.0: Build a reasonably sized FTS table (5000 rows).
40#
41#   1.1: Run the integrity check code to check it passes.
42#
43#   1.2: Make a series of minor changes to the underlying FTS data structures
44#        (e.g. delete or insert a row from the %_content table). Check that
45#        this causes the integrity-check code to fail.
46#
47
48# Build an FTS table and check the integrity-check passes.
49#
50do_test 1.0 { fts3_build_db_1 5000 } {}
51do_test 1.1 { fts_integrity db t1 } {ok}
52
53# Mess around with the underlying tables. Check that this causes the
54# integrity-check test to fail.
55#
56foreach {tn disruption} {
57  1 {
58    INSERT INTO t1_content(docid, c0x, c1y) VALUES(NULL, 'a', 'b');
59  }
60  2 {
61    DELETE FROM t1_content WHERE docid = (SELECT max(docid) FROM t1_content);
62  }
63  3 {
64    DELETE FROM t1_segdir WHERE level=0 AND idx=(
65      SELECT max(idx) FROM t1_segdir WHERE level=0
66    );
67  }
68} {
69  do_execsql_test  1.2.1.$tn "BEGIN; $disruption"
70  do_catchsql_test 1.2.2.$tn {
71    INSERT INTO t1 (t1) VALUES('integrity-check')
72  } {1 {database disk image is malformed}}
73  do_execsql_test  1.2.3.$tn "ROLLBACK"
74}
75
76do_test 1.3 { fts_integrity db t1 } {ok}
77
78#-------------------------------------------------------------------------
79# Test cases 2.*
80#
81#   2.0: Build a reasonably sized FTS table (20000 rows) that includes
82#        prefix indexes.
83#
84#   2.1: Run the integrity check code to check it passes.
85#
86#   2.2: Make a series of minor changes to the underlying FTS data structures
87#        (e.g. delete or insert a row from the %_content table). Check that
88#        this causes the integrity-check code to fail.
89#
90
91do_test 2.0 { fts3_build_db_2 -extra {prefix="3,1"} 20000 } {}
92do_test 2.1 { fts_integrity db t2 } {ok}
93foreach {tn disruption} {
94  1 {
95    INSERT INTO t2_content VALUES(NULL, 'xyz')
96  }
97  3 {
98    DELETE FROM t2_segdir WHERE level=0 AND idx=(
99      SELECT max(idx) FROM t2_segdir WHERE level=1024
100    );
101  }
102} {
103  do_execsql_test  2.2.1.$tn "BEGIN; $disruption"
104  do_catchsql_test 2.2.2.$tn {
105    INSERT INTO t2 (t2) VALUES('integrity-check')
106  } {1 {database disk image is malformed}}
107  do_execsql_test  2.2.3.$tn "ROLLBACK"
108}
109
110
111#-------------------------------------------------------------------------
112# Test cases 3.*
113#
114#   3.0: Build a reasonably sized FTS table (5000 rows) that includes
115#        prefix indexes and uses the languageid= feature.
116#
117#   3.1: Run the integrity check code to check it passes.
118#
119#   3.2: Make a series of minor changes to the underlying FTS data structures
120#        (e.g. delete or insert a row from the %_content table). Check that
121#        this causes the integrity-check code to fail.
122#
123do_test 3.0 {
124  reset_db
125  fts3_build_db_1 5000
126  execsql {
127    CREATE VIRTUAL TABLE t3 USING fts4(x, y, prefix="2,3", languageid=langid);
128  }
129  foreach docid [execsql {SELECT docid FROM t1 ORDER BY 1 ASC}] {
130    execsql {
131      INSERT INTO t3(x, y, langid)
132      SELECT x, y, (docid%9)*4 FROM t1 WHERE docid=$docid;
133    }
134  }
135} {}
136do_test 3.1 { fts_integrity db t3 } {ok}
137
138foreach {tn disruption} {
139  1 {
140    INSERT INTO t3_content(c0x, c1y, langid) VALUES(NULL, 'a', 0);
141  }
142  2 {
143    UPDATE t3_content SET langid=langid+1 WHERE rowid = (
144      SELECT max(rowid) FROM t3_content
145    )
146  }
147} {
148  do_execsql_test  3.2.1.$tn "BEGIN; $disruption"
149  do_catchsql_test 3.2.2.$tn {
150    INSERT INTO t3 (t3) VALUES('integrity-check')
151  } {1 {database disk image is malformed}}
152  do_execsql_test  3.2.3.$tn "ROLLBACK"
153}
154
155#--------------------------------------------------------------------------
156# Test case 4.*
157#
158# Test that the integrity-check works if there are "notindexed" columns.
159#
160do_execsql_test 4.0 {
161  CREATE VIRTUAL TABLE t4 USING fts4(a, b, c, notindexed=b);
162  INSERT INTO t4 VALUES('text one', 'text two', 'text three');
163  INSERT INTO t4(t4) VALUES('integrity-check');
164}
165
166do_execsql_test 4.1 {
167  PRAGMA writable_schema = 1;
168  UPDATE sqlite_master
169    SET sql = 'CREATE VIRTUAL TABLE t4 USING fts4(a, b, c)'
170    WHERE name = 't4';
171}
172
173do_test 4.2 {
174  db close
175  sqlite3 db test.db
176  catchsql {
177    INSERT INTO t4(t4) VALUES('integrity-check');
178  }
179} {1 {database disk image is malformed}}
180reset_db
181
182#--------------------------------------------------------------------------
183# Test case 5.*
184#
185# Test that the integrity-check works if there is uncommitted data.
186#
187do_execsql_test 5.0 {
188  BEGIN;
189  CREATE VIRTUAL TABLE t5 USING fts4(a, prefix="1,2,3");
190  INSERT INTO t5 VALUES('And down by Kosiosko, where the reed-banks sweep');
191  INSERT INTO t5 VALUES('and sway, and the rolling plains are wide, the');
192  INSERT INTO t5 VALUES('man from snowy river is a household name today,');
193  INSERT INTO t5 VALUES('and the stockmen tell the story of his ride');
194}
195
196do_execsql_test 5.1 {
197  INSERT INTO t5(t5) VALUES('integrity-check');
198} {}
199
200do_catchsql_test 5.2 {
201  INSERT INTO t5_content VALUES(5, 'his hardy mountain pony');
202  INSERT INTO t5(t5) VALUES('integrity-check');
203} {1 {database disk image is malformed}}
204
205do_execsql_test 5.3 ROLLBACK
206
207do_execsql_test 5.4 {
208  CREATE VIRTUAL TABLE t5 USING fts4(a, prefix="1,2,3");
209  INSERT INTO t5(t5) VALUES('integrity-check');
210} {}
211
212finish_test
213