sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / test / incrblob4.test
| 1 | # 2012 March 23 |
| 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 | # |
| 12 | # |
| 13 | set testdir [file dirname $argv0] |
| 14 | source $testdir/tester.tcl |
| 15 | ifcapable {!incrblob} { finish_test ; return } |
| 16 | set testprefix incrblob4 |
| 17 | |
| 18 | proc create_t1 {} { |
| 19 | execsql { |
| 20 | PRAGMA page_size = 1024; |
| 21 | CREATE TABLE t1(k INTEGER PRIMARY KEY, v); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | proc populate_t1 {} { |
| 26 | set data [list a b c d e f g h i j k l m n o p q r s t u v w x y z] |
| 27 | foreach d $data { |
| 28 | set blob [string repeat $d 900] |
| 29 | execsql { INSERT INTO t1(v) VALUES($blob) } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | |
| 34 | do_test 1.1 { |
| 35 | create_t1 |
| 36 | populate_t1 |
| 37 | } {} |
| 38 | |
| 39 | do_test 1.2 { |
| 40 | set blob [db incrblob t1 v 5] |
| 41 | read $blob 10 |
| 42 | } {eeeeeeeeee} |
| 43 | |
| 44 | do_test 1.3 { |
| 45 | execsql { DELETE FROM t1 } |
| 46 | populate_t1 |
| 47 | } {} |
| 48 | |
| 49 | |
| 50 | |
| 51 | do_test 2.1 { |
| 52 | reset_db |
| 53 | create_t1 |
| 54 | populate_t1 |
| 55 | } {} |
| 56 | |
| 57 | do_test 2.2 { |
| 58 | set blob [db incrblob t1 v 10] |
| 59 | read $blob 10 |
| 60 | } {jjjjjjjjjj} |
| 61 | |
| 62 | do_test 2.3 { |
| 63 | set new [string repeat % 900] |
| 64 | execsql { DELETE FROM t1 WHERE k=10 } |
| 65 | execsql { DELETE FROM t1 WHERE k=9 } |
| 66 | execsql { INSERT INTO t1(v) VALUES($new) } |
| 67 | } {} |
| 68 | |
| 69 | |
| 70 | |
| 71 | do_test 3.1 { |
| 72 | reset_db |
| 73 | create_t1 |
| 74 | populate_t1 |
| 75 | } {} |
| 76 | |
| 77 | do_test 3.2 { |
| 78 | set blob [db incrblob t1 v 20] |
| 79 | read $blob 10 |
| 80 | } {tttttttttt} |
| 81 | |
| 82 | do_test 3.3 { |
| 83 | set new [string repeat % 900] |
| 84 | execsql { UPDATE t1 SET v = $new WHERE k = 20 } |
| 85 | execsql { DELETE FROM t1 WHERE k=19 } |
| 86 | execsql { INSERT INTO t1(v) VALUES($new) } |
| 87 | } {} |
| 88 | |
| 89 | #------------------------------------------------------------------------- |
| 90 | # Test that it is not possible to DROP a table with an incremental blob |
| 91 | # cursor open on it. |
| 92 | # |
| 93 | do_execsql_test 4.1 { |
| 94 | CREATE TABLE t2(a INTEGER PRIMARY KEY, b); |
| 95 | INSERT INTO t2 VALUES(456, '0123456789'); |
| 96 | } |
| 97 | do_test 4.2 { |
| 98 | set blob [db incrblob -readonly t2 b 456] |
| 99 | read $blob 5 |
| 100 | } {01234} |
| 101 | do_catchsql_test 4.3 { |
| 102 | DROP TABLE t2 |
| 103 | } {1 {database table is locked}} |
| 104 | do_test 4.4 { |
| 105 | sqlite3_extended_errcode db |
| 106 | } {SQLITE_LOCKED} |
| 107 | close $blob |
| 108 | |
| 109 | #------------------------------------------------------------------------- |
| 110 | |
| 111 | ifcapable preupdate { |
| 112 | |
| 113 | reset_db |
| 114 | do_execsql_test 5.1 { |
| 115 | CREATE TABLE t2(a INTEGER PRIMARY KEY, b); |
| 116 | INSERT INTO t2 VALUES(1000, 'abcdefghijklmnopqrstuvwxyz'); |
| 117 | INSERT INTO t2 VALUES(2000, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); |
| 118 | INSERT INTO t2 VALUES(3000, 'abcdefghijklmnopqrstuvwxyz'); |
| 119 | } |
| 120 | |
| 121 | do_test 5.2.1 { |
| 122 | execsql BEGIN |
| 123 | set blob [db incrblob t2 b 2000] |
| 124 | seek $blob 0 |
| 125 | puts -nonewline $blob "hello " |
| 126 | flush $blob |
| 127 | execsql ROLLBACK |
| 128 | } {} |
| 129 | |
| 130 | do_test 5.2.2 { |
| 131 | puts -nonewline $blob "world" |
| 132 | set rc [catch { flush $blob } msg] |
| 133 | list $rc [regsub {input/output} $msg {I/O}] |
| 134 | } "1 {error flushing \"$blob\": I/O error}" |
| 135 | catch { close $blob } |
| 136 | |
| 137 | set preupdate_count 0 |
| 138 | proc preupdate {args} { incr ::preupdate_count ; return {} } |
| 139 | db preupdate hook preupdate |
| 140 | |
| 141 | set preupdate_count 0 |
| 142 | do_test 5.3.1 { |
| 143 | execsql BEGIN |
| 144 | set blob [db incrblob t2 b 1000] |
| 145 | seek $blob 0 |
| 146 | puts -nonewline $blob "hello " |
| 147 | flush $blob |
| 148 | execsql ROLLBACK |
| 149 | } {} |
| 150 | |
| 151 | do_test 5.3.2 { |
| 152 | puts -nonewline $blob "world" |
| 153 | set rc [catch { flush $blob } msg] |
| 154 | list $rc [regsub {input/output} $msg {I/O}] |
| 155 | } "1 {error flushing \"$blob\": I/O error}" |
| 156 | catch { close $blob } |
| 157 | |
| 158 | do_test 5.3.3 { |
| 159 | set ::preupdate_count |
| 160 | } {1} |
| 161 | |
| 162 | set preupdate_count 0 |
| 163 | do_test 5.4.1 { |
| 164 | execsql BEGIN |
| 165 | set blob [db incrblob t2 b 1000] |
| 166 | seek $blob 0 |
| 167 | puts -nonewline $blob "hello " |
| 168 | flush $blob |
| 169 | execsql { DELETE FROM t2 WHERE a=3000; } |
| 170 | } {} |
| 171 | |
| 172 | do_test 5.4.2 { |
| 173 | puts -nonewline $blob "world" |
| 174 | list [catch { flush $blob } msg] $msg |
| 175 | } "0 {}" |
| 176 | catch { close $blob } |
| 177 | catchsql { ROLLBACK } |
| 178 | |
| 179 | do_test 5.3.3 { |
| 180 | set ::preupdate_count |
| 181 | } {3} |
| 182 | |
| 183 | set preupdate_count 0 |
| 184 | do_test 5.4.3 { |
| 185 | execsql BEGIN |
| 186 | set blob [db incrblob t2 b 2000] |
| 187 | seek $blob 0 |
| 188 | puts -nonewline $blob "hello " |
| 189 | flush $blob |
| 190 | execsql { UPDATE t2 SET b='abcdefghijklmnopqrstuvwxyz' WHERE a=2000 } |
| 191 | } {} |
| 192 | |
| 193 | do_test 5.4.4 { |
| 194 | puts -nonewline $blob "world" |
| 195 | set rc [catch { flush $blob } msg] |
| 196 | list $rc [regsub {input/output} $msg {I/O}] |
| 197 | } "1 {error flushing \"$blob\": I/O error}" |
| 198 | catch { close $blob } |
| 199 | catchsql { ROLLBACK } |
| 200 | |
| 201 | do_test 5.4.5 { |
| 202 | set ::preupdate_count |
| 203 | } {2} |
| 204 | |
| 205 | } |
| 206 | |
| 207 | finish_test |