1# 2018-01-02 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 the "memdb" VFS 13# 14 15set testdir [file dirname $argv0] 16source $testdir/tester.tcl 17set testprefix memdb1 18do_not_use_codec 19 20ifcapable !deserialize { 21 finish_test 22 return 23} 24 25# Create a MEMDB and populate it with some dummy data. 26# Then extract the database into the $::db1 variable. 27# Verify that the size of $::db1 is the same as the size of 28# the database. 29# 30unset -nocomplain db1 31unset -nocomplain sz1 32unset -nocomplain pgsz 33do_test 100 { 34 db eval { 35 CREATE TABLE t1(a,b); 36 INSERT INTO t1 VALUES(1,2); 37 } 38 set ::pgsz [db one {PRAGMA page_size}] 39 set ::sz1 [expr {$::pgsz*[db one {PRAGMA page_count}]}] 40 set ::db1 [db serialize] 41 expr {[string length $::db1]==$::sz1} 42} 1 43set fd [open db1.db wb] 44puts -nonewline $fd $db1 45close $fd 46 47# Create a new MEMDB and initialize it to the content of $::db1 48# Verify that the content is the same. 49# 50db close 51sqlite3 db 52db deserialize $db1 53do_execsql_test 110 { 54 SELECT * FROM t1; 55} {1 2} 56 57# What happens when we try to VACUUM a MEMDB database? 58# 59do_execsql_test 120 { 60 PRAGMA auto_vacuum = off; 61 VACUUM; 62} {} 63do_execsql_test 130 { 64 CREATE TABLE t2(x, y); 65 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) 66 INSERT INTO t2(x, y) SELECT x, randomblob(1000) FROM c; 67 DROP TABLE t2; 68 PRAGMA page_count; 69} {116} 70do_execsql_test 140 { 71 VACUUM; 72 PRAGMA page_count; 73} {2} 74 75# Build a largish on-disk database and serialize it. Verify that the 76# serialization works. 77# 78db close 79forcedelete test.db 80sqlite3 db test.db 81do_execsql_test 200 { 82 CREATE TABLE t3(x, y); 83 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400) 84 INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c; 85 PRAGMA quick_check; 86} {ok} 87set fd [open test.db rb] 88unset -nocomplain direct 89set direct [read $fd] 90close $fd 91do_test 210 { 92 string length [db serialize] 93} [string length $direct] 94do_test 220 { 95 db eval {ATTACH ':memory:' AS aux1} 96 db deserialize aux1 $::direct 97 db eval { 98 SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3; 99 } 100} {} 101unset -nocomplain direct 102 103# Do the same with a :memory: database. 104# 105db close 106sqlite3 db :memory: 107do_execsql_test 300 { 108 CREATE TABLE t3(x, y); 109 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400) 110 INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c; 111 PRAGMA quick_check; 112} {ok} 113do_test 310 { 114 db eval {ATTACH ':memory:' AS aux1} 115 db deserialize aux1 [db serialize main] 116 db eval { 117 SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3; 118 } 119} {} 120 121# Deserialize an empty database 122# 123db close 124sqlite3 db 125db deserialize {} 126do_execsql_test 400 { 127 PRAGMA integrity_check; 128} {ok} 129do_execsql_test 410 { 130 CREATE TABLE t4(a,b); 131 INSERT INTO t4 VALUES('hello','world!'); 132 PRAGMA integrity_check; 133 SELECT * FROM t4; 134} {ok hello world!} 135 136# Deserialize something that is not a database. 137# 138db close 139sqlite3 db 140do_test 500 { 141 set rc [catch {db deserialize not-a-database} msg] 142 lappend rc $msg 143} {0 {}} 144do_catchsql_test 510 { 145 PRAGMA integrity_check; 146} {1 {file is not a database}} 147 148# Abuse the serialize and deserialize commands. Make sure errors are caught. 149# 150do_test 600 { 151 set rc [catch {db deserialize} msg] 152 lappend rc $msg 153} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}} 154do_test 610 { 155 set rc [catch {db deserialize a b c} msg] 156 lappend rc $msg 157} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}} 158do_test 620 { 159 set rc [catch {db serialize a b} msg] 160 lappend rc $msg 161} {1 {wrong # args: should be "db serialize ?DATABASE?"}} 162 163finish_test 164