xref: /sqlite-3.40.0/test/strict1.test (revision 1f3366cd)
144183f83Sdrh# 2021-08-18
244183f83Sdrh#
344183f83Sdrh# The author disclaims copyright to this source code.  In place of
444183f83Sdrh# a legal notice, here is a blessing:
544183f83Sdrh#
644183f83Sdrh#    May you do good and not evil.
744183f83Sdrh#    May you find forgiveness for yourself and forgive others.
844183f83Sdrh#    May you share freely, never taking more than you give.
944183f83Sdrh#
1044183f83Sdrh#***********************************************************************
1144183f83Sdrh#
1244183f83Sdrh# This file implements regression tests for SQLite library.  The
1344183f83Sdrh# focus of this file is testing STRICT tables.
1444183f83Sdrh#
1544183f83Sdrh
1644183f83Sdrhset testdir [file dirname $argv0]
1744183f83Sdrhsource $testdir/tester.tcl
1844183f83Sdrhset testprefix strict1
1944183f83Sdrh
2044183f83Sdrh# STRICT tables have on a limited number of allowed datatypes.
2144183f83Sdrh#
2244183f83Sdrhdo_catchsql_test strict1-1.1 {
23b9fd0101Sdrh  CREATE TABLE t1(a) STRICT;
24b9fd0101Sdrh} {1 {missing datatype for t1.a}}
2544183f83Sdrhdo_catchsql_test strict1-1.2 {
26b9fd0101Sdrh  CREATE TABLE t1(a PRIMARY KEY) STRICT, WITHOUT ROWID;
27b9fd0101Sdrh} {1 {missing datatype for t1.a}}
2844183f83Sdrhdo_catchsql_test strict1-1.3 {
29b9fd0101Sdrh  CREATE TABLE t1(a PRIMARY KEY) WITHOUT ROWID, STRICT;
30b9fd0101Sdrh} {1 {missing datatype for t1.a}}
3144183f83Sdrhdo_catchsql_test strict1-1.4 {
3244183f83Sdrh  CREATE TABLE t1(a BANJO PRIMARY KEY) WITHOUT ROWID, STRICT;
3344183f83Sdrh} {1 {unknown datatype for t1.a: "BANJO"}}
3444183f83Sdrhdo_catchsql_test strict1-1.5 {
3544183f83Sdrh  CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f DATE) strict;
3644183f83Sdrh} {1 {unknown datatype for t1.f: "DATE"}}
3744183f83Sdrhdo_catchsql_test strict1-1.6 {
3844183f83Sdrh  CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f TEXT(50)) WITHOUT ROWID, STRICT;
3944183f83Sdrh} {1 {unknown datatype for t1.f: "TEXT(50)"}}
4044183f83Sdrh
4172532f52Sdrhdo_execsql_test strict1-2.0 {
4272532f52Sdrh  CREATE TABLE t1(
4372532f52Sdrh    a INT,
4472532f52Sdrh    b INTEGER,
4572532f52Sdrh    c BLOB,
4672532f52Sdrh    d TEXT,
4772532f52Sdrh    e REAL
4872532f52Sdrh  ) STRICT;
4937f3ac8fSdan} {}
5037f3ac8fSdanifcapable vtab {
5137f3ac8fSdan  do_execsql_test strict1-2.0a {
522e50f670Sdrh    SELECT strict FROM pragma_table_list('t1');
532e50f670Sdrh  } {1}
5437f3ac8fSdan}
5572532f52Sdrhdo_catchsql_test strict1-2.1 {
5672532f52Sdrh  INSERT INTO t1(a) VALUES('xyz');
57faf9c77bSdrh} {1 {cannot store TEXT value in INT column t1.a}}
5872532f52Sdrhdo_catchsql_test strict1-2.2 {
5972532f52Sdrh  INSERT INTO t1(b) VALUES('xyz');
60faf9c77bSdrh} {1 {cannot store TEXT value in INTEGER column t1.b}}
6172532f52Sdrhdo_catchsql_test strict1-2.3 {
6272532f52Sdrh  INSERT INTO t1(c) VALUES('xyz');
63faf9c77bSdrh} {1 {cannot store TEXT value in BLOB column t1.c}}
6472532f52Sdrhdo_catchsql_test strict1-2.4 {
6572532f52Sdrh  INSERT INTO t1(d) VALUES(x'3142536475');
66faf9c77bSdrh} {1 {cannot store BLOB value in TEXT column t1.d}}
6772532f52Sdrhdo_catchsql_test strict1-2.5 {
6872532f52Sdrh  INSERT INTO t1(e) VALUES('xyz');
69faf9c77bSdrh} {1 {cannot store TEXT value in REAL column t1.e}}
7072532f52Sdrh
712e50f670Sdrh
7272532f52Sdrhdo_execsql_test strict1-3.1 {
7372532f52Sdrh  INSERT INTO t1(a, b) VALUES(1,2),('3','4'),(5.0, 6.0),(null,null);
7472532f52Sdrh  SELECT a, b, '|' FROM t1;
7572532f52Sdrh} {1 2 | 3 4 | 5 6 | {} {} |}
7672532f52Sdrhdo_catchsql_test strict1-3.2 {
7772532f52Sdrh  INSERT INTO t1(a) VALUES(1.2);
78faf9c77bSdrh} {1 {cannot store REAL value in INT column t1.a}}
7972532f52Sdrhdo_catchsql_test strict1-3.3 {
8072532f52Sdrh  INSERT INTO t1(a) VALUES(x'313233');
81faf9c77bSdrh} {1 {cannot store BLOB value in INT column t1.a}}
8272532f52Sdrhdo_catchsql_test strict1-3.4 {
8372532f52Sdrh  INSERT INTO t1(b) VALUES(1.2);
84faf9c77bSdrh} {1 {cannot store REAL value in INTEGER column t1.b}}
8572532f52Sdrhdo_catchsql_test strict1-3.5 {
8672532f52Sdrh  INSERT INTO t1(b) VALUES(x'313233');
87faf9c77bSdrh} {1 {cannot store BLOB value in INTEGER column t1.b}}
8872532f52Sdrh
8972532f52Sdrhdo_execsql_test strict1-4.1 {
9072532f52Sdrh  DELETE FROM t1;
9172532f52Sdrh  INSERT INTO t1(c) VALUES(x'313233'), (NULL);
9272532f52Sdrh  SELECT typeof(c), c FROM t1;
9372532f52Sdrh} {blob 123 null {}}
9472532f52Sdrhdo_catchsql_test strict1-4.2 {
9572532f52Sdrh  INSERT INTO t1(c) VALUES('456');
96faf9c77bSdrh} {1 {cannot store TEXT value in BLOB column t1.c}}
9772532f52Sdrh
98ab16578bSdrhdo_execsql_test strict1-5.1 {
99ab16578bSdrh  DELETE FROM t1;
100ab16578bSdrh  INSERT INTO t1(d) VALUES('xyz'),(4),(5.5),(NULL);
101ab16578bSdrh  SELECT typeof(d), d FROM t1;
102ab16578bSdrh} {text xyz text 4 text 5.5 null {}}
103ab16578bSdrhdo_catchsql_test strict1-5.2 {
104ab16578bSdrh  INSERT INTO t1(d) VALUES(x'4567');
105faf9c77bSdrh} {1 {cannot store BLOB value in TEXT column t1.d}}
106ab16578bSdrh
107ab16578bSdrhdo_execsql_test strict1-6.1 {
108ab16578bSdrh  DELETE FROM t1;
109ab16578bSdrh  INSERT INTO t1(e) VALUES(1),(2.5),('3'),('4.5'),(6.0),(NULL);
110ab16578bSdrh  SELECT typeof(e), e FROM t1;
111ab16578bSdrh} {real 1.0 real 2.5 real 3.0 real 4.5 real 6.0 null {}}
112ab16578bSdrhdo_catchsql_test strict1-6.2 {
113ab16578bSdrh  INSERT INTO t1(e) VALUES('xyz');
114faf9c77bSdrh} {1 {cannot store TEXT value in REAL column t1.e}}
115ab16578bSdrhdo_catchsql_test strict1-6.3 {
116ab16578bSdrh  INSERT INTO t1(e) VALUES(x'3456');
117faf9c77bSdrh} {1 {cannot store BLOB value in REAL column t1.e}}
118ab16578bSdrh
1192841abedSdanifcapable altertable {
120926aac51Sdrh  do_execsql_test strict1-7.1 {
121926aac51Sdrh    DROP TABLE IF EXISTS t4;
122926aac51Sdrh    CREATE TABLE t4(
123926aac51Sdrh      a INT AS (b*2) VIRTUAL,
124926aac51Sdrh      b INT AS (c*2) STORED,
125926aac51Sdrh      c INT PRIMARY KEY
126926aac51Sdrh    ) STRICT;
127926aac51Sdrh    INSERT INTO t4(c) VALUES(1);
128926aac51Sdrh    SELECT * FROM t4;
129926aac51Sdrh  } {4 2 1}
1302841abedSdan  do_catchsql_test strict1-7.2 {
131ac894af8Sdrh    ALTER TABLE t4 ADD COLUMN d VARCHAR;
132ac894af8Sdrh  } {1 {error in table t4 after add column: unknown datatype for t4.d: "VARCHAR"}}
1332841abedSdan  do_catchsql_test strict1-7.3 {
134ac894af8Sdrh    ALTER TABLE t4 ADD COLUMN d;
135ac894af8Sdrh  } {1 {error in table t4 after add column: missing datatype for t4.d}}
1362841abedSdan}
137926aac51Sdrh
138*1f3366cdSdrh# 2022-01-17 https://sqlite.org/forum/forumpost/fa012c77796d9399
139*1f3366cdSdrh#
140*1f3366cdSdrhreset_db
141*1f3366cdSdrhdo_execsql_test strict1-8.1 {
142*1f3366cdSdrh  CREATE TABLE csv_import_table (
143*1f3366cdSdrh    "debit" TEXT,
144*1f3366cdSdrh    "credit" TEXT
145*1f3366cdSdrh  );
146*1f3366cdSdrh  INSERT INTO csv_import_table VALUES ('', '250.00');
147*1f3366cdSdrh  CREATE TABLE IF NOT EXISTS transactions (
148*1f3366cdSdrh      debit REAL,
149*1f3366cdSdrh      credit REAL,
150*1f3366cdSdrh      amount REAL GENERATED ALWAYS AS (ifnull(credit, 0.0) - ifnull(debit, 0.0))
151*1f3366cdSdrh  ) STRICT;
152*1f3366cdSdrh  INSERT INTO transactions
153*1f3366cdSdrh  SELECT
154*1f3366cdSdrh      nullif(debit, '') AS debit,
155*1f3366cdSdrh      nullif(credit, '') AS credit
156*1f3366cdSdrh  FROM csv_import_table;
157*1f3366cdSdrh  SELECT * FROM transactions;
158*1f3366cdSdrh} {{} 250.0 250.0}
159*1f3366cdSdrhdo_execsql_test strict1-8.2 {
160*1f3366cdSdrh  CREATE TABLE t1(x REAL, y REAL AS (x)) STRICT;
161*1f3366cdSdrh  INSERT INTO t1 VALUES(5),(4611686018427387904);
162*1f3366cdSdrh  SELECT *, '|' FROM t1;
163*1f3366cdSdrh} {/5.0 5.0 4.6116\d*e\+18 4.6116\d+e\+18 |/}
164*1f3366cdSdrh
16544183f83Sdrhfinish_test
166