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