interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / default.test

221 lines · 6686 bytes Raw Blame History
1# 2005 August 18
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 corner cases of the DEFAULT syntax
13# on table definitions.
14#
15# $Id: default.test,v 1.3 2009/02/19 14:39:25 danielk1977 Exp $
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21ifcapable bloblit {
22 do_test default-1.1 {
23 execsql {
24 CREATE TABLE t1(
25 a INTEGER,
26 b BLOB DEFAULT x'6869'
27 );
28 INSERT INTO t1(a) VALUES(1);
29 SELECT * from t1;
30 }
31 } {1 hi}
32}
33do_test default-1.2 {
34 execsql {
35 CREATE TABLE t2(
36 x INTEGER,
37 y INTEGER DEFAULT NULL
38 );
39 INSERT INTO t2(x) VALUES(1);
40 SELECT * FROM t2;
41 }
42} {1 {}}
43do_test default-1.3 {
44 catchsql {
45 CREATE TABLE t3(
46 x INTEGER,
47 y INTEGER DEFAULT (max(x,5))
48 )
49 }
50} {1 {default value of column [y] is not constant}}
51
52ifcapable pragma {
53 do_test default-2.1 {
54 execsql {
55 CREATE TABLE t4(c DEFAULT 'abc');
56 PRAGMA table_info(t4);
57 }
58 } {0 c {} 0 'abc' 0}
59 do_test default-2.2 {
60 execsql {
61 INSERT INTO t4 DEFAULT VALUES;
62 PRAGMA table_info(t4);
63 }
64 } {0 c {} 0 'abc' 0}
65}
66
67do_execsql_test default-3.1 {
68 CREATE TABLE t3(
69 a INTEGER PRIMARY KEY AUTOINCREMENT,
70 b INT DEFAULT 12345 UNIQUE NOT NULL CHECK( b>=0 AND b<99999 ),
71 c VARCHAR(123,456) DEFAULT 'hello' NOT NULL ON CONFLICT REPLACE,
72 d REAL,
73 e FLOATING POINT(5,10) DEFAULT 4.36,
74 f NATIONAL CHARACTER(15) COLLATE RTRIM,
75 g LONG INTEGER DEFAULT( 3600*12 )
76 );
77 INSERT INTO t3 VALUES(null, 5, 'row1', '5.25', 'xyz', 321, '432');
78 SELECT a, typeof(a), b, typeof(b), c, typeof(c),
79 d, typeof(d), e, typeof(e), f, typeof(f),
80 g, typeof(g) FROM t3;
81} {1 integer 5 integer row1 text 5.25 real xyz text 321 text 432 integer}
82do_execsql_test default-3.2 {
83 DELETE FROM t3;
84 INSERT INTO t3 DEFAULT VALUES;
85 SELECT * FROM t3;
86} {2 12345 hello {} 4.36 {} 43200}
87do_execsql_test default-3.3 {
88 CREATE TABLE t300(
89 a INT DEFAULT 2147483647,
90 b INT DEFAULT 2147483648,
91 c INT DEFAULT +9223372036854775807,
92 d INT DEFAULT -2147483647,
93 e INT DEFAULT -2147483648,
94 f INT DEFAULT -9223372036854775808,
95 g INT DEFAULT (-(-9223372036854775808)),
96 h INT DEFAULT (-(-9223372036854775807))
97 );
98 INSERT INTO t300 DEFAULT VALUES;
99 SELECT * FROM t300;
100} {2147483647 2147483648 9223372036854775807 -2147483647 -2147483648 -9223372036854775808 9.22337203685478e+18 9223372036854775807}
101
102# Do now allow bound parameters in new DEFAULT values.
103# Silently convert bound parameters to NULL in DEFAULT causes
104# in the sqlite_master table, for backwards compatibility.
105#
106db close
107forcedelete test.db
108sqlite3 db test.db
109sqlite3_db_config db DEFENSIVE 0
110do_execsql_test default-4.0 {
111 CREATE TABLE t1(a TEXT, b TEXT DEFAULT(99));
112 PRAGMA writable_schema=ON;
113 UPDATE sqlite_master SET sql='CREATE TABLE t1(a TEXT, b TEXT DEFAULT(:xyz))';
114} {}
115db close
116sqlite3 db test.db
117do_execsql_test default-4.1 {
118 INSERT INTO t1(a) VALUES('xyzzy');
119 SELECT a, quote(b) FROM t1;
120} {xyzzy NULL}
121do_catchsql_test default-4.2 {
122 CREATE TABLE t2(a TEXT, b TEXT DEFAULT(:xyz));
123} {1 {default value of column [b] is not constant}}
124do_catchsql_test default-4.3 {
125 CREATE TABLE t2(a TEXT, b TEXT DEFAULT(abs(:xyz)));
126} {1 {default value of column [b] is not constant}}
127do_catchsql_test default-4.4 {
128 CREATE TABLE t2(a TEXT, b TEXT DEFAULT(98+coalesce(5,:xyz)));
129} {1 {default value of column [b] is not constant}}
130
131# 2020-03-09 out-of-bounds memory access discovered by "Eternal Sakura"
132# and reported to chromium.
133#
134reset_db
135do_catchsql_test default-5.1 {
136 CREATE TABLE t1 (a,b DEFAULT(random() NOTNULL IN (RAISE(IGNORE),2,3)));
137 INSERT INTO t1(a) VALUES(1);
138} {1 {default value of column [b] is not constant}}
139do_catchsql_test default-5.2 {
140 CREATE TABLE Table0 (Col0 DEFAULT (RAISE(IGNORE) ) ) ;
141 INSERT INTO Table0 DEFAULT VALUES ;
142} {1 {default value of column [Col0] is not constant}}
143
144#-------------------------------------------------------------------------
145# Large integer values as defaults for columns with real affinity. Report
146# <https://sqlite.org/bugs/forumpost/26a45166b3>
147#
148ifcapable altertable {
149 do_execsql_test 6.0 {
150 CREATE TABLE t(id INTEGER PRIMARY KEY, k INT);
151 INSERT INTO t VALUES(1,1),(2,2);
152 ALTER TABLE t ADD COLUMN c REAL DEFAULT 9223372036854775807;
153 CREATE INDEX ti ON t(c);
154 INSERT INTO t(id, k) VALUES(3,3);
155 }
156
157 do_execsql_test 1.1 {
158 SELECT DISTINCT typeof(c), quote(c) FROM t;
159 } {real 9.2233720368547758e+18}
160
161 do_execsql_test 6.2 {
162 SELECT (SELECT c FROM t WHERE id=1) = (SELECT c FROM t WHERE id=3);
163 } {1}
164
165 do_execsql_test 6.3 {
166 SELECT id FROM t WHERE c = 9.2233720368547758e+18;
167 } {1 2 3}
168
169 do_execsql_test 6.4 {
170 SELECT id FROM t NOT INDEXED WHERE c = 9.2233720368547758e+18;
171 } {1 2 3}
172
173 do_execsql_test 6.5 {
174 PRAGMA integrity_check;
175 } {ok}
176
177 do_execsql_test 6.6 {
178 CREATE TABLE u(id INTEGER PRIMARY KEY, k INT);
179 INSERT INTO u VALUES(1,1);
180 ALTER TABLE u ADD COLUMN c REAL DEFAULT (9223372036854775807);
181 CREATE UNIQUE INDEX ui ON u(c);
182 }
183
184 do_catchsql_test 6.7 {
185 INSERT INTO u(id,k) VALUES(2,2);
186 } {1 {UNIQUE constraint failed: u.c}}
187
188 do_execsql_test 6.8 {
189 SELECT count(*) FROM u NOT INDEXED WHERE c = (SELECT c FROM u WHERE id=1);
190 } {1}
191 do_execsql_test 6.9 {
192 PRAGMA integrity_check;
193 } {ok}
194
195 do_execsql_test 6.10 {
196 CREATE TABLE c1(id INTEGER PRIMARY KEY, k INT,
197 c REAL DEFAULT (9223372036854775807));
198 INSERT INTO c1(id,k) VALUES(1,1),(2,2),(3,3);
199 CREATE INDEX c1i ON c1(c);
200 SELECT id FROM c1 WHERE c = 9.2233720368547758e+18;
201 } {1 2 3}
202
203 do_execsql_test 6.11 {
204 CREATE TABLE c2(id INTEGER PRIMARY KEY, k INT);
205 INSERT INTO c2 VALUES(1,1),(2,2);
206 ALTER TABLE c2 ADD COLUMN c REAL DEFAULT (1000000000000000000);
207 INSERT INTO c2(id,k) VALUES(3,3);
208 CREATE INDEX c2i ON c2(c);
209 SELECT id FROM c2 WHERE c = 1.0e+18;
210 } {1 2 3}
211 do_execsql_test 6.12 {
212 CREATE TABLE c3(id INTEGER PRIMARY KEY, k INT);
213 INSERT INTO c3 VALUES(1,1),(2,2);
214 ALTER TABLE c3 ADD COLUMN c INT DEFAULT (9223372036854775807);
215 INSERT INTO c3(id,k) VALUES(3,3);
216 CREATE INDEX c3i ON c3(c);
217 SELECT id FROM c3 WHERE c = 9223372036854775807;
218 } {1 2 3}
219}
220
221finish_test