blob: a32ef06f445fe9f6ab820f10dbcc61bbefa0d5af [file] [log] [blame]
drh110af532008-04-15 00:01:59 +00001# 2008 April 14
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.
12# The focus of this file is in making sure that rolling back
13# a statement journal works correctly.
14#
drhdda70fe2009-06-05 17:09:11 +000015# $Id: tempdb.test,v 1.4 2009/06/05 17:09:12 drh Exp $
drh110af532008-04-15 00:01:59 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
dan69aedc82018-01-13 13:07:49 +000020if {[atomic_batch_write test.db]} {
21 finish_test
22 return
23}
24
drh110af532008-04-15 00:01:59 +000025# Use a temporary database.
26#
27db close
28sqlite3 db {}
29
30# Force a statement journal rollback on a database file that
31# has never been opened.
32#
33do_test tempdb-1.1 {
34 execsql {
35 BEGIN;
36 CREATE TABLE t1(x UNIQUE);
37 CREATE TABLE t2(y);
38 INSERT INTO t2 VALUES('hello');
39 INSERT INTO t2 VALUES(NULL);
40 }
41 # Because of the transaction, the temporary database file
42 # has not even been opened yet. The following statement
43 # will cause a statement journal rollback on this non-existant
44 # file.
45 catchsql {
46 INSERT INTO t1
47 SELECT CASE WHEN y IS NULL THEN test_error('oops', 11) ELSE y END
48 FROM t2;
49 }
50} {1 oops}
51
52# Verify that no writes occurred in t1.
53#
54do_test tempdb-1.2 {
55 execsql {
56 SELECT * FROM t1
57 }
58} {}
59
danielk1977d8293352009-04-30 09:10:37 +000060do_test tempdb-2.1 {
danielk1977bd2edd62009-05-01 05:23:17 +000061 # Set $::jrnl_in_memory if the journal file is expected to be in-memory.
62 # Similarly, set $::subj_in_memory if the sub-journal file is expected
63 # to be in memory. These variables are used to calculate the expected
64 # number of open files in the test cases below.
65 #
dan430e74c2010-06-07 17:47:26 +000066 set jrnl_in_memory [expr {[permutation] eq "inmemory_journal"}]
dand2db0902010-09-20 14:55:33 +000067 set subj_in_memory [expr {$jrnl_in_memory || $TEMP_STORE>=2}]
danielk1977bd2edd62009-05-01 05:23:17 +000068
danielk1977d8293352009-04-30 09:10:37 +000069 db close
70 sqlite3 db test.db
71} {}
72do_test tempdb-2.2 {
73 execsql {
74 CREATE TABLE t1 (a PRIMARY KEY, b, c);
75 CREATE TABLE t2 (a, b, c);
76 BEGIN;
77 INSERT INTO t1 VALUES(1, 2, 3);
78 INSERT INTO t1 VALUES(4, 5, 6);
dan459564f2010-06-03 12:35:28 +000079 INSERT INTO t2 VALUES(7, 8, 9);
danielk1977d8293352009-04-30 09:10:37 +000080 INSERT INTO t2 SELECT * FROM t1;
81 }
82 catchsql { INSERT INTO t1 SELECT * FROM t2 }
83 set sqlite_open_file_count
drh3ac9a862016-03-04 14:23:10 +000084} [expr 1 + (0==$jrnl_in_memory)]
danielk1977d8293352009-04-30 09:10:37 +000085do_test tempdb-2.3 {
86 execsql {
87 PRAGMA temp_store = 'memory';
88 ROLLBACK;
89 BEGIN;
90 INSERT INTO t1 VALUES(1, 2, 3);
91 INSERT INTO t1 VALUES(4, 5, 6);
92 INSERT INTO t2 SELECT * FROM t1;
93 }
94 catchsql { INSERT INTO t1 SELECT * FROM t2 }
95 set sqlite_open_file_count
danielk1977bd2edd62009-05-01 05:23:17 +000096} [expr 1 + (0==$jrnl_in_memory)]
danielk1977d8293352009-04-30 09:10:37 +000097
drh110af532008-04-15 00:01:59 +000098finish_test