summaryrefslogtreecommitdiffstats
path: root/ext/fts5/test/fts5content.test
blob: 986d7f331137914d0016d217eb7077bf7fe61273 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# 2014 Dec 20
#
# 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 contains tests for the content= and content_rowid= options.
#

source [file join [file dirname [info script]] fts5_common.tcl]
set testprefix fts5content

# If SQLITE_ENABLE_FTS5 is defined, omit this file.
ifcapable !fts5 {
  finish_test
  return
}

#-------------------------------------------------------------------------
# Contentless tables
#
do_execsql_test 1.1 {
  CREATE VIRTUAL TABLE f1 USING fts5(a, b, content='');
  INSERT INTO f1(rowid, a, b) VALUES(1, 'one',   'o n e');
  INSERT INTO f1(rowid, a, b) VALUES(2, 'two',   't w o');
  INSERT INTO f1(rowid, a, b) VALUES(3, 'three', 't h r e e');
}

do_execsql_test 1.2 {
  SELECT rowid FROM f1 WHERE f1 MATCH 'o';
} {1 2}

do_execsql_test 1.3 {
  INSERT INTO f1(a, b) VALUES('four',   'f o u r');
  SELECT rowid FROM f1 WHERE f1 MATCH 'o';
} {1 2 4}

do_execsql_test 1.4 {
  SELECT rowid, a, b FROM f1 WHERE f1 MATCH 'o';
} {1 {} {} 2 {} {} 4 {} {}}

do_execsql_test 1.5 {
  SELECT rowid, highlight(f1, 0, '[', ']') FROM f1 WHERE f1 MATCH 'o';
} {1 {} 2 {} 4 {}}

do_execsql_test 1.6 {
  SELECT rowid, highlight(f1, 0, '[', ']') IS NULL FROM f1 WHERE f1 MATCH 'o';
} {1 1 2 1 4 1}

do_execsql_test 1.7 {
  SELECT rowid, snippet(f1, -1, '[', ']', '...', 5) IS NULL 
  FROM f1 WHERE f1 MATCH 'o';
} {1 1 2 1 4 1}

do_execsql_test 1.8 {
  SELECT rowid, snippet(f1, 1, '[', ']', '...', 5) IS NULL 
  FROM f1 WHERE f1 MATCH 'o';
} {1 1 2 1 4 1}

do_execsql_test 1.9 {
  SELECT rowid FROM f1;
} {1 2 3 4}

do_execsql_test 1.10 {
  SELECT * FROM f1;
} {{} {}  {} {}  {} {}  {} {}}

do_execsql_test 1.11 {
  SELECT rowid, a, b FROM f1 ORDER BY rowid ASC;
} {1 {} {}  2 {} {}  3 {} {}  4 {} {}}

do_execsql_test 1.12 {
  SELECT a IS NULL FROM f1;
} {1 1 1 1}

do_catchsql_test 1.13 {
  DELETE FROM f1 WHERE rowid = 2;
} {1 {cannot DELETE from contentless fts5 table: f1}}

do_catchsql_test 1.14 {
  UPDATE f1 SET a = 'a b c' WHERE rowid = 2;
} {1 {cannot UPDATE contentless fts5 table: f1}}

do_execsql_test 1.15 {
  INSERT INTO f1(f1, rowid, a, b) VALUES('delete', 2, 'two', 't w o');
} {}

do_execsql_test 1.16 {
  SELECT rowid FROM f1 WHERE f1 MATCH 'o';
} {1 4}

do_execsql_test 1.17 {
  SELECT rowid FROM f1;
} {1 3 4}

#-------------------------------------------------------------------------
# External content tables
#
reset_db
do_execsql_test 2.1 {
  -- Create a table. And an external content fts5 table to index it.
  CREATE TABLE tbl(a INTEGER PRIMARY KEY, b, c);
  CREATE VIRTUAL TABLE fts_idx USING fts5(b, c, content='tbl', content_rowid='a');

  -- Triggers to keep the FTS index up to date.
  CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
    INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
  END;
  CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
    INSERT INTO fts_idx(fts_idx, rowid, b, c) 
        VALUES('delete', old.a, old.b, old.c);
  END;
  CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
    INSERT INTO fts_idx(fts_idx, rowid, b, c) 
        VALUES('delete', old.a, old.b, old.c);
    INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
  END;
}

do_execsql_test 2.2 {
  INSERT INTO tbl VALUES(1, 'one', 'o n e');
  INSERT INTO tbl VALUES(NULL, 'two', 't w o');
  INSERT INTO tbl VALUES(3, 'three', 't h r e e');
}

do_execsql_test 2.3 {
  INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
}

do_execsql_test 2.4 {
  DELETE FROM tbl WHERE rowid=2;
  INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
}

do_execsql_test 2.5 {
  UPDATE tbl SET c = c || ' x y z';
  INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
}

do_execsql_test 2.6 {
  SELECT * FROM fts_idx WHERE fts_idx MATCH 't AND x';
} {three {t h r e e x y z}}

do_execsql_test 2.7 {
  SELECT highlight(fts_idx, 1, '[', ']') FROM fts_idx 
  WHERE fts_idx MATCH 't AND x';
} {{[t] h r e e [x] y z}}

#-------------------------------------------------------------------------
# Quick tests of the 'delete-all' command.
#
do_execsql_test 3.1 {
  CREATE VIRTUAL TABLE t3 USING fts5(x, content='');
  INSERT INTO t3 VALUES('a b c');
  INSERT INTO t3 VALUES('d e f');
}

do_execsql_test 3.2 {
  SELECT count(*) FROM t3_docsize;
  SELECT count(*) FROM t3_data;
} {2 4}

do_execsql_test 3.3 {
  INSERT INTO t3(t3) VALUES('delete-all');
  SELECT count(*) FROM t3_docsize;
  SELECT count(*) FROM t3_data;
} {0 2}

do_execsql_test 3.4 {
  INSERT INTO t3 VALUES('a b c');
  INSERT INTO t3 VALUES('d e f');
  SELECT rowid FROM t3 WHERE t3 MATCH 'e';
} {2}

do_execsql_test 3.5 {
  SELECT rowid FROM t3 WHERE t3 MATCH 'c';
} {1}

do_execsql_test 3.6 {
  SELECT count(*) FROM t3_docsize;
  SELECT count(*) FROM t3_data;
} {2 4}

do_execsql_test 3.7 {
  CREATE VIRTUAL TABLE t4 USING fts5(x);
} {}
do_catchsql_test 3.8 {
  INSERT INTO t4(t4) VALUES('delete-all');
} {1 {'delete-all' may only be used with a contentless or external content fts5 table}}

#-------------------------------------------------------------------------
# Test an external content table with a more interesting schema.
#
do_execsql_test 4.1 {
  CREATE TABLE x2(a, "key col" PRIMARY KEY, b, c) WITHOUT ROWID;
  INSERT INTO x2 VALUES('a b',   1, 'c d' , 'e f');
  INSERT INTO x2 VALUES('x y', -40, 'z z' , 'y x');

  CREATE VIRTUAL TABLE t2 USING fts5(a, c, content=x2, content_rowid='key col');
  INSERT INTO t2(t2) VALUES('rebuild');
}

do_execsql_test 4.2 { SELECT rowid FROM t2 } {-40 1}
do_execsql_test 4.3 { SELECT rowid FROM t2 WHERE t2 MATCH 'c'} {}
do_execsql_test 4.4 { SELECT rowid FROM t2 WHERE t2 MATCH 'a'} {1}
do_execsql_test 4.5 { SELECT rowid FROM t2 WHERE t2 MATCH 'x'} {-40}

do_execsql_test 4.6 { INSERT INTO t2(t2) VALUES('integrity-check') } {}

do_execsql_test 4.7 { 
  DELETE FROM x2 WHERE "key col" = 1;
  INSERT INTO t2(t2, rowid, a, c) VALUES('delete', 1, 'a b', 'e f');
  INSERT INTO t2(t2) VALUES('integrity-check');
}

do_execsql_test 4.8 { SELECT rowid FROM t2 WHERE t2 MATCH 'b'} {}
do_execsql_test 4.9 { SELECT rowid FROM t2 WHERE t2 MATCH 'y'} {-40}

#-------------------------------------------------------------------------
# Test that if the 'rowid' field of a 'delete' is not an integer, no
# changes are made to the FTS index.
#
do_execsql_test 5.0 {
  CREATE VIRTUAL TABLE t5 USING fts5(a, b, content=);
  INSERT INTO t5(rowid, a, b) VALUES(-1, 'one',   'two');
  INSERT INTO t5(rowid, a, b) VALUES( 0, 'three', 'four');
  INSERT INTO t5(rowid, a, b) VALUES( 1, 'five',  'six');
}

set ::checksum [execsql {SELECT md5sum(id, block) FROM t5_data}]

do_execsql_test 5.1 {
  INSERT INTO t5(t5, rowid, a, b) VALUES('delete', NULL, 'three', 'four');
  SELECT md5sum(id, block) FROM t5_data;
} $::checksum


#-------------------------------------------------------------------------
# Check that a contentless table can be dropped.
#
reset_db
do_execsql_test 6.1 {
  CREATE VIRTUAL TABLE xx USING fts5(x, y, content="");
  SELECT name FROM sqlite_master;
} {xx xx_data xx_idx xx_docsize xx_config}
do_execsql_test 6.2 {
  DROP TABLE xx;
  SELECT name FROM sqlite_master;
} {}

#---------------------------------------------------------------------------
# Check that an fts5 table cannot be its own content table.
#
reset_db
do_execsql_test 7.1.1 {
  CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 );
  INSERT INTO t1( a ) VALUES('abc');
}
do_catchsql_test 7.1.2 { 
  SELECT * FROM t1; 
} {1 {recursively defined fts5 content table}}
do_catchsql_test 7.1.3 { 
  SELECT * FROM t1('abc'); 
} {1 {recursively defined fts5 content table}}
do_catchsql_test 7.1.4 { 
  SELECT count(*) FROM t1;
} {1 {recursively defined fts5 content table}}
do_catchsql_test 7.1.5 { 
  SELECT * FROM t1('abc') ORDER BY rank;
} {1 {recursively defined fts5 content table}}

reset_db
do_execsql_test 7.2.1 {
  CREATE VIRTUAL TABLE t1 USING fts5(a, c=t2 );
  CREATE VIRTUAL TABLE t2 USING fts5(a, c=t1 );
  INSERT INTO t1( a ) VALUES('abc');
}
do_catchsql_test 7.2.2 { 
  SELECT * FROM t1; 
} {1 {recursively defined fts5 content table}}
do_catchsql_test 7.2.3 { 
  SELECT * FROM t1('abc'); 
} {1 {recursively defined fts5 content table}}
do_catchsql_test 7.2.4 { 
  SELECT count(*) FROM t1;
} {1 {recursively defined fts5 content table}}
do_catchsql_test 7.2.5 { 
  SELECT * FROM t1('abc') ORDER BY rank;
} {1 {recursively defined fts5 content table}}

#---------------------------------------------------------------------------
# Check that if the content table is a view, and that view contains an
# error, a reasonable error message is returned if the user tries to
# read from the view via the fts5 table.
#
reset_db
do_execsql_test 8.1 {
  CREATE VIEW a1 AS 
    SELECT 1 AS r, text_value(1) AS t
      UNION ALL
    SELECT 2 AS r, text_value(2) AS t;

  CREATE VIRTUAL TABLE t1 USING fts5(t, content='a1', content_rowid='r');
}

foreach {tn sql} {
  1 "SELECT * FROM t1"
  2 "INSERT INTO t1(t1) VALUES('rebuild')"
  3 "SELECT * FROM t1 WHERE rowid=1"
} {
  do_catchsql_test 8.2.$tn $sql {1 {no such function: text_value}}
}

proc text_value {i} {
  if {$i==1} { return "one" }
  if {$i==2} { return "two" }
  return "many"
}
db func text_value text_value

do_execsql_test 8.3.1 { SELECT * FROM t1 } {one two}
do_execsql_test 8.3.2 { INSERT INTO t1(t1) VALUES('rebuild') }
do_execsql_test 8.3.3 { SELECT * FROM t1 WHERE rowid=1 } {one}
do_execsql_test 8.3.4 { SELECT rowid FROM t1('two') } {2}

finish_test