drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 9 | # |
| 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 | # |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame^] | 15 | # $Id: insert2.test,v 1.6 2002/02/03 19:06:03 drh Exp $ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Create some tables with data that we can select against |
| 21 | # |
| 22 | do_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 | # |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 33 | do_test insert2-1.1.1 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 34 | execsql { |
| 35 | CREATE TABLE t1(log int, cnt int); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 36 | PRAGMA count_changes=on; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 37 | INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log; |
| 38 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 39 | } {6} |
| 40 | do_test insert2-1.1.2 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 41 | execsql {SELECT * FROM t1 ORDER BY log} |
| 42 | } {0 1 1 1 2 2 3 4 4 8 5 4} |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 43 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 44 | do_test insert2-1.2.1 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 45 | catch {execsql {DROP TABLE t1}} |
| 46 | execsql { |
| 47 | CREATE TABLE t1(log int, cnt int); |
| 48 | INSERT INTO t1 |
| 49 | SELECT log, count(*) FROM d1 GROUP BY log |
| 50 | EXCEPT SELECT n-1,log FROM d1; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 51 | } |
| 52 | } {4} |
| 53 | do_test insert2-1.2.2 { |
| 54 | execsql { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 55 | SELECT * FROM t1 ORDER BY log; |
| 56 | } |
| 57 | } {0 1 3 4 4 8 5 4} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 58 | do_test insert2-1.3.1 { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 59 | catch {execsql {DROP TABLE t1}} |
| 60 | execsql { |
| 61 | CREATE TABLE t1(log int, cnt int); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 62 | PRAGMA count_changes=off; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 63 | INSERT INTO t1 |
| 64 | SELECT log, count(*) FROM d1 GROUP BY log |
| 65 | INTERSECT SELECT n-1,log FROM d1; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 66 | } |
| 67 | } {} |
| 68 | do_test insert2-1.3.2 { |
| 69 | execsql { |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 70 | SELECT * FROM t1 ORDER BY log; |
| 71 | } |
| 72 | } {1 1 2 2} |
drh | cc85b41 | 2000-06-07 15:11:27 +0000 | [diff] [blame] | 73 | do_test insert2-1.4 { |
| 74 | catch {execsql {DROP TABLE t1}} |
| 75 | set r [execsql { |
| 76 | CREATE TABLE t1(log int, cnt int); |
| 77 | CREATE INDEX i1 ON t1(log); |
| 78 | CREATE INDEX i2 ON t1(cnt); |
| 79 | INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log; |
| 80 | SELECT * FROM t1 ORDER BY log; |
| 81 | }] |
| 82 | lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}] |
| 83 | lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}] |
| 84 | } {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] | 85 | |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame^] | 86 | do_test insert2-2.0 { |
| 87 | execsql { |
| 88 | CREATE TABLE t3(a,b,c); |
| 89 | CREATE TABLE t4(x,y); |
| 90 | INSERT INTO t4 VALUES(1,2); |
| 91 | SELECT * FROM t4; |
| 92 | } |
| 93 | } {1 2} |
| 94 | do_test insert2-2.1 { |
| 95 | execsql { |
| 96 | INSERT INTO t3(a,c) SELECT * FROM t4; |
| 97 | SELECT * FROM t3; |
| 98 | } |
| 99 | } {1 {} 2} |
| 100 | do_test insert2-2.2 { |
| 101 | execsql { |
| 102 | DELETE FROM t3; |
| 103 | INSERT INTO t3(c,b) SELECT * FROM t4; |
| 104 | SELECT * FROM t3; |
| 105 | } |
| 106 | } {{} 2 1} |
| 107 | do_test insert2-2.3 { |
| 108 | execsql { |
| 109 | DELETE FROM t3; |
| 110 | INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4; |
| 111 | SELECT * FROM t3; |
| 112 | } |
| 113 | } {hi 2 1} |
| 114 | |
| 115 | do_test insert2-4.0 { |
| 116 | set x [execsql {PRAGMA sanity_check}] |
| 117 | if {$x==""} {set x ok} |
| 118 | set x |
| 119 | } {ok} |
| 120 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 121 | finish_test |