SQLite

Artifact [b2d1a8c8b8]
Login

Artifact b2d1a8c8b8f86881f50b481eae233f8abfb61436:


# 2016 August 23
#
# 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 $testdir/tester.tcl
ifcapable !session {finish_test; return}

set testprefix changebatch1

proc do_changebatch_test {tn args} {
  set C [list]
  foreach a $args {
    sqlite3session S db main
    S attach *
    execsql $a
    lappend C [S changeset]
    S delete
  }

  sqlite3changebatch cb db
  set i 1
  foreach ::cs [lrange $C 0 end-1] {
    do_test $tn.$i { cb add [set ::cs] } SQLITE_OK
    incr i
  }

  set ::cs [lindex $C end]
  do_test $tn.$i { cb add [set ::cs] } SQLITE_CONSTRAINT

  cb delete
}

do_execsql_test 1.0 {
  CREATE TABLE t1(a PRIMARY KEY, b);
}

do_changebatch_test 1.1 {
  INSERT INTO t1 VALUES(1, 1);
} {
  DELETE FROM t1 WHERE a=1;
}

do_execsql_test 1.2.0 {
  INSERT INTO t1 VALUES(1, 1);
  INSERT INTO t1 VALUES(2, 2);
  INSERT INTO t1 VALUES(3, 3);
}
do_changebatch_test 1.2.1 {
  DELETE FROM t1 WHERE a=2;
} {
  INSERT INTO t1 VALUES(2, 2);
}

#-------------------------------------------------------------------------
do_execsql_test 2.0 {
  CREATE TABLE x1(a, b PRIMARY KEY, c UNIQUE);
  CREATE TABLE x2(a PRIMARY KEY, b UNIQUE, c UNIQUE);

  INSERT INTO x1 VALUES(1, 1, 'a');
  INSERT INTO x1 VALUES(1, 2, 'b');
  INSERT INTO x1 VALUES(1, 3, 'c');
}

do_changebatch_test 2.1 {
  DELETE FROM x1 WHERE b=2;
} {
  UPDATE x1 SET c='b' WHERE b=3;
}

do_changebatch_test 2.2 {
  DELETE FROM x1 WHERE b=1;
} {
  INSERT INTO x1 VALUES(1, 5, 'a');
}

finish_test