forked from roji/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionTests.cs
More file actions
177 lines (165 loc) · 7.3 KB
/
FunctionTests.cs
File metadata and controls
177 lines (165 loc) · 7.3 KB
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
#region License
// The PostgreSQL License
//
// Copyright (C) 2017 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
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()
{
using (var conn = OpenConnection())
{
conn.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()
{
using (var conn = OpenConnection())
{
conn.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()
{
using (var conn = OpenConnection())
{
conn.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_in", "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()
{
using (var conn = OpenConnection())
{
conn.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]
public void Void()
{
using (var conn = OpenConnection())
{
TestUtil.MinimumPgVersion(conn, "9.1.0", "no binary output function available for type void before 9.1.0");
var command = new NpgsqlCommand("pg_sleep", conn);
command.Parameters.AddWithValue(0);
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
}
}
[Test]
public void NamedParameters()
{
using (var conn = OpenConnection())
{
TestUtil.MinimumPgVersion(conn, "9.4.0", "make_timestamp was introduced in 9.4");
using (var command = new NpgsqlCommand("make_timestamp", conn))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("year", 2015);
command.Parameters.AddWithValue("month", 8);
command.Parameters.AddWithValue("mday", 1);
command.Parameters.AddWithValue("hour", 2);
command.Parameters.AddWithValue("min", 3);
command.Parameters.AddWithValue("sec", 4);
var dt = (DateTime) command.ExecuteScalar();
Assert.AreEqual(new DateTime(2015, 8, 1, 2, 3, 4), dt);
command.Parameters[0].Value = 2014;
command.Parameters[0].ParameterName = ""; // 2014 will be sent as a positional parameter
dt = (DateTime) command.ExecuteScalar();
Assert.AreEqual(new DateTime(2014, 8, 1, 2, 3, 4), dt);
}
}
}
[Test]
public void TooManyOutputParams()
{
using (var conn = OpenConnection())
{
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));
}
}
}
}