xref: /sqlite-3.40.0/test/strict1.test (revision fb8ca7de)
1# 2021-08-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#
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing STRICT tables.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix strict1
19
20# STRICT tables have on a limited number of allowed datatypes.
21#
22do_catchsql_test strict1-1.1 {
23  CREATE TABLE t1(a) STRICT;
24} {1 {missing datatype for t1.a}}
25do_catchsql_test strict1-1.2 {
26  CREATE TABLE t1(a PRIMARY KEY) STRICT, WITHOUT ROWID;
27} {1 {missing datatype for t1.a}}
28do_catchsql_test strict1-1.3 {
29  CREATE TABLE t1(a PRIMARY KEY) WITHOUT ROWID, STRICT;
30} {1 {missing datatype for t1.a}}
31do_catchsql_test strict1-1.4 {
32  CREATE TABLE t1(a BANJO PRIMARY KEY) WITHOUT ROWID, STRICT;
33} {1 {unknown datatype for t1.a: "BANJO"}}
34do_catchsql_test strict1-1.5 {
35  CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f DATE) strict;
36} {1 {unknown datatype for t1.f: "DATE"}}
37do_catchsql_test strict1-1.6 {
38  CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f TEXT(50)) WITHOUT ROWID, STRICT;
39} {1 {unknown datatype for t1.f: "TEXT(50)"}}
40
41do_execsql_test strict1-2.0 {
42  CREATE TABLE t1(
43    a INT,
44    b INTEGER,
45    c BLOB,
46    d TEXT,
47    e REAL
48  ) STRICT;
49} {}
50do_catchsql_test strict1-2.1 {
51  INSERT INTO t1(a) VALUES('xyz');
52} {1 {cannot store TEXT value in INT column t1.a}}
53do_catchsql_test strict1-2.2 {
54  INSERT INTO t1(b) VALUES('xyz');
55} {1 {cannot store TEXT value in INTEGER column t1.b}}
56do_catchsql_test strict1-2.3 {
57  INSERT INTO t1(c) VALUES('xyz');
58} {1 {cannot store TEXT value in BLOB column t1.c}}
59do_catchsql_test strict1-2.4 {
60  INSERT INTO t1(d) VALUES(x'3142536475');
61} {1 {cannot store BLOB value in TEXT column t1.d}}
62do_catchsql_test strict1-2.5 {
63  INSERT INTO t1(e) VALUES('xyz');
64} {1 {cannot store TEXT value in REAL column t1.e}}
65
66do_execsql_test strict1-3.1 {
67  INSERT INTO t1(a, b) VALUES(1,2),('3','4'),(5.0, 6.0),(null,null);
68  SELECT a, b, '|' FROM t1;
69} {1 2 | 3 4 | 5 6 | {} {} |}
70do_catchsql_test strict1-3.2 {
71  INSERT INTO t1(a) VALUES(1.2);
72} {1 {cannot store REAL value in INT column t1.a}}
73do_catchsql_test strict1-3.3 {
74  INSERT INTO t1(a) VALUES(x'313233');
75} {1 {cannot store BLOB value in INT column t1.a}}
76do_catchsql_test strict1-3.4 {
77  INSERT INTO t1(b) VALUES(1.2);
78} {1 {cannot store REAL value in INTEGER column t1.b}}
79do_catchsql_test strict1-3.5 {
80  INSERT INTO t1(b) VALUES(x'313233');
81} {1 {cannot store BLOB value in INTEGER column t1.b}}
82
83do_execsql_test strict1-4.1 {
84  DELETE FROM t1;
85  INSERT INTO t1(c) VALUES(x'313233'), (NULL);
86  SELECT typeof(c), c FROM t1;
87} {blob 123 null {}}
88do_catchsql_test strict1-4.2 {
89  INSERT INTO t1(c) VALUES('456');
90} {1 {cannot store TEXT value in BLOB column t1.c}}
91
92do_execsql_test strict1-5.1 {
93  DELETE FROM t1;
94  INSERT INTO t1(d) VALUES('xyz'),(4),(5.5),(NULL);
95  SELECT typeof(d), d FROM t1;
96} {text xyz text 4 text 5.5 null {}}
97do_catchsql_test strict1-5.2 {
98  INSERT INTO t1(d) VALUES(x'4567');
99} {1 {cannot store BLOB value in TEXT column t1.d}}
100
101do_execsql_test strict1-6.1 {
102  DELETE FROM t1;
103  INSERT INTO t1(e) VALUES(1),(2.5),('3'),('4.5'),(6.0),(NULL);
104  SELECT typeof(e), e FROM t1;
105} {real 1.0 real 2.5 real 3.0 real 4.5 real 6.0 null {}}
106do_catchsql_test strict1-6.2 {
107  INSERT INTO t1(e) VALUES('xyz');
108} {1 {cannot store TEXT value in REAL column t1.e}}
109do_catchsql_test strict1-6.3 {
110  INSERT INTO t1(e) VALUES(x'3456');
111} {1 {cannot store BLOB value in REAL column t1.e}}
112
113finish_test
114