reindex.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # 2004 November 5
  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.
  12. # This file implements tests for the REINDEX command.
  13. #
  14. # $Id: reindex.test,v 1.4 2008/07/12 14:52:20 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # There is nothing to test if REINDEX is disable for this build.
  18. #
  19. ifcapable {!reindex} {
  20. finish_test
  21. return
  22. }
  23. # Basic sanity checks.
  24. #
  25. do_test reindex-1.1 {
  26. execsql {
  27. CREATE TABLE t1(a,b);
  28. INSERT INTO t1 VALUES(1,2);
  29. INSERT INTO t1 VALUES(3,4);
  30. CREATE INDEX i1 ON t1(a);
  31. REINDEX;
  32. }
  33. } {}
  34. integrity_check reindex-1.2
  35. do_test reindex-1.3 {
  36. execsql {
  37. REINDEX t1;
  38. }
  39. } {}
  40. integrity_check reindex-1.4
  41. do_test reindex-1.5 {
  42. execsql {
  43. REINDEX i1;
  44. }
  45. } {}
  46. integrity_check reindex-1.6
  47. do_test reindex-1.7 {
  48. execsql {
  49. REINDEX main.t1;
  50. }
  51. } {}
  52. do_test reindex-1.8 {
  53. execsql {
  54. REINDEX main.i1;
  55. }
  56. } {}
  57. do_test reindex-1.9 {
  58. catchsql {
  59. REINDEX bogus
  60. }
  61. } {1 {unable to identify the object to be reindexed}}
  62. # Set up a table for testing that includes several different collating
  63. # sequences including some that we can modify.
  64. #
  65. do_test reindex-2.1 {
  66. proc c1 {a b} {
  67. return [expr {-[string compare $a $b]}]
  68. }
  69. proc c2 {a b} {
  70. return [expr {-[string compare [string tolower $a] [string tolower $b]]}]
  71. }
  72. db collate c1 c1
  73. db collate c2 c2
  74. execsql {
  75. CREATE TABLE t2(
  76. a TEXT PRIMARY KEY COLLATE c1,
  77. b TEXT UNIQUE COLLATE c2,
  78. c TEXT COLLATE nocase,
  79. d TEST COLLATE binary
  80. );
  81. INSERT INTO t2 VALUES('abc','abc','abc','abc');
  82. INSERT INTO t2 VALUES('ABCD','ABCD','ABCD','ABCD');
  83. INSERT INTO t2 VALUES('bcd','bcd','bcd','bcd');
  84. INSERT INTO t2 VALUES('BCDE','BCDE','BCDE','BCDE');
  85. SELECT a FROM t2 ORDER BY a;
  86. }
  87. } {bcd abc BCDE ABCD}
  88. do_test reindex-2.2 {
  89. execsql {
  90. SELECT b FROM t2 ORDER BY b;
  91. }
  92. } {BCDE bcd ABCD abc}
  93. do_test reindex-2.3 {
  94. execsql {
  95. SELECT c FROM t2 ORDER BY c;
  96. }
  97. } {abc ABCD bcd BCDE}
  98. do_test reindex-2.4 {
  99. execsql {
  100. SELECT d FROM t2 ORDER BY d;
  101. }
  102. } {ABCD BCDE abc bcd}
  103. # Change a collating sequence function. Verify that REINDEX rebuilds
  104. # the index.
  105. #
  106. do_test reindex-2.5 {
  107. proc c1 {a b} {
  108. return [string compare $a $b]
  109. }
  110. execsql {
  111. SELECT a FROM t2 ORDER BY a;
  112. }
  113. } {bcd abc BCDE ABCD}
  114. ifcapable {integrityck} {
  115. do_test reindex-2.5.1 {
  116. string equal ok [execsql {PRAGMA integrity_check}]
  117. } {0}
  118. }
  119. do_test reindex-2.6 {
  120. execsql {
  121. REINDEX c2;
  122. SELECT a FROM t2 ORDER BY a;
  123. }
  124. } {bcd abc BCDE ABCD}
  125. do_test reindex-2.7 {
  126. execsql {
  127. REINDEX t1;
  128. SELECT a FROM t2 ORDER BY a;
  129. }
  130. } {bcd abc BCDE ABCD}
  131. do_test reindex-2.8 {
  132. execsql {
  133. REINDEX c1;
  134. SELECT a FROM t2 ORDER BY a;
  135. }
  136. } {ABCD BCDE abc bcd}
  137. integrity_check reindex-2.8.1
  138. # Try to REINDEX an index for which the collation sequence is not available.
  139. #
  140. do_test reindex-3.1 {
  141. sqlite3 db2 test.db
  142. catchsql {
  143. REINDEX c1;
  144. } db2
  145. } {1 {no such collation sequence: c1}}
  146. do_test reindex-3.2 {
  147. proc need_collate {collation} {
  148. db2 collate c1 c1
  149. }
  150. db2 collation_needed need_collate
  151. catchsql {
  152. REINDEX c1;
  153. } db2
  154. } {0 {}}
  155. do_test reindex-3.3 {
  156. catchsql {
  157. REINDEX;
  158. } db2
  159. } {1 {no such collation sequence: c2}}
  160. do_test reindex-3.99 {
  161. db2 close
  162. } {}
  163. finish_test