OLD | NEW |
1 # 2003 April 4 | 1 # 2003 April 4 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # This file implements regression tests for SQLite library. The | 11 # This file implements regression tests for SQLite library. The |
12 # focus of this script is testing the ATTACH and DETACH commands | 12 # focus of this script is testing the ATTACH and DETACH commands |
13 # and related functionality. | 13 # and related functionality. |
14 # | 14 # |
15 # $Id: attach.test,v 1.52 2009/05/29 14:39:08 drh Exp $ | 15 # $Id: attach.test,v 1.52 2009/05/29 14:39:08 drh Exp $ |
16 # | 16 # |
17 | 17 |
18 set testdir [file dirname $argv0] | 18 set testdir [file dirname $argv0] |
19 source $testdir/tester.tcl | 19 source $testdir/tester.tcl |
20 | 20 |
21 ifcapable !attach { | 21 ifcapable !attach { |
22 finish_test | 22 finish_test |
23 return | 23 return |
24 } | 24 } |
25 | 25 |
26 for {set i 2} {$i<=15} {incr i} { | 26 for {set i 2} {$i<=15} {incr i} { |
27 file delete -force test$i.db | 27 forcedelete test$i.db |
28 file delete -force test$i.db-journal | 28 forcedelete test$i.db-journal |
29 } | 29 } |
30 | 30 |
31 do_test attach-1.1 { | 31 do_test attach-1.1 { |
32 execsql { | 32 execsql { |
33 CREATE TABLE t1(a,b); | 33 CREATE TABLE t1(a,b); |
34 INSERT INTO t1 VALUES(1,2); | 34 INSERT INTO t1 VALUES(1,2); |
35 INSERT INTO t1 VALUES(3,4); | 35 INSERT INTO t1 VALUES(3,4); |
36 SELECT * FROM t1; | 36 SELECT * FROM t1; |
37 } | 37 } |
38 } {1 2 3 4} | 38 } {1 2 3 4} |
39 do_test attach-1.2 { | 39 do_test attach-1.2 { |
40 sqlite3 db2 test2.db | 40 sqlite3 db2 test2.db |
41 execsql { | 41 execsql { |
42 CREATE TABLE t2(x,y); | 42 CREATE TABLE t2(x,y); |
43 INSERT INTO t2 VALUES(1,'x'); | 43 INSERT INTO t2 VALUES(1,'x'); |
44 INSERT INTO t2 VALUES(2,'y'); | 44 INSERT INTO t2 VALUES(2,'y'); |
45 SELECT * FROM t2; | 45 SELECT * FROM t2; |
46 } db2 | 46 } db2 |
47 } {1 x 2 y} | 47 } {1 x 2 y} |
48 do_test attach-1.3 { | 48 do_test attach-1.3 { |
49 execsql { | 49 execsql { |
50 ATTACH DATABASE 'test2.db' AS two; | 50 ATTACH DATABASE 'test2.db' AS two; |
51 SELECT * FROM two.t2; | 51 SELECT * FROM two.t2; |
52 } | 52 } |
53 } {1 x 2 y} | 53 } {1 x 2 y} |
| 54 |
| 55 # Tests for the sqlite3_db_filename interface |
| 56 # |
| 57 do_test attach-1.3.1 { |
| 58 file tail [sqlite3_db_filename db main] |
| 59 } {test.db} |
| 60 do_test attach-1.3.2 { |
| 61 file tail [sqlite3_db_filename db MAIN] |
| 62 } {test.db} |
| 63 do_test attach-1.3.3 { |
| 64 file tail [sqlite3_db_filename db temp] |
| 65 } {} |
| 66 do_test attach-1.3.4 { |
| 67 file tail [sqlite3_db_filename db two] |
| 68 } {test2.db} |
| 69 do_test attach-1.3.5 { |
| 70 file tail [sqlite3_db_filename db three] |
| 71 } {} |
| 72 |
54 do_test attach-1.4 { | 73 do_test attach-1.4 { |
55 execsql { | 74 execsql { |
56 SELECT * FROM t2; | 75 SELECT * FROM t2; |
57 } | 76 } |
58 } {1 x 2 y} | 77 } {1 x 2 y} |
59 do_test attach-1.5 { | 78 do_test attach-1.5 { |
60 execsql { | 79 execsql { |
61 DETACH DATABASE two; | 80 DETACH DATABASE two; |
62 SELECT * FROM t1; | 81 SELECT * FROM t1; |
63 } | 82 } |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 } {910 1112 1516} | 640 } {910 1112 1516} |
622 } ;# ifcapable view | 641 } ;# ifcapable view |
623 | 642 |
624 # Tests for the sqliteFix...() routines in attach.c | 643 # Tests for the sqliteFix...() routines in attach.c |
625 # | 644 # |
626 ifcapable {trigger} { | 645 ifcapable {trigger} { |
627 do_test attach-5.1 { | 646 do_test attach-5.1 { |
628 db close | 647 db close |
629 sqlite3 db test.db | 648 sqlite3 db test.db |
630 db2 close | 649 db2 close |
631 file delete -force test2.db | 650 forcedelete test2.db |
632 sqlite3 db2 test2.db | 651 sqlite3 db2 test2.db |
633 catchsql { | 652 catchsql { |
634 ATTACH DATABASE 'test.db' AS orig; | 653 ATTACH DATABASE 'test.db' AS orig; |
635 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN | 654 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN |
636 SELECT 'no-op'; | 655 SELECT 'no-op'; |
637 END; | 656 END; |
638 } db2 | 657 } db2 |
639 } {1 {trigger r1 cannot reference objects in database orig}} | 658 } {1 {trigger r1 cannot reference objects in database orig}} |
640 do_test attach-5.2 { | 659 do_test attach-5.2 { |
641 catchsql { | 660 catchsql { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 } | 737 } |
719 } {0 {}} | 738 } {0 {}} |
720 if {$tcl_platform(platform)=="unix"} { | 739 if {$tcl_platform(platform)=="unix"} { |
721 do_test attach-6.2 { | 740 do_test attach-6.2 { |
722 sqlite3 dbx cannot-read | 741 sqlite3 dbx cannot-read |
723 dbx eval {CREATE TABLE t1(a,b,c)} | 742 dbx eval {CREATE TABLE t1(a,b,c)} |
724 dbx close | 743 dbx close |
725 file attributes cannot-read -permission 0000 | 744 file attributes cannot-read -permission 0000 |
726 if {[file writable cannot-read]} { | 745 if {[file writable cannot-read]} { |
727 puts "\n**** Tests do not work when run as root ****" | 746 puts "\n**** Tests do not work when run as root ****" |
728 file delete -force cannot-read | 747 forcedelete cannot-read |
729 exit 1 | 748 exit 1 |
730 } | 749 } |
731 catchsql { | 750 catchsql { |
732 ATTACH DATABASE 'cannot-read' AS noread; | 751 ATTACH DATABASE 'cannot-read' AS noread; |
733 } | 752 } |
734 } {1 {unable to open database: cannot-read}} | 753 } {1 {unable to open database: cannot-read}} |
735 do_test attach-6.2.2 { | 754 do_test attach-6.2.2 { |
736 db errorcode | 755 db errorcode |
737 } {14} | 756 } {14} |
738 file delete -force cannot-read | 757 forcedelete cannot-read |
739 } | 758 } |
740 | 759 |
741 # Check the error message if we try to access a database that has | 760 # Check the error message if we try to access a database that has |
742 # not been attached. | 761 # not been attached. |
743 do_test attach-6.3 { | 762 do_test attach-6.3 { |
744 catchsql { | 763 catchsql { |
745 CREATE TABLE no_such_db.t1(a, b, c); | 764 CREATE TABLE no_such_db.t1(a, b, c); |
746 } | 765 } |
747 } {1 {unknown database no_such_db}} | 766 } {1 {unknown database no_such_db}} |
748 for {set i 2} {$i<=15} {incr i} { | 767 for {set i 2} {$i<=15} {incr i} { |
749 catch {db$i close} | 768 catch {db$i close} |
750 } | 769 } |
751 db close | 770 db close |
752 file delete -force test2.db | 771 forcedelete test2.db |
753 file delete -force no-such-file | 772 forcedelete no-such-file |
754 | 773 |
755 ifcapable subquery { | 774 ifcapable subquery { |
756 do_test attach-7.1 { | 775 do_test attach-7.1 { |
757 file delete -force test.db test.db-journal | 776 forcedelete test.db test.db-journal |
758 sqlite3 db test.db | 777 sqlite3 db test.db |
759 catchsql { | 778 catchsql { |
760 DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY | 779 DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY |
761 REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL ) | 780 REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL ) |
762 } | 781 } |
763 } {1 {no such table: AAAAAA}} | 782 } {1 {no such table: AAAAAA}} |
764 } | 783 } |
765 | 784 |
766 # Create a malformed file (a file that is not a valid database) | 785 # Create a malformed file (a file that is not a valid database) |
767 # and try to attach it | 786 # and try to attach it |
768 # | 787 # |
769 do_test attach-8.1 { | 788 do_test attach-8.1 { |
770 set fd [open test2.db w] | 789 set fd [open test2.db w] |
771 puts $fd "This file is not a valid SQLite database" | 790 puts $fd "This file is not a valid SQLite database" |
772 close $fd | 791 close $fd |
773 catchsql { | 792 catchsql { |
774 ATTACH 'test2.db' AS t2; | 793 ATTACH 'test2.db' AS t2; |
775 } | 794 } |
776 } {1 {file is encrypted or is not a database}} | 795 } {1 {file is encrypted or is not a database}} |
777 do_test attach-8.2 { | 796 do_test attach-8.2 { |
778 db errorcode | 797 db errorcode |
779 } {26} | 798 } {26} |
780 file delete -force test2.db | 799 forcedelete test2.db |
781 do_test attach-8.3 { | 800 do_test attach-8.3 { |
782 sqlite3 db2 test2.db | 801 sqlite3 db2 test2.db |
783 db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE} | 802 db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE} |
784 catchsql { | 803 catchsql { |
785 ATTACH 'test2.db' AS t2; | 804 ATTACH 'test2.db' AS t2; |
786 } | 805 } |
787 } {1 {database is locked}} | 806 } {1 {database is locked}} |
788 do_test attach-8.4 { | 807 do_test attach-8.4 { |
789 db errorcode | 808 db errorcode |
790 } {5} | 809 } {5} |
791 db2 close | 810 db2 close |
792 file delete -force test2.db | 811 forcedelete test2.db |
793 | 812 |
794 # Test that it is possible to attach the same database more than | 813 # Test that it is possible to attach the same database more than |
795 # once when not in shared-cache mode. That this is not possible in | 814 # once when not in shared-cache mode. That this is not possible in |
796 # shared-cache mode is tested in shared7.test. | 815 # shared-cache mode is tested in shared7.test. |
797 do_test attach-9.1 { | 816 do_test attach-9.1 { |
798 file delete -force test4.db | 817 forcedelete test4.db |
799 execsql { | 818 execsql { |
800 ATTACH 'test4.db' AS aux1; | 819 ATTACH 'test4.db' AS aux1; |
801 CREATE TABLE aux1.t1(a, b); | 820 CREATE TABLE aux1.t1(a, b); |
802 INSERT INTO aux1.t1 VALUES(1, 2); | 821 INSERT INTO aux1.t1 VALUES(1, 2); |
803 ATTACH 'test4.db' AS aux2; | 822 ATTACH 'test4.db' AS aux2; |
804 SELECT * FROM aux2.t1; | 823 SELECT * FROM aux2.t1; |
805 } | 824 } |
806 } {1 2} | 825 } {1 2} |
807 do_test attach-9.2 { | 826 do_test attach-9.2 { |
808 catchsql { | 827 catchsql { |
(...skipping 25 matching lines...) Expand all Loading... |
834 SELECT name FROM inmem.sqlite_master; | 853 SELECT name FROM inmem.sqlite_master; |
835 } | 854 } |
836 } {noname inmem} | 855 } {noname inmem} |
837 do_test attach-10.2 { | 856 do_test attach-10.2 { |
838 lrange [execsql { | 857 lrange [execsql { |
839 PRAGMA database_list; | 858 PRAGMA database_list; |
840 }] 9 end | 859 }] 9 end |
841 } {4 noname {} 5 inmem {}} | 860 } {4 noname {} 5 inmem {}} |
842 | 861 |
843 finish_test | 862 finish_test |
OLD | NEW |