forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionTests.cs
251 lines (228 loc) · 10.9 KB
/
FunctionTests.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Npgsql;
using NpgsqlTypes;
using NUnit.Framework;
namespace Npgsql.Tests
{
/// <summary>
/// A fixture for tests which interact with functions.
/// All tests should create functions in the pg_temp schema only to ensure there's no interaction between
/// the tests.
/// </summary>
public class FunctionTests : TestBase
{
[Test, Description("Simple function with no parameters, results accessed as a resultset")]
public void ResultSet()
{
ExecuteNonQuery(@"CREATE FUNCTION pg_temp.func() RETURNS integer AS 'SELECT 8;' LANGUAGE 'sql'");
using (var cmd = new NpgsqlCommand("pg_temp.func", Conn) { CommandType = CommandType.StoredProcedure })
{
Assert.That(cmd.ExecuteScalar(), Is.EqualTo(8));
}
}
[Test, Description("Basic function call with an in parameter")]
public void InParam()
{
ExecuteNonQuery(@"CREATE FUNCTION pg_temp.echo(IN param text) RETURNS text AS 'BEGIN RETURN param; END;' LANGUAGE 'plpgsql'");
using (var cmd = new NpgsqlCommand("pg_temp.echo", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param", "hello");
Assert.That(cmd.ExecuteScalar(), Is.EqualTo("hello"));
}
}
[Test, Description("Basic function call with an out parameter")]
public void OutParam()
{
ExecuteNonQuery(@"CREATE FUNCTION pg_temp.echo (IN param_in text, OUT param_out text) AS 'BEGIN param_out=param_in; END;' LANGUAGE 'plpgsql'");
using (var cmd = new NpgsqlCommand("pg_temp.echo", Conn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param", "hello");
var outParam = new NpgsqlParameter("param_out", DbType.String) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(outParam);
cmd.ExecuteNonQuery();
Assert.That(outParam.Value, Is.EqualTo("hello"));
}
}
[Test, Description("Basic function call with an in/out parameter")]
public void InOutParam()
{
ExecuteNonQuery(@"CREATE FUNCTION pg_temp.inc (INOUT param integer) AS 'BEGIN param=param+1; END;' LANGUAGE 'plpgsql'");
using (var cmd = new NpgsqlCommand("pg_temp.inc", Conn)) {
cmd.CommandType = CommandType.StoredProcedure;
var outParam = new NpgsqlParameter("param", DbType.Int32) {
Direction = ParameterDirection.InputOutput,
Value = 8
};
cmd.Parameters.Add(outParam);
cmd.ExecuteNonQuery();
Assert.That(outParam.Value, Is.EqualTo(9));
}
}
[Test]
[MinPgVersion(9, 1, 0, "no binary output function available for type void before 9.1.0")]
public void Void()
{
var command = new NpgsqlCommand("pg_sleep", Conn);
command.Parameters.AddWithValue("sleep_time", 0);
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
}
[Test]
public void TooManyOutputParams()
{
var command = new NpgsqlCommand("VALUES (4,5), (6,7)", Conn);
command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32)
{
Direction = ParameterDirection.Output,
Value = -1
});
command.Parameters.Add(new NpgsqlParameter("b", DbType.Int32)
{
Direction = ParameterDirection.Output,
Value = -1
});
command.Parameters.Add(new NpgsqlParameter("c", DbType.Int32)
{
Direction = ParameterDirection.Output,
Value = -1
});
command.ExecuteNonQuery();
Assert.That(command.Parameters["a"].Value, Is.EqualTo(4));
Assert.That(command.Parameters["b"].Value, Is.EqualTo(5));
Assert.That(command.Parameters["c"].Value, Is.EqualTo(-1));
}
[Test]
public void MultipleRefCursorSupport()
{
ExecuteNonQuery(@"CREATE FUNCTION pg_temp.func() RETURNS SETOF refcursor AS 'DECLARE ref1 refcursor; ref2 refcursor; BEGIN OPEN ref1 FOR SELECT 1; RETURN NEXT ref1; OPEN ref2 FOR SELECT 2; RETURN next ref2; RETURN; END;' LANGUAGE 'plpgsql';");
using (Conn.BeginTransaction())
{
var command = new NpgsqlCommand("pg_temp.func", Conn);
command.CommandType = CommandType.StoredProcedure;
using (var dr = command.ExecuteReader())
{
dr.Read();
Assert.That(dr.GetInt32(0), Is.EqualTo(1));
dr.NextResult();
dr.Read();
Assert.That(dr.GetInt32(0), Is.EqualTo(2));
}
}
}
[Test]
public void SingleRow()
{
ExecuteNonQuery(@"CREATE FUNCTION pg_temp.func() RETURNS TABLE (a INT, b INT) AS 'VALUES (1,2), (3,4);' LANGUAGE 'sql'");
using (var cmd = new NpgsqlCommand("pg_temp.func", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
using (var reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
Assert.That(reader.Read(), Is.True);
Assert.That(reader.Read(), Is.False);
}
}
}
#region Parameter Derivation
[Test, Description("Tests function parameter derivation with IN, OUT and INOUT parameters")]
public void DeriveParametersVarious()
{
// This function returns record because of the two Out (InOut & Out) parameters
ExecuteNonQuery(@"CREATE OR REPLACE FUNCTION pg_temp.func(IN param1 INT, OUT param2 text, INOUT param3 INT) RETURNS record AS
'
BEGIN
param2 = ''sometext'';
param3 = param1 + param3;
END;
' LANGUAGE 'plpgsql';");
var cmd = new NpgsqlCommand("pg_temp.func", Conn);
cmd.CommandType = CommandType.StoredProcedure;
NpgsqlCommandBuilder.DeriveParameters(cmd);
Assert.That(cmd.Parameters, Has.Count.EqualTo(3));
Assert.That(cmd.Parameters[0].Direction, Is.EqualTo(ParameterDirection.Input));
Assert.That(cmd.Parameters[1].Direction, Is.EqualTo(ParameterDirection.Output));
Assert.That(cmd.Parameters[2].Direction, Is.EqualTo(ParameterDirection.InputOutput));
cmd.Parameters[0].Value = 5;
cmd.Parameters[2].Value = 4;
cmd.ExecuteNonQuery();
Assert.That(cmd.Parameters[0].Value, Is.EqualTo(5));
Assert.That(cmd.Parameters[1].Value, Is.EqualTo("sometext"));
Assert.That(cmd.Parameters[2].Value, Is.EqualTo(9));
}
[Test, Description("Tests function parameter derivation with IN-only parameters")]
public void DeriveParametersInOnly()
{
// This function returns record because of the two Out (InOut & Out) parameters
ExecuteNonQuery(@"CREATE OR REPLACE FUNCTION pg_temp.func(IN param1 INT, IN param2 INT) RETURNS int AS
'
BEGIN
RETURN param1 + param2;
END;
' LANGUAGE 'plpgsql';");
var cmd = new NpgsqlCommand("pg_temp.func", Conn);
cmd.CommandType = CommandType.StoredProcedure;
NpgsqlCommandBuilder.DeriveParameters(cmd);
Assert.That(cmd.Parameters, Has.Count.EqualTo(2));
Assert.That(cmd.Parameters[0].Direction, Is.EqualTo(ParameterDirection.Input));
Assert.That(cmd.Parameters[1].Direction, Is.EqualTo(ParameterDirection.Input));
cmd.Parameters[0].Value = 5;
cmd.Parameters[1].Value = 4;
Assert.That(cmd.ExecuteScalar(), Is.EqualTo(9));
}
[Test, Description("Tests function parameter derivation with no parameters")]
public void DeriveParametersNoParams()
{
// This function returns record because of the two Out (InOut & Out) parameters
ExecuteNonQuery(@"CREATE OR REPLACE FUNCTION pg_temp.func() RETURNS int AS
'
BEGIN
RETURN 4;
END;
' LANGUAGE 'plpgsql';");
var cmd = new NpgsqlCommand("pg_temp.func", Conn);
cmd.CommandType = CommandType.StoredProcedure;
NpgsqlCommandBuilder.DeriveParameters(cmd);
Assert.That(cmd.Parameters, Is.Empty);
}
[Test]
public void FunctionCaseSensitiveNameDeriveParameters()
{
ExecuteNonQuery(@"CREATE OR REPLACE FUNCTION pg_temp.""FunctionCaseSensitive""(int4, text) returns int4 as
$BODY$
begin
return 0;
end
$BODY$
language 'plpgsql';");
var command = new NpgsqlCommand("pg_temp.\"FunctionCaseSensitive\"", Conn);
NpgsqlCommandBuilder.DeriveParameters(command);
Assert.AreEqual(NpgsqlDbType.Integer, command.Parameters[0].NpgsqlDbType);
Assert.AreEqual(NpgsqlDbType.Text, command.Parameters[1].NpgsqlDbType);
}
[Test]
public void DeriveParametersWithParameterNameFromFunction()
{
ExecuteNonQuery(@"CREATE OR REPLACE FUNCTION pg_temp.testoutparameter2(x int, y int, out sum int, out product int) as 'select $1 + $2, $1 * $2' language 'sql';");
var command = new NpgsqlCommand("pg_temp.testoutparameter2", Conn);
command.CommandType = CommandType.StoredProcedure;
NpgsqlCommandBuilder.DeriveParameters(command);
Assert.AreEqual(":x", command.Parameters[0].ParameterName);
Assert.AreEqual(":y", command.Parameters[1].ParameterName);
}
[Test]
public void DeriveInvalidFunction()
{
var invalidCommandName = new NpgsqlCommand("invalidfunctionname", Conn);
Assert.That(() => NpgsqlCommandBuilder.DeriveParameters(invalidCommandName),
Throws.Exception.TypeOf<InvalidOperationException>()
.With.Message.Contains("does not exist"));
}
#endregion
public FunctionTests(string backendVersion) : base(backendVersion) { }
}
}