/
test.sql
151 lines (125 loc) · 3.25 KB
/
test.sql
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
\unset ECHO
\set QUIET 1
-- Turn off echo and keep things quiet.
-- Format the output for nice TAP.
\pset format unaligned
\pset tuples_only true
\pset pager
-- Revert all changes on failure.
\set ON_ERROR_ROLLBACK 1
\set ON_ERROR_STOP true
-- Load the TAP functions.
BEGIN;
\i pgtap.sql
\i plparrot.sql
-- Plan the tests.
SELECT plan(14);
CREATE OR REPLACE FUNCTION create_plparrot()
RETURNS BOOLEAN
LANGUAGE SQL
AS $$
CREATE LANGUAGE plparrot;
SELECT true;
$$;
CREATE FUNCTION test_void() RETURNS void AS $$
.sub main :anon
.return()
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_int() RETURNS int AS $$
.sub main :anon
.return(1)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_int_in(int) RETURNS int AS $$
.sub main :anon
.param int x
.return(1)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_int_out(int) RETURNS int AS $$
.sub main :anon
.param int x
.return(42)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_increment_int_int(int) RETURNS int AS $$
.sub main :anon
.param int x
inc x
.return(x)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_float() RETURNS float AS $$
.sub main :anon
$N0 = 1.0
.return($N0)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_float_add(float) RETURNS float AS $$
.sub main :anon
.param num x
x += 5
.return(x)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_text_in(text) RETURNS text AS $$
.sub main :anon
.param string s
.return(s)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_text_out(text) RETURNS text AS $$
.sub main :anon
$S1 = 'blue'
.return($S1)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_varchar_in(varchar) RETURNS varchar AS $$
.sub main :anon
.param string s
.return(s)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_varchar_out(varchar) RETURNS varchar AS $$
.sub main :anon
$S1 = 'blue'
.return($S1)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_char_in(char) RETURNS char AS $$
.sub main :anon
.param string s
.return(s)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_char_out(char) RETURNS char AS $$
.sub main :anon
$S1 = 'b'
.return($S1)
.end
$$ LANGUAGE plparrot;
CREATE FUNCTION test_int_float(int, float) RETURNS int AS $$
.sub main :anon
.param int x
.param num y
.return(1)
.end
$$ LANGUAGE plparrot;
select is(test_text_in('cheese'), 'cheese', 'We can pass a text in');
select is(test_text_out('cheese'), 'blue', 'We can return a text');
select is(test_varchar_in('cheese'), 'cheese', 'We can pass a varchar in');
select is(test_varchar_out('cheese'), 'blue', 'We can return a varchar');
select is(test_int_float(42,6.9), 1, 'We can pass an int and float as arguments');
select is(test_char_in('c'), 'c', 'We can pass a char in');
select is(test_char_out('c'), 'b', 'We can return a char');
select is(test_int_in(42),1,'We can pass in an int');
select is(test_int_out(1),42,'We can return an int');
select is(test_increment_int_int(42),43,'We can increment an int and return it');
select is(test_int(),1,'We can return an int');
select is(test_void()::text,''::text,'We can return void');
select is(test_float(), 1.0::float ,'We can return a float');
select is(test_float_add(42), 47.0::float ,'We can add to a float and return it');
-- Finish the tests and clean up.
SELECT * FROM finish();
rollback;