# 2015 October 27
#
# 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 script is testing the FTS5 module.
#

source [file join [file dirname [info script]] fts5_common.tcl]
set testprefix fts5conflict

# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
ifcapable !fts5 {
  finish_test
  return
}

do_execsql_test 1.0 {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, a, b);
  CREATE VIRTUAL TABLE ft USING fts5(a, b, content=t1, content_rowid=x);
}

do_execsql_test 1.1 {
  REPLACE INTO ft(rowid, a, b) VALUES(1, 'a b c', 'a b c');
  REPLACE INTO t1              VALUES(1, 'a b c', 'a b c');
}

do_execsql_test 1.2 {
  INSERT INTO ft(ft) VALUES('integrity-check');
}

do_execsql_test 2.0 {
  CREATE TABLE tbl(a INTEGER PRIMARY KEY, b, c);
  CREATE VIRTUAL TABLE fts_idx USING fts5(b, c, content=tbl, content_rowid=a);
  CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
    INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
  END;
  CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
    INSERT INTO fts_idx(fts_idx, rowid, b, c) 
      VALUES('delete', old.a, old.b, old.c);
  END;
  CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
    INSERT INTO fts_idx(fts_idx, rowid, b, c) 
      VALUES('delete', old.a, old.b, old.c);
    INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
  END;
}

do_execsql_test 2.1 {
  PRAGMA recursive_triggers = 1;
  INSERT INTO tbl VALUES(1,   'x y z', '1 2 3');
  INSERT INTO tbl VALUES(10,  'x y z', '1 2 3');
  INSERT INTO tbl VALUES(100, 'x 1 z', '1 y 3');

  UPDATE tbl SET b = '1 2 x' WHERE rowid=10;
  REPLACE INTO tbl VALUES(1, '4 5 6', '3 2 1');
  DELETE FROM tbl WHERE a=100;

  INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
}

#-------------------------------------------------------------------------
# Tests for OR IGNORE conflict handling.
#
reset_db
foreach_detail_mode $::testprefix {

  do_execsql_test 3.0 {
    CREATE VIRTUAL TABLE t1 USING fts5(xyz, detail=%DETAIL%);

    BEGIN;
    INSERT INTO t1(rowid, xyz) VALUES(13, 'thirteen documents');
    INSERT INTO t1(rowid, xyz) VALUES(14, 'fourteen documents');
    INSERT INTO t1(rowid, xyz) VALUES(15, 'fifteen documents');
    COMMIT;
  }

  set db_cksum [cksum]
    foreach {tn sql} {
    1 {
      INSERT OR IGNORE INTO t1(rowid, xyz) VALUES(14, 'new text');
    }
    2 {
      UPDATE OR IGNORE t1 SET rowid=13 WHERE rowid=15;
    }
    3 {
      INSERT OR IGNORE INTO t1(rowid, xyz) 
      SELECT 13, 'some text'
      UNION ALL
      SELECT 14, 'some text'
      UNION ALL
      SELECT 15, 'some text'
    }
  } {
    do_execsql_test 3.1.$tn.1 $sql
    do_test 3.1.$tn.2 { cksum } $db_cksum
  }

}


finish_test
