-
Notifications
You must be signed in to change notification settings - Fork 376
/
insert3.test.lua
executable file
·331 lines (299 loc) · 8.85 KB
/
insert3.test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(20)
--!./tcltestrunner.lua
-- 2005 January 13
--
-- The author disclaims copyright to this source code. In place of
-- a legal notice, here is a blessing:
--
-- May you do good and not evil.
-- May you find forgiveness for yourself and forgive others.
-- May you share freely, never taking more than you give.
--
-------------------------------------------------------------------------
-- This file implements regression tests for SQLite library. The
-- focus of this file is testing corner cases of the INSERT statement.
--
-- $Id: insert3.test,v 1.9 2009/04/23 14:58:40 danielk1977 Exp $
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
-- All the tests in this file require trigger support
--
-- Create a table and a corresponding insert trigger. Do a self-insert
-- into the table.
--
test:do_execsql_test(
"insert3-1.0",
[[
CREATE TABLE t1(rowid INTEGER PRIMARY KEY AUTOINCREMENT, a,b);
CREATE TABLE log(rowid INTEGER PRIMARY KEY AUTOINCREMENT, x UNIQUE, y);
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
UPDATE log SET y=y+1 WHERE x=new.a;
INSERT OR IGNORE INTO log(x, y) VALUES(new.a, 1);
END;
INSERT INTO t1(a, b) VALUES('hello','world');
INSERT INTO t1(a, b) VALUES(5,10);
SELECT x,y FROM log ORDER BY x;
]], {
-- <insert3-1.0>
5, 1, "hello", 1
-- </insert3-1.0>
})
test:do_execsql_test(
"insert3-1.1",
[[
INSERT INTO t1(a, b) SELECT a, b+10 FROM t1;
SELECT x, y FROM log ORDER BY x;
]], {
-- <insert3-1.1>
5, 2, "hello", 2
-- </insert3-1.1>
})
test:do_execsql_test(
"insert3-1.2",
[[
CREATE TABLE log2(rowid INTEGER PRIMARY KEY AUTOINCREMENT, x UNIQUE,y);
CREATE TRIGGER r2 BEFORE INSERT ON t1 BEGIN
UPDATE log2 SET y=y+1 WHERE x=new.b;
INSERT OR IGNORE INTO log2(x, y) VALUES(new.b,1);
END;
INSERT INTO t1(a, b) VALUES(453,'hi');
SELECT x,y FROM log ORDER BY x;
]], {
-- <insert3-1.2>
5, 2, 453, 1, "hello", 2
-- </insert3-1.2>
})
test:do_execsql_test(
"insert3-1.3",
[[
SELECT x,y FROM log2 ORDER BY x;
]], {
-- <insert3-1.3>
"hi", 1
-- </insert3-1.3>
})
test:do_execsql_test(
"insert3-1.4.1",
[[
INSERT INTO t1(a,b) SELECT a,b FROM t1;
SELECT 'a:', x, y FROM log UNION ALL
SELECT 'b:', x, y FROM log2 ORDER BY x;
]], {
-- <insert3-1.4.1>
"a:", 5, 4, "b:", 10, 2, "b:", 20, 1, "a:", 453, 2, "a:", "hello", 4, "b:", "hi", 2, "b:", "world", 1
-- </insert3-1.4.1>
})
test:do_execsql_test(
"insert3-1.4.2",
[[
SELECT 'a:', x, y FROM log UNION ALL
SELECT 'b:', x, y FROM log2 ORDER BY x, y;
]], {
-- <insert3-1.4.2>
"a:", 5, 4, "b:", 10, 2, "b:", 20, 1, "a:", 453, 2, "a:", "hello", 4, "b:", "hi", 2, "b:", "world", 1
-- </insert3-1.4.2>
})
test:do_execsql_test(
"insert3-1.5",
[[
INSERT INTO t1(a) VALUES('xyz');
SELECT x,y FROM log ORDER BY x;
]], {
-- <insert3-1.5>
5, 4, 453, 2, "hello", 4, "xyz", 1
-- </insert3-1.5>
})
test:do_execsql_test(
"insert3-2.1",
[[
CREATE TABLE t2(
a INTEGER PRIMARY KEY AUTOINCREMENT,
b DEFAULT 'b',
c DEFAULT 'c'
);
CREATE TABLE t2dup(rowid INTEGER PRIMARY KEY AUTOINCREMENT, a,b,c);
CREATE TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN
INSERT INTO t2dup(a,b,c) VALUES(new.a,new.b,new.c);
END;
INSERT INTO t2(a) VALUES(123);
INSERT INTO t2(b) VALUES(234);
INSERT INTO t2(c) VALUES(345);
SELECT * FROM t2dup;
]], {
-- <insert3-2.1>
1, 123, "b", "c", 2, -1, 234, "c", 3, -1, "b", 345
-- </insert3-2.1>
})
test:do_execsql_test(
"insert3-2.2",
[[
DELETE FROM t2dup;
INSERT INTO t2(a) SELECT 1 FROM t1 LIMIT 1;
INSERT INTO t2(b) SELECT 987 FROM t1 LIMIT 1;
INSERT INTO t2(c) SELECT 876 FROM t1 LIMIT 1;
SELECT * FROM t2dup;
]], {
-- <insert3-2.2>
4, 1, "b", "c", 5, -1, 987, "c", 6, -1, "b", 876
-- </insert3-2.2>
})
-- Test for proper detection of malformed WHEN clauses on INSERT triggers.
--
test:do_execsql_test(
"insert3-3.1",
[[
CREATE TABLE t3(id INTEGER PRIMARY KEY AUTOINCREMENT, a,b,c);
CREATE TRIGGER t3r1 BEFORE INSERT on t3 WHEN nosuchcol BEGIN
SELECT 'illegal WHEN clause';
END;
]], {
-- <insert3-3.1>
-- </insert3-3.1>
})
test:do_catchsql_test(
"insert3-3.2",
[[
INSERT INTO t3 (a,b,c)VALUES(1,2,3)
]], {
-- <insert3-3.2>
1, "no such column: NOSUCHCOL"
-- </insert3-3.2>
})
test:do_execsql_test(
"insert3-3.3",
[[
CREATE TABLE t4(id INTEGER PRIMARY KEY AUTOINCREMENT, a,b,c);
CREATE TRIGGER t4r1 AFTER INSERT on t4 WHEN nosuchcol BEGIN
SELECT 'illegal WHEN clause';
END;
]], {
-- <insert3-3.3>
-- </insert3-3.3>
})
test:do_catchsql_test(
"insert3-3.4",
[[
INSERT INTO t4 (a,b,c)VALUES(1,2,3)
]], {
-- <insert3-3.4>
1, "no such column: NOSUCHCOL"
-- </insert3-3.4>
})
-- ifcapable {trigger}
-- Tests for the INSERT INTO ... DEFAULT VALUES construct
--
test:do_execsql_test(
"insert3-3.5",
[[
CREATE TABLE t5(
a INTEGER PRIMARY KEY AUTOINCREMENT,
b DEFAULT 'xyz'
);
INSERT INTO t5 DEFAULT VALUES;
SELECT * FROM t5;
]], {
-- <insert3-3.5>
1, "xyz"
-- </insert3-3.5>
})
test:do_execsql_test(
"insert3-3.6",
[[
INSERT INTO t5 DEFAULT VALUES;
SELECT * FROM t5;
]], {
-- <insert3-3.6>
1, "xyz", 2, "xyz"
-- </insert3-3.6>
})
test:do_execsql_test(
"insert3-3.7",
[[
CREATE TABLE t6(id INTEGER PRIMARY KEY AUTOINCREMENT, x,y DEFAULT 4.3, z DEFAULT x'6869');
INSERT INTO t6 DEFAULT VALUES;
SELECT * FROM t6;
]], {
-- <insert3-3.7>
1, "", 4.3, "hi"
-- </insert3-3.7>
})
test:execsql("CREATE TABLE t7(id INTEGER PRIMARY KEY AUTOINCREMENT, a INT DEFAULT 1);")
test:do_execsql_test(
"insert3-4.1",
[[
INSERT INTO t7 DEFAULT VALUES;
]], {
-- <insert3-4.1>
-- <insert3-4.1>
})
test:do_execsql_test(
"insert3-4.1",
[[
INSERT INTO t7(a) VALUES(10);
]], {
-- <insert3-4.1>
-- <insert3-4.1>
})
-- gh-2618 function last_insert_id
test:execsql("CREATE TABLE t8(id INTEGER PRIMARY KEY AUTOINCREMENT, a INT);")
test:do_execsql_test(
"insert3-5.1",
[[
INSERT INTO t8 VALUES (null, 11);
SELECT LAST_INSERT_ID();
]], {
-- <insert3-5.1>
1
-- <insert3-5.1>
})
test:do_execsql_test(
"insert3-5.2",
[[
INSERT INTO t8 VALUES (null, 44), (null, 55), (null, 66);
SELECT LAST_INSERT_ID();
]], {
-- <insert3-5.1>
2
-- <insert3-5.1>
})
test:drop_all_tables()
---------------------------------------------------------------------------
-- While developing tests for a different feature (savepoint) the following
-- sequence was found to cause an assert() in btree.c to fail. These
-- tests are included to ensure that that bug is fixed.
--
-- Tarantool: comment until randstr() is implemented
-- test:do_test(
-- "insert3-4.1",
-- function()
-- test:execsql([[
-- CREATE TABLE t1(id INTEGER PRIMARY KEY AUTOINCREMENT, a, b, c);
-- CREATE INDEX i1 ON t1(a, b);
-- BEGIN;
-- INSERT INTO t1 (a,b,c)VALUES(randstr(10,400),randstr(10,400),randstr(10,400));
-- ]])
-- r = "randstr(10,400)"
-- -- for _ in X(0, "X!for", [=[["set ii 0","$ii < 10","incr ii"]]=]) do
-- for ii=0,9 do
-- test:execsql(string.format("INSERT INTO t1 (a, b, c) SELECT %s, %s, %s FROM t1", r, r, r))
-- end
-- return test:execsql(" COMMIT ")
-- end, {
-- -- <insert3-4.1>
-- -- </insert3-4.1>
-- })
-- test:do_execsql_test(
-- "insert3-4.2",
-- [[
-- BEGIN;
-- UPDATE t1 SET a = randstr(10,10) WHERE (id%4)==0;
-- DELETE FROM t1 WHERE id%2;
-- INSERT INTO t1 SELECT randstr(10,400), randstr(10,400), c FROM t1;
-- COMMIT;
-- ]], {
-- -- <insert3-4.2>
-- -- </insert3-4.2>
-- })
test:finish_test()