Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8359077

Browse files
committed
PL/Python: Move ereport wrapper test cases to separate file
In commit5c3c3cd, the new tests wereapparently just dumped into the first convenient file. Move them to aseparate file dedicated to testing that functionality and leave theplpython_test test to test basic functionality, as it did before.
1 parentf64340e commit8359077

File tree

5 files changed

+329
-329
lines changed

5 files changed

+329
-329
lines changed

‎src/pl/plpython/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ REGRESS = \
8484
plpython_trigger\
8585
plpython_types\
8686
plpython_error\
87+
plpython_ereport\
8788
plpython_unicode\
8889
plpython_quote\
8990
plpython_composite\
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
CREATE FUNCTION elog_test() RETURNS void
2+
AS $$
3+
plpy.debug('debug', detail = 'some detail')
4+
plpy.log('log', detail = 'some detail')
5+
plpy.info('info', detail = 'some detail')
6+
plpy.info()
7+
plpy.info('the question', detail = 42);
8+
plpy.info('This is message text.',
9+
detail = 'This is detail text',
10+
hint = 'This is hint text.',
11+
sqlstate = 'XX000',
12+
schema = 'any info about schema',
13+
table = 'any info about table',
14+
column = 'any info about column',
15+
datatype = 'any info about datatype',
16+
constraint = 'any info about constraint')
17+
plpy.notice('notice', detail = 'some detail')
18+
plpy.warning('warning', detail = 'some detail')
19+
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
20+
$$ LANGUAGE plpythonu;
21+
SELECT elog_test();
22+
INFO: info
23+
DETAIL: some detail
24+
INFO: ()
25+
INFO: the question
26+
DETAIL: 42
27+
INFO: This is message text.
28+
DETAIL: This is detail text
29+
HINT: This is hint text.
30+
NOTICE: notice
31+
DETAIL: some detail
32+
WARNING: warning
33+
DETAIL: some detail
34+
ERROR: plpy.Error: stop on error
35+
DETAIL: some detail
36+
HINT: some hint
37+
CONTEXT: Traceback (most recent call last):
38+
PL/Python function "elog_test", line 18, in <module>
39+
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
40+
PL/Python function "elog_test"
41+
do $$ plpy.info('other types', detail = (10,20)) $$ LANGUAGE plpythonu;
42+
INFO: other types
43+
DETAIL: (10, 20)
44+
do $$
45+
import time;
46+
from datetime import date
47+
plpy.info('other types', detail = date(2016,2,26))
48+
$$ LANGUAGE plpythonu;
49+
INFO: other types
50+
DETAIL: 2016-02-26
51+
do $$
52+
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
53+
plpy.info('other types', detail = basket)
54+
$$ LANGUAGE plpythonu;
55+
INFO: other types
56+
DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
57+
-- should fail
58+
do $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
59+
ERROR: invalid SQLSTATE code
60+
CONTEXT: PL/Python anonymous code block
61+
do $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
62+
ERROR: 'blabla' is an invalid keyword argument for this function
63+
CONTEXT: PL/Python anonymous code block
64+
do $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
65+
ERROR: the message is already specified
66+
CONTEXT: PL/Python anonymous code block
67+
do $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
68+
ERROR: the message is already specified
69+
CONTEXT: PL/Python anonymous code block
70+
-- raise exception in python, handle exception in plgsql
71+
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
72+
_sqlstate text DEFAULT NULL,
73+
_schema text DEFAULT NULL, _table text DEFAULT NULL, _column text DEFAULT NULL,
74+
_datatype text DEFAULT NULL, _constraint text DEFAULT NULL)
75+
RETURNS void AS $$
76+
kwargs = { "message":_message, "detail":_detail, "hint":_hint,
77+
"sqlstate":_sqlstate, "schema":_schema, "table":_table,
78+
"column":_column, "datatype":_datatype, "constraint":_constraint }
79+
# ignore None values - should work on Python2.3
80+
dict = {}
81+
for k in kwargs:
82+
if kwargs[k] is not None:
83+
dict[k] = kwargs[k]
84+
plpy.error(**dict)
85+
$$ LANGUAGE plpythonu;
86+
SELECT raise_exception('hello', 'world');
87+
ERROR: plpy.Error: hello
88+
DETAIL: world
89+
CONTEXT: Traceback (most recent call last):
90+
PL/Python function "raise_exception", line 10, in <module>
91+
plpy.error(**dict)
92+
PL/Python function "raise_exception"
93+
SELECT raise_exception('message text', 'detail text', _sqlstate => 'YY333');
94+
ERROR: plpy.Error: message text
95+
DETAIL: detail text
96+
CONTEXT: Traceback (most recent call last):
97+
PL/Python function "raise_exception", line 10, in <module>
98+
plpy.error(**dict)
99+
PL/Python function "raise_exception"
100+
SELECT raise_exception(_message => 'message text',
101+
_detail => 'detail text',
102+
_hint => 'hint text',
103+
_sqlstate => 'XX555',
104+
_schema => 'schema text',
105+
_table => 'table text',
106+
_column => 'column text',
107+
_datatype => 'datatype text',
108+
_constraint => 'constraint text');
109+
ERROR: plpy.Error: message text
110+
DETAIL: detail text
111+
HINT: hint text
112+
CONTEXT: Traceback (most recent call last):
113+
PL/Python function "raise_exception", line 10, in <module>
114+
plpy.error(**dict)
115+
PL/Python function "raise_exception"
116+
SELECT raise_exception(_message => 'message text',
117+
_hint => 'hint text',
118+
_schema => 'schema text',
119+
_column => 'column text',
120+
_constraint => 'constraint text');
121+
ERROR: plpy.Error: message text
122+
HINT: hint text
123+
CONTEXT: Traceback (most recent call last):
124+
PL/Python function "raise_exception", line 10, in <module>
125+
plpy.error(**dict)
126+
PL/Python function "raise_exception"
127+
DO $$
128+
DECLARE
129+
__message text;
130+
__detail text;
131+
__hint text;
132+
__sqlstate text;
133+
__schema_name text;
134+
__table_name text;
135+
__column_name text;
136+
__datatype text;
137+
__constraint text;
138+
BEGIN
139+
BEGIN
140+
PERFORM raise_exception(_message => 'message text',
141+
_detail => 'detail text',
142+
_hint => 'hint text',
143+
_sqlstate => 'XX555',
144+
_schema => 'schema text',
145+
_table => 'table text',
146+
_column => 'column text',
147+
_datatype => 'datatype text',
148+
_constraint => 'constraint text');
149+
EXCEPTION WHEN SQLSTATE 'XX555' THEN
150+
GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
151+
__detail = PG_EXCEPTION_DETAIL,
152+
__hint = PG_EXCEPTION_HINT,
153+
__sqlstate = RETURNED_SQLSTATE,
154+
__schema_name = SCHEMA_NAME,
155+
__table_name = TABLE_NAME,
156+
__column_name = COLUMN_NAME,
157+
__datatype = PG_DATATYPE_NAME,
158+
__constraint = CONSTRAINT_NAME;
159+
RAISE NOTICE 'handled exception'
160+
USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
161+
'schema:(%s), table:(%s), column:(%s), datatype:(%s), constraint:(%s)',
162+
__message, __detail, __hint, __sqlstate, __schema_name,
163+
__table_name, __column_name, __datatype, __constraint);
164+
END;
165+
END;
166+
$$;
167+
NOTICE: handled exception
168+
DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint text), sqlstate: (XX555), schema:(schema text), table:(table text), column:(column text), datatype:(datatype text), constraint:(constraint text)
169+
-- the displayed context is different between Python2 and Python3,
170+
-- but that's not important for this test
171+
\set SHOW_CONTEXT never
172+
do $$
173+
try:
174+
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table=> 'users_tab', _datatype => 'user_type')")
175+
except Exception, e:
176+
plpy.info(e.spidata)
177+
raise e
178+
$$ LANGUAGE plpythonu;
179+
INFO: (119577128, None, 'some hint', None, 0, None, 'users_tab', None, 'user_type', None)
180+
ERROR: plpy.SPIError: plpy.Error: my message
181+
HINT: some hint
182+
do $$
183+
try:
184+
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table = 'users_tab', datatype = 'user_type')
185+
except Exception, e:
186+
plpy.info('sqlstate: %s, hint: %s, tablename: %s, datatype: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
187+
raise e
188+
$$ LANGUAGE plpythonu;
189+
INFO: sqlstate: XX987, hint: some hint, tablename: users_tab, datatype: user_type
190+
ERROR: plpy.Error: my message
191+
HINT: some hint

‎src/pl/plpython/expected/plpython_test.out

Lines changed: 0 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -72,194 +72,3 @@ CONTEXT: Traceback (most recent call last):
7272
PL/Python function "elog_test_basic", line 10, in <module>
7373
plpy.error('error')
7474
PL/Python function "elog_test_basic"
75-
CREATE FUNCTION elog_test() RETURNS void
76-
AS $$
77-
plpy.debug('debug', detail = 'some detail')
78-
plpy.log('log', detail = 'some detail')
79-
plpy.info('info', detail = 'some detail')
80-
plpy.info()
81-
plpy.info('the question', detail = 42);
82-
plpy.info('This is message text.',
83-
detail = 'This is detail text',
84-
hint = 'This is hint text.',
85-
sqlstate = 'XX000',
86-
schema = 'any info about schema',
87-
table = 'any info about table',
88-
column = 'any info about column',
89-
datatype = 'any info about datatype',
90-
constraint = 'any info about constraint')
91-
plpy.notice('notice', detail = 'some detail')
92-
plpy.warning('warning', detail = 'some detail')
93-
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
94-
$$ LANGUAGE plpythonu;
95-
SELECT elog_test();
96-
INFO: info
97-
DETAIL: some detail
98-
INFO: ()
99-
INFO: the question
100-
DETAIL: 42
101-
INFO: This is message text.
102-
DETAIL: This is detail text
103-
HINT: This is hint text.
104-
NOTICE: notice
105-
DETAIL: some detail
106-
WARNING: warning
107-
DETAIL: some detail
108-
ERROR: plpy.Error: stop on error
109-
DETAIL: some detail
110-
HINT: some hint
111-
CONTEXT: Traceback (most recent call last):
112-
PL/Python function "elog_test", line 18, in <module>
113-
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
114-
PL/Python function "elog_test"
115-
do $$ plpy.info('other types', detail = (10,20)) $$ LANGUAGE plpythonu;
116-
INFO: other types
117-
DETAIL: (10, 20)
118-
do $$
119-
import time;
120-
from datetime import date
121-
plpy.info('other types', detail = date(2016,2,26))
122-
$$ LANGUAGE plpythonu;
123-
INFO: other types
124-
DETAIL: 2016-02-26
125-
do $$
126-
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
127-
plpy.info('other types', detail = basket)
128-
$$ LANGUAGE plpythonu;
129-
INFO: other types
130-
DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
131-
-- should fail
132-
do $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
133-
ERROR: invalid SQLSTATE code
134-
CONTEXT: PL/Python anonymous code block
135-
do $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
136-
ERROR: 'blabla' is an invalid keyword argument for this function
137-
CONTEXT: PL/Python anonymous code block
138-
do $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
139-
ERROR: the message is already specified
140-
CONTEXT: PL/Python anonymous code block
141-
do $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
142-
ERROR: the message is already specified
143-
CONTEXT: PL/Python anonymous code block
144-
-- raise exception in python, handle exception in plgsql
145-
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
146-
_sqlstate text DEFAULT NULL,
147-
_schema text DEFAULT NULL, _table text DEFAULT NULL, _column text DEFAULT NULL,
148-
_datatype text DEFAULT NULL, _constraint text DEFAULT NULL)
149-
RETURNS void AS $$
150-
kwargs = { "message":_message, "detail":_detail, "hint":_hint,
151-
"sqlstate":_sqlstate, "schema":_schema, "table":_table,
152-
"column":_column, "datatype":_datatype, "constraint":_constraint }
153-
# ignore None values - should work on Python2.3
154-
dict = {}
155-
for k in kwargs:
156-
if kwargs[k] is not None:
157-
dict[k] = kwargs[k]
158-
plpy.error(**dict)
159-
$$ LANGUAGE plpythonu;
160-
SELECT raise_exception('hello', 'world');
161-
ERROR: plpy.Error: hello
162-
DETAIL: world
163-
CONTEXT: Traceback (most recent call last):
164-
PL/Python function "raise_exception", line 10, in <module>
165-
plpy.error(**dict)
166-
PL/Python function "raise_exception"
167-
SELECT raise_exception('message text', 'detail text', _sqlstate => 'YY333');
168-
ERROR: plpy.Error: message text
169-
DETAIL: detail text
170-
CONTEXT: Traceback (most recent call last):
171-
PL/Python function "raise_exception", line 10, in <module>
172-
plpy.error(**dict)
173-
PL/Python function "raise_exception"
174-
SELECT raise_exception(_message => 'message text',
175-
_detail => 'detail text',
176-
_hint => 'hint text',
177-
_sqlstate => 'XX555',
178-
_schema => 'schema text',
179-
_table => 'table text',
180-
_column => 'column text',
181-
_datatype => 'datatype text',
182-
_constraint => 'constraint text');
183-
ERROR: plpy.Error: message text
184-
DETAIL: detail text
185-
HINT: hint text
186-
CONTEXT: Traceback (most recent call last):
187-
PL/Python function "raise_exception", line 10, in <module>
188-
plpy.error(**dict)
189-
PL/Python function "raise_exception"
190-
SELECT raise_exception(_message => 'message text',
191-
_hint => 'hint text',
192-
_schema => 'schema text',
193-
_column => 'column text',
194-
_constraint => 'constraint text');
195-
ERROR: plpy.Error: message text
196-
HINT: hint text
197-
CONTEXT: Traceback (most recent call last):
198-
PL/Python function "raise_exception", line 10, in <module>
199-
plpy.error(**dict)
200-
PL/Python function "raise_exception"
201-
DO $$
202-
DECLARE
203-
__message text;
204-
__detail text;
205-
__hint text;
206-
__sqlstate text;
207-
__schema_name text;
208-
__table_name text;
209-
__column_name text;
210-
__datatype text;
211-
__constraint text;
212-
BEGIN
213-
BEGIN
214-
PERFORM raise_exception(_message => 'message text',
215-
_detail => 'detail text',
216-
_hint => 'hint text',
217-
_sqlstate => 'XX555',
218-
_schema => 'schema text',
219-
_table => 'table text',
220-
_column => 'column text',
221-
_datatype => 'datatype text',
222-
_constraint => 'constraint text');
223-
EXCEPTION WHEN SQLSTATE 'XX555' THEN
224-
GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
225-
__detail = PG_EXCEPTION_DETAIL,
226-
__hint = PG_EXCEPTION_HINT,
227-
__sqlstate = RETURNED_SQLSTATE,
228-
__schema_name = SCHEMA_NAME,
229-
__table_name = TABLE_NAME,
230-
__column_name = COLUMN_NAME,
231-
__datatype = PG_DATATYPE_NAME,
232-
__constraint = CONSTRAINT_NAME;
233-
RAISE NOTICE 'handled exception'
234-
USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
235-
'schema:(%s), table:(%s), column:(%s), datatype:(%s), constraint:(%s)',
236-
__message, __detail, __hint, __sqlstate, __schema_name,
237-
__table_name, __column_name, __datatype, __constraint);
238-
END;
239-
END;
240-
$$;
241-
NOTICE: handled exception
242-
DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint text), sqlstate: (XX555), schema:(schema text), table:(table text), column:(column text), datatype:(datatype text), constraint:(constraint text)
243-
-- the displayed context is different between Python2 and Python3,
244-
-- but that's not important for this test
245-
\set SHOW_CONTEXT never
246-
do $$
247-
try:
248-
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table=> 'users_tab', _datatype => 'user_type')")
249-
except Exception, e:
250-
plpy.info(e.spidata)
251-
raise e
252-
$$ LANGUAGE plpythonu;
253-
INFO: (119577128, None, 'some hint', None, 0, None, 'users_tab', None, 'user_type', None)
254-
ERROR: plpy.SPIError: plpy.Error: my message
255-
HINT: some hint
256-
do $$
257-
try:
258-
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table = 'users_tab', datatype = 'user_type')
259-
except Exception, e:
260-
plpy.info('sqlstate: %s, hint: %s, tablename: %s, datatype: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
261-
raise e
262-
$$ LANGUAGE plpythonu;
263-
INFO: sqlstate: XX987, hint: some hint, tablename: users_tab, datatype: user_type
264-
ERROR: plpy.Error: my message
265-
HINT: some hint

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp