# 2018 October 18
#
# 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.
#

if {![info exists testdir]} {
  set testdir [file join [file dirname [info script]] .. .. test]
} 
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl
ifcapable !session {finish_test; return}

set testprefix sessioninvert

proc iter_invert {C} {
  set x [list]
  sqlite3session_foreach -invert c $C { lappend x $c }
  set x
}

proc do_invert_test {tn sql {iter {}}} {

  forcecopy test.db test.db2
  sqlite3 db2 test.db2

  set C [changeset_from_sql $sql]

  forcecopy test.db test.db3
  sqlite3 db3 test.db3
  uplevel [list do_test $tn.1 [list compare_db db db3] {}]

  set I [sqlite3changeset_invert $C]
  sqlite3changeset_apply db $I {}
  uplevel [list do_test $tn.2 [list compare_db db db2] {}]
  
  sqlite3changeset_apply_v2 -invert db3 $C {}
  uplevel [list do_test $tn.3 [list compare_db db db3] {}]

  if {$iter!=""} {
    uplevel [list do_test $tn.4 [list iter_invert $C] [list {*}$iter]]
  }

  catch { db2 close }
  catch { db3 close }
}

do_execsql_test 1.0 {
  CREATE TABLE t1(a PRIMARY KEY, b, c);
  CREATE TABLE t2(d, e, f, PRIMARY KEY(e, f));

  INSERT INTO t1 VALUES(1, 'one', 'i');
  INSERT INTO t1 VALUES(2, 'two', 'ii');
  INSERT INTO t1 VALUES(3, 'three', 'iii');
  INSERT INTO t1 VALUES(4, 'four', 'iv');
  INSERT INTO t1 VALUES(5, 'five', 'v');
  INSERT INTO t1 VALUES(6, 'six', 'vi');

  INSERT INTO t2 SELECT * FROM t1;
}

do_invert_test 1.1 {
  INSERT INTO t1 VALUES(7, 'seven', 'vii');
} {
  {DELETE t1 0 X.. {i 7 t seven t vii} {}}
}

do_invert_test 1.2 {
  DELETE FROM t1 WHERE a<4;
} {
  {INSERT t1 0 X.. {} {i 1 t one t i}}
  {INSERT t1 0 X.. {} {i 2 t two t ii}}
  {INSERT t1 0 X.. {} {i 3 t three t iii}}
}

do_invert_test 1.3 {
  UPDATE t1 SET c=5;
} {
  {UPDATE t1 0 X.. {i 1 {} {} i 5} {{} {} {} {} t i}}
  {UPDATE t1 0 X.. {i 2 {} {} i 5} {{} {} {} {} t ii}}
  {UPDATE t1 0 X.. {i 3 {} {} i 5} {{} {} {} {} t iii}}
  {UPDATE t1 0 X.. {i 4 {} {} i 5} {{} {} {} {} t iv}}
  {UPDATE t1 0 X.. {i 5 {} {} i 5} {{} {} {} {} t v}}
  {UPDATE t1 0 X.. {i 6 {} {} i 5} {{} {} {} {} t vi}}
}

do_invert_test 1.4 {
  UPDATE t1 SET b = a+1 WHERE a%2;
  DELETE FROM t2;
  INSERT INTO t1 VALUES(10, 'ten', NULL);
}

do_invert_test 1.5 {
  UPDATE t2 SET d = d-1;
} {
  {UPDATE t2 0 .XX {i 2 t three t iii} {i 3 {} {} {} {}}}
  {UPDATE t2 0 .XX {i 1 t two t ii} {i 2 {} {} {} {}}}
  {UPDATE t2 0 .XX {i 5 t six t vi} {i 6 {} {} {} {}}}
  {UPDATE t2 0 .XX {i 3 t four t iv} {i 4 {} {} {} {}}}
  {UPDATE t2 0 .XX {i 0 t one t i} {i 1 {} {} {} {}}}
  {UPDATE t2 0 .XX {i 4 t five t v} {i 5 {} {} {} {}}}
}

do_execsql_test 2.0 { 
  ANALYZE;
  PRAGMA writable_schema = 1;
  DROP TABLE IF EXISTS sqlite_stat4;
  SELECT * FROM sqlite_stat1;
} {
  t2 sqlite_autoindex_t2_1 {6 1 1} 
  t1 sqlite_autoindex_t1_1 {6 1}
}

do_invert_test 2.1 {
  INSERT INTO sqlite_stat1 VALUES('t3', 'idx2', '1 2 3');
} {
  {DELETE sqlite_stat1 0 XX. {t t3 t idx2 t {1 2 3}} {}}
}

do_invert_test 2.2 {
  DELETE FROM sqlite_stat1;
} {
  {INSERT sqlite_stat1 0 XX. {} {t t1 t sqlite_autoindex_t1_1 t {6 1}}}
  {INSERT sqlite_stat1 0 XX. {} {t t2 t sqlite_autoindex_t2_1 t {6 1 1}}}
}

do_invert_test 2.3 {
  UPDATE sqlite_stat1 SET stat = 'hello world';
}

do_test 3.0 {
  forcecopy test.db test.db2
  sqlite3 db2 test.db2
  set P [patchset_from_sql {
    INSERT INTO t2 VALUES(1, 2, 3);
    DELETE FROM t2 WHERE d = 3;
  }]

  list [catch { sqlite3changeset_apply_v2 -invert db2 $P {} } msg] $msg
} {1 SQLITE_CORRUPT}

do_test 3.1 {
  list [catch { sqlite3session_foreach -invert db2 $P {} } msg] $msg
} {1 SQLITE_CORRUPT}

do_test 3.2 {
  sqlite3changeset_apply_v2 db2 $P {} 
  compare_db db db2
} {}

db2 close

#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 4.0 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
  INSERT INTO t1 VALUES(1, 'one');
  INSERT INTO t1 VALUES(2, 'two');
  INSERT INTO t1 VALUES(3, 'three');
  INSERT INTO t1 VALUES(4, 'four');
}

do_invert_test 4.1 {
  DELETE FROM t1;
  INSERT INTO t1 VALUES(1, 'two');
  INSERT INTO t1 VALUES(2, 'five');
  INSERT INTO t1 VALUES(3, 'one');
  INSERT INTO t1 VALUES(4, 'three');
} {
  {UPDATE t1 0 X. {i 1 t two} {{} {} t one}}
  {UPDATE t1 0 X. {i 2 t five} {{} {} t two}}
  {UPDATE t1 0 X. {i 3 t one} {{} {} t three}} 
  {UPDATE t1 0 X. {i 4 t three} {{} {} t four}}
}

#-------------------------------------------------------------------------
#
do_test 5.0 {
  set C [db one {SELECT unhex('54000009')}]
  list [catch { sqlite3changeset_invert $C } msg] $msg
} {1 SQLITE_CORRUPT}

finish_test
