# 2017-10-11
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the checkindex extension.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix checkindex

ifcapable !vtab||!compound {
  finish_test
  return
}

if {[file exists ../checkindex.so]==0} {
  finish_test
  return
}

do_execsql_test 1.0 {
  CREATE TABLE t1(a, b);
  CREATE INDEX i1 ON t1(a);
  INSERT INTO t1 VALUES('one', 2);
  INSERT INTO t1 VALUES('two', 4);
  INSERT INTO t1 VALUES('three', 6);
  INSERT INTO t1 VALUES('four', 8);
  INSERT INTO t1 VALUES('five', 10);
}

db enable_load_extension 1
do_execsql_test 1.1 {
  SELECT load_extension('../checkindex.so');
} {{}}

do_execsql_test 1.2 {
  SELECT errmsg IS NULL, current_key FROM incremental_index_check('i1');
} {
  1 five,5
  1 four,4
  1 one,1
  1 three,3
  1 two,2
}


do_test 1.3 {
  set tblroot [db one { SELECT rootpage FROM sqlite_master WHERE name='t1' }]
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $tblroot
  db eval {CREATE TABLE xt1(a, b)}
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0

  execsql {
    UPDATE xt1 SET a='six' WHERE rowid=3;
    DELETE FROM xt1 WHERE rowid = 5;
  }

  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 1
} {}

do_execsql_test 1.4 {
  SELECT errmsg IS NULL, current_key FROM incremental_index_check('i1');
} {
  0 five,5
  1 four,4
  1 one,1
  0 three,3
  1 two,2
}
do_execsql_test 1.5 {
  SELECT errmsg, current_key FROM incremental_index_check('i1');
} {
  {row missing} five,5
  {} four,4
  {} one,1
  {row data mismatch} three,3
  {} two,2
}



finish_test

