Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / errmsg.test
blobf32da59a391d48243ebbf7ee01c3f7f1ae68b01c
1 # 2001 September 15
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 #***********************************************************************
11 # Test that if sqlite3_prepare_v2() is used to prepare a query, the
12 # error-message associated with an sqlite3_step() error is available
13 # immediately. Whereas if sqlite3_prepare() is used, it is not available
14 # until sqlite3_finalize() or sqlite3_reset() has been called.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 set testprefix errmsg
22 # Test organization:
24 #   errmsg-1.*         User-defined SQL function errors
25 #   errmsg-2.*         Errors generated by the VDBE (constraint failures etc.)
26 #   errmsg-3.*         SQLITE_SCHEMA and statement recompilation errors.
29 proc error_messages_worker {prepare sql schema} {
30   set ret [list]
32   set stmt [$prepare db $sql -1 dummy]
33   execsql $schema
34   lappend ret [sqlite3_step $stmt]
35   lappend ret [sqlite3_errmsg db]
36   lappend ret [sqlite3_finalize $stmt]
37   lappend ret [sqlite3_errmsg db]
39   set ret
42 proc error_messages_v2 {sql {schema {}}} {
43   error_messages_worker sqlite3_prepare_v2 $sql $schema
46 proc error_messages {sql {schema {}}} {
47   error_messages_worker sqlite3_prepare $sql $schema
50 proc sql_error {msg} { error $msg }
51 db func sql_error sql_error
53 #-------------------------------------------------------------------------
54 # Test error messages returned by user-defined SQL functions.
56 do_test 1.1 {
57   error_messages "SELECT sql_error('custom message')"
58 } [list {*}{
59     SQLITE_ERROR {SQL logic error} 
60     SQLITE_ERROR {custom message}
62 do_test 1.2 {
63   error_messages_v2 "SELECT sql_error('custom message')"
64 } [list {*}{
65     SQLITE_ERROR {custom message}
66     SQLITE_ERROR {custom message}
69 #-------------------------------------------------------------------------
70 # Test error messages generated directly by VDBE code (e.g. constraint
71 # failures).
73 do_execsql_test 2.1 {
74   CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
75   INSERT INTO t1 VALUES('abc', 'def');
77 do_test 2.2 {
78   error_messages "INSERT INTO t1 VALUES('ghi', 'def')"
79 } [list {*}{
80     SQLITE_ERROR      {SQL logic error} 
81     SQLITE_CONSTRAINT {UNIQUE constraint failed: t1.b}
83 verify_ex_errcode 2.2b SQLITE_CONSTRAINT_UNIQUE
84 do_test 2.3 {
85   error_messages_v2 "INSERT INTO t1 VALUES('ghi', 'def')"
86 } [list {*}{
87     SQLITE_CONSTRAINT {UNIQUE constraint failed: t1.b}
88     SQLITE_CONSTRAINT {UNIQUE constraint failed: t1.b}
90 verify_ex_errcode 2.3b SQLITE_CONSTRAINT_UNIQUE
92 #-------------------------------------------------------------------------
93 # Test SQLITE_SCHEMA errors. And, for _v2(), test that if the schema
94 # change invalidates the SQL statement itself the error message is returned
95 # correctly.
97 do_execsql_test 3.1.1 {
98   CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
99   INSERT INTO t2 VALUES('abc', 'def');
101 do_test 3.1.2 {
102   error_messages "SELECT a FROM t2" "DROP TABLE t2"
103 } [list {*}{
104     SQLITE_ERROR {SQL logic error} 
105     SQLITE_SCHEMA {database schema has changed}
107 do_execsql_test 3.2.1 {
108   CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
109   INSERT INTO t2 VALUES('abc', 'def');
111 do_test 3.2.2 {
112   error_messages_v2 "SELECT a FROM t2" "DROP TABLE t2"
113 } [list {*}{
114     SQLITE_ERROR {no such table: t2} 
115     SQLITE_ERROR {no such table: t2}
118 finish_test