Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / sql / bdb_persistent_pragma.test
1 #
2 #    May you do good and not evil.
3 #    May you find forgiveness for yourself and forgive others.
4 #    May you share freely, never taking more than you give.
5 #
6 #***********************************************************************
7 # This file implements regression tests for the persistent pragma API.
8 #
9
10
11 set testdir [file dirname $argv0]/../../lang/sql/sqlite/test
12 source $testdir/tester.tcl
13
14 # Test that pragma journal_size_limit works as expected.
15 #
16
17 set ::pragmafile test.db-journal/pragma
18
19 # Get the value of a persistent pragma that has not been set
20 do_test persistent-1.1.0 {
21   execsql {
22     PRAGMA persistent_echo;
23   }
24 } {{}}
25
26 # Get the schema version
27 do_test persistent-1.1.1 {
28   execsql {
29     PRAGMA persistent_version;
30   }
31 } {1.0}
32  
33 # Test that calling the pragma did not create the file
34 do_test persistent-1.2 {
35   file exists $::pragmafile
36 } {0}
37
38 # Set the persistent pragma
39 do_test persistent-1.3 {
40   execsql {
41     PRAGMA persistent_echo="test";
42   }
43 } {test}
44
45 # Test that setting the pragma did create the file
46 do_test persistent-1.4 {
47   file exists $::pragmafile
48 } {1}
49
50 # Get the pragma
51 do_test persistent-1.5 {
52   execsql {
53     PRAGMA persistent_echo;
54   }
55 } {test}
56
57 db close
58
59 sqlite3 db test.db
60
61 # Test that the pragma is still set after closing the database
62 do_test persistent-1.6 {
63   execsql {
64     PRAGMA persistent_echo;
65   }
66 } {test}
67
68 # Set the pragma to a new value
69 do_test persistent-1.7 {
70   execsql {
71     PRAGMA persistent_echo="test2";
72   }
73 } {test2}
74
75 # Get the pragma
76 do_test persistent-1.8 {
77   execsql {
78     PRAGMA persistent_echo;
79   }
80 } {test2}
81
82 # Get the schema version again
83 do_test persistent-1.8.1 {
84   execsql {
85     PRAGMA persistent_version;
86   }
87 } {1.0}
88
89 # Corrupt the journal directory and get the pragma again. [#19811]
90 do_test persistent-1.9 {
91   # Corrupt the journal directory
92   set filename "test.db-journal"
93   file rename -force $filename "tmpdir"
94   set fileId [open $filename "w"]
95   puts $fileId "Corrupt the directory with this data"
96   close $fileId
97
98   # Get the pragma again to see if the error is correctly handled
99   execsql {
100     PRAGMA persistent_echo;
101   }
102
103   # Restore the juronal file
104   file delete -force $filename
105   file rename -force "tmpdir" $filename
106 } {}
107
108 db close
109
110 #corrupt the pragma file
111 set out [open $::pragmafile w]
112 puts $out "This is not a valid pragma file"
113 close $out
114
115 sqlite3 db test.db
116
117 #Test that a corrupted pragma file is handled correctly
118 do_test persistent-2.0 {
119   catchsql { PRAGMA persistent_echo; }
120 } {1 {Persistent pragma database corrupted. All persistent pragma values lost. Please re-enter all pragmas.}}
121
122 # Test that the corrupted pragma file has been deleted
123 do_test persistent-2.1 {
124   file exists $::pragmafile
125 } {0}
126
127 # Set the pragma, recreating the file
128 do_test persistent-2.2 {
129   execsql {
130     PRAGMA persistent_echo="test3";
131   }
132 } {test3}
133
134 # Get the pragma
135 do_test persistent-2.3 {
136   execsql {
137     PRAGMA persistent_echo;
138   }
139 } {test3}
140
141 # Test that the pragma file has been recreated
142 do_test persistent-2.4 {
143   file exists $::pragmafile
144 } {1}
145
146 # Test the persistent_version pragma
147 do_test persistent-3.0 {
148   execsql {
149     PRAGMA persistent_version;
150   }
151 } {1.0}
152
153 # Setting the persistent_version value does nothing.
154 do_test persistent-3.1 {
155   execsql {
156     PRAGMA persistent_version="2.0";
157   }
158 } {1.0}
159
160 db close
161
162 sqlite3 db test.db
163
164 # Test that a value set before opening the environment
165 # exists after opening it.
166 do_test persistent-4.0 {
167   execsql {
168     PRAGMA persistent_echo="test3";
169   }
170 } {test3}
171
172 do_test persistent-4.1 {
173   execsql {
174     create table t1(a);
175   }
176 } {}
177
178 do_test persistent-4.2 {
179   execsql {
180     PRAGMA persistent_echo;
181   }
182 } {test3}
183
184 # Test that updating a value 3 times does not corrupt it
185 do_test persistent-5.0 {
186   execsql {
187     PRAGMA persistent_echo=test4;
188   }
189 } {test4}
190
191 do_test persistent-5.1 {
192   execsql {
193     PRAGMA persistent_echo=test4;
194   }
195 } {test4}
196
197 do_test persistent-5.2 {
198   execsql {
199     PRAGMA persistent_echo=test4;
200   }
201 } {test4}
202
203 do_test persistent-5.3 {
204   execsql {
205     PRAGMA persistent_echo;
206   }
207 } {test4}
208
209
210 finish_test