sl@0: # 2004 November 5
sl@0: #
sl@0: # The author disclaims copyright to this source code.  In place of
sl@0: # a legal notice, here is a blessing:
sl@0: #
sl@0: #    May you do good and not evil.
sl@0: #    May you find forgiveness for yourself and forgive others.
sl@0: #    May you share freely, never taking more than you give.
sl@0: #
sl@0: #***********************************************************************
sl@0: # This file implements regression tests for SQLite library.
sl@0: # This file implements tests for the REINDEX command.
sl@0: #
sl@0: # $Id: reindex.test,v 1.4 2008/07/12 14:52:20 drh Exp $
sl@0: 
sl@0: set testdir [file dirname $argv0]
sl@0: source $testdir/tester.tcl
sl@0: 
sl@0: # There is nothing to test if REINDEX is disable for this build.
sl@0: #
sl@0: ifcapable {!reindex} {
sl@0:   finish_test
sl@0:   return
sl@0: }
sl@0: 
sl@0: # Basic sanity checks.
sl@0: #
sl@0: do_test reindex-1.1 {
sl@0:   execsql {
sl@0:     CREATE TABLE t1(a,b);
sl@0:     INSERT INTO t1 VALUES(1,2);
sl@0:     INSERT INTO t1 VALUES(3,4);
sl@0:     CREATE INDEX i1 ON t1(a);
sl@0:     REINDEX;
sl@0:   }
sl@0: } {}
sl@0: integrity_check reindex-1.2
sl@0: do_test reindex-1.3 {
sl@0:   execsql {
sl@0:     REINDEX t1;
sl@0:   }
sl@0: } {}
sl@0: integrity_check reindex-1.4
sl@0: do_test reindex-1.5 {
sl@0:   execsql {
sl@0:     REINDEX i1;
sl@0:   }
sl@0: } {}
sl@0: integrity_check reindex-1.6
sl@0: do_test reindex-1.7 {
sl@0:   execsql {
sl@0:     REINDEX main.t1;
sl@0:   }
sl@0: } {}
sl@0: do_test reindex-1.8 {
sl@0:   execsql {
sl@0:     REINDEX main.i1;
sl@0:   }
sl@0: } {}
sl@0: do_test reindex-1.9 {
sl@0:   catchsql {
sl@0:     REINDEX bogus
sl@0:   }
sl@0: } {1 {unable to identify the object to be reindexed}}
sl@0: 
sl@0: # Set up a table for testing that includes several different collating
sl@0: # sequences including some that we can modify.
sl@0: #
sl@0: do_test reindex-2.1 {
sl@0:   proc c1 {a b} {
sl@0:     return [expr {-[string compare $a $b]}]
sl@0:   }
sl@0:   proc c2 {a b} {
sl@0:     return [expr {-[string compare [string tolower $a] [string tolower $b]]}]
sl@0:   }
sl@0:   db collate c1 c1
sl@0:   db collate c2 c2
sl@0:   execsql {
sl@0:     CREATE TABLE t2(
sl@0:       a TEXT PRIMARY KEY COLLATE c1,
sl@0:       b TEXT UNIQUE COLLATE c2,
sl@0:       c TEXT COLLATE nocase,
sl@0:       d TEST COLLATE binary
sl@0:     );
sl@0:     INSERT INTO t2 VALUES('abc','abc','abc','abc');
sl@0:     INSERT INTO t2 VALUES('ABCD','ABCD','ABCD','ABCD');
sl@0:     INSERT INTO t2 VALUES('bcd','bcd','bcd','bcd');
sl@0:     INSERT INTO t2 VALUES('BCDE','BCDE','BCDE','BCDE');
sl@0:     SELECT a FROM t2 ORDER BY a;
sl@0:   }
sl@0: } {bcd abc BCDE ABCD}
sl@0: do_test reindex-2.2 {
sl@0:   execsql {
sl@0:     SELECT b FROM t2 ORDER BY b;
sl@0:   }
sl@0: } {BCDE bcd ABCD abc}
sl@0: do_test reindex-2.3 {
sl@0:   execsql {
sl@0:     SELECT c FROM t2 ORDER BY c;
sl@0:   }
sl@0: } {abc ABCD bcd BCDE}
sl@0: do_test reindex-2.4 {
sl@0:   execsql {
sl@0:     SELECT d FROM t2 ORDER BY d;
sl@0:   }
sl@0: } {ABCD BCDE abc bcd}
sl@0: 
sl@0: # Change a collating sequence function.  Verify that REINDEX rebuilds
sl@0: # the index.
sl@0: #
sl@0: do_test reindex-2.5 {
sl@0:   proc c1 {a b} {
sl@0:     return [string compare $a $b]
sl@0:   }
sl@0:   execsql {
sl@0:     SELECT a FROM t2 ORDER BY a;
sl@0:   }
sl@0: } {bcd abc BCDE ABCD}
sl@0: ifcapable {integrityck} {
sl@0:   do_test reindex-2.5.1 {
sl@0:     string equal ok [execsql {PRAGMA integrity_check}]
sl@0:   } {0}
sl@0: }
sl@0: do_test reindex-2.6 {
sl@0:   execsql {
sl@0:     REINDEX c2;
sl@0:     SELECT a FROM t2 ORDER BY a;
sl@0:   }
sl@0: } {bcd abc BCDE ABCD}
sl@0: do_test reindex-2.7 {
sl@0:   execsql {
sl@0:     REINDEX t1;
sl@0:     SELECT a FROM t2 ORDER BY a;
sl@0:   }
sl@0: } {bcd abc BCDE ABCD}
sl@0: do_test reindex-2.8 {
sl@0:   execsql {
sl@0:     REINDEX c1;
sl@0:     SELECT a FROM t2 ORDER BY a;
sl@0:   }
sl@0: } {ABCD BCDE abc bcd}
sl@0: integrity_check reindex-2.8.1
sl@0: 
sl@0: # Try to REINDEX an index for which the collation sequence is not available.
sl@0: #
sl@0: do_test reindex-3.1 {
sl@0:   sqlite3 db2 test.db
sl@0:   catchsql {
sl@0:     REINDEX c1;
sl@0:   } db2
sl@0: } {1 {no such collation sequence: c1}}
sl@0: do_test reindex-3.2 {
sl@0:   proc need_collate {collation} {
sl@0:     db2 collate c1 c1
sl@0:   }
sl@0:   db2 collation_needed need_collate
sl@0:   catchsql {
sl@0:     REINDEX c1;
sl@0:   } db2
sl@0: } {0 {}}
sl@0: do_test reindex-3.3 {
sl@0:   catchsql {
sl@0:     REINDEX;
sl@0:   } db2
sl@0: } {1 {no such collation sequence: c2}}
sl@0: 
sl@0: do_test reindex-3.99 {
sl@0:   db2 close
sl@0: } {}
sl@0: 
sl@0: finish_test