File: fktap.sql

package info (click to toggle)
pgtap 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,792 kB
  • sloc: sql: 25,795; sh: 790; makefile: 287; perl: 175
file content (532 lines) | stat: -rw-r--r-- 14,117 bytes parent folder | download | duplicates (2)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
\unset ECHO
\i test/setup.sql

SELECT plan(134);
--SELECT * from no_plan();

-- These will be rolled back. :-)
SET client_min_messages = warning;
CREATE TABLE public.pk (
    id    INT NOT NULL PRIMARY KEY,
    name  TEXT DEFAULT ''
);

CREATE TABLE public.fk (
    id    INT NOT NULL PRIMARY KEY,
    pk_id INT NOT NULL REFERENCES pk(id)
);

CREATE TABLE public.pk2 (
    num int NOT NULL UNIQUE,
    dot int NOT NULL,
    PRIMARY KEY (num, dot)
);

CREATE TABLE public.fk2 (
    pk2_num int NOT NULL,
    pk2_dot int NOT NULL,
    FOREIGN KEY(pk2_num, pk2_dot) REFERENCES pk2(num, dot)
);

CREATE TABLE public.fk3(
    id    INT NOT NULL PRIMARY KEY,
    pk_id INT NOT NULL REFERENCES pk(id),
    pk2_num int NOT NULL,
    pk2_dot int NOT NULL,
    foo_id INT NOT NULL,
    FOREIGN KEY(pk2_num, pk2_dot) REFERENCES pk2( num, dot)
);

CREATE TABLE public.pk3(
    id INT UNIQUE
);

CREATE TABLE public.fk4 (
    id INT REFERENCES pk3(id)
);

CREATE TEMP TABLE temp_pk(
    id    INT NOT NULL PRIMARY KEY,
    name  TEXT DEFAULT ''
);

CREATE TEMP TABLE temp_fk (
    id    INT NOT NULL PRIMARY KEY,
    pk_id INT NOT NULL REFERENCES temp_pk(id)
);

-- Create a funcion to return the temp scheme name.
DO $F$
BEGIN
    IF pg_version_num() >= 95000 THEN
        EXECUTE 'CREATE FUNCTION tmpns() RETURNS NAME AS $$ SELECT pg_my_temp_schema()::regnamespace::name $$ LANGUAGE SQL;';
    ELSE
        EXECUTE 'CREATE FUNCTION tmpns() RETURNS NAME AS $$ SELECT nspname FROM pg_namespace WHERE oid = pg_my_temp_schema() $$ LANGUAGE SQL;';
    END IF;
END;
$F$;

RESET client_min_messages;

/****************************************************************************/
-- Test has_fk().
SELECT * FROM check_test(
    has_fk( 'public', 'fk', 'public.fk should have an fk' ),
    true,
    'has_fk( schema, table, description )',
    'public.fk should have an fk'
);

SELECT * FROM check_test(
    has_fk( 'fk', 'fk should have an fk' ),
    'true',
    'has_fk( table, description )',
    'fk should have an fk'
);

SELECT * FROM check_test(
    has_fk( 'fk4', 'fk4 should have an fk' ),
    'true',
    'has_fk( table4, description )',
    'fk4 should have an fk'
);

SELECT * FROM check_test(
    has_fk( 'public', 'fk4', 'fk4 should have an fk' ),
    'true',
    'has_fk( schema, table4, description )',
    'fk4 should have an fk'
);

SELECT * FROM check_test(
    has_fk( 'fk' ),
    true,
    'has_fk( table )',
    'Table fk should have a foreign key constraint'
);

SELECT * FROM check_test(
    has_fk( 'pg_catalog', 'pg_class', 'pg_catalog.pg_class should have an fk' ),
    false,
    'has_fk( schema, table, description ) fail',
    'pg_catalog.pg_class should have an fk'
);

SELECT * FROM check_test(
    has_fk( 'pg_class', 'pg_class should have an fk' ),
    false,
    'has_fk( table, description ) fail',
    'pg_class should have an fk'
);

/****************************************************************************/
-- Test hasnt_fk().
SELECT * FROM check_test(
    hasnt_fk( 'public', 'fk', 'public.fk should not have an fk' ),
    false,
    'hasnt_fk( schema, table, description )',
    'public.fk should not have an fk'
);

SELECT * FROM check_test(
    hasnt_fk( 'fk', 'fk should not have an fk' ),
    'false',
    'hasnt_fk( table, description )',
    'fk should not have an fk'
);

