fts2token.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # 2007 June 21
  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 focus
  12. # of this script is testing the pluggable tokeniser feature of the
  13. # FTS2 module.
  14. #
  15. # $Id: fts2token.test,v 1.3 2007/06/25 12:05:40 danielk1977 Exp $
  16. #
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # If SQLITE_ENABLE_FTS2 is defined, omit this file.
  20. ifcapable !fts2 {
  21. finish_test
  22. return
  23. }
  24. proc escape_string {str} {
  25. set out ""
  26. foreach char [split $str ""] {
  27. scan $char %c i
  28. if {$i<=127} {
  29. append out $char
  30. } else {
  31. append out [format {\x%.4x} $i]
  32. }
  33. }
  34. set out
  35. }
  36. #--------------------------------------------------------------------------
  37. # Test cases fts2token-1.* are the warm-body test for the SQL scalar
  38. # function fts2_tokenizer(). The procedure is as follows:
  39. #
  40. # 1: Verify that there is no such fts2 tokenizer as 'blah'.
  41. #
  42. # 2: Query for the built-in tokenizer 'simple'. Insert a copy of the
  43. # retrieved value as tokenizer 'blah'.
  44. #
  45. # 3: Test that the value returned for tokenizer 'blah' is now the
  46. # same as that retrieved for 'simple'.
  47. #
  48. # 4: Test that it is now possible to create an fts2 table using
  49. # tokenizer 'blah' (it was not possible in step 1).
  50. #
  51. # 5: Test that the table created to use tokenizer 'blah' is usable.
  52. #
  53. do_test fts2token-1.1 {
  54. catchsql {
  55. CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize blah);
  56. }
  57. } {1 {unknown tokenizer: blah}}
  58. do_test fts2token-1.2 {
  59. execsql {
  60. SELECT fts2_tokenizer('blah', fts2_tokenizer('simple')) IS NULL;
  61. }
  62. } {0}
  63. do_test fts2token-1.3 {
  64. execsql {
  65. SELECT fts2_tokenizer('blah') == fts2_tokenizer('simple');
  66. }
  67. } {1}
  68. do_test fts2token-1.4 {
  69. catchsql {
  70. CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize blah);
  71. }
  72. } {0 {}}
  73. do_test fts2token-1.5 {
  74. execsql {
  75. INSERT INTO t1(content) VALUES('There was movement at the station');
  76. INSERT INTO t1(content) VALUES('For the word has passed around');
  77. INSERT INTO t1(content) VALUES('That the colt from ol regret had got away');
  78. SELECT content FROM t1 WHERE content MATCH 'movement'
  79. }
  80. } {{There was movement at the station}}
  81. #--------------------------------------------------------------------------
  82. # Test cases fts2token-2.* test error cases in the scalar function based
  83. # API for getting and setting tokenizers.
  84. #
  85. do_test fts2token-2.1 {
  86. catchsql {
  87. SELECT fts2_tokenizer('nosuchtokenizer');
  88. }
  89. } {1 {unknown tokenizer: nosuchtokenizer}}
  90. #--------------------------------------------------------------------------
  91. # Test cases fts2token-3.* test the three built-in tokenizers with a
  92. # simple input string via the built-in test function. This is as much
  93. # to test the test function as the tokenizer implementations.
  94. #
  95. do_test fts2token-3.1 {
  96. execsql {
  97. SELECT fts2_tokenizer_test('simple', 'I don''t see how');
  98. }
  99. } {{0 i I 1 don don 2 t t 3 see see 4 how how}}
  100. do_test fts2token-3.2 {
  101. execsql {
  102. SELECT fts2_tokenizer_test('porter', 'I don''t see how');
  103. }
  104. } {{0 i I 1 don don 2 t t 3 see see 4 how how}}
  105. ifcapable icu {
  106. do_test fts2token-3.3 {
  107. execsql {
  108. SELECT fts2_tokenizer_test('icu', 'I don''t see how');
  109. }
  110. } {{0 i I 1 don't don't 2 see see 3 how how}}
  111. }
  112. #--------------------------------------------------------------------------
  113. # Test cases fts2token-4.* test the ICU tokenizer. In practice, this
  114. # tokenizer only has two modes - "thai" and "everybody else". Some other
  115. # Asian languages (Lao, Khmer etc.) require the same special treatment as
  116. # Thai, but ICU doesn't support them yet.
  117. #
  118. ifcapable icu {
  119. proc do_icu_test {name locale input output} {
  120. set ::out [db eval { SELECT fts2_tokenizer_test('icu', $locale, $input) }]
  121. do_test $name {
  122. lindex $::out 0
  123. } $output
  124. }
  125. do_icu_test fts2token-4.1 en_US {} {}
  126. do_icu_test fts2token-4.2 en_US {Test cases fts2} [list \
  127. 0 test Test 1 cases cases 2 fts2 fts2
  128. ]
  129. # The following test shows that ICU is smart enough to recognise
  130. # Thai chararacters, even when the locale is set to English/United
  131. # States.
  132. #
  133. set input "\u0e2d\u0e30\u0e44\u0e23\u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a"
  134. set output "0 \u0e2d\u0e30\u0e44\u0e23 \u0e2d\u0e30\u0e44\u0e23 "
  135. append output "1 \u0e19\u0e30 \u0e19\u0e30 "
  136. append output "2 \u0e04\u0e23\u0e31\u0e1a \u0e04\u0e23\u0e31\u0e1a"
  137. do_icu_test fts2token-4.3 th_TH $input $output
  138. do_icu_test fts2token-4.4 en_US $input $output
  139. # ICU handles an unknown locale by falling back to the default.
  140. # So this is not an error.
  141. do_icu_test fts2token-4.5 MiddleOfTheOcean $input $output
  142. set longtoken "AReallyReallyLongTokenOneThatWillSurelyRequire"
  143. append longtoken "AReallocInTheIcuTokenizerCode"
  144. set input "short tokens then "
  145. append input $longtoken
  146. set output "0 short short "
  147. append output "1 tokens tokens "
  148. append output "2 then then "
  149. append output "3 [string tolower $longtoken] $longtoken"
  150. do_icu_test fts2token-4.6 MiddleOfTheOcean $input $output
  151. do_icu_test fts2token-4.7 th_TH $input $output
  152. do_icu_test fts2token-4.8 en_US $input $output
  153. }
  154. do_test fts2token-internal {
  155. execsql { SELECT fts2_tokenizer_internal_test() }
  156. } {ok}
  157. finish_test