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 | # |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame^] | 14 | # $Id: progress.test,v 1.3 2004/06/29 12:39:08 drh 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 |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame^] | 47 | do_test progress-1.0.1 { |
| 48 | db progress |
| 49 | } {::namespace inscope :: {incr counter} ; expr 0} |
| 50 | do_test progress-1.0.2 { |
| 51 | set v [catch {db progress xyz bogus} msg] |
| 52 | lappend v $msg |
| 53 | } {1 {expected integer but got "xyz"}} |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 54 | |
| 55 | # Test that the query is abandoned when the progress callback returns non-zero |
| 56 | do_test progress1.1 { |
| 57 | set counter 0 |
| 58 | db progress 1 "[namespace code {incr counter}] ; expr 1" |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 59 | set rc [catch {execsql { |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 60 | SELECT * FROM t1 |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 61 | }}] |
| 62 | list $counter $rc |
| 63 | } {1 1} |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 64 | |
| 65 | # Test that the query is rolled back when the progress callback returns |
| 66 | # non-zero. |
| 67 | do_test progress1.2 { |
| 68 | |
| 69 | # This figures out how many opcodes it takes to copy 5 extra rows into t1. |
| 70 | db progress 1 "[namespace code {incr five_rows}] ; expr 0" |
| 71 | set five_rows 0 |
| 72 | execsql { |
| 73 | INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 6 |
| 74 | } |
| 75 | db progress 0 "" |
| 76 | execsql { |
| 77 | DELETE FROM t1 WHERE a > 10 |
| 78 | } |
| 79 | |
| 80 | # Now set up the progress callback to abandon the query after the number of |
| 81 | # opcodes to copy 5 rows. That way, when we try to copy 6 rows, we know |
| 82 | # some data will have been inserted into the table by the time the progress |
| 83 | # callback abandons the query. |
| 84 | db progress $five_rows "expr 1" |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 85 | catchsql { |
| 86 | INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 9 |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 87 | } |
| 88 | execsql { |
| 89 | SELECT count(*) FROM t1 |
| 90 | } |
| 91 | } 10 |
| 92 | |
| 93 | # Test that an active transaction remains active and not rolled back after the |
| 94 | # progress query abandons a query. |
| 95 | do_test progress1.3 { |
| 96 | |
| 97 | db progress 0 "" |
| 98 | execsql BEGIN |
| 99 | execsql { |
| 100 | INSERT INTO t1 VALUES(11) |
| 101 | } |
| 102 | db progress 1 "expr 1" |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 103 | catchsql { |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 104 | INSERT INTO t1 VALUES(12) |
| 105 | } |
| 106 | db progress 0 "" |
| 107 | execsql COMMIT |
| 108 | execsql { |
| 109 | SELECT count(*) FROM t1 |
| 110 | } |
| 111 | } 11 |
| 112 | |
| 113 | # Check that a value of 0 for N means no progress callback |
| 114 | do_test progress1.4 { |
| 115 | set counter 0 |
| 116 | db progress 0 "[namespace code {incr counter}] ; expr 0" |
| 117 | execsql { |
| 118 | SELECT * FROM t1; |
| 119 | } |
| 120 | set counter |
| 121 | } 0 |
| 122 | |
| 123 | db progress 0 "" |
| 124 | |
| 125 | finish_test |