# 2003 April 3
#
# 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 TCL interface to the
# SQLite library. 
#
# The focus of the tests in this file is the  following interface:
#
#      sqlite_begin_hook
#      sqlite_commit_hook
#
# $Id: hook.test,v 1.1 2003/04/03 15:46:05 drh Exp $

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

do_test hook-1.1 {
  db begin_hook
} {}
do_test hook-1.2 {
  db commit_hook
} {}

do_test hook-2.1 {
  set begin_cnt 0
  proc begin_hook {} {
    incr ::begin_cnt
    return 0
  }
  db begin_hook begin_hook
  db begin_hook
} {begin_hook}
do_test hook-2.2 {
  set begin_cnt
} {0}
do_test hook-2.3 {
  execsql {
    CREATE TABLE t1(a,b);
  }
  set begin_cnt
} {1}
do_test hook-2.4 {
  execsql {
    INSERT INTO t1 VALUES(1,2);
    INSERT INTO t1 SELECT a+1, b+1 FROM t1;
    INSERT INTO t1 SELECT a+2, b+2 FROM t1;
  }
  set begin_cnt
} {4}
do_test hook-2.5 {
  execsql {
    SELECT * FROM t1
  }
} {1 2 2 3 3 4 4 5}
do_test hook-2.6 {
  set begin_cnt
} {4}
do_test hook-2.7 {
  proc begin_hook {} {
    incr ::begin_cnt
    return 1
  }
  catchsql {
    INSERT INTO t1 VALUES(9,10);
  }
} {1 {constraint failed}}
do_test hook-2.8 {
  set begin_cnt
} {5}
do_test hook-2.9 {
  execsql {
    SELECT * FROM t1;
  }
} {1 2 2 3 3 4 4 5}
do_test hook-2.10 {
  db begin_hook {}
  db begin_hook
} {}
do_test hook-2.11 {
  execsql {
    INSERT INTO t1 VALUES(9,10);
    SELECT * FROM t1
  }
} {1 2 2 3 3 4 4 5 9 10}

do_test hook-3.1 {
  set commit_cnt 0
  proc commit_hook {} {
    incr ::commit_cnt
    return 0
  }
  db commit_hook ::commit_hook
  db commit_hook
} {::commit_hook}
do_test hook-3.2 {
  set commit_cnt
} {0}
do_test hook-3.3 {
  execsql {
    CREATE TABLE t2(a,b);
  }
  set commit_cnt
} {1}
do_test hook-3.4 {
  execsql {
    INSERT INTO t2 VALUES(1,2);
    INSERT INTO t2 SELECT a+1, b+1 FROM t2;
    INSERT INTO t2 SELECT a+2, b+2 FROM t2;
  }
  set commit_cnt
} {4}
do_test hook-3.5 {
  set commit_cnt {}
  proc commit_hook {} {
    set ::commit_cnt [execsql {SELECT * FROM t2}]
    return 0
  }
  execsql {
    INSERT INTO t2 VALUES(5,6);
  }
  set commit_cnt
} {1 2 2 3 3 4 4 5 5 6}
do_test hook-3.6 {
  set commit_cnt {}
  proc commit_hook {} {
    set ::commit_cnt [execsql {SELECT * FROM t2}]
    return 1
  }
  catchsql {
    INSERT INTO t2 VALUES(6,7);
  }
} {1 {constraint failed}}
do_test hook-3.7 {
  set commit_cnt
} {1 2 2 3 3 4 4 5 5 6 6 7}
do_test hook-3.8 {
  execsql {SELECT * FROM t2}
} {1 2 2 3 3 4 4 5 5 6}


finish_test
