1cd61c281Sdrh# 2002 March 6 2cd61c281Sdrh# 3cd61c281Sdrh# The author disclaims copyright to this source code. In place of 4cd61c281Sdrh# a legal notice, here is a blessing: 5cd61c281Sdrh# 6cd61c281Sdrh# May you do good and not evil. 7cd61c281Sdrh# May you find forgiveness for yourself and forgive others. 8cd61c281Sdrh# May you share freely, never taking more than you give. 9cd61c281Sdrh# 10cd61c281Sdrh#*********************************************************************** 11cd61c281Sdrh# This file implements regression tests for SQLite library. 12cd61c281Sdrh# 13cd61c281Sdrh# This file implements tests for the PRAGMA command. 14cd61c281Sdrh# 15*91cf71b0Sdanielk1977# $Id: pragma.test,v 1.14 2004/06/26 06:37:07 danielk1977 Exp $ 16cd61c281Sdrh 17cd61c281Sdrhset testdir [file dirname $argv0] 18cd61c281Sdrhsource $testdir/tester.tcl 19cd61c281Sdrh 20*91cf71b0Sdanielk1977# Test organization: 21*91cf71b0Sdanielk1977# 22*91cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db. 23*91cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db. 24*91cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check. 25*91cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db. 26*91cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a 27*91cf71b0Sdanielk1977# transaction. 28*91cf71b0Sdanielk1977# 29*91cf71b0Sdanielk1977 30cd61c281Sdrh# Delete the preexisting database to avoid the special setup 31cd61c281Sdrh# that the "all.test" script does. 32cd61c281Sdrh# 33cd61c281Sdrhdb close 34cd61c281Sdrhfile delete test.db 35ef4ac8f9Sdrhset DB [sqlite3 db test.db] 36cd61c281Sdrh 37cd61c281Sdrhdo_test pragma-1.1 { 38cd61c281Sdrh execsql { 39cd61c281Sdrh PRAGMA cache_size; 40cd61c281Sdrh PRAGMA default_cache_size; 41cd61c281Sdrh PRAGMA synchronous; 42cd61c281Sdrh } 43*91cf71b0Sdanielk1977} {2000 2000 2} 44cd61c281Sdrhdo_test pragma-1.2 { 45cd61c281Sdrh execsql { 46cd61c281Sdrh PRAGMA cache_size=1234; 47cd61c281Sdrh PRAGMA cache_size; 48cd61c281Sdrh PRAGMA default_cache_size; 49cd61c281Sdrh PRAGMA synchronous; 50cd61c281Sdrh } 51*91cf71b0Sdanielk1977} {1234 2000 2} 52cd61c281Sdrhdo_test pragma-1.3 { 53cd61c281Sdrh db close 54ef4ac8f9Sdrh sqlite3 db test.db 55cd61c281Sdrh execsql { 56cd61c281Sdrh PRAGMA cache_size; 57cd61c281Sdrh PRAGMA default_cache_size; 58cd61c281Sdrh PRAGMA synchronous; 59cd61c281Sdrh } 60*91cf71b0Sdanielk1977} {2000 2000 2} 61cd61c281Sdrhdo_test pragma-1.4 { 62cd61c281Sdrh execsql { 63cd61c281Sdrh PRAGMA synchronous=OFF; 64cd61c281Sdrh PRAGMA cache_size; 65cd61c281Sdrh PRAGMA default_cache_size; 66cd61c281Sdrh PRAGMA synchronous; 67cd61c281Sdrh } 68*91cf71b0Sdanielk1977} {2000 2000 0} 69cd61c281Sdrhdo_test pragma-1.5 { 70cd61c281Sdrh execsql { 71cd61c281Sdrh PRAGMA cache_size=4321; 72cd61c281Sdrh PRAGMA cache_size; 73cd61c281Sdrh PRAGMA default_cache_size; 74cd61c281Sdrh PRAGMA synchronous; 75cd61c281Sdrh } 76*91cf71b0Sdanielk1977} {4321 2000 0} 77cd61c281Sdrhdo_test pragma-1.6 { 78cd61c281Sdrh execsql { 79cd61c281Sdrh PRAGMA synchronous=ON; 80cd61c281Sdrh PRAGMA cache_size; 81cd61c281Sdrh PRAGMA default_cache_size; 82cd61c281Sdrh PRAGMA synchronous; 83cd61c281Sdrh } 84*91cf71b0Sdanielk1977} {4321 2000 1} 85cd61c281Sdrhdo_test pragma-1.7 { 86cd61c281Sdrh db close 87ef4ac8f9Sdrh sqlite3 db test.db 88cd61c281Sdrh execsql { 89cd61c281Sdrh PRAGMA cache_size; 90cd61c281Sdrh PRAGMA default_cache_size; 91cd61c281Sdrh PRAGMA synchronous; 92cd61c281Sdrh } 93*91cf71b0Sdanielk1977} {2000 2000 2} 94cd61c281Sdrhdo_test pragma-1.8 { 95cd61c281Sdrh execsql { 96cd61c281Sdrh PRAGMA default_cache_size=123; 97cd61c281Sdrh PRAGMA cache_size; 98cd61c281Sdrh PRAGMA default_cache_size; 99cd61c281Sdrh PRAGMA synchronous; 100cd61c281Sdrh } 101*91cf71b0Sdanielk1977} {123 123 2} 102*91cf71b0Sdanielk1977do_test pragma-1.9 { 103cd61c281Sdrh db close 104ef4ac8f9Sdrh set ::DB [sqlite3 db test.db] 105cd61c281Sdrh execsql { 106cd61c281Sdrh PRAGMA cache_size; 107cd61c281Sdrh PRAGMA default_cache_size; 108cd61c281Sdrh PRAGMA synchronous; 109cd61c281Sdrh } 110*91cf71b0Sdanielk1977} {123 123 2} 111*91cf71b0Sdanielk1977do_test pragma-1.10 { 1125a38705eSdrh execsql { 1134303feedSdrh PRAGMA synchronous=NORMAL; 1144303feedSdrh PRAGMA cache_size; 1154303feedSdrh PRAGMA default_cache_size; 1164303feedSdrh PRAGMA synchronous; 1174303feedSdrh } 118*91cf71b0Sdanielk1977} {123 123 1} 119*91cf71b0Sdanielk1977do_test pragma-1.11 { 1204303feedSdrh execsql { 1214303feedSdrh PRAGMA synchronous=FULL; 1224303feedSdrh PRAGMA cache_size; 1234303feedSdrh PRAGMA default_cache_size; 1244303feedSdrh PRAGMA synchronous; 1254303feedSdrh } 126*91cf71b0Sdanielk1977} {123 123 2} 127*91cf71b0Sdanielk1977do_test pragma-1.12 { 1284303feedSdrh db close 129ef4ac8f9Sdrh set ::DB [sqlite3 db test.db] 1304303feedSdrh execsql { 1314303feedSdrh PRAGMA cache_size; 1324303feedSdrh PRAGMA default_cache_size; 1334303feedSdrh PRAGMA synchronous; 1344303feedSdrh } 135*91cf71b0Sdanielk1977} {123 123 2} 136*91cf71b0Sdanielk1977 137*91cf71b0Sdanielk1977# Test modifying the safety_level of an attached database. 138*91cf71b0Sdanielk1977do_test pragma-2.1 { 139*91cf71b0Sdanielk1977 file delete -force test2.db 140*91cf71b0Sdanielk1977 file delete -force test2.db-journal 1414303feedSdrh execsql { 142*91cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 1434303feedSdrh } 144*91cf71b0Sdanielk1977} {} 145*91cf71b0Sdanielk1977do_test pragma-2.2 { 1464303feedSdrh execsql { 147*91cf71b0Sdanielk1977 pragma aux.synchronous; 1484303feedSdrh } 149*91cf71b0Sdanielk1977} {2} 150*91cf71b0Sdanielk1977do_test pragma-2.3 { 151*91cf71b0Sdanielk1977 execsql { 152*91cf71b0Sdanielk1977 pragma aux.synchronous = OFF; 153*91cf71b0Sdanielk1977 pragma aux.synchronous; 154*91cf71b0Sdanielk1977 pragma synchronous; 155*91cf71b0Sdanielk1977 } 156*91cf71b0Sdanielk1977} {0 2} 157*91cf71b0Sdanielk1977do_test pragma-2.4 { 158*91cf71b0Sdanielk1977 execsql { 159*91cf71b0Sdanielk1977 pragma aux.synchronous = ON; 160*91cf71b0Sdanielk1977 pragma synchronous; 161*91cf71b0Sdanielk1977 pragma aux.synchronous; 162*91cf71b0Sdanielk1977 } 163*91cf71b0Sdanielk1977} {2 1} 1644303feedSdrh 165ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check 166ed717fe3Sdrh# pragma finds it. 167ed717fe3Sdrh# 168ef4ac8f9Sdrhif {![sqlite3 -has-codec]} { 169ed717fe3Sdrhdo_test pragma-3.1 { 170ed717fe3Sdrh execsql { 171ed717fe3Sdrh BEGIN; 172ed717fe3Sdrh CREATE TABLE t2(a,b,c); 173ed717fe3Sdrh CREATE INDEX i2 ON t2(a); 174ed717fe3Sdrh INSERT INTO t2 VALUES(11,2,3); 175ed717fe3Sdrh INSERT INTO t2 VALUES(22,3,4); 176ed717fe3Sdrh COMMIT; 177ed717fe3Sdrh SELECT rowid, * from t2; 178ed717fe3Sdrh } 179ed717fe3Sdrh} {1 11 2 3 2 22 3 4} 180ed717fe3Sdrhdo_test pragma-3.2 { 181ed717fe3Sdrh set rootpage [execsql {SELECT rootpage FROM sqlite_master WHERE name='i2'}] 18296fc5fe6Sdanielk1977 set db [btree_open test.db 100 0] 183ed717fe3Sdrh btree_begin_transaction $db 184ed717fe3Sdrh set c [btree_cursor $db $rootpage 1] 185ed717fe3Sdrh btree_first $c 186ed717fe3Sdrh btree_delete $c 187ed717fe3Sdrh btree_commit $db 188ed717fe3Sdrh btree_close $db 189ed717fe3Sdrh execsql {PRAGMA integrity_check} 1904be295beSdrh} {{rowid 1 missing from index i2} {wrong # of entries in index i2}} 1919eb9e26bSdrh}; # endif has-codec 1925a38705eSdrh 193*91cf71b0Sdanielk1977do_test pragma-3.3 { 194*91cf71b0Sdanielk1977 execsql { 195*91cf71b0Sdanielk1977 DROP INDEX i2; 196*91cf71b0Sdanielk1977 } 197*91cf71b0Sdanielk1977} {} 198*91cf71b0Sdanielk1977 199*91cf71b0Sdanielk1977# Test modifying the cache_size of an attached database. 200*91cf71b0Sdanielk1977do_test pragma-4.1 { 201*91cf71b0Sdanielk1977 execsql { 202*91cf71b0Sdanielk1977 pragma aux.cache_size; 203*91cf71b0Sdanielk1977 pragma aux.default_cache_size; 204*91cf71b0Sdanielk1977 } 205*91cf71b0Sdanielk1977} {2000 2000} 2061bdd9b57Sdrhdo_test pragma-4.2 { 2071bdd9b57Sdrh execsql { 208*91cf71b0Sdanielk1977 pragma aux.cache_size = 50; 209*91cf71b0Sdanielk1977 pragma aux.cache_size; 210*91cf71b0Sdanielk1977 pragma aux.default_cache_size; 2111bdd9b57Sdrh } 212*91cf71b0Sdanielk1977} {50 2000} 2131bdd9b57Sdrhdo_test pragma-4.3 { 2141bdd9b57Sdrh execsql { 215*91cf71b0Sdanielk1977 pragma aux.default_cache_size = 456; 216*91cf71b0Sdanielk1977 pragma aux.cache_size; 217*91cf71b0Sdanielk1977 pragma aux.default_cache_size; 2181bdd9b57Sdrh } 219*91cf71b0Sdanielk1977} {456 456} 2201bdd9b57Sdrhdo_test pragma-4.4 { 2211bdd9b57Sdrh execsql { 222*91cf71b0Sdanielk1977 pragma cache_size; 223*91cf71b0Sdanielk1977 pragma default_cache_size; 2241bdd9b57Sdrh } 225*91cf71b0Sdanielk1977} {123 123} 2261bdd9b57Sdrhdo_test pragma-4.5 { 2271bdd9b57Sdrh execsql { 228*91cf71b0Sdanielk1977 DETACH aux; 229*91cf71b0Sdanielk1977 ATTACH 'test3.db' AS aux; 230*91cf71b0Sdanielk1977 pragma aux.cache_size; 231*91cf71b0Sdanielk1977 pragma aux.default_cache_size; 2321bdd9b57Sdrh } 233*91cf71b0Sdanielk1977} {2000 2000} 2341bdd9b57Sdrhdo_test pragma-4.6 { 2351bdd9b57Sdrh execsql { 236*91cf71b0Sdanielk1977 DETACH aux; 237*91cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 238*91cf71b0Sdanielk1977 pragma aux.cache_size; 239*91cf71b0Sdanielk1977 pragma aux.default_cache_size; 2401bdd9b57Sdrh } 241*91cf71b0Sdanielk1977} {456 456} 2421bdd9b57Sdrh 243*91cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is 244*91cf71b0Sdanielk1977# disallowed. 245*91cf71b0Sdanielk1977do_test pragma-5.0 { 2461bdd9b57Sdrh execsql { 247*91cf71b0Sdanielk1977 pragma synchronous; 2481bdd9b57Sdrh } 249*91cf71b0Sdanielk1977} {2} 250*91cf71b0Sdanielk1977do_test pragma-5.1 { 2511bdd9b57Sdrh catchsql { 2521bdd9b57Sdrh BEGIN; 253*91cf71b0Sdanielk1977 pragma synchronous = OFF; 2541bdd9b57Sdrh } 255*91cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}} 256*91cf71b0Sdanielk1977do_test pragma-5.2 { 257*91cf71b0Sdanielk1977 execsql { 258*91cf71b0Sdanielk1977 pragma synchronous; 259*91cf71b0Sdanielk1977 } 260*91cf71b0Sdanielk1977} {2} 2611bdd9b57Sdrh 262cd61c281Sdrhfinish_test 263