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*6bf89570Sdrh# $Id: pragma.test,v 1.20 2004/11/03 16:27:02 drh Exp $ 16cd61c281Sdrh 17cd61c281Sdrhset testdir [file dirname $argv0] 18cd61c281Sdrhsource $testdir/tester.tcl 19cd61c281Sdrh 2091cf71b0Sdanielk1977# Test organization: 2191cf71b0Sdanielk1977# 2291cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db. 2391cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db. 2491cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check. 2591cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db. 2691cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a 2791cf71b0Sdanielk1977# transaction. 2891cf71b0Sdanielk1977# 2991cf71b0Sdanielk1977 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 } 4391cf71b0Sdanielk1977} {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 } 5191cf71b0Sdanielk1977} {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 } 6091cf71b0Sdanielk1977} {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 } 6891cf71b0Sdanielk1977} {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 } 7691cf71b0Sdanielk1977} {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 } 8491cf71b0Sdanielk1977} {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 } 9391cf71b0Sdanielk1977} {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 } 10191cf71b0Sdanielk1977} {123 123 2} 10291cf71b0Sdanielk1977do_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 } 11091cf71b0Sdanielk1977} {123 123 2} 11191cf71b0Sdanielk1977do_test pragma-1.10 { 1125a38705eSdrh execsql { 1134303feedSdrh PRAGMA synchronous=NORMAL; 1144303feedSdrh PRAGMA cache_size; 1154303feedSdrh PRAGMA default_cache_size; 1164303feedSdrh PRAGMA synchronous; 1174303feedSdrh } 11891cf71b0Sdanielk1977} {123 123 1} 11991cf71b0Sdanielk1977do_test pragma-1.11 { 1204303feedSdrh execsql { 1214303feedSdrh PRAGMA synchronous=FULL; 1224303feedSdrh PRAGMA cache_size; 1234303feedSdrh PRAGMA default_cache_size; 1244303feedSdrh PRAGMA synchronous; 1254303feedSdrh } 12691cf71b0Sdanielk1977} {123 123 2} 12791cf71b0Sdanielk1977do_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 } 13591cf71b0Sdanielk1977} {123 123 2} 13691cf71b0Sdanielk1977 1375260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition 1385260f7e9Sdrh# to keywords like "off" and "full". 1395260f7e9Sdrh# 1405260f7e9Sdrhdo_test pragma-1.13 { 1415260f7e9Sdrh execsql { 1425260f7e9Sdrh PRAGMA synchronous=0; 1435260f7e9Sdrh PRAGMA synchronous; 1445260f7e9Sdrh } 1455260f7e9Sdrh} {0} 1465260f7e9Sdrhdo_test pragma-1.14 { 1475260f7e9Sdrh execsql { 1485260f7e9Sdrh PRAGMA synchronous=2; 1495260f7e9Sdrh PRAGMA synchronous; 1505260f7e9Sdrh } 1515260f7e9Sdrh} {2} 1525260f7e9Sdrh 1535260f7e9Sdrh# Test turning "flag" pragmas on and off. 1545260f7e9Sdrh# 1555260f7e9Sdrhdo_test pragma-1.15 { 1565260f7e9Sdrh execsql { 1575260f7e9Sdrh PRAGMA vdbe_listing=YES; 1585260f7e9Sdrh PRAGMA vdbe_listing; 1595260f7e9Sdrh } 1605260f7e9Sdrh} {1} 1615260f7e9Sdrhdo_test pragma-1.16 { 1625260f7e9Sdrh execsql { 1635260f7e9Sdrh PRAGMA vdbe_listing=NO; 1645260f7e9Sdrh PRAGMA vdbe_listing; 1655260f7e9Sdrh } 1665260f7e9Sdrh} {0} 1675260f7e9Sdrhdo_test pragma-1.17 { 1685260f7e9Sdrh execsql { 1695260f7e9Sdrh PRAGMA parser_trace=ON; 1705260f7e9Sdrh PRAGMA parser_trace=OFF; 1715260f7e9Sdrh } 1725260f7e9Sdrh} {} 1735260f7e9Sdrhdo_test pragma-1.18 { 1745260f7e9Sdrh execsql { 1755260f7e9Sdrh PRAGMA bogus = -1234; -- Parsing of negative values 1765260f7e9Sdrh } 1775260f7e9Sdrh} {} 1785260f7e9Sdrh 17991cf71b0Sdanielk1977# Test modifying the safety_level of an attached database. 18091cf71b0Sdanielk1977do_test pragma-2.1 { 18191cf71b0Sdanielk1977 file delete -force test2.db 18291cf71b0Sdanielk1977 file delete -force test2.db-journal 1834303feedSdrh execsql { 18491cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 1854303feedSdrh } 18691cf71b0Sdanielk1977} {} 18791cf71b0Sdanielk1977do_test pragma-2.2 { 1884303feedSdrh execsql { 18991cf71b0Sdanielk1977 pragma aux.synchronous; 1904303feedSdrh } 19191cf71b0Sdanielk1977} {2} 19291cf71b0Sdanielk1977do_test pragma-2.3 { 19391cf71b0Sdanielk1977 execsql { 19491cf71b0Sdanielk1977 pragma aux.synchronous = OFF; 19591cf71b0Sdanielk1977 pragma aux.synchronous; 19691cf71b0Sdanielk1977 pragma synchronous; 19791cf71b0Sdanielk1977 } 19891cf71b0Sdanielk1977} {0 2} 19991cf71b0Sdanielk1977do_test pragma-2.4 { 20091cf71b0Sdanielk1977 execsql { 20191cf71b0Sdanielk1977 pragma aux.synchronous = ON; 20291cf71b0Sdanielk1977 pragma synchronous; 20391cf71b0Sdanielk1977 pragma aux.synchronous; 20491cf71b0Sdanielk1977 } 20591cf71b0Sdanielk1977} {2 1} 2064303feedSdrh 207ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check 208ed717fe3Sdrh# pragma finds it. 209ed717fe3Sdrh# 21025d6543dSdrh# These tests won't work if the database is encrypted 21125d6543dSdrh# 212ed717fe3Sdrhdo_test pragma-3.1 { 213ed717fe3Sdrh execsql { 214ed717fe3Sdrh BEGIN; 215ed717fe3Sdrh CREATE TABLE t2(a,b,c); 216ed717fe3Sdrh CREATE INDEX i2 ON t2(a); 217ed717fe3Sdrh INSERT INTO t2 VALUES(11,2,3); 218ed717fe3Sdrh INSERT INTO t2 VALUES(22,3,4); 219ed717fe3Sdrh COMMIT; 220ed717fe3Sdrh SELECT rowid, * from t2; 221ed717fe3Sdrh } 222ed717fe3Sdrh} {1 11 2 3 2 22 3 4} 22325d6543dSdrhif {![sqlite3 -has-codec]} { 224ed717fe3Sdrh do_test pragma-3.2 { 225ed717fe3Sdrh set rootpage [execsql {SELECT rootpage FROM sqlite_master WHERE name='i2'}] 22696fc5fe6Sdanielk1977 set db [btree_open test.db 100 0] 227ed717fe3Sdrh btree_begin_transaction $db 228ed717fe3Sdrh set c [btree_cursor $db $rootpage 1] 229ed717fe3Sdrh btree_first $c 230ed717fe3Sdrh btree_delete $c 231ed717fe3Sdrh btree_commit $db 232ed717fe3Sdrh btree_close $db 233ed717fe3Sdrh execsql {PRAGMA integrity_check} 2344be295beSdrh } {{rowid 1 missing from index i2} {wrong # of entries in index i2}} 23525d6543dSdrh} 23691cf71b0Sdanielk1977do_test pragma-3.3 { 23791cf71b0Sdanielk1977 execsql { 23891cf71b0Sdanielk1977 DROP INDEX i2; 23991cf71b0Sdanielk1977 } 24091cf71b0Sdanielk1977} {} 24191cf71b0Sdanielk1977 24291cf71b0Sdanielk1977# Test modifying the cache_size of an attached database. 24391cf71b0Sdanielk1977do_test pragma-4.1 { 24491cf71b0Sdanielk1977 execsql { 24591cf71b0Sdanielk1977 pragma aux.cache_size; 24691cf71b0Sdanielk1977 pragma aux.default_cache_size; 24791cf71b0Sdanielk1977 } 24891cf71b0Sdanielk1977} {2000 2000} 2491bdd9b57Sdrhdo_test pragma-4.2 { 2501bdd9b57Sdrh execsql { 25191cf71b0Sdanielk1977 pragma aux.cache_size = 50; 25291cf71b0Sdanielk1977 pragma aux.cache_size; 25391cf71b0Sdanielk1977 pragma aux.default_cache_size; 2541bdd9b57Sdrh } 25591cf71b0Sdanielk1977} {50 2000} 2561bdd9b57Sdrhdo_test pragma-4.3 { 2571bdd9b57Sdrh execsql { 25891cf71b0Sdanielk1977 pragma aux.default_cache_size = 456; 25991cf71b0Sdanielk1977 pragma aux.cache_size; 26091cf71b0Sdanielk1977 pragma aux.default_cache_size; 2611bdd9b57Sdrh } 26291cf71b0Sdanielk1977} {456 456} 2631bdd9b57Sdrhdo_test pragma-4.4 { 2641bdd9b57Sdrh execsql { 26591cf71b0Sdanielk1977 pragma cache_size; 26691cf71b0Sdanielk1977 pragma default_cache_size; 2671bdd9b57Sdrh } 26891cf71b0Sdanielk1977} {123 123} 2691bdd9b57Sdrhdo_test pragma-4.5 { 2701bdd9b57Sdrh execsql { 27191cf71b0Sdanielk1977 DETACH aux; 27291cf71b0Sdanielk1977 ATTACH 'test3.db' AS aux; 27391cf71b0Sdanielk1977 pragma aux.cache_size; 27491cf71b0Sdanielk1977 pragma aux.default_cache_size; 2751bdd9b57Sdrh } 27691cf71b0Sdanielk1977} {2000 2000} 2771bdd9b57Sdrhdo_test pragma-4.6 { 2781bdd9b57Sdrh execsql { 27991cf71b0Sdanielk1977 DETACH aux; 28091cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 28191cf71b0Sdanielk1977 pragma aux.cache_size; 28291cf71b0Sdanielk1977 pragma aux.default_cache_size; 2831bdd9b57Sdrh } 28491cf71b0Sdanielk1977} {456 456} 2851bdd9b57Sdrh 28691cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is 28791cf71b0Sdanielk1977# disallowed. 28891cf71b0Sdanielk1977do_test pragma-5.0 { 2891bdd9b57Sdrh execsql { 29091cf71b0Sdanielk1977 pragma synchronous; 2911bdd9b57Sdrh } 29291cf71b0Sdanielk1977} {2} 29391cf71b0Sdanielk1977do_test pragma-5.1 { 2941bdd9b57Sdrh catchsql { 2951bdd9b57Sdrh BEGIN; 29691cf71b0Sdanielk1977 pragma synchronous = OFF; 2971bdd9b57Sdrh } 29891cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}} 29991cf71b0Sdanielk1977do_test pragma-5.2 { 30091cf71b0Sdanielk1977 execsql { 30191cf71b0Sdanielk1977 pragma synchronous; 30291cf71b0Sdanielk1977 } 30391cf71b0Sdanielk1977} {2} 3045260f7e9Sdrhcatchsql {COMMIT;} 3055260f7e9Sdrh 3065260f7e9Sdrh# Test schema-query pragmas 3075260f7e9Sdrh# 3085260f7e9Sdrhdo_test pragma-6.1 { 309956bc92cSdrh set res {} 3105260f7e9Sdrh foreach {idx name file} [execsql {pragma database_list}] { 3115260f7e9Sdrh lappend res $idx $name 3125260f7e9Sdrh } 3135260f7e9Sdrh set res 314a71aa001Sdrh} {0 main 1 temp 2 aux} 3155260f7e9Sdrhdo_test pragma-6.2 { 3165260f7e9Sdrh execsql { 3175260f7e9Sdrh pragma table_info(t2) 3185260f7e9Sdrh } 3195260f7e9Sdrh} {0 a numeric 0 {} 0 1 b numeric 0 {} 0 2 c numeric 0 {} 0} 320*6bf89570Sdrhifcapable {foreignkey} { 3215260f7e9Sdrh do_test pragma-6.3 { 3225260f7e9Sdrh execsql { 3235260f7e9Sdrh CREATE TABLE t3(a int references t2(b), b UNIQUE); 3245260f7e9Sdrh pragma foreign_key_list(t3); 3255260f7e9Sdrh } 3265260f7e9Sdrh } {0 0 t2 a b} 3275260f7e9Sdrh do_test pragma-6.4 { 3285260f7e9Sdrh execsql { 3295260f7e9Sdrh pragma index_list(t3); 3305260f7e9Sdrh } 3315260f7e9Sdrh } {0 sqlite_autoindex_t3_1 1} 332*6bf89570Sdrh} 333*6bf89570Sdrhifcapable {!foreignkey} { 334*6bf89570Sdrh execsql {CREATE TABLE t3(a,b UNIQUE)} 335*6bf89570Sdrh} 3365260f7e9Sdrhdo_test pragma-6.5 { 3375260f7e9Sdrh execsql { 3385260f7e9Sdrh CREATE INDEX t3i1 ON t3(a,b); 3395260f7e9Sdrh pragma index_info(t3i1); 3405260f7e9Sdrh } 3415260f7e9Sdrh} {0 0 a 1 1 b} 3425260f7e9Sdrh 3435260f7e9Sdrh# Miscellaneous tests 3445260f7e9Sdrh# 3455260f7e9Sdrhdo_test pragma-7.1 { 3465260f7e9Sdrh # Make sure a pragma knows to read the schema if it needs to 3475260f7e9Sdrh db close 3485260f7e9Sdrh sqlite3 db test.db 3495260f7e9Sdrh execsql { 3505260f7e9Sdrh pragma index_list(t3); 3515260f7e9Sdrh } 3525260f7e9Sdrh} {0 t3i1 0 1 sqlite_autoindex_t3_1 1} 3535260f7e9Sdrhdo_test pragma-7.2 { 3545260f7e9Sdrh db close 3555260f7e9Sdrh sqlite3 db test.db 3565260f7e9Sdrh catchsql { 3575260f7e9Sdrh pragma encoding=bogus; 3585260f7e9Sdrh } 3595260f7e9Sdrh} {1 {unsupported encoding: bogus}} 3605260f7e9Sdrhdo_test pragma-7.3 { 3615260f7e9Sdrh db close 3625260f7e9Sdrh sqlite3 db test.db 3635260f7e9Sdrh execsql { 3645260f7e9Sdrh pragma lock_status; 3655260f7e9Sdrh } 3665260f7e9Sdrh} {main unlocked temp closed} 3675260f7e9Sdrh 3685260f7e9Sdrh 3691bdd9b57Sdrh 370cd61c281Sdrhfinish_test 371