xref: /sqlite-3.40.0/test/tempdb.test (revision 69aedc8d)
1110af533Sdrh# 2008 April 14
2110af533Sdrh#
3110af533Sdrh# The author disclaims copyright to this source code.  In place of
4110af533Sdrh# a legal notice, here is a blessing:
5110af533Sdrh#
6110af533Sdrh#    May you do good and not evil.
7110af533Sdrh#    May you find forgiveness for yourself and forgive others.
8110af533Sdrh#    May you share freely, never taking more than you give.
9110af533Sdrh#
10110af533Sdrh#***********************************************************************
11110af533Sdrh# This file implements regression tests for SQLite library.
12110af533Sdrh# The focus of this file is in making sure that rolling back
13110af533Sdrh# a statement journal works correctly.
14110af533Sdrh#
15dda70fe3Sdrh# $Id: tempdb.test,v 1.4 2009/06/05 17:09:12 drh Exp $
16110af533Sdrh
17110af533Sdrhset testdir [file dirname $argv0]
18110af533Sdrhsource $testdir/tester.tcl
19110af533Sdrh
20*69aedc8dSdanif {[atomic_batch_write test.db]} {
21*69aedc8dSdan  finish_test
22*69aedc8dSdan  return
23*69aedc8dSdan}
24*69aedc8dSdan
25110af533Sdrh# Use a temporary database.
26110af533Sdrh#
27110af533Sdrhdb close
28110af533Sdrhsqlite3 db {}
29110af533Sdrh
30110af533Sdrh# Force a statement journal rollback on a database file that
31110af533Sdrh# has never been opened.
32110af533Sdrh#
33110af533Sdrhdo_test tempdb-1.1 {
34110af533Sdrh  execsql {
35110af533Sdrh    BEGIN;
36110af533Sdrh    CREATE TABLE t1(x UNIQUE);
37110af533Sdrh    CREATE TABLE t2(y);
38110af533Sdrh    INSERT INTO t2 VALUES('hello');
39110af533Sdrh    INSERT INTO t2 VALUES(NULL);
40110af533Sdrh  }
41110af533Sdrh  # Because of the transaction, the temporary database file
42110af533Sdrh  # has not even been opened yet.  The following statement
43110af533Sdrh  # will cause a statement journal rollback on this non-existant
44110af533Sdrh  # file.
45110af533Sdrh  catchsql {
46110af533Sdrh    INSERT INTO t1
47110af533Sdrh    SELECT CASE WHEN y IS NULL THEN test_error('oops', 11) ELSE y END
48110af533Sdrh      FROM t2;
49110af533Sdrh  }
50110af533Sdrh} {1 oops}
51110af533Sdrh
52110af533Sdrh# Verify that no writes occurred in t1.
53110af533Sdrh#
54110af533Sdrhdo_test tempdb-1.2 {
55110af533Sdrh  execsql {
56110af533Sdrh    SELECT * FROM t1
57110af533Sdrh  }
58110af533Sdrh} {}
59110af533Sdrh
60d829335eSdanielk1977do_test tempdb-2.1 {
61bd2edd67Sdanielk1977  # Set $::jrnl_in_memory if the journal file is expected to be in-memory.
62bd2edd67Sdanielk1977  # Similarly, set $::subj_in_memory if the sub-journal file is expected
63bd2edd67Sdanielk1977  # to be in memory. These variables are used to calculate the expected
64bd2edd67Sdanielk1977  # number of open files in the test cases below.
65bd2edd67Sdanielk1977  #
66430e74cdSdan  set jrnl_in_memory [expr {[permutation] eq "inmemory_journal"}]
67d2db0906Sdan  set subj_in_memory [expr {$jrnl_in_memory || $TEMP_STORE>=2}]
68bd2edd67Sdanielk1977
69d829335eSdanielk1977  db close
70d829335eSdanielk1977  sqlite3 db test.db
71d829335eSdanielk1977} {}
72d829335eSdanielk1977do_test tempdb-2.2 {
73d829335eSdanielk1977  execsql {
74d829335eSdanielk1977    CREATE TABLE t1 (a PRIMARY KEY, b, c);
75d829335eSdanielk1977    CREATE TABLE t2 (a, b, c);
76d829335eSdanielk1977    BEGIN;
77d829335eSdanielk1977      INSERT INTO t1 VALUES(1, 2, 3);
78d829335eSdanielk1977      INSERT INTO t1 VALUES(4, 5, 6);
79459564f4Sdan      INSERT INTO t2 VALUES(7, 8, 9);
80d829335eSdanielk1977      INSERT INTO t2 SELECT * FROM t1;
81d829335eSdanielk1977  }
82d829335eSdanielk1977  catchsql { INSERT INTO t1 SELECT * FROM t2 }
83d829335eSdanielk1977  set sqlite_open_file_count
843ac9a864Sdrh} [expr 1 + (0==$jrnl_in_memory)]
85d829335eSdanielk1977do_test tempdb-2.3 {
86d829335eSdanielk1977  execsql {
87d829335eSdanielk1977    PRAGMA temp_store = 'memory';
88d829335eSdanielk1977    ROLLBACK;
89d829335eSdanielk1977    BEGIN;
90d829335eSdanielk1977      INSERT INTO t1 VALUES(1, 2, 3);
91d829335eSdanielk1977      INSERT INTO t1 VALUES(4, 5, 6);
92d829335eSdanielk1977      INSERT INTO t2 SELECT * FROM t1;
93d829335eSdanielk1977  }
94d829335eSdanielk1977  catchsql { INSERT INTO t1 SELECT * FROM t2 }
95d829335eSdanielk1977  set sqlite_open_file_count
96bd2edd67Sdanielk1977} [expr 1 + (0==$jrnl_in_memory)]
97d829335eSdanielk1977
98110af533Sdrhfinish_test
99