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