xref: /sqlite-3.40.0/test/insert3.test (revision d6fe961e)
1# 2005 January 13
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.  The
12# focus of this file is testing corner cases of the INSERT statement.
13#
14# $Id: insert3.test,v 1.1 2005/01/14 01:22:01 drh Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Create a table and a corresponding insert trigger.  Do a self-insert
20# into the table.
21#
22do_test insert3-1.0 {
23  execsql {
24    CREATE TABLE t1(a,b);
25    CREATE TABLE log(x UNIQUE, y);
26    CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
27      UPDATE log SET y=y+1 WHERE x=new.a;
28      INSERT OR IGNORE INTO log VALUES(new.a, 1);
29    END;
30    INSERT INTO t1 VALUES('hello','world');
31    INSERT INTO t1 VALUES(5,10);
32    SELECT * FROM log ORDER BY x;
33  }
34} {5 1 hello 1}
35do_test insert3-1.1 {
36  execsql {
37    INSERT INTO t1 SELECT a, b+10 FROM t1;
38    SELECT * FROM log ORDER BY x;
39  }
40} {5 2 hello 2}
41do_test insert3-1.2 {
42  execsql {
43    CREATE TABLE log2(x PRIMARY KEY,y);
44    CREATE TRIGGER r2 BEFORE INSERT ON t1 BEGIN
45      UPDATE log2 SET y=y+1 WHERE x=new.b;
46      INSERT OR IGNORE INTO log2 VALUES(new.b,1);
47    END;
48    INSERT INTO t1 VALUES(453,'hi');
49    SELECT * FROM log ORDER BY x;
50  }
51} {5 2 453 1 hello 2}
52do_test insert3-1.3 {
53  execsql {
54    SELECT * FROM log2 ORDER BY x;
55  }
56} {hi 1}
57do_test insert3-1.4 {
58  execsql {
59    INSERT INTO t1 SELECT * FROM t1;
60    SELECT 'a:', x, y FROM log UNION ALL SELECT 'b:', x, y FROM log2 ORDER BY x;
61  }
62} {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
63do_test insert3-1.5 {
64  execsql {
65    INSERT INTO t1(a) VALUES('xyz');
66    SELECT * FROM log ORDER BY x;
67  }
68} {5 4 453 2 hello 4 xyz 1}
69
70do_test insert3-2.1 {
71  execsql {
72    CREATE TABLE t2(
73      a INTEGER PRIMARY KEY,
74      b DEFAULT 'b',
75      c DEFAULT 'c'
76    );
77    CREATE TABLE t2dup(a,b,c);
78    CREATE TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN
79      INSERT INTO t2dup(a,b,c) VALUES(new.a,new.b,new.c);
80    END;
81    INSERT INTO t2(a) VALUES(123);
82    INSERT INTO t2(b) VALUES(234);
83    INSERT INTO t2(c) VALUES(345);
84    SELECT * FROM t2dup;
85  }
86} {123 b c -1 234 c -1 b 345}
87do_test insert3-2.2 {
88  execsql {
89    DELETE FROM t2dup;
90    INSERT INTO t2(a) SELECT 1 FROM t1 LIMIT 1;
91    INSERT INTO t2(b) SELECT 987 FROM t1 LIMIT 1;
92    INSERT INTO t2(c) SELECT 876 FROM t1 LIMIT 1;
93    SELECT * FROM t2dup;
94  }
95} {1 b c -1 987 c -1 b 876}
96
97finish_test
98