errmsg.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. # 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.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. set testprefix errmsg
  19. # Test organization:
  20. #
  21. # errmsg-1.* User-defined SQL function errors
  22. # errmsg-2.* Errors generated by the VDBE (constraint failures etc.)
  23. # errmsg-3.* SQLITE_SCHEMA and statement recompilation errors.
  24. #
  25. proc error_messages_worker {prepare sql schema} {
  26. set ret [list]
  27. set stmt [$prepare db $sql -1 dummy]
  28. execsql $schema
  29. lappend ret [sqlite3_step $stmt]
  30. lappend ret [sqlite3_errmsg db]
  31. lappend ret [sqlite3_finalize $stmt]
  32. lappend ret [sqlite3_errmsg db]
  33. set ret
  34. }
  35. proc error_messages_v2 {sql {schema {}}} {
  36. error_messages_worker sqlite3_prepare_v2 $sql $schema
  37. }
  38. proc error_messages {sql {schema {}}} {
  39. error_messages_worker sqlite3_prepare $sql $schema
  40. }
  41. proc sql_error {msg} { error $msg }
  42. db func sql_error sql_error
  43. #-------------------------------------------------------------------------
  44. # Test error messages returned by user-defined SQL functions.
  45. #
  46. do_test 1.1 {
  47. error_messages "SELECT sql_error('custom message')"
  48. } [list {*}{
  49. SQLITE_ERROR {SQL logic error or missing database}
  50. SQLITE_ERROR {custom message}
  51. }]
  52. do_test 1.2 {
  53. error_messages_v2 "SELECT sql_error('custom message')"
  54. } [list {*}{
  55. SQLITE_ERROR {custom message}
  56. SQLITE_ERROR {custom message}
  57. }]
  58. #-------------------------------------------------------------------------
  59. # Test error messages generated directly by VDBE code (e.g. constraint
  60. # failures).
  61. #
  62. do_execsql_test 2.1 {
  63. CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
  64. INSERT INTO t1 VALUES('abc', 'def');
  65. }
  66. do_test 2.2 {
  67. error_messages "INSERT INTO t1 VALUES('ghi', 'def')"
  68. } [list {*}{
  69. SQLITE_ERROR {SQL logic error or missing database}
  70. SQLITE_CONSTRAINT {column b is not unique}
  71. }]
  72. verify_ex_errcode 2.2b SQLITE_CONSTRAINT_UNIQUE
  73. do_test 2.3 {
  74. error_messages_v2 "INSERT INTO t1 VALUES('ghi', 'def')"
  75. } [list {*}{
  76. SQLITE_CONSTRAINT {column b is not unique}
  77. SQLITE_CONSTRAINT {column b is not unique}
  78. }]
  79. verify_ex_errcode 2.3b SQLITE_CONSTRAINT_UNIQUE
  80. #-------------------------------------------------------------------------
  81. # Test SQLITE_SCHEMA errors. And, for _v2(), test that if the schema
  82. # change invalidates the SQL statement itself the error message is returned
  83. # correctly.
  84. #
  85. do_execsql_test 3.1.1 {
  86. CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
  87. INSERT INTO t2 VALUES('abc', 'def');
  88. }
  89. do_test 3.1.2 {
  90. error_messages "SELECT a FROM t2" "DROP TABLE t2"
  91. } [list {*}{
  92. SQLITE_ERROR {SQL logic error or missing database}
  93. SQLITE_SCHEMA {database schema has changed}
  94. }]
  95. do_execsql_test 3.2.1 {
  96. CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
  97. INSERT INTO t2 VALUES('abc', 'def');
  98. }
  99. do_test 3.2.2 {
  100. error_messages_v2 "SELECT a FROM t2" "DROP TABLE t2"
  101. } [list {*}{
  102. SQLITE_ERROR {no such table: t2}
  103. SQLITE_ERROR {no such table: t2}
  104. }]
  105. finish_test