xref: /sqlite-3.40.0/test/insert2.test (revision 3fc190cc)
1# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA  02111-1307, USA.
17#
18# Author contact information:
19#   [email protected]
20#   http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library.  The
24# focus of this file is testing the INSERT statement that takes is
25# result from a SELECT.
26#
27# $Id: insert2.test,v 1.3 2001/09/14 03:24:25 drh Exp $
28
29set testdir [file dirname $argv0]
30source $testdir/tester.tcl
31
32# Create some tables with data that we can select against
33#
34do_test insert2-1.0 {
35  execsql {CREATE TABLE d1(n int, log int);}
36  for {set i 1} {$i<=20} {incr i} {
37    for {set j 0} {pow(2,$j)<$i} {incr j} {}
38    execsql "INSERT INTO d1 VALUES($i,$j)"
39  }
40  execsql {SELECT * FROM d1 ORDER BY n}
41} {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}
42
43# Insert into a new table from the old one.
44#
45do_test insert2-1.1 {
46  execsql {
47    CREATE TABLE t1(log int, cnt int);
48---vdbe-trace-on--
49    INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
50  }
51  execsql {SELECT * FROM t1 ORDER BY log}
52} {0 1 1 1 2 2 3 4 4 8 5 4}
53
54do_test insert2-1.2 {
55  catch {execsql {DROP TABLE t1}}
56  execsql {
57    CREATE TABLE t1(log int, cnt int);
58    INSERT INTO t1
59       SELECT log, count(*) FROM d1 GROUP BY log
60       EXCEPT SELECT n-1,log FROM d1;
61    SELECT * FROM t1 ORDER BY log;
62  }
63} {0 1 3 4 4 8 5 4}
64do_test insert2-1.3 {
65  catch {execsql {DROP TABLE t1}}
66  execsql {
67    CREATE TABLE t1(log int, cnt int);
68    INSERT INTO t1
69       SELECT log, count(*) FROM d1 GROUP BY log
70       INTERSECT SELECT n-1,log FROM d1;
71    SELECT * FROM t1 ORDER BY log;
72  }
73} {1 1 2 2}
74do_test insert2-1.4 {
75  catch {execsql {DROP TABLE t1}}
76  set r [execsql {
77    CREATE TABLE t1(log int, cnt int);
78    CREATE INDEX i1 ON t1(log);
79    CREATE INDEX i2 ON t1(cnt);
80    INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
81    SELECT * FROM t1 ORDER BY log;
82  }]
83  lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
84  lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
85} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
86
87finish_test
88