drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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 | # drh@hwaci.com |
| 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 | # |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame^] | 27 | # $Id: insert2.test,v 1.3 2001/09/14 03:24:25 drh Exp $ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 28 | |
| 29 | set testdir [file dirname $argv0] |
| 30 | source $testdir/tester.tcl |
| 31 | |
| 32 | # Create some tables with data that we can select against |
| 33 | # |
| 34 | do_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 | # |
| 45 | do_test insert2-1.1 { |
| 46 | execsql { |
| 47 | CREATE TABLE t1(log int, cnt int); |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame^] | 48 | ---vdbe-trace-on-- |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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} |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame^] | 53 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 54 | do_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} |
| 64 | do_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} |
drh | cc85b41 | 2000-06-07 15:11:27 +0000 | [diff] [blame] | 74 | do_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}} |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 86 | |
| 87 | finish_test |