# 2014 August 30
#
# 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 contains tests for the RBU module. More specifically, it
# contains tests to ensure that RBU works with FTS tables.
#

if {![info exists testdir]} {
  set testdir [file join [file dirname [info script]] .. .. test]
}
source $testdir/tester.tcl
set ::testprefix rbufts

ifcapable !fts3 {
  finish_test
  return
}

proc step_rbu {target rbu} {
  while 1 {
    sqlite3rbu rbu $target $rbu
    set rc [rbu step]
    rbu close
    if {$rc != "SQLITE_OK"} break
  }
  set rc
}

proc apply_rbu_update {target sql} {
  forcedelete rbu.db
  sqlite3 dbrbu rbu.db
  execsql $sql dbrbu
  dbrbu close

  step_rbu $target rbu.db
}

do_execsql_test 1.1.0 {
  CREATE TABLE t1(i INTEGER PRIMARY KEY, a, b);
  CREATE VIRTUAL TABLE xx USING fts4(content=t1, a, b);
  INSERT INTO t1(rowid, a, b) VALUES(10, 'a b c', 'c b a');
  INSERT INTO t1(rowid, a, b) VALUES(20, 'a b c', 'd e f');
  INSERT INTO t1(rowid, a, b) VALUES(30, 'd e f', 'a b c');
  INSERT INTO t1(rowid, a, b) VALUES(40, 'd e f', 'd e f');
}

do_execsql_test 1.1.1 {
  INSERT INTO xx(xx) VALUES('rebuild');
  INSERT INTO xx(xx) VALUES('integrity-check');
}

do_test 1.1.2 {
  apply_rbu_update test.db {
    CREATE TABLE data_t1(i, a, b, rbu_control);
    INSERT INTO data_t1 VALUES(20, NULL, NULL, 1);        -- delete
    INSERT INTO data_t1 VALUES(30, 'x y z', NULL, '.x.'); -- update
    INSERT INTO data_t1 VALUES(50, '1 2 3', 'x y z', 0);  -- insert

    CREATE VIEW data0_xx AS 
    SELECT i AS rbu_rowid, a, b, 
    CASE WHEN rbu_control IN (0, 1) 
    THEN rbu_control ELSE substr(rbu_control, 2) END AS rbu_control
    FROM data_t1;
  }
} {SQLITE_DONE}

do_execsql_test 1.1.3 {
  INSERT INTO xx(xx) VALUES('integrity-check');
}

reset_db
do_execsql_test 1.2.1 {
  CREATE TABLE ccc(addr, text);
  CREATE VIRTUAL TABLE ccc_fts USING fts4(addr, text, content=ccc);
  INSERT INTO ccc VALUES('a b c', 'd e f');
  INSERT INTO ccc VALUES('a b c', 'd e f');
  INSERT INTO ccc_fts(ccc_fts) VALUES('rebuild');
  INSERT INTO ccc_fts(ccc_fts) VALUES('integrity-check');
}

do_test 1.2.2 {
  apply_rbu_update test.db {
    CREATE TABLE data_ccc(addr, text, rbu_rowid, rbu_control);
    CREATE VIEW data0_ccc_fts AS SELECT * FROM data_ccc;
    INSERT INTO data_ccc VALUES(NULL, NULL, 1, 1);
    INSERT INTO data_ccc VALUES('x y z', NULL, 2, 'x.');
    INSERT INTO data_ccc VALUES('y y y', '1 1 1', 3, 0);
  }
} {SQLITE_DONE}

do_execsql_test 1.2.3 {
  INSERT INTO ccc_fts(ccc_fts) VALUES('integrity-check');
}
do_execsql_test 1.2.4 {
  SELECT rowid, * FROM ccc_fts;
} {2 {x y z} {d e f} 3 {y y y} {1 1 1}}

#-------------------------------------------------------------------------
# Test the outcome of attempting to delete or update a row within a 
# contentless FTS table using RBU. An error.
#
reset_db
do_execsql_test 3.1 {
  CREATE VIRTUAL TABLE ft USING fts4(x, content=);
  INSERT INTO ft(rowid, x) VALUES(1, '1 2 3');
  INSERT INTO ft(rowid, x) VALUES(2, '4 5 6');
}

do_test 3.2 {
  list [catch { apply_rbu_update test.db {
    CREATE TABLE data_ft(x, rbu_rowid, rbu_control);
    INSERT INTO data_ft VALUES(NULL, 2, 1);
  } } msg] $msg]
} {1 {SQLITE_ERROR - SQL logic error]}}

do_test 3.3 {
  list [catch { apply_rbu_update test.db {
    CREATE TABLE data_ft(x, rbu_rowid, rbu_control);
    INSERT INTO data_ft VALUES('7 8 9', 1, 'x');
  } } msg] $msg]
} {1 {SQLITE_ERROR - SQL logic error]}}



finish_test