SELECT * FROM check_test(
    hasnt_fk( 'fk' ),
    false,
    'hasnt_fk( table )',
    'Table fk should not have a foreign key constraint'
);

SELECT * FROM check_test(
    hasnt_fk( 'pg_catalog', 'pg_class', 'pg_catalog.pg_class should not have an fk' ),
    true,
    'hasnt_fk( schema, table, description ) pass',
    'pg_catalog.pg_class should not have an fk'
);

SELECT * FROM check_test(
    hasnt_fk( 'pg_class', 'pg_class should not have an fk' ),
    true,
    'hasnt_fk( table, description ) pass',
    'pg_class should not have an fk'
);

/****************************************************************************/
-- Test col_is_fk().

SELECT * FROM check_test(
    col_is_fk( 'public', 'fk', 'pk_id', 'public.fk.pk_id should be an fk' ),
    true,
    'col_is_fk( schema, table, column, description )',
    'public.fk.pk_id should be an fk'
);

SELECT * FROM check_test(
    col_is_fk( 'fk', 'pk_id', 'fk.pk_id should be an fk' ),
    true,
    'col_is_fk( table, column, description )',
    'fk.pk_id should be an fk'
);

SELECT * FROM check_test(
    col_is_fk( 'fk', 'pk_id' ),
    true,
    'col_is_fk( table, column )',
    'Column fk(pk_id) should be a foreign key'
);

SELECT * FROM check_test(
    col_is_fk( 'public', 'fk', 'name', 'public.fk.name should be an fk' ),
    false,
    'col_is_fk( schema, table, column, description )',
    'public.fk.name should be an fk',
    '    Table public.fk has foreign key constraints on these columns:
        pk_id'
);

SELECT * FROM check_test(
    col_is_fk( 'fk3', 'name', 'fk3.name should be an fk' ),
    false,
    'col_is_fk( table, column, description )',
    'fk3.name should be an fk',
    '    Table fk3 has foreign key constraints on these columns:
        pk2_num, pk2_dot
        pk_id'
);

-- Check table with multiple FKs.
SELECT * FROM check_test(
    col_is_fk( 'fk3', 'pk_id' ),
    true,
    'multi-fk col_is_fk test',
    'Column fk3(pk_id) should be a foreign key'
);

-- Check failure for table with no FKs.
SELECT * FROM check_test(
    col_is_fk( 'public', 'pk', 'name', 'pk.name should be an fk' ),
    false,
    'col_is_fk with no FKs',
    'pk.name should be an fk',
    '    Table public.pk has no foreign key columns'
);

SELECT * FROM check_test(
    col_is_fk( 'pk', 'name' ),
    false,
    'col_is_fk with no FKs',
    'Column pk(name) should be a foreign key',
    '    Table pk has no foreign key columns'
);


/****************************************************************************/
-- Test col_is_fk() with an array of columns.

SELECT * FROM check_test(
    col_is_fk( 'public', 'fk2', ARRAY['pk2_num', 'pk2_dot'], 'id + pk2_dot should be an fk' ),
    true,
    'col_is_fk( schema, table, column[], description )',
    'id + pk2_dot should be an fk'
);

SELECT * FROM check_test(
    col_is_fk( 'fk2', ARRAY['pk2_num', 'pk2_dot'], 'id + pk2_dot should be an fk' ),
    true,
    'col_is_fk( table, column[], description )',
    'id + pk2_dot should be an fk'
);

SELECT * FROM check_test(
    col_is_fk( 'fk2', ARRAY['pk2_num', 'pk2_dot'] ),
    true,
    'col_is_fk( table, column[] )',
    'Columns fk2(pk2_num, pk2_dot) should be a foreign key'
);

/****************************************************************************/
-- Test col_isnt_fk().

SELECT * FROM check_test(
    col_isnt_fk( 'public', 'fk', 'pk_id', 'public.fk.pk_id should not be an fk' ),
    false,
    'col_isnt_fk( schema, table, column, description )',
    'public.fk.pk_id should not be an fk',
    ''    
);

SELECT * FROM check_test(
    col_isnt_fk( 'fk', 'pk_id', 'fk.pk_id should not be an fk' ),
    false,
    'col_isnt_fk( table, column, description )',
    'fk.pk_id should not be an fk',
    ''
);

SELECT * FROM check_test(
    col_isnt_fk( 'fk', 'pk_id' ),
    false,
    'col_isnt_fk( table, column )',
    'Column fk(pk_id) should not be a foreign key',
    ''
);

SELECT * FROM check_test(
    col_isnt_fk( 'public', 'fk', 'name', 'public.fk.name should not be an fk' ),
    true,
    'col_isnt_fk( schema, table, column, description )',
    'public.fk.name should not be an fk',
    ''
);

SELECT * FROM check_test(
    col_isnt_fk( 'fk3', 'name', 'fk3.name should not be an fk' ),
    true,
    'col_isnt_fk( table, column, description )',
    'fk3.name should not be an fk',
    ''
);

-- Check table with multiple FKs.
SELECT * FROM check_test(
    col_isnt_fk( 'fk3', 'pk_id' ),
    false,
    'multi-fk col_isnt_fk test',
    'Column fk3(pk_id) should not be a foreign key',
    ''
);

-- Check failure for table with no FKs.
SELECT * FROM check_test(
    col_isnt_fk( 'public', 'pk', 'name', 'pk.name should not be an fk' ),
    true,
    'col_isnt_fk with no FKs',
    'pk.name should not be an fk',
    ''
);

SELECT * FROM check_test(
    col_isnt_fk( 'pk', 'name' ),
    true,
    'col_isnt_fk with no FKs',
    'Column pk(name) should not be a foreign key',
    ''
);

/****************************************************************************/
-- Test col_isnt_fk() with an array of columns.

SELECT * FROM check_test(
    col_isnt_fk( 'public', 'fk2', ARRAY['pk2_num', 'pk2_dot'], 'id + pk2_dot should not be an fk' ),
    false,
    'col_isnt_fk( schema, table, column[], description )',
    'id + pk2_dot should not be an fk'
);

SELECT * FROM check_test(
    col_isnt_fk( 'fk2', ARRAY['pk2_num', 'pk2_dot'], 'id + pk2_dot should not be an fk' ),
    false,
    'col_isnt_fk( table, column[], description )',
    'id + pk2_dot should not be an fk'
);

SELECT * FROM check_test(
    col_isnt_fk( 'fk2', ARRAY['pk2_num', 'pk2_dot'] ),
    false,
    'col_isnt_fk( table, column[] )',
    'Columns fk2(pk2_num, pk2_dot) should not be a foreign key'
);

/****************************************************************************/
-- Test fk_ok().
SELECT * FROM check_test(
    fk_ok( 'public', 'fk', ARRAY['pk_id'], 'public', 'pk', ARRAY['id'], 'WHATEVER' ),
    true,
    'full fk_ok array',
    'WHATEVER'
);

-- Make sure it works with the temp schema.
SELECT * FROM check_test(
    fk_ok( tmpns(), 'temp_fk', ARRAY['pk_id'], tmpns(), 'temp_pk', ARRAY['id'], 'WHATEVER' ),
    true,
    'pg_my_temp_schema()',
    'WHATEVER'
);

SELECT * FROM check_test(
    fk_ok( 'public', 'fk2', ARRAY['pk2_num', 'pk2_dot'], 'public', 'pk2', ARRAY['num', 'dot'] ),
    true,
    'multiple fk fk_ok desc',
    'public.fk2(pk2_num, pk2_dot) should reference public.pk2(num, dot)'
);

SELECT * FROM check_test(
    fk_ok( 'public', 'fk', ARRAY['pk_id'], 'public', 'pk', ARRAY['id'] ),
    true,
    'fk_ok array desc',
    'public.fk(pk_id) should reference public.pk(id)'
);

SELECT * FROM check_test(
    fk_ok( 'fk', ARRAY['pk_id'], 'pk', ARRAY['id'] ),
    true,
    'fk_ok array noschema desc',
    'fk(pk_id) should reference pk(id)'
);

SELECT * FROM check_test(
    fk_ok( 'fk2', ARRAY['pk2_num', 'pk2_dot'], 'pk2', ARRAY['num', 'dot'] ),
    true,
    'multiple fk fk_ok noschema desc',
    'fk2(pk2_num, pk2_dot) should reference pk2(num, dot)'
);

SELECT * FROM check_test(
    fk_ok( 'fk', ARRAY['pk_id'], 'pk', ARRAY['id'], 'WHATEVER' ),
    true,
    'fk_ok array noschema',
    'WHATEVER'
);

SELECT * FROM check_test(
    fk_ok( 'public', 'fk', 'pk_id', 'public', 'pk', 'id', 'WHATEVER' ),
    true,
    'basic fk_ok',
    'WHATEVER'
);

SELECT * FROM check_test(
    fk_ok( 'public', 'fk', 'pk_id', 'public', 'pk', 'id' ),
    true,
    'basic fk_ok desc',
    'public.fk(pk_id) should reference public.pk(id)'
);

SELECT * FROM check_test(
    fk_ok( 'fk', 'pk_id', 'pk', 'id', 'WHATEVER' ),
    true,
    'basic fk_ok noschema',
    'WHATEVER'
);

SELECT * FROM check_test(
    fk_ok( 'fk', 'pk_id', 'pk', 'id' ),
    true,
    'basic fk_ok noschema desc',
    'fk(pk_id) should reference pk(id)',
    ''
);

-- Make sure check_test() works properly with no name argument.
SELECT * FROM check_test(
    fk_ok( 'fk', 'pk_id', 'pk', 'id' ),
    true
);

SELECT * FROM check_test(
    fk_ok( 'public', 'fk', ARRAY['pk_id'], 'public', 'pk', ARRAY['fid'], 'WHATEVER' ),
    false,
    'fk_ok fail',
    'WHATEVER',
    '        have: public.fk(pk_id) REFERENCES public.pk(id)
        want: public.fk(pk_id) REFERENCES public.pk(fid)'
);

SELECT * FROM check_test(
    fk_ok( 'public', 'fk', ARRAY['pk_id'], 'public', 'pk', ARRAY['fid'] ),
    false,
    'fk_ok fail desc',
    'public.fk(pk_id) should reference public.pk(fid)',
    '        have: public.fk(pk_id) REFERENCES public.pk(id)
        want: public.fk(pk_id) REFERENCES public.pk(fid)'
);

SELECT * FROM check_test(
    fk_ok( 'fk', ARRAY['pk_id'], 'pk', ARRAY['fid'], 'WHATEVER' ),
    false,
    'fk_ok fail no schema',
    'WHATEVER',
    '        have: fk(pk_id) REFERENCES pk(id)
        want: fk(pk_id) REFERENCES pk(fid)'
);

SELECT * FROM check_test(
    fk_ok( 'fk', ARRAY['pk_id'], 'pk', ARRAY['fid'] ),
    false,
    'fk_ok fail no schema desc',
    'fk(pk_id) should reference pk(fid)',
    '        have: fk(pk_id) REFERENCES pk(id)
        want: fk(pk_id) REFERENCES pk(fid)'
);

SELECT * FROM check_test(
    fk_ok( 'fk', ARRAY['pk_id'], 'ok', ARRAY['fid'], 'WHATEVER' ),
    false,
    'fk_ok bad PK test',
    'WHATEVER',
    '        have: fk(pk_id) REFERENCES pk(id)
        want: fk(pk_id) REFERENCES ok(fid)'
);

-- Try a table with multiple FKs.
SELECT * FROM check_test(
    fk_ok( 'public', 'fk3', 'pk_id', 'public', 'pk', 'id' ),
    true,
    'double fk schema test',
    'public.fk3(pk_id) should reference public.pk(id)',
    ''
);

SELECT * FROM check_test(
    fk_ok( 'fk3', 'pk_id', 'pk', 'id' ),
    true,
    'double fk test',
    'fk3(pk_id) should reference pk(id)',
    ''
);

-- Try the second FK on that table, which happens to be a multicolumn FK.
SELECT * FROM check_test(
    fk_ok(
        'public', 'fk3', ARRAY['pk2_num', 'pk2_dot'],
        'public', 'pk2', ARRAY['num', 'dot']
    ),
    true,
    'double fk and col schema test',
    'public.fk3(pk2_num, pk2_dot) should reference public.pk2(num, dot)',
    ''
);

-- Try FK columns that reference nothing.
SELECT * FROM check_test(
    fk_ok( 'public', 'fk3', 'id', 'public', 'foo', 'id' ),
    false,
    'missing fk test',
    'public.fk3(id) should reference public.foo(id)',
    '        have: public.fk3(id) REFERENCES NOTHING
        want: public.fk3(id) REFERENCES public.foo(id)'
);

-- Try non-existent FK colums.
SELECT * FROM check_test(
    fk_ok(
        'fk3', ARRAY['pk2_blah', 'pk2_dot'],
        'pk2', ARRAY['num', 'dot']
    ),
    false,
    'bad FK column test',
    'fk3(pk2_blah, pk2_dot) should reference pk2(num, dot)',
    '        have: fk3(pk2_blah, pk2_dot) REFERENCES NOTHING
        want: fk3(pk2_blah, pk2_dot) REFERENCES pk2(num, dot)'
);

/****************************************************************************/
-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;