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