xref: /illumos-gate/usr/src/lib/libsqlite/test/trigger4.test (revision 7dbbfe7762f9eabac3999ee1a8b38311d428f7a8)
1#
2# The author disclaims copyright to this source code.  In place of
3# a legal notice, here is a blessing:
4#
5#    May you do good and not evil.
6#    May you find forgiveness for yourself and forgive others.
7#    May you share freely, never taking more than you give.
8#
9#***********************************************************************
10#
11# This file tests the triggers of views.
12#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16
17do_test trigger4-1.1 {
18  execsql {
19    create table test1(id integer primary key,a);
20    create table test2(id integer,b);
21    create view test as
22      select test1.id as id,a as a,b as b
23      from test1 join test2 on test2.id =  test1.id;
24    create trigger I_test instead of insert on test
25      begin
26        insert into test1 (id,a) values (NEW.id,NEW.a);
27        insert into test2 (id,b) values (NEW.id,NEW.b);
28      end;
29    insert into test values(1,2,3);
30    select * from test1;
31  }
32} {1 2}
33do_test trigger4-1.2 {
34  execsql {
35    select * from test2;
36  }
37} {1 3}
38do_test trigger4-1.3 {
39  db close
40  sqlite db test.db
41  execsql {
42    insert into test values(4,5,6);
43    select * from test1;
44  }
45} {1 2 4 5}
46do_test trigger4-1.4 {
47  execsql {
48    select * from test2;
49  }
50} {1 3 4 6}
51
52do_test trigger4-2.1 {
53  execsql {
54    create trigger U_test instead of update on test
55      begin
56        update test1 set a=NEW.a where id=NEW.id;
57        update test2 set b=NEW.b where id=NEW.id;
58      end;
59    update test set a=22 where id=1;
60    select * from test1;
61  }
62} {1 22 4 5}
63do_test trigger4-2.2 {
64  execsql {
65    select * from test2;
66  }
67} {1 3 4 6}
68do_test trigger4-2.3 {
69  db close
70  sqlite db test.db
71  execsql {
72    update test set b=66 where id=4;
73    select * from test1;
74  }
75} {1 22 4 5}
76do_test trigger4-2.4 {
77  execsql {
78    select * from test2;
79  }
80} {1 3 4 66}
81
82do_test trigger4-3.1 {
83  catchsql {
84    drop table test2;
85    insert into test values(7,8,9);
86  }
87} {1 {no such table: main.test2}}
88do_test trigger4-3.2 {
89  db close
90  sqlite db test.db
91  catchsql {
92    insert into test values(7,8,9);
93  }
94} {1 {no such table: main.test2}}
95do_test trigger4-3.3 {
96  catchsql {
97    update test set a=222 where id=1;
98  }
99} {1 {no such table: main.test2}}
100do_test trigger4-3.4 {
101  execsql {
102    select * from test1;
103  }
104} {1 22 4 5}
105do_test trigger4-3.5 {
106  execsql {
107    create table test2(id,b);
108    insert into test values(7,8,9);
109    select * from test1;
110  }
111} {1 22 4 5 7 8}
112do_test trigger4-3.6 {
113  execsql {
114    select * from test2;
115  }
116} {7 9}
117do_test trigger4-3.7 {
118  db close
119  sqlite db test.db
120  execsql {
121    update test set b=99 where id=7;
122    select * from test2;
123  }
124} {7 99}
125
126integrity_check trigger4-4.1
127
128finish_test
129