File: check.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 (169 lines) | stat: -rw-r--r-- 4,583 bytes parent folder | download | duplicates (7)
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
\unset ECHO
\i test/setup.sql

SELECT plan(48);

-- This will be rolled back. :-)
SET client_min_messages = warning;
CREATE TABLE public.sometab(
    id    INT NOT NULL PRIMARY KEY,
    name  TEXT DEFAULT '' CHECK ( name IN ('foo', 'bar', 'baz') ),
    numb  NUMERIC(10, 2),
    myint NUMERIC(8),
    CHECK (numb > 1.0 AND myint < 10)
);
RESET client_min_messages;

/****************************************************************************/
-- Test has_check().

SELECT * FROM check_test(
    has_check( 'public', 'sometab', 'public.sometab should have a check constraint' ),
    true,
    'has_check( schema, table, desc )',
    'public.sometab should have a check constraint',
    ''
);

SELECT * FROM check_test(
    has_check( 'sometab', 'sometab should have a check constraint' ),
    true,
    'has_check( table, desc )',
    'sometab should have a check constraint',
    ''
);

SELECT * FROM check_test(
    has_check( 'sometab' ),
    true,
    'has_check( table )',
    'Table sometab should have a check constraint',
    ''
);

SELECT * FROM check_test(
    has_check( 'pg_catalog', 'pg_class', 'pg_catalog.pg_class should have a check constraint' ),
    false,
    'has_check( schema, table, descr ) fail',
    'pg_catalog.pg_class should have a check constraint',
    ''
);

SELECT * FROM check_test(
    has_check( 'pg_class', 'pg_class should have a check constraint' ),
    false,
    'has_check( table, desc ) fail',
    'pg_class should have a check constraint',
    ''
);

/****************************************************************************/
-- Test col_has_check().

SELECT * FROM check_test(
    col_has_check( 'public', 'sometab', 'name', 'public.sometab.name should have a check' ),
    true,
    'col_has_check( sch, tab, col, desc )',
    'public.sometab.name should have a check',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'public', 'sometab', ARRAY['numb', 'myint'], 'public.sometab.numb+myint should have a check' ),
    true,
    'col_has_check( sch, tab, cols, desc )',
    'public.sometab.numb+myint should have a check',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'sometab', 'name', 'sometab.name should have a check' ),
    true,
    'col_has_check( tab, col, desc )',
    'sometab.name should have a check',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'sometab', ARRAY['numb', 'myint'], 'sometab.numb+myint should have a check' ),
    true,
    'col_has_check( tab, cols, desc )',
    'sometab.numb+myint should have a check',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'sometab', 'name' ),
    true,
    'col_has_check( table, column )',
    'Column sometab(name) should have a check constraint',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'sometab', ARRAY['numb', 'myint'] ),
    true,
    'col_has_check( table, columns )',
    'Columns sometab(numb, myint) should have a check constraint',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'public', 'sometab', 'id', 'public.sometab.id should have a check' ),
    false,
    'col_has_check( sch, tab, col, desc ) fail',
    'public.sometab.id should have a check',
    '        have: {name}
              {numb,myint}
        want: {id}'
);

SELECT * FROM check_test(
    col_has_check( 'sometab', 'id', 'sometab.id should have a check' ),
    false,
    'col_has_check( tab, col, desc ) fail',
    'sometab.id should have a check',
    '        have: {name}
              {numb,myint}
        want: {id}'
);

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

SET LOCAL client_min_messages = warning;
CREATE TABLE public.argh (
    id   INT  NOT NULL,
    name TEXT NOT NULL,
    CHECK ( id IN (1, 2) AND name IN ('foo', 'bar'))
);
RESET client_min_messages;

SELECT * FROM check_test(
    col_has_check( 'public', 'argh', ARRAY['id', 'name'], 'id + name should have a check' ),
    true,
    'col_has_check( sch, tab, col[], desc )',
    'id + name should have a check',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'argh', ARRAY['id', 'name'], 'id + name should have a check' ),
    true,
    'col_has_check( tab, col[], desc )',
    'id + name should have a check',
    ''
);

SELECT * FROM check_test(
    col_has_check( 'argh', ARRAY['id', 'name'] ),
    true,
    'col_has_check( tab, col[] )',
    'Columns argh(id, name) should have a check constraint',
    ''
);

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