danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1 | # 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 | # |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame^] | 14 | # $Id: progress.test,v 1.2 2004/05/31 08:26:50 danielk1977 Exp $ |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # Build some test data |
| 20 | # |
| 21 | execsql { |
| 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. |
| 39 | do_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 |
| 49 | do_test progress1.1 { |
| 50 | set counter 0 |
| 51 | db progress 1 "[namespace code {incr counter}] ; expr 1" |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame^] | 52 | set rc [catch {execsql { |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 53 | SELECT * FROM t1 |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame^] | 54 | }}] |
| 55 | list $counter $rc |
| 56 | } {1 1} |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 57 | |
| 58 | # Test that the query is rolled back when the progress callback returns |
| 59 | # non-zero. |
| 60 | do_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" |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame^] | 78 | catchsql { |
| 79 | INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 9 |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 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. |
| 88 | do_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" |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame^] | 96 | catchsql { |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 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 |
| 107 | do_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 | |
| 116 | db progress 0 "" |
| 117 | |
| 118 | finish_test |