danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 1 | # 2002 March 6 |
| 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. |
| 12 | # |
| 13 | # This file implements tests for the PRAGMA command. |
| 14 | # |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 15 | # $Id: pragma2.test,v 1.4 2007/10/09 08:29:33 danielk1977 Exp $ |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Test organization: |
| 21 | # |
| 22 | # pragma2-1.*: Test freelist_count pragma on the main database. |
| 23 | # pragma2-2.*: Test freelist_count pragma on an attached database. |
| 24 | # pragma2-3.*: Test trying to write to the freelist_count is a no-op. |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 25 | # pragma2-4.*: Tests for PRAGMA cache_spill |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 26 | # |
| 27 | |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 28 | ifcapable !pragma||!schema_pragmas { |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 29 | finish_test |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | # Delete the preexisting database to avoid the special setup |
| 34 | # that the "all.test" script does. |
| 35 | # |
| 36 | db close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 37 | delete_file test.db test.db-journal |
| 38 | delete_file test3.db test3.db-journal |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 39 | sqlite3 db test.db; set DB [sqlite3_connection_pointer db] |
drh | 5db8281 | 2007-06-27 10:20:00 +0000 | [diff] [blame] | 40 | db eval {PRAGMA auto_vacuum=0} |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 41 | |
| 42 | do_test pragma2-1.1 { |
| 43 | execsql { |
| 44 | PRAGMA freelist_count; |
| 45 | } |
| 46 | } {0} |
| 47 | do_test pragma2-1.2 { |
| 48 | execsql { |
| 49 | CREATE TABLE abc(a, b, c); |
| 50 | PRAGMA freelist_count; |
| 51 | } |
| 52 | } {0} |
| 53 | do_test pragma2-1.3 { |
| 54 | execsql { |
| 55 | DROP TABLE abc; |
| 56 | PRAGMA freelist_count; |
| 57 | } |
| 58 | } {1} |
| 59 | do_test pragma2-1.4 { |
| 60 | execsql { |
| 61 | PRAGMA main.freelist_count; |
| 62 | } |
| 63 | } {1} |
| 64 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 65 | forcedelete test2.db |
| 66 | forcedelete test2.db-journal |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 67 | |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 68 | ifcapable attach { |
| 69 | do_test pragma2-2.1 { |
| 70 | execsql { |
| 71 | ATTACH 'test2.db' AS aux; |
| 72 | PRAGMA aux.auto_vacuum=OFF; |
| 73 | PRAGMA aux.freelist_count; |
| 74 | } |
| 75 | } {0} |
| 76 | do_test pragma2-2.2 { |
| 77 | execsql { |
| 78 | CREATE TABLE aux.abc(a, b, c); |
| 79 | PRAGMA aux.freelist_count; |
| 80 | } |
| 81 | } {0} |
| 82 | do_test pragma2-2.3 { |
| 83 | set ::val [string repeat 0123456789 1000] |
| 84 | execsql { |
| 85 | INSERT INTO aux.abc VALUES(1, 2, $::val); |
| 86 | PRAGMA aux.freelist_count; |
| 87 | } |
| 88 | } {0} |
| 89 | do_test pragma2-2.4 { |
| 90 | expr {[file size test2.db] / 1024} |
| 91 | } {11} |
| 92 | do_test pragma2-2.5 { |
| 93 | execsql { |
| 94 | DELETE FROM aux.abc; |
| 95 | PRAGMA aux.freelist_count; |
| 96 | } |
| 97 | } {9} |
| 98 | |
| 99 | do_test pragma2-3.1 { |
| 100 | execsql { |
| 101 | PRAGMA aux.freelist_count; |
| 102 | PRAGMA main.freelist_count; |
| 103 | PRAGMA freelist_count; |
| 104 | } |
| 105 | } {9 1 1} |
| 106 | do_test pragma2-3.2 { |
| 107 | execsql { |
| 108 | PRAGMA freelist_count = 500; |
| 109 | PRAGMA freelist_count; |
| 110 | } |
| 111 | } {1 1} |
| 112 | do_test pragma2-3.3 { |
| 113 | execsql { |
| 114 | PRAGMA aux.freelist_count = 500; |
| 115 | PRAGMA aux.freelist_count; |
| 116 | } |
| 117 | } {9 9} |
| 118 | } |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 119 | |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 120 | # Default setting of PRAGMA cache_spill is always ON |
| 121 | # |
| 122 | db close |
| 123 | delete_file test.db test.db-journal |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 124 | delete_file test2.db test2.db-journal |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 125 | sqlite3 db test.db |
| 126 | do_execsql_test pragma2-4.1 { |
| 127 | PRAGMA cache_spill; |
| 128 | PRAGMA main.cache_spill; |
| 129 | PRAGMA temp.cache_spill; |
| 130 | } {1 1 1} |
| 131 | do_execsql_test pragma2-4.2 { |
| 132 | PRAGMA cache_spill=OFF; |
| 133 | PRAGMA cache_spill; |
| 134 | PRAGMA main.cache_spill; |
| 135 | PRAGMA temp.cache_spill; |
| 136 | } {0 0 0} |
| 137 | do_execsql_test pragma2-4.3 { |
| 138 | PRAGMA page_size=1024; |
| 139 | PRAGMA cache_size=50; |
| 140 | BEGIN; |
| 141 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d); |
| 142 | INSERT INTO t1 VALUES(1, randomblob(400), 1, randomblob(400)); |
| 143 | INSERT INTO t1 SELECT a+1, randomblob(400), a+1, randomblob(400) FROM t1; |
| 144 | INSERT INTO t1 SELECT a+2, randomblob(400), a+2, randomblob(400) FROM t1; |
| 145 | INSERT INTO t1 SELECT a+4, randomblob(400), a+4, randomblob(400) FROM t1; |
| 146 | INSERT INTO t1 SELECT a+8, randomblob(400), a+8, randomblob(400) FROM t1; |
| 147 | INSERT INTO t1 SELECT a+16, randomblob(400), a+16, randomblob(400) FROM t1; |
| 148 | INSERT INTO t1 SELECT a+32, randomblob(400), a+32, randomblob(400) FROM t1; |
| 149 | INSERT INTO t1 SELECT a+64, randomblob(400), a+64, randomblob(400) FROM t1; |
| 150 | COMMIT; |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 151 | ATTACH 'test2.db' AS aux1; |
| 152 | CREATE TABLE aux1.t2(a INTEGER PRIMARY KEY, b, c, d); |
| 153 | INSERT INTO t2 SELECT * FROM t1; |
| 154 | DETACH aux1; |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 155 | PRAGMA cache_spill=ON; |
| 156 | } {} |
dan | e704713 | 2013-08-19 18:37:18 +0000 | [diff] [blame] | 157 | sqlite3_release_memory |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 158 | do_test pragma2-4.4 { |
| 159 | db eval { |
| 160 | BEGIN; |
| 161 | UPDATE t1 SET c=c+1; |
| 162 | PRAGMA lock_status; |
| 163 | } |
| 164 | } {main exclusive temp unknown} ;# EXCLUSIVE lock due to cache spill |
| 165 | do_test pragma2-4.5 { |
| 166 | db eval { |
| 167 | COMMIT; |
| 168 | PRAGMA cache_spill=OFF; |
| 169 | BEGIN; |
| 170 | UPDATE t1 SET c=c-1; |
| 171 | PRAGMA lock_status; |
| 172 | } |
| 173 | } {main reserved temp unknown} ;# No cache spill, so no exclusive lock |
| 174 | |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 175 | # Verify that newly attached databases inherit the cache_spill=OFF |
| 176 | # setting. |
| 177 | # |
| 178 | do_execsql_test pragma2-4.6 { |
| 179 | COMMIT; |
| 180 | ATTACH 'test2.db' AS aux1; |
| 181 | PRAGMA aux1.cache_size=50; |
| 182 | BEGIN; |
| 183 | UPDATE t2 SET c=c+1; |
| 184 | PRAGMA lock_status; |
| 185 | } {main unlocked temp unknown aux1 reserved} |
| 186 | do_execsql_test pragma2-4.7 { |
| 187 | COMMIT; |
dan | e704713 | 2013-08-19 18:37:18 +0000 | [diff] [blame] | 188 | } |
| 189 | sqlite3_release_memory |
| 190 | do_execsql_test pragma2-4.8 { |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 191 | PRAGMA cache_spill=ON; -- Applies to all databases |
| 192 | BEGIN; |
| 193 | UPDATE t2 SET c=c-1; |
| 194 | PRAGMA lock_status; |
| 195 | } {main unlocked temp unknown aux1 exclusive} |
| 196 | |
| 197 | |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 198 | finish_test |