xref: /sqlite-3.40.0/test/misc4.test (revision ea4d9e2d)
1# 2004 Jun 27
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.
12#
13# This file implements tests for miscellanous features that were
14# left out of other test files.
15#
16# $Id: misc4.test,v 1.22 2007/08/13 15:28:35 danielk1977 Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Prepare a statement that will create a temporary table.  Then do
22# a rollback.  Then try to execute the prepared statement.
23#
24do_test misc4-1.1 {
25  set DB [sqlite3_connection_pointer db]
26  execsql {
27    CREATE TABLE t1(x);
28    INSERT INTO t1 VALUES(1);
29  }
30} {}
31
32ifcapable tempdb {
33  do_test misc4-1.2 {
34    set sql {CREATE TEMP TABLE t2 AS SELECT * FROM t1}
35    set stmt [sqlite3_prepare $DB $sql -1 TAIL]
36    execsql {
37      BEGIN;
38      CREATE TABLE t3(a,b,c);
39      INSERT INTO t1 SELECT * FROM t1;
40      ROLLBACK;
41    }
42  } {}
43
44  # Because the previous transaction included a DDL statement and
45  # was rolled back, statement $stmt was marked as expired. Executing it
46  # now returns SQLITE_SCHEMA.
47  do_test misc4-1.2.1 {
48    list [sqlite3_step $stmt] [sqlite3_finalize $stmt]
49  } {SQLITE_ERROR SQLITE_SCHEMA}
50  do_test misc4-1.2.2 {
51    set stmt [sqlite3_prepare $DB $sql -1 TAIL]
52    set TAIL
53  } {}
54
55  do_test misc4-1.3 {
56    sqlite3_step $stmt
57  } SQLITE_DONE
58  do_test misc4-1.4 {
59    execsql {
60      SELECT * FROM temp.t2;
61    }
62  } {1}
63
64  # Drop the temporary table, then rerun the prepared  statement to
65  # recreate it again.  This recreates ticket #807.
66  #
67  do_test misc4-1.5 {
68    execsql {DROP TABLE t2}
69    sqlite3_reset $stmt
70    sqlite3_step $stmt
71  } {SQLITE_ERROR}
72  do_test misc4-1.6 {
73    sqlite3_finalize $stmt
74  } {SQLITE_SCHEMA}
75}
76
77# Prepare but do not execute various CREATE statements.  Then before
78# those statements are executed, try to use the tables, indices, views,
79# are triggers that were created.
80#
81do_test misc4-2.1 {
82  set stmt [sqlite3_prepare $DB {CREATE TABLE t3(x);} -1 TAIL]
83  catchsql {
84    INSERT INTO t3 VALUES(1);
85  }
86} {1 {no such table: t3}}
87do_test misc4-2.2 {
88  sqlite3_step $stmt
89} SQLITE_DONE
90do_test misc4-2.3 {
91  sqlite3_finalize $stmt
92} SQLITE_OK
93do_test misc4-2.4 {
94  catchsql {
95    INSERT INTO t3 VALUES(1);
96  }
97} {0 {}}
98
99# Ticket #966
100#
101ifcapable compound {
102do_test misc4-3.1 {
103  execsql {
104    CREATE TABLE Table1(ID integer primary key, Value TEXT);
105    INSERT INTO Table1 VALUES(1, 'x');
106    CREATE TABLE Table2(ID integer NOT NULL, Value TEXT);
107    INSERT INTO Table2 VALUES(1, 'z');
108    INSERT INTO Table2 VALUES (1, 'a');
109    SELECT ID, Value FROM Table1
110       UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1
111    ORDER BY 1, 2;
112  }
113} {1 x 1 z}
114do_test misc4-3.2 {
115  catchsql {
116    SELECT ID, Value FROM Table1
117       UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
118    ORDER BY 1, 2;
119  }
120} {1 {aggregate functions are not allowed in the GROUP BY clause}}
121} ;# ifcapable compound
122
123# Ticket #1047.  Make sure column types are preserved in subqueries.
124#
125ifcapable subquery {
126  do_test misc4-4.1 {
127    execsql {
128      create table a(key varchar, data varchar);
129      create table b(key varchar, period integer);
130      insert into a values('01','data01');
131      insert into a values('+1','data+1');
132
133      insert into b values ('01',1);
134      insert into b values ('01',2);
135      insert into b values ('+1',3);
136      insert into b values ('+1',4);
137
138      select a.*, x.*
139        from a, (select key,sum(period) from b group by key) as x
140        where a.key=x.key;
141    }
142  } {01 data01 01 3 +1 data+1 +1 7}
143
144  # This test case tests the same property as misc4-4.1, but it is
145  # a bit smaller which makes it easier to work with while debugging.
146  do_test misc4-4.2 {
147    execsql {
148      CREATE TABLE ab(a TEXT, b TEXT);
149      INSERT INTO ab VALUES('01', '1');
150    }
151    execsql {
152      select * from ab, (select b from ab) as x where x.b = ab.a;
153    }
154  } {}
155}
156
157
158# Ticket #1036.  When creating tables from a SELECT on a view, use the
159# short names of columns.
160#
161ifcapable view {
162  do_test misc4-5.1 {
163    execsql {
164      create table t4(a,b);
165      create table t5(a,c);
166      insert into t4 values (1,2);
167      insert into t5 values (1,3);
168      create view myview as select t4.a a from t4 inner join t5 on t4.a=t5.a;
169      create table problem as select * from myview;
170    }
171    execsql2 {
172      select * FROM problem;
173    }
174  } {a 1}
175  do_test misc4-5.2 {
176    execsql2 {
177      create table t6 as select * from t4, t5;
178      select * from t6;
179    }
180  } {a 1 b 2 a:1 1 c 3}
181}
182
183# Ticket #1086
184do_test misc4-6.1 {
185  execsql {
186    CREATE TABLE abc(a);
187    INSERT INTO abc VALUES(1);
188    CREATE TABLE def(d, e, f, PRIMARY KEY(d, e));
189  }
190} {}
191do_test misc4-6.2 {
192  execsql {
193    SELECT a FROM abc LEFT JOIN def ON (abc.a=def.d);
194  }
195} {1}
196
197finish_test
198