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