summaryrefslogtreecommitdiffstats
path: root/test/delete_db.test
blob: 6edd9c242e01d5502d16200eba95b1a59a52dd3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# 2016 September 10
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the code in test_delete.c (the
# sqlite3_delete_database() API).
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix delete_db

if {[atomic_batch_write test.db]} {
  finish_test
  return
}

proc delete_all {} {
  foreach f [glob -nocomplain test2*] { file delete $f }
  foreach f [glob -nocomplain test3*] { file delete $f }
}

proc copydb {} {
  foreach f [glob -nocomplain test3*] { file delete $f }
  foreach f [glob -nocomplain test2*] { 
    set p [string range $f 5 end]
    file copy "test2$p" "test3$p"
  }
}

proc files {} {
  lsort [glob -nocomplain test3*]
}

db close
delete_all
sqlite3 db test2.database

#-------------------------------------------------------------------------
#
# 1.1: Journal files.
# 1.2: Wal files.
# 1.3: Multiplexor with journal file.
# 1.4: Multiplexor with wal file.
#
# 2.* are a copy of 1.* with the multiplexor enabled.
#
# 3.* tests errors.
#

do_test 1.1.0 {
  execsql {
    CREATE TABLE t1(x, y);
    BEGIN;
      INSERT INTO t1 VALUES(1, 2);
  }
  copydb
  files
} {test3.database test3.database-journal}

do_test 1.1.1 {
  sqlite3_delete_database test3.database
  files
} {}

do_test 1.2.0 {
  execsql {
    COMMIT;
    PRAGMA journal_mode = wal;
    INSERT INTO t1 VALUES(3, 4);
  }
  copydb
  files
} {test3.database test3.database-shm test3.database-wal}
do_test 1.2.1 {
  sqlite3_delete_database test3.database
  files
} {}

db close
delete_all
sqlite3_multiplex_initialize "" 0
sqlite3 db test2.database -vfs multiplex
sqlite3_multiplex_control db "main" chunk_size 32768

do_test 1.3.0 {
  execsql { PRAGMA auto_vacuum = 0; }
  execsql {
    CREATE TABLE x1(a, b);
    WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 )
    INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s;
    BEGIN;
      UPDATE x1 SET a=randomblob(101)
  }
  copydb
  files
} [list {*}{
  test3.database test3.database-journal test3.database001 
  test3.database002 test3.database003
}]
do_test 1.3.1 {
  sqlite3_delete_database test3.database
  files
} {}


do_test 1.4.0 {
  execsql {
    COMMIT;
    PRAGMA journal_mode = wal;
    UPDATE x1 SET a=randomblob(102)
  }
  copydb
  files
} [list {*}{
  test3.database test3.database-shm test3.database-wal test3.database001 
  test3.database002 test3.database003
}]
do_test 1.4.1 {
  sqlite3_delete_database test3.database
  files
} {}


ifcapable 8_3_names {
  db close
  delete_all
  sqlite3 db file:test2.db?8_3_names=1 -uri 1

  do_test 2.1.0 {
    execsql {
      CREATE TABLE t1(x, y);
      BEGIN;
        INSERT INTO t1 VALUES(1, 2);
    }
    copydb
    files
  } {test3.db test3.nal}
  
  do_test 2.1.1 {
    sqlite3_delete_database test3.db
    files
  } {}
  
  do_test 2.2.0 {
    execsql {
      COMMIT;
      PRAGMA journal_mode = wal;
      INSERT INTO t1 VALUES(3, 4);
    }
    copydb
    files
  } {test3.db test3.shm test3.wal}
  do_test 2.2.1 {
    sqlite3_delete_database test3.db
    files
  } {}


  db close
  delete_all
  sqlite3_multiplex_initialize "" 0
  sqlite3 db file:test2.db?8_3_names=1 -uri 1 -vfs multiplex
  sqlite3_multiplex_control db "main" chunk_size 32768
  
  do_test 2.3.0 {
    execsql { PRAGMA auto_vacuum = 0; }
    execsql {
      CREATE TABLE x1(a, b);
      WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 )
      INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s;
      BEGIN;
        UPDATE x1 SET a=randomblob(101)
    }
    copydb
    files
  } [list {*}{
    test3.001 test3.002 test3.003 test3.db test3.nal 
  }]
  do_test 2.3.1 {
    sqlite3_delete_database test3.db
    files
  } {}
  
  
  do_test 2.4.0 {
    execsql {
      COMMIT;
      PRAGMA journal_mode = wal;
      UPDATE x1 SET a=randomblob(102)
    }
    copydb
    files
  } [list {*}{
    test3.001 test3.002 test3.003 test3.db test3.db-shm test3.wal 
  }]
  do_test 2.4.1 {
    sqlite3_delete_database test3.db
    files
  } {}
}

db close
delete_all
sqlite3_multiplex_shutdown 

do_test 3.0 {
  file mkdir dir2.db
  sqlite3_delete_database dir2.db
} {SQLITE_ERROR}
do_test 3.1 {
  sqlite3_delete_database dir2.db/test.db
} {SQLITE_OK}

finish_test