Update tests in returning1.test to account for [c7896e88].
[sqlite.git] / test / incrblob3.test
bloba9097e01a5cd490ac2eef3ebd6d2603193be97db
1 # 2010 October 20
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix incrblob3
18 ifcapable !incrblob {
19   finish_test
20   return
23 sqlite3 db test.db
24 sqlite3_db_config_lookaside db 0 0 0
26 do_execsql_test incrblob3-1.1 {
27   CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
28   INSERT INTO blobs VALUES(1, zeroblob(100));
29   INSERT INTO blobs VALUES(2, zeroblob(100));
30 } {}
32 # Test the sqlite3_blob_reopen()/read()/write() functions.
34 do_test incrblob3-1.2 {
35   set ::blob [db incrblob blobs v 1]
36   puts $::blob "hello world"
37 } {}
39 do_test incrblob3-1.3 {
40   sqlite3_blob_reopen $::blob 2
41   puts $::blob "world hello"
42 } {}
44 do_test incrblob3-1.4 {
45   sqlite3_blob_reopen $::blob 1
46   gets $::blob
47 } {hello world}
49 do_test incrblob3-1.5 {
50   sqlite3_blob_reopen $::blob 2
51   gets $::blob
52 } {world hello}
54 do_test incrblob3-1.6 { close $::blob } {}
56 # Test some error conditions.
58 #   incrblob3-2.1: Attempting to reopen a row that does not exist.
59 #   incrblob3-2.2: Attempting to reopen a row that does not contain a blob
60 #                  or text value.
62 do_test incrblob3-2.1.1 {
63   set ::blob [db incrblob blobs v 1]
64   list [catch {sqlite3_blob_reopen $::blob 3} msg] $msg
65 } {1 SQLITE_ERROR}
66 do_test incrblob3-2.1.2 {
67   list [sqlite3_errcode db] [sqlite3_errmsg db]
68 } {SQLITE_ERROR {no such rowid: 3}}
69 do_test incrblob3-2.1.3 {
70   list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
71 } {1 SQLITE_ABORT}
72 do_test incrblob3-2.1.4 { close $::blob } {}
74 do_execsql_test incrblob3-2.2.1 {
75   INSERT INTO blobs VALUES(3, 42);
76   INSERT INTO blobs VALUES(4, 54.4);
77   INSERT INTO blobs VALUES(5, NULL);
79 foreach {tn rowid type} {
80   1 3 integer
81   2 4 real
82   3 5 null
83 } {
84   do_test incrblob3-2.2.$tn.1 {
85     set ::blob [db incrblob blobs v 1]
86     list [catch {sqlite3_blob_reopen $::blob $rowid} msg] $msg
87   } {1 SQLITE_ERROR}
88   do_test incrblob3-2.2.$tn.2 {
89     list [sqlite3_errcode db] [sqlite3_errmsg db]
90   } "SQLITE_ERROR {cannot open value of type $type}"
92   do_test incrblob3-2.2.$tn.3 {
93     list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
94   } {1 SQLITE_ABORT}
95   do_test incrblob3-2.2.$tn.4 {
96     list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg
97   } {1 SQLITE_ABORT}
98   do_test incrblob3-2.2.$tn.5 {
99     list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg
100   } {1 SQLITE_ABORT}
101   do_test incrblob3-2.2.$tn.6 {
102     sqlite3_blob_bytes $::blob
103   } {0}
105   do_test incrblob3-2.2.$tn.7 { close $::blob } {}
108 # Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE.
110 #   incrblob3-3.1: sqlite3_blob_reopen()
111 #   incrblob3-3.2: sqlite3_blob_read()
112 #   incrblob3-3.3: sqlite3_blob_write()
113 #   incrblob3-3.4: sqlite3_blob_bytes()
115 do_test incrblob3-3.1 {
116   list [catch {sqlite3_blob_reopen {} 3} msg] $msg
117 } {1 SQLITE_MISUSE}
119 do_test incrblob3-3.2 {
120   list [catch {sqlite3_blob_read {} 0 10} msg] $msg
121 } {1 SQLITE_MISUSE}
123 do_test incrblob3-3.3 {
124   list [catch {sqlite3_blob_write {} 0 "abcd"} msg] $msg
125 } {1 SQLITE_MISUSE}
127 do_test incrblob3-3.4 { sqlite3_blob_bytes {} } {0}
129 do_test incrblob3-3.5 { sqlite3_blob_close {} } {}
131 # Test out-of-range reading and writing
133 do_test incrblob3-4.1 {
134   set ::blob [db incrblob blobs v 1]
135   sqlite3_blob_bytes $::blob
136 } {100}
137 do_test incrblob3-4.2 {
138   list [catch { sqlite3_blob_read $::blob -1 10 } msg] $msg
139 } {1 SQLITE_ERROR}
140 do_test incrblob3-4.3 {
141   list [catch { sqlite3_blob_read $::blob 0 -10 } msg] $msg
142 } {1 SQLITE_ERROR}
143 do_test incrblob3-4.4 {
144   list [catch { sqlite3_blob_read $::blob 95 10 } msg] $msg
145 } {1 SQLITE_ERROR}
146 do_test incrblob3-4.5 {
147   list [catch { sqlite3_blob_write $::blob -1 "abcdefghij" 10 } msg] $msg
148 } {1 SQLITE_ERROR}
149 do_test incrblob3-4.6 {
150   list [catch { sqlite3_blob_write $::blob 0 "abcdefghij" -10 } msg] $msg
151 } {1 SQLITE_ERROR}
152 do_test incrblob3-4.7 {
153   list [catch { sqlite3_blob_write $::blob 95 "abcdefghij" } msg] $msg
154 } {1 SQLITE_ERROR}
156 do_test incrblob3-4.8 { close $::blob } {}
158 # Test that modifying the row a blob handle points to aborts the blob.
160 do_test incrblob3-5.1 {
161   set ::blob [db incrblob blobs v 1]
162   sqlite3_blob_bytes $::blob
163 } {100}
164 do_test incrblob3-5.2 {
165   execsql { UPDATE blobs SET v = '123456789012345678901234567890' WHERE k = 1 }
166   list [catch { sqlite3_blob_read $::blob 0 10 } msg] $msg
167 } {1 SQLITE_ABORT}
169 # Test various errors that can occur in sqlite3_blob_open():
171 #   1. Trying to open a virtual table column.
172 #   2. Trying to open a view column.
173 #   3. Trying to open a column that does not exist.
174 #   4. Trying to open a read/write handle on an indexed column.
175 #   5. Trying to open a read/write handle on the child key of an FK constraint.
177 ifcapable fts3 {
178   do_test incrblob3-6.1 {
179     execsql {
180       CREATE VIRTUAL TABLE ft USING fts3;
181       INSERT INTO ft VALUES('rules to open a column to which');
182     }
184     list [catch { db incrblob ft content 1 } msg] $msg
185   } {1 {cannot open virtual table: ft}}
187 ifcapable view {
188   do_test incrblob3-6.2 {
189     execsql { CREATE VIEW v1 AS SELECT * FROM blobs }
190     list [catch { db incrblob v1 content 1 } msg] $msg
191   } {1 {cannot open view: v1}}
194 do_test incrblob3-6.3 {
195   list [catch { db incrblob blobs content 1 } msg] $msg
196 } {1 {no such column: "content"}}
198 do_test incrblob3-6.4.1 {
199   execsql { 
200     CREATE TABLE t1(a, b);
201     CREATE INDEX i1 ON t1(b);
202     INSERT INTO t1 VALUES(zeroblob(100), zeroblob(100));
203   }
204   list [catch { db incrblob t1 b 1 } msg] $msg
205 } {1 {cannot open indexed column for writing}}
206 do_test incrblob3-6.4.2 {
207   set ::blob [db incrblob t1 a 1]
208   close $::blob
209 } {}
210 do_test incrblob3-6.4.3 {
211   set ::blob [db incrblob -readonly t1 b 1]
212   close $::blob
213 } {}
215 do_test incrblob3-6.5.1 {
216   execsql { 
217     CREATE TABLE p1(a PRIMARY KEY);
218     CREATE TABLE c1(a, b REFERENCES p1);
219     PRAGMA foreign_keys = 1;
220     INSERT INTO p1 VALUES(zeroblob(100));
221     INSERT INTO c1 VALUES(zeroblob(100), zeroblob(100));
222   }
223   list [catch { db incrblob c1 b 1 } msg] $msg
224 } {1 {cannot open foreign key column for writing}}
226 do_test incrblob3-6.5.2 {
227   set ::blob [db incrblob c1 a 1]
228   close $::blob
229 } {}
230 do_test incrblob3-6.5.3 {
231   set ::blob [db incrblob -readonly c1 b 1]
232   close $::blob
233 } {}
234 do_test incrblob3-6.5.4 {
235   execsql { PRAGMA foreign_keys = 0 }
236   set ::blob [db incrblob c1 b 1]
237   close $::blob
238 } {}
241 # Test that sqlite3_blob_open() handles transient and persistent schema 
242 # errors correctly.
244 do_test incrblob3-7.1 {
245   sqlite3 db2 test.db
246   sqlite3_db_config_lookaside db2 0 0 0
247   execsql { CREATE TABLE t2(x) } db2
248   set ::blob [db incrblob blobs v 1]
249   close $::blob
250 } {}
251 db2 close
253 testvfs tvfs -default 1
254 tvfs filter xAccess
255 tvfs script access_method
257 proc access_method {args} {
258   set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
259   incr schemacookie
260   hexio_write test.db 40 [hexio_render_int32 $schemacookie]
262   set dbversion [hexio_get_int [hexio_read test.db 24 4]]
263   incr dbversion
264   hexio_write test.db 24 [hexio_render_int32 $dbversion]
266   return ""
269 do_test incrblob3-7.2 {
270   sqlite3 db test.db 
271   sqlite3_db_config_lookaside db 0 0 0
272   list [catch {db incrblob blobs v 1} msg] $msg
273 } {1 {database schema has changed}}
274 db close
275 tvfs delete
277 #-------------------------------------------------------------------------
279 reset_db
280 forcedelete test.db2
281 do_execsql_test 8.1 {
282   CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
283   ATTACH 'test.db2' AS aux;
284   CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);
286   INSERT INTO t1 VALUES(4, 'hello');
287   INSERT INTO aux.t1 VALUES(4, 'world');
290 do_test 8.2 {
291   set ::blob [db incrblob -readonly main t1 b 4]
292   read $::blob
293 } {hello}
294 close $::blob
296 do_test 8.3 {
297   set ::blob [db incrblob -readonly aux t1 b 4]
298   read $::blob
299 } {world}
300 close $::blob
302 do_test 8.4 {
303   set ::blob [db incrblob -readonly t1 b 4]
304   read $::blob
305 } {hello}
306 close $::blob
308 do_test 8.5 {
309   list [catch { db incrblob -readonly nosuchdb t1 b 4 } msg] $msg
310 } {1 {no such table: nosuchdb.t1}}
313 db close
314 finish_test