1#
2# 2001 September 15
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing the 'progress callback'.
14#
15# $Id: progress.test,v 1.1 2003/10/18 09:37:27 danielk1977 Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22execsql {
23  BEGIN;
24  CREATE TABLE t1(a);
25  INSERT INTO t1 VALUES(1);
26  INSERT INTO t1 VALUES(2);
27  INSERT INTO t1 VALUES(3);
28  INSERT INTO t1 VALUES(4);
29  INSERT INTO t1 VALUES(5);
30  INSERT INTO t1 VALUES(6);
31  INSERT INTO t1 VALUES(7);
32  INSERT INTO t1 VALUES(8);
33  INSERT INTO t1 VALUES(9);
34  INSERT INTO t1 VALUES(10);
35  COMMIT;
36}
37
38
39# Test that the progress callback is invoked.
40do_test progress-1.0 {
41  set counter 0
42  db progress 1 "[namespace code {incr counter}] ; expr 0"
43  execsql {
44    SELECT * FROM t1
45  }
46  expr $counter > 1
47} 1
48
49# Test that the query is abandoned when the progress callback returns non-zero
50do_test progress1.1 {
51  set counter 0
52  db progress 1 "[namespace code {incr counter}] ; expr 1"
53  execsql {
54    SELECT * FROM t1
55  }
56  set counter
57} 1
58
59# Test that the query is rolled back when the progress callback returns
60# non-zero.
61do_test progress1.2 {
62
63  # This figures out how many opcodes it takes to copy 5 extra rows into t1.
64  db progress 1 "[namespace code {incr five_rows}] ; expr 0"
65  set five_rows 0
66  execsql {
67    INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 6
68  }
69  db progress 0 ""
70  execsql {
71    DELETE FROM t1 WHERE a > 10
72  }
73
74  # Now set up the progress callback to abandon the query after the number of
75  # opcodes to copy 5 rows. That way, when we try to copy 6 rows, we know
76  # some data will have been inserted into the table by the time the progress
77  # callback abandons the query.
78  db progress $five_rows "expr 1"
79  execsql {
80    INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 7
81  }
82  execsql {
83    SELECT count(*) FROM t1
84  }
85} 10
86
87# Test that an active transaction remains active and not rolled back after the
88# progress query abandons a query.
89do_test progress1.3 {
90
91  db progress 0 ""
92  execsql BEGIN
93  execsql {
94    INSERT INTO t1 VALUES(11)
95  }
96  db progress 1 "expr 1"
97  execsql {
98    INSERT INTO t1 VALUES(12)
99  }
100  db progress 0 ""
101  execsql COMMIT
102  execsql {
103    SELECT count(*) FROM t1
104  }
105} 11
106
107# Check that a value of 0 for N means no progress callback
108do_test progress1.4 {
109  set counter 0
110  db progress 0 "[namespace code {incr counter}] ; expr 0"
111  execsql {
112    SELECT * FROM t1;
113  }
114  set counter
115} 0
116
117db progress 0 ""
118
119finish_test
120