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