vtab_alter.test 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # 2007 June 26
  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 ALTER TABLE ... RENAME TO
  13. # command on virtual tables.
  14. #
  15. # $Id: vtab_alter.test,v 1.3 2007/12/13 21:54:11 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable !vtab||!altertable {
  19. finish_test
  20. return
  21. }
  22. # Register the echo module.
  23. #
  24. # This test uses a special feature of the echo module. If the name
  25. # of the virtual table is a prefix of the name of the underlying
  26. # real table (for example if the v-table is "tbl" and the real table
  27. # is "tbl_base"), then the name of the real table is modified
  28. # when an "ALTER TABLE ... RENAME TO" is executed on the v-table.
  29. # For example:
  30. #
  31. # sqlite> CREATE TABLE t1_base(a, b, c);
  32. # sqlite> CREATE VIRTUAL TABLE t1 USING(t1_base);
  33. # sqlite> ALTER TABLE t1 RENAME TO t2;
  34. # sqlite> SELECT tbl_name FROM sqlite_master;
  35. # t2_base
  36. # t2
  37. #
  38. register_echo_module [sqlite3_connection_pointer db]
  39. # Try to rename an echo table. Make sure nothing terrible happens.
  40. #
  41. do_test vtab_alter-1.1 {
  42. execsql { CREATE TABLE t1(a, b VARCHAR, c INTEGER) }
  43. } {}
  44. do_test vtab_alter-1.2 {
  45. execsql { CREATE VIRTUAL TABLE t1echo USING echo(t1) }
  46. } {}
  47. do_test vtab_alter-1.3 {
  48. catchsql { SELECT * FROM t1echo }
  49. } {0 {}}
  50. do_test vtab_alter-1.4 {
  51. execsql { ALTER TABLE t1echo RENAME TO new }
  52. } {}
  53. do_test vtab_alter-1.5 {
  54. catchsql { SELECT * FROM t1echo }
  55. } {1 {no such table: t1echo}}
  56. do_test vtab_alter-1.6 {
  57. catchsql { SELECT * FROM new }
  58. } {0 {}}
  59. # Try to rename an echo table that renames its base table. Make
  60. # sure nothing terrible happens.
  61. #
  62. do_test vtab_alter-2.1 {
  63. execsql {
  64. DROP TABLE new;
  65. DROP TABLE t1;
  66. CREATE TABLE t1_base(a, b, c);
  67. CREATE VIRTUAL TABLE t1 USING echo('*_base');
  68. }
  69. } {}
  70. do_test vtab_alter-2.2 {
  71. execsql {
  72. INSERT INTO t1_base VALUES(1, 2, 3);
  73. SELECT * FROM t1;
  74. }
  75. } {1 2 3}
  76. do_test vtab_alter-2.3 {
  77. execsql { ALTER TABLE t1 RENAME TO x }
  78. } {}
  79. do_test vtab_alter-2.4 {
  80. execsql { SELECT * FROM x; }
  81. } {1 2 3}
  82. do_test vtab_alter-2.5 {
  83. execsql { SELECT * FROM x_base; }
  84. } {1 2 3}
  85. # Cause an error to occur when the echo module renames its
  86. # backing store table.
  87. #
  88. do_test vtab_alter-3.1 {
  89. execsql { CREATE TABLE y_base(a, b, c) }
  90. catchsql { ALTER TABLE x RENAME TO y }
  91. } {1 {SQL logic error or missing database}}
  92. do_test vtab_alter-3.2 {
  93. execsql { SELECT * FROM x }
  94. } {1 2 3}
  95. finish_test