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