fts5vocab2.test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # 2017 August 10
  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. #
  12. # The tests in this file focus on testing the fts5vocab module.
  13. #
  14. source [file join [file dirname [info script]] fts5_common.tcl]
  15. set testprefix fts5vocab2
  16. # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
  17. ifcapable !fts5 {
  18. finish_test
  19. return
  20. }
  21. do_execsql_test 1.0 {
  22. CREATE VIRTUAL TABLE t1 USING fts5(a, b);
  23. CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  24. INSERT INTO t1 VALUES('one two', 'two three');
  25. INSERT INTO t1 VALUES('three four', 'four five five five');
  26. }
  27. do_execsql_test 1.1 {
  28. SELECT * FROM v1;
  29. } {
  30. five 2 b 1
  31. five 2 b 2
  32. five 2 b 3
  33. four 2 a 1
  34. four 2 b 0
  35. one 1 a 0
  36. three 1 b 1
  37. three 2 a 0
  38. two 1 a 1
  39. two 1 b 0
  40. }
  41. do_execsql_test 1.2 {
  42. SELECT * FROM v1 WHERE term='three';
  43. } {
  44. three 1 b 1
  45. three 2 a 0
  46. }
  47. do_execsql_test 1.3 {
  48. BEGIN;
  49. DELETE FROM t1 WHERE rowid=2;
  50. SELECT * FROM v1;
  51. ROLLBACK;
  52. } {
  53. one 1 a 0
  54. three 1 b 1
  55. two 1 a 1
  56. two 1 b 0
  57. }
  58. do_execsql_test 1.4 {
  59. BEGIN;
  60. DELETE FROM t1 WHERE rowid=1;
  61. SELECT * FROM v1;
  62. ROLLBACK;
  63. } {
  64. five 2 b 1
  65. five 2 b 2
  66. five 2 b 3
  67. four 2 a 1
  68. four 2 b 0
  69. three 2 a 0
  70. }
  71. do_execsql_test 1.5 {
  72. DELETE FROM t1;
  73. SELECT * FROM v1;
  74. } {}
  75. #-------------------------------------------------------------------------
  76. #
  77. do_execsql_test 2.0 {
  78. DROP TABLE IF EXISTS t1;
  79. DROP TABLE IF EXISTS v1;
  80. CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=column);
  81. CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  82. INSERT INTO t1 VALUES('one two', 'two three');
  83. INSERT INTO t1 VALUES('three four', 'four five five five');
  84. }
  85. do_execsql_test 2.1 {
  86. SELECT * FROM v1;
  87. } {
  88. five 2 b {}
  89. four 2 a {}
  90. four 2 b {}
  91. one 1 a {}
  92. three 1 b {}
  93. three 2 a {}
  94. two 1 a {}
  95. two 1 b {}
  96. }
  97. do_execsql_test 2.2 {
  98. SELECT * FROM v1 WHERE term='three';
  99. } {
  100. three 1 b {}
  101. three 2 a {}
  102. }
  103. do_execsql_test 2.3 {
  104. BEGIN;
  105. DELETE FROM t1 WHERE rowid=2;
  106. SELECT * FROM v1;
  107. ROLLBACK;
  108. } {
  109. one 1 a {}
  110. three 1 b {}
  111. two 1 a {}
  112. two 1 b {}
  113. }
  114. do_execsql_test 2.4 {
  115. BEGIN;
  116. DELETE FROM t1 WHERE rowid=1;
  117. SELECT * FROM v1;
  118. ROLLBACK;
  119. } {
  120. five 2 b {}
  121. four 2 a {}
  122. four 2 b {}
  123. three 2 a {}
  124. }
  125. do_execsql_test 2.5 {
  126. DELETE FROM t1;
  127. SELECT * FROM v1;
  128. } {}
  129. #-------------------------------------------------------------------------
  130. #
  131. do_execsql_test 3.0 {
  132. DROP TABLE IF EXISTS t1;
  133. DROP TABLE IF EXISTS v1;
  134. CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=none);
  135. CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  136. INSERT INTO t1 VALUES('one two', 'two three');
  137. INSERT INTO t1 VALUES('three four', 'four five five five');
  138. }
  139. do_execsql_test 3.1 {
  140. SELECT * FROM v1;
  141. } {
  142. five 2 {} {}
  143. four 2 {} {}
  144. one 1 {} {}
  145. three 1 {} {}
  146. three 2 {} {}
  147. two 1 {} {}
  148. }
  149. do_execsql_test 3.2 {
  150. SELECT * FROM v1 WHERE term='three';
  151. } {
  152. three 1 {} {}
  153. three 2 {} {}
  154. }
  155. do_execsql_test 3.3 {
  156. BEGIN;
  157. DELETE FROM t1 WHERE rowid=2;
  158. SELECT * FROM v1;
  159. ROLLBACK;
  160. } {
  161. one 1 {} {}
  162. three 1 {} {}
  163. two 1 {} {}
  164. }
  165. do_execsql_test 3.4 {
  166. BEGIN;
  167. DELETE FROM t1 WHERE rowid=1;
  168. SELECT * FROM v1;
  169. ROLLBACK;
  170. } {
  171. five 2 {} {}
  172. four 2 {} {}
  173. three 2 {} {}
  174. }
  175. do_execsql_test 3.5 {
  176. DELETE FROM t1;
  177. SELECT * FROM v1;
  178. } {}
  179. #-------------------------------------------------------------------------
  180. #
  181. reset_db
  182. do_execsql_test 4.0 {
  183. CREATE VIRTUAL TABLE v1 USING fts5vocab(nosuchtable, col);
  184. }
  185. do_catchsql_test 4.1 {
  186. SELECT * FROM v1 WHERE term=='nosuchterm';
  187. } {1 {no such fts5 table: main.nosuchtable}}
  188. do_execsql_test 4.2.1 {
  189. CREATE TABLE nosuchtable(nosuchtable, y, z);
  190. }
  191. do_catchsql_test 4.2.2 {
  192. SELECT * FROM v1 WHERE term=='nosuchterm';
  193. } {1 {no such fts5 table: main.nosuchtable}}
  194. ifcapable fts3 {
  195. do_execsql_test 4.3.1 {
  196. DROP TABLE nosuchtable;
  197. CREATE VIRTUAL TABLE nosuchtable USING fts3(a, b);
  198. } {}
  199. do_catchsql_test 4.3.2 {
  200. SELECT * FROM v1 WHERE term=='nosuchterm';
  201. } {1 {no such fts5 table: main.nosuchtable}}
  202. do_catchsql_test 4.3.3 {
  203. INSERT INTO nosuchtable VALUES('id', '*id');
  204. SELECT * FROM v1 WHERE term=='nosuchterm';
  205. } {1 {no such fts5 table: main.nosuchtable}}
  206. }
  207. #-------------------------------------------------------------------------
  208. # Check that the fts5 table cannot be written while there are vocab
  209. # cursors open.
  210. reset_db
  211. do_execsql_test 5.0 {
  212. CREATE VIRTUAL TABLE t1 USING fts5(a);
  213. CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  214. INSERT INTO t1 VALUES('one'), ('two'), ('three'), ('four');
  215. }
  216. do_test 5.1 {
  217. list [catch {
  218. db eval { SELECT * FROM v1 } {
  219. db eval {INSERT INTO t1 VALUES('five')}
  220. }
  221. } msg] $msg
  222. } {1 {query aborted}}
  223. do_execsql_test 5.2 {
  224. SELECT * FROM t1
  225. } {one two three four five}
  226. #-------------------------------------------------------------------------
  227. # Check that the fts5 table cannot be written while there are vocab
  228. # cursors open.
  229. reset_db
  230. do_execsql_test 5.0 {
  231. CREATE VIRTUAL TABLE t1 USING fts5(a);
  232. CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  233. WITH s(i) AS (
  234. VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10000
  235. )
  236. INSERT INTO t1 SELECT
  237. 'State Emergency Service (SES), Rural Fire Service (RFS) and Volunteers'
  238. FROM s;
  239. }
  240. do_catchsql_test 5.1 {
  241. INSERT INTO t1 SELECT rowid FROM v1
  242. } {1 {query aborted}}
  243. do_catchsql_test 5.2 {
  244. DELETE FROM t1 WHERE rowid>100;
  245. INSERT INTO t1 SELECT randomblob(3000) FROM v1
  246. } {1 {query aborted}}
  247. #-------------------------------------------------------------------------
  248. reset_db
  249. sqlite3_fts5_may_be_corrupt 1
  250. do_execsql_test 6.0 {
  251. BEGIN TRANSACTION;
  252. CREATE VIRTUAL TABLE t1 USING fts5(a,b unindexed,c,tokenize="porter ascii",tokendata=1);
  253. REPLACE INTO t1_data VALUES(1,X'03090009');
  254. REPLACE INTO t1_data VALUES(10,X'000000000103030003010101020101030101');
  255. REPLACE INTO t1_data VALUES(137438953473,X'0000002e023061010202010162010203010163010204010167010601020201016801060102030101690106010204040606060808');
  256. REPLACE INTO t1_data VALUES(274877906945,X'0000001f013067020802010202010168020803010203010169020804010204040909');
  257. REPLACE INTO t1_data VALUES(412316860417,X'0000002e023061030202010162030203010163030204010167030601020201016803060102030101690306010204040606060808');
  258. COMMIT;
  259. }
  260. do_execsql_test 6.1 {
  261. CREATE VIRTUAL TABLE t3 USING fts5vocab('t1', 'row');
  262. }
  263. do_catchsql_test 6.2 {
  264. SELECT * FROM t3;
  265. } {1 {database disk image is malformed}}
  266. sqlite3_fts5_may_be_corrupt 0
  267. finish_test