File: unique.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (436 lines) | stat: -rwxr-xr-x 9,602 bytes parent folder | download | duplicates (3)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(35)

--!./tcltestrunner.lua
-- 2001 September 27
--
-- 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 sql library.  The
-- focus of this file is testing the CREATE UNIQUE INDEX statement,
-- and primary keys, and the UNIQUE constraint on table columns
--
-- $Id: unique.test,v 1.9 2009/05/02 15:46:47 drh Exp $
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]

test:do_catchsql_test(
    "unique-1.1",
    [[
        CREATE TABLE t1(
           a int PRIMARY KEY,
           b int PRIMARY KEY,
           c text
        );
    ]], {
        -- <unique-1.1>
        1, [[Failed to create space 'T1': primary key has been already declared]]
        -- </unique-1.1>
    })

test:do_catchsql_test(
    "unique-1.1b",
    [[
        CREATE TABLE t1(
           a int PRIMARY KEY,
           b int UNIQUE,
           c text
        );
    ]], {
        -- <unique-1.1b>
        0
        -- </unique-1.1b>
    })

test:do_catchsql_test(
    "unique-1.2",
    [[
        INSERT INTO t1(a,b,c) VALUES(1,2,'3')
    ]], {
        -- <unique-1.2>
        0
        -- </unique-1.2>
    })

test:do_catchsql_test(
    "unique-1.3",
    [[
        INSERT INTO t1(a,b,c) VALUES(1,3,'4')
    ]], {
        -- <unique-1.3>
        1, "Duplicate key exists in unique index 'pk_unnamed_T1_1' in space 'T1'"
        -- </unique-1.3>
    })

-- Verify the previous test has not inserted anything.
test:do_execsql_test(
    "unique-1.4",
    [[
        SELECT * FROM t1 ORDER BY a;
    ]], {
        -- <unique-1.4>
        1, 2, "3"
        -- </unique-1.4>
    })

test:do_catchsql_test(
    "unique-1.5",
    [[
        INSERT INTO t1(a,b,c) VALUES(3,2,'4')
    ]], {
        -- <unique-1.5>
        1, "Duplicate key exists in unique index 'unique_unnamed_T1_2' in space 'T1'"
        -- </unique-1.5>
    })

-- Verify the previous test has not inserted anything.
test:do_execsql_test(
    "unique-1.6",
    [[
        SELECT * FROM t1 ORDER BY a;
    ]], {
        -- <unique-1.6>
        1, 2, "3"
        -- </unique-1.6>
    })

test:do_catchsql_test(
    "unique-1.7",
    [[
        INSERT INTO t1(a,b,c) VALUES(3,4,'5')
    ]], {
        -- <unique-1.7>
        0
        -- </unique-1.7>
    })

test:do_execsql_test(
    "unique-1.8",
    [[
        SELECT * FROM t1 ORDER BY a;
    ]], {
        -- <unique-1.8>
        1, 2, "3", 3, 4, "5"
        -- </unique-1.8>
    })

--integrity_check unique-1.9
test:do_execsql_test(
    "unique-2.0",
    [[
        DROP TABLE t1;
        CREATE TABLE t2(id int primary key, a int, b int);
        INSERT INTO t2(id, a,b) VALUES(1, 1,2);
        INSERT INTO t2(id, a,b) VALUES(2, 3,4);
        SELECT a,b FROM t2 ORDER BY a;
    ]], {
        -- <unique-2.0>
        1, 2, 3, 4
        -- </unique-2.0>
    })

test:do_catchsql_test(
    "unique-2.1",
    [[
        CREATE UNIQUE INDEX i2 ON t2(a)
    ]], {
        -- <unique-2.1>
        0
        -- </unique-2.1>
    })

test:do_catchsql_test(
    "unique-2.2",
    [[
        SELECT a,b FROM t2 ORDER BY a
    ]], {
        -- <unique-2.2>
        0, {1, 2, 3, 4}
        -- </unique-2.2>
    })

test:do_catchsql_test(
    "unique-2.3",
    [[
        INSERT INTO t2 VALUES(3, 1,5);
    ]], {
        -- <unique-2.3>
        1, "Duplicate key exists in unique index 'I2' in space 'T2'"
        -- </unique-2.3>
    })

-- Verify the previous test has not inserted anything.
test:do_catchsql_test(
    "unique-2.4",
    [[
        SELECT a,b FROM t2 ORDER BY a
    ]], {
        -- <unique-2.4>
        0, {1, 2, 3, 4}
        -- </unique-2.4>
    })

test:do_catchsql_test(
    "unique-2.5",
    [[
        DROP INDEX i2 ON t2;
        SELECT a,b FROM t2 ORDER BY a;
    ]], {
        -- <unique-2.5>
        0, {1, 2, 3, 4}
        -- </unique-2.5>
    })

test:do_catchsql_test(
    "unique-2.6",
    [[
        INSERT INTO t2 VALUES(4, 1,5)
    ]], {
        -- <unique-2.6>
        0
        -- </unique-2.6>
    })

test:do_catchsql_test(
    "unique-2.7",
    [[
        SELECT a,b FROM t2 ORDER BY a, b;
    ]], {
        -- <unique-2.7>
        0, {1, 2, 1, 5, 3, 4}
        -- </unique-2.7>
    })

test:do_catchsql_test(
    "unique-2.8",
    [[
        CREATE UNIQUE INDEX i2 ON t2(a);
    ]], {
        -- <unique-2.8>
        1, "Duplicate key exists in unique index 'I2' in space 'T2'"
        -- </unique-2.8>
    })

test:do_catchsql_test(
    "unique-2.9",
    [[
        CREATE INDEX i2 ON t2(a);
    ]], {
        -- <unique-2.9>
        0
        -- </unique-2.9>
    })

--integrity_check unique-2.10
-- Test the UNIQUE keyword as used on two or more fields.
--
test:do_catchsql_test(
    "unique-3.1",
    [[
        CREATE TABLE t3(
          id int primary key,
           a int,
           b int,
           c int,
           d int,
           unique(a,c,d)
         );
    ]], {
        -- <unique-3.1>
        0
        -- </unique-3.1>
    })

test:do_catchsql_test(
    "unique-3.2",
    [[
        INSERT INTO t3(id,a,b,c,d) VALUES(1,1,2,3,4);
        SELECT a,b,c,d FROM t3 ORDER BY a,b,c,d;
    ]], {
        -- <unique-3.2>
        0, {1, 2, 3, 4}
        -- </unique-3.2>
    })

test:do_catchsql_test(
    "unique-3.3",
    [[
        INSERT INTO t3(id,a,b,c,d) VALUES(2,1,2,3,5);
        SELECT a,b,c,d FROM t3 ORDER BY a,b,c,d;
    ]], {
        -- <unique-3.3>
        0, {1, 2, 3, 4, 1, 2, 3, 5}
        -- </unique-3.3>
    })

test:do_catchsql_test(
    "unique-3.4",
    [[
        INSERT INTO t3(id,a,b,c,d) VALUES(3,1,4,3,5);
        SELECT a,b,c,d FROM t3 ORDER BY a,b,c,d;
    ]], {
        -- <unique-3.4>
        1, "Duplicate key exists in unique index 'unique_unnamed_T3_2' in space 'T3'"
        -- </unique-3.4>
    })

--integrity_check unique-3.5
-- Make sure NULLs are distinct as far as the UNIQUE tests are
-- concerned.
--
test:do_execsql_test(
    "unique-4.1",
    [[
        CREATE TABLE t4(id int primary key,a int UNIQUE, b int, c int, UNIQUE(b,c));
        INSERT INTO t4 VALUES(1,1,2,3);
        INSERT INTO t4 VALUES(2, NULL, 2, NULL);
        SELECT a,b,c FROM t4;
    ]], {
        -- <unique-4.1>
        1, 2, 3, "", 2, ""
        -- </unique-4.1>
    })

test:do_catchsql_test(
    "unique-4.2",
    [[
        INSERT INTO t4 VALUES(3, NULL, 3, 4);
    ]], {
        -- <unique-4.2>
        0
        -- </unique-4.2>
    })

test:do_execsql_test(
    "unique-4.3",
    [[
        SELECT a,b,c FROM t4
    ]], {
        -- <unique-4.3>
        1, 2, 3, "", 2, "", "", 3, 4
        -- </unique-4.3>
    })

test:do_catchsql_test(
    "unique-4.4",
    [[
        INSERT INTO t4 VALUES(4, 2, 2, NULL);
    ]], {
        -- <unique-4.4>
        0
        -- </unique-4.4>
    })

test:do_execsql_test(
    "unique-4.5",
    [[
        SELECT a,b,c FROM t4
    ]], {
        -- <unique-4.5>
        1, 2, 3, "", 2, "", "", 3, 4, 2, 2, ""
        -- </unique-4.5>
    })

-- Ticket #1301.  Any NULL value in a set of unique columns should
-- cause the rows to be distinct.
--
test:do_catchsql_test(
    "unique-4.6",
    [[
        INSERT INTO t4 VALUES(5, NULL, 2, NULL);
    ]], {
        -- <unique-4.6>
        0
        -- </unique-4.6>
    })

test:do_execsql_test(
    "unique-4.7",
    [[
        SELECT a,b,c FROM t4
    ]], {
        -- <unique-4.7>
        1, 2, 3, "", 2, "", "", 3, 4, 2, 2, "", "", 2, ""
        -- </unique-4.7>
    })

test:do_catchsql_test(
    "unique-4.8",
    [[
        CREATE UNIQUE INDEX i4a ON t4(a,b)
    ]], {
        -- <unique-4.8>
        0
        -- </unique-4.8>
    })

test:do_catchsql_test(
    "unique-4.9",
    [[
        CREATE UNIQUE INDEX i4b ON t4(a,b,c)
    ]], {
        -- <unique-4.9>
        0
        -- </unique-4.9>
    })

test:do_catchsql_test(
    "unique-4.10",
    [[
        CREATE UNIQUE INDEX i4c ON t4(b)
    ]], {
        -- <unique-4.10>
        1, "Duplicate key exists in unique index 'I4C' in space 'T4'"
        -- </unique-4.10>
    })

--integrity_check unique-4.99
-- Test the error message generation logic.  In particular, make sure we
-- do not overflow the static buffer used to generate the error message.
--
test:do_execsql_test(
    "unique-5.1",
    [[
        CREATE TABLE t5(
          id  INT primary key,
          first_column_with_long_name INT ,
          second_column_with_long_name INT ,
          third_column_with_long_name INT ,
            fourth_column_with_long_name INT ,
          fifth_column_with_long_name INT ,
          sixth_column_with_long_name INT ,
          UNIQUE(
            first_column_with_long_name,
            second_column_with_long_name,
            third_column_with_long_name,
            fourth_column_with_long_name,
            fifth_column_with_long_name,
            sixth_column_with_long_name
          )
        );
        INSERT INTO t5 VALUES(1,1,2,3,4,5,6);
        SELECT * FROM t5;
    ]], {
        -- <unique-5.1>
        1, 1, 2, 3, 4, 5, 6
        -- </unique-5.1>
    })

test:do_catchsql_test(
    "unique-5.2",
    [[
        INSERT INTO t5 VALUES(2, 1,2,3,4,5,6);
    ]], {
        -- <unique-5.2>
        1, "Duplicate key exists in unique index 'unique_unnamed_T5_2' in space 'T5'"
        -- </unique-5.2>
    })

test:finish_test()