blob: 59fb2e76e997f429b6d6e72daeb4d8574abf34a7 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh5974a302000-06-07 14:42:26 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh5974a302000-06-07 14:42:26 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh5974a302000-06-07 14:42:26 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the INSERT statement that takes is
13# result from a SELECT.
14#
drhb19a2bc2001-09-16 00:13:26 +000015# $Id: insert2.test,v 1.4 2001/09/16 00:13:28 drh Exp $
drh5974a302000-06-07 14:42:26 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Create some tables with data that we can select against
21#
22do_test insert2-1.0 {
23 execsql {CREATE TABLE d1(n int, log int);}
24 for {set i 1} {$i<=20} {incr i} {
25 for {set j 0} {pow(2,$j)<$i} {incr j} {}
26 execsql "INSERT INTO d1 VALUES($i,$j)"
27 }
28 execsql {SELECT * FROM d1 ORDER BY n}
29} {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5}
30
31# Insert into a new table from the old one.
32#
33do_test insert2-1.1 {
34 execsql {
35 CREATE TABLE t1(log int, cnt int);
drh3fc190c2001-09-14 03:24:23 +000036---vdbe-trace-on--
drh5974a302000-06-07 14:42:26 +000037 INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
38 }
39 execsql {SELECT * FROM t1 ORDER BY log}
40} {0 1 1 1 2 2 3 4 4 8 5 4}
drh3fc190c2001-09-14 03:24:23 +000041
drh5974a302000-06-07 14:42:26 +000042do_test insert2-1.2 {
43 catch {execsql {DROP TABLE t1}}
44 execsql {
45 CREATE TABLE t1(log int, cnt int);
46 INSERT INTO t1
47 SELECT log, count(*) FROM d1 GROUP BY log
48 EXCEPT SELECT n-1,log FROM d1;
49 SELECT * FROM t1 ORDER BY log;
50 }
51} {0 1 3 4 4 8 5 4}
52do_test insert2-1.3 {
53 catch {execsql {DROP TABLE t1}}
54 execsql {
55 CREATE TABLE t1(log int, cnt int);
56 INSERT INTO t1
57 SELECT log, count(*) FROM d1 GROUP BY log
58 INTERSECT SELECT n-1,log FROM d1;
59 SELECT * FROM t1 ORDER BY log;
60 }
61} {1 1 2 2}
drhcc85b412000-06-07 15:11:27 +000062do_test insert2-1.4 {
63 catch {execsql {DROP TABLE t1}}
64 set r [execsql {
65 CREATE TABLE t1(log int, cnt int);
66 CREATE INDEX i1 ON t1(log);
67 CREATE INDEX i2 ON t1(cnt);
68 INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
69 SELECT * FROM t1 ORDER BY log;
70 }]
71 lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
72 lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
73} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
drh5974a302000-06-07 14:42:26 +000074
75finish_test