fts2o.test 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # 2007 June 20
  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 FTS2 module.
  13. #
  14. # $Id: fts2o.test,v 1.4 2007/07/02 10:16:50 danielk1977 Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # If SQLITE_ENABLE_FTS2 is not defined, omit this file.
  19. ifcapable !fts2 {
  20. finish_test
  21. return
  22. }
  23. #---------------------------------------------------------------------
  24. # These tests, fts2o-1.*, test that ticket #2429 is fixed.
  25. #
  26. db eval {
  27. CREATE VIRTUAL TABLE t1 USING fts2(a, b, c);
  28. INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one four two');
  29. }
  30. do_test fts2o-1.1 {
  31. execsql {
  32. SELECT rowid, snippet(t1) FROM t1 WHERE c MATCH 'four';
  33. }
  34. } {1 {one <b>four</b> two}}
  35. do_test fts2o-1.2 {
  36. execsql {
  37. SELECT rowid, snippet(t1) FROM t1 WHERE b MATCH 'four';
  38. }
  39. } {1 {one <b>four</b>}}
  40. do_test fts2o-1.3 {
  41. execsql {
  42. SELECT rowid, snippet(t1) FROM t1 WHERE a MATCH 'four';
  43. }
  44. } {1 {one three <b>four</b>}}
  45. #---------------------------------------------------------------------
  46. # Test that it is possible to rename an fts2 table.
  47. #
  48. do_test fts2o-2.1 {
  49. execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
  50. } {t1 t1_content t1_segments t1_segdir}
  51. do_test fts2o-2.2 {
  52. execsql { ALTER TABLE t1 RENAME to fts_t1; }
  53. } {}
  54. do_test fts2o-2.3 {
  55. execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; }
  56. } {1 {one three <b>four</b>}}
  57. do_test fts2o-2.4 {
  58. execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
  59. } {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir}
  60. # See what happens when renaming the fts2 table fails.
  61. #
  62. do_test fts2o-2.5 {
  63. catchsql {
  64. CREATE TABLE t1_segdir(a, b, c);
  65. ALTER TABLE fts_t1 RENAME to t1;
  66. }
  67. } {1 {SQL logic error or missing database}}
  68. do_test fts2o-2.6 {
  69. execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; }
  70. } {1 {one three <b>four</b>}}
  71. do_test fts2o-2.7 {
  72. execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
  73. } {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir t1_segdir}
  74. # See what happens when renaming the fts2 table fails inside a transaction.
  75. #
  76. do_test fts2o-2.8 {
  77. execsql {
  78. BEGIN;
  79. INSERT INTO fts_t1(a, b, c) VALUES('one two three', 'one four', 'one two');
  80. }
  81. } {}
  82. do_test fts2o-2.9 {
  83. catchsql {
  84. ALTER TABLE fts_t1 RENAME to t1;
  85. }
  86. } {1 {SQL logic error or missing database}}
  87. do_test fts2o-2.10 {
  88. execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; }
  89. } {1 {one three <b>four</b>}}
  90. do_test fts2o-2.11 {
  91. execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'}
  92. } {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir t1_segdir}
  93. do_test fts2o-2.12 {
  94. execsql COMMIT
  95. execsql {SELECT a FROM fts_t1}
  96. } {{one three four} {one two three}}
  97. do_test fts2o-2.12 {
  98. execsql { SELECT a, b, c FROM fts_t1 WHERE c MATCH 'four'; }
  99. } {{one three four} {one four} {one four two}}
  100. #-------------------------------------------------------------------
  101. # Close, delete and reopen the database. The following test should
  102. # be run on an initially empty db.
  103. #
  104. db close
  105. forcedelete test.db test.db-journal
  106. sqlite3 db test.db
  107. do_test fts2o-3.1 {
  108. execsql {
  109. CREATE VIRTUAL TABLE t1 USING fts2(a, b, c);
  110. INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one two');
  111. SELECT a, b, c FROM t1 WHERE c MATCH 'two';
  112. }
  113. } {{one three four} {one four} {one two}}
  114. # This test was crashing at one point.
  115. #
  116. do_test fts2o-3.2 {
  117. execsql {
  118. SELECT a, b, c FROM t1 WHERE c MATCH 'two';
  119. CREATE TABLE t3(a, b, c);
  120. SELECT a, b, c FROM t1 WHERE c MATCH 'two';
  121. }
  122. } {{one three four} {one four} {one two} {one three four} {one four} {one two}}
  123. #---------------------------------------------------------------------
  124. # Test that it is possible to rename an fts2 table in an attached
  125. # database.
  126. #
  127. forcedelete test2.db test2.db-journal
  128. do_test fts2o-3.1 {
  129. execsql {
  130. ATTACH 'test2.db' AS aux;
  131. CREATE VIRTUAL TABLE aux.t1 USING fts2(a, b, c);
  132. INSERT INTO aux.t1(a, b, c) VALUES(
  133. 'neung song sahm', 'neung see', 'neung see song'
  134. );
  135. }
  136. } {}
  137. do_test fts2o-3.2 {
  138. execsql { SELECT a, b, c FROM aux.t1 WHERE a MATCH 'song'; }
  139. } {{neung song sahm} {neung see} {neung see song}}
  140. do_test fts2o-3.3 {
  141. execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; }
  142. } {{one three four} {one four} {one two}}
  143. do_test fts2o-3.4 {
  144. execsql { ALTER TABLE aux.t1 RENAME TO t2 }
  145. } {}
  146. do_test fts2o-3.2 {
  147. execsql { SELECT a, b, c FROM t2 WHERE a MATCH 'song'; }
  148. } {{neung song sahm} {neung see} {neung see song}}
  149. do_test fts2o-3.3 {
  150. execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; }
  151. } {{one three four} {one four} {one two}}
  152. finish_test