xref: /sqlite-3.40.0/test/delete_db.test (revision fb32c44e)
1# 2016 September 10
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 testing the code in test_delete.c (the
13# sqlite3_delete_database() API).
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix delete_db
19
20if {[atomic_batch_write test.db]} {
21  finish_test
22  return
23}
24
25proc delete_all {} {
26  foreach f [glob -nocomplain test2*] { file delete $f }
27  foreach f [glob -nocomplain test3*] { file delete $f }
28}
29
30proc copydb {} {
31  foreach f [glob -nocomplain test3*] { file delete $f }
32  foreach f [glob -nocomplain test2*] {
33    set p [string range $f 5 end]
34    file copy "test2$p" "test3$p"
35  }
36}
37
38proc files {} {
39  lsort [glob -nocomplain test3*]
40}
41
42db close
43delete_all
44sqlite3 db test2.database
45
46#-------------------------------------------------------------------------
47#
48# 1.1: Journal files.
49# 1.2: Wal files.
50# 1.3: Multiplexor with journal file.
51# 1.4: Multiplexor with wal file.
52#
53# 2.* are a copy of 1.* with the multiplexor enabled.
54#
55# 3.* tests errors.
56#
57
58do_test 1.1.0 {
59  execsql {
60    CREATE TABLE t1(x, y);
61    BEGIN;
62      INSERT INTO t1 VALUES(1, 2);
63  }
64  copydb
65  files
66} {test3.database test3.database-journal}
67
68do_test 1.1.1 {
69  sqlite3_delete_database test3.database
70  files
71} {}
72
73do_test 1.2.0 {
74  execsql {
75    COMMIT;
76    PRAGMA journal_mode = wal;
77    INSERT INTO t1 VALUES(3, 4);
78  }
79  copydb
80  files
81} {test3.database test3.database-shm test3.database-wal}
82do_test 1.2.1 {
83  sqlite3_delete_database test3.database
84  files
85} {}
86
87db close
88delete_all
89sqlite3_multiplex_initialize "" 0
90sqlite3 db test2.database -vfs multiplex
91sqlite3_multiplex_control db "main" chunk_size 32768
92
93do_test 1.3.0 {
94  execsql { PRAGMA auto_vacuum = 0; }
95  execsql {
96    CREATE TABLE x1(a, b);
97    WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 )
98    INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s;
99    BEGIN;
100      UPDATE x1 SET a=randomblob(101)
101  }
102  copydb
103  files
104} [list {*}{
105  test3.database test3.database-journal test3.database001
106  test3.database002 test3.database003
107}]
108do_test 1.3.1 {
109  sqlite3_delete_database test3.database
110  files
111} {}
112
113
114do_test 1.4.0 {
115  execsql {
116    COMMIT;
117    PRAGMA journal_mode = wal;
118    UPDATE x1 SET a=randomblob(102)
119  }
120  copydb
121  files
122} [list {*}{
123  test3.database test3.database-shm test3.database-wal test3.database001
124  test3.database002 test3.database003
125}]
126do_test 1.4.1 {
127  sqlite3_delete_database test3.database
128  files
129} {}
130
131
132ifcapable 8_3_names {
133  db close
134  delete_all
135  sqlite3 db file:test2.db?8_3_names=1 -uri 1
136
137  do_test 2.1.0 {
138    execsql {
139      CREATE TABLE t1(x, y);
140      BEGIN;
141        INSERT INTO t1 VALUES(1, 2);
142    }
143    copydb
144    files
145  } {test3.db test3.nal}
146
147  do_test 2.1.1 {
148    sqlite3_delete_database test3.db
149    files
150  } {}
151
152  do_test 2.2.0 {
153    execsql {
154      COMMIT;
155      PRAGMA journal_mode = wal;
156      INSERT INTO t1 VALUES(3, 4);
157    }
158    copydb
159    files
160  } {test3.db test3.shm test3.wal}
161  do_test 2.2.1 {
162    sqlite3_delete_database test3.db
163    files
164  } {}
165
166
167  db close
168  delete_all
169  sqlite3_multiplex_initialize "" 0
170  sqlite3 db file:test2.db?8_3_names=1 -uri 1 -vfs multiplex
171  sqlite3_multiplex_control db "main" chunk_size 32768
172
173  do_test 2.3.0 {
174    execsql { PRAGMA auto_vacuum = 0; }
175    execsql {
176      CREATE TABLE x1(a, b);
177      WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 )
178      INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s;
179      BEGIN;
180        UPDATE x1 SET a=randomblob(101)
181    }
182    copydb
183    files
184  } [list {*}{
185    test3.001 test3.002 test3.003 test3.db test3.nal
186  }]
187  do_test 2.3.1 {
188    sqlite3_delete_database test3.db
189    files
190  } {}
191
192
193  do_test 2.4.0 {
194    execsql {
195      COMMIT;
196      PRAGMA journal_mode = wal;
197      UPDATE x1 SET a=randomblob(102)
198    }
199    copydb
200    files
201  } [list {*}{
202    test3.001 test3.002 test3.003 test3.db test3.db-shm test3.wal
203  }]
204  do_test 2.4.1 {
205    sqlite3_delete_database test3.db
206    files
207  } {}
208}
209
210db close
211delete_all
212sqlite3_multiplex_shutdown
213
214do_test 3.0 {
215  file mkdir dir2.db
216  sqlite3_delete_database dir2.db
217} {SQLITE_ERROR}
218do_test 3.1 {
219  sqlite3_delete_database dir2.db/test.db
220} {SQLITE_OK}
221
222finish_test
223