forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestBase.cs
265 lines (236 loc) · 9.56 KB
/
TestBase.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// Author:
// Francisco Figueiredo Jr. <[email protected]>
//
// Copyright (C) 2002-2005 The Npgsql Development Team
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using NLog.Config;
using NLog.Targets;
using System.Text;
using Npgsql;
using Npgsql.Logging;
using Npgsql.Tests;
using NpgsqlTypes;
using NUnit.Framework;
namespace Npgsql.Tests
{
[TestFixture("9.4")]
[TestFixture("9.3")]
[TestFixture("9.2")]
[TestFixture("9.1")]
[TestFixture("9.0")]
public abstract class TestBase
{
protected Version BackendVersion { get; private set; }
/// <summary>
/// Constructs the parameterized test fixture
/// </summary>
/// <param name="backendVersion">
/// The version of the Postgres backend to be used, major and minor veresions (e.g. 9.3).
/// Used to select the conn string environment variable to be used.
/// </param>
protected TestBase(string backendVersion)
{
BackendVersion = new Version(backendVersion);
}
/// <summary>
/// A connection to the test database, set up prior to running each test.
/// </summary>
internal NpgsqlConnection Conn { get; set; }
/// <summary>
/// The connection string that will be used when opening the connection to the tests database.
/// May be overridden in fixtures, e.g. to set special connection parameters
/// </summary>
protected virtual string ConnectionString { get { return _connectionString; } }
private string _connectionString;
static bool _loggingSetUp;
/// <summary>
/// New ConectionString property crafted to change the database name from original TestBase.ConnectionString to append a "_ef" suffix.
/// i.e.: the TestBase.ConnectionString database is npgsql_tests. Entity Framework database will be npgsql_tests_ef.
/// </summary>
protected virtual string ConnectionStringEF
{
get
{
if (connectionStringEF == null)
{
//Reuse all strings just add _ef at end of database name for
var connectionSB = new NpgsqlConnectionStringBuilder(ConnectionString);
connectionSB.Database += "_ef";
connectionStringEF = connectionSB.ConnectionString;
}
return connectionStringEF;
}
}
private string connectionStringEF;
/// <summary>
/// Unless the NPGSQL_TEST_DB environment variable is defined, this is used as the connection string for the
/// test database.
/// </summary>
private const string DEFAULT_CONNECTION_STRING = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests";
#region Setup / Teardown
[TestFixtureSetUp]
public virtual void TestFixtureSetup()
{
SetupLogging();
var connStringEnvVar = "NPGSQL_TEST_DB_" + BackendVersion.ToString().Replace(".", "_");
_connectionString = Environment.GetEnvironmentVariable(connStringEnvVar);
if (_connectionString != null)
{
Console.WriteLine("Using connection string provided in env var {0}: {1}", connStringEnvVar, _connectionString);
return;
}
if (BackendVersion == LatestBackendVersion)
{
_connectionString = DEFAULT_CONNECTION_STRING;
Console.WriteLine("Using internal default connection string: " + _connectionString);
}
else
{
Assert.Ignore("Skipping tests for backend version {0}, environment variable {1} isn't defined", BackendVersion, connStringEnvVar);
}
}
[SetUp]
protected virtual void SetUp()
{
Conn = new NpgsqlConnection(ConnectionString);
try
{
Conn.Open();
}
catch (NpgsqlException e)
{
if (e.Code == "3D000")
TestUtil.IgnoreExceptOnBuildServer("Please create a database npgsql_tests, owned by user npgsql_tests");
else if (e.Code == "28P01")
TestUtil.IgnoreExceptOnBuildServer("Please create a user npgsql_tests as follows: create user npgsql_tests with password 'npgsql_tests'");
else
throw;
}
}
[TearDown]
protected virtual void TearDown()
{
try { Conn.Close(); }
finally { Conn = null; }
}
protected void CreateSchema(string schemaName)
{
if (Conn.PostgreSqlVersion >= new Version(9, 3))
ExecuteNonQuery(String.Format("CREATE SCHEMA IF NOT EXISTS {0}", schemaName));
else
{
try { ExecuteNonQuery(String.Format("CREATE SCHEMA {0}", schemaName)); }
catch (NpgsqlException e) {
if (e.Code != "42P06")
throw;
}
}
}
/// <summary>
/// Uses reflection to read the [TextFixture] attributes on this class and extract the latest
/// Postgres backend version specified within them.
/// </summary>
private Version LatestBackendVersion
{
get
{
return typeof(TestBase)
.GetCustomAttributes(typeof (TestFixtureAttribute), false)
.Cast<TestFixtureAttribute>()
.Select(a => new Version((string) a.Arguments[0]))
.Max();
}
}
protected virtual void SetupLogging()
{
var config = new LoggingConfiguration();
var consoleTarget = new ConsoleTarget();
consoleTarget.Layout = @"${message}";
config.AddTarget("console", consoleTarget);
var rule = new LoggingRule("*", NLog.LogLevel.Debug, consoleTarget);
config.LoggingRules.Add(rule);
NLog.LogManager.Configuration = config;
if (!_loggingSetUp)
{
NpgsqlLogManager.Provider = new NLogLoggingProvider();
_loggingSetUp = true;
}
}
#endregion
#region Utilities for use by tests
protected int ExecuteNonQuery(string sql, NpgsqlConnection conn = null, NpgsqlTransaction tx = null)
{
if (conn == null)
conn = Conn;
var cmd = tx == null ? new NpgsqlCommand(sql, conn) : new NpgsqlCommand(sql, conn, tx);
using (cmd)
return cmd.ExecuteNonQuery();
}
protected object ExecuteScalar(string sql, NpgsqlConnection conn = null, NpgsqlTransaction tx = null)
{
if (conn == null)
conn = Conn;
var cmd = tx == null ? new NpgsqlCommand(sql, conn) : new NpgsqlCommand(sql, conn, tx);
using (cmd)
return cmd.ExecuteScalar();
}
#if !NET40
protected async Task<int> ExecuteNonQueryAsync(string sql, NpgsqlConnection conn = null, NpgsqlTransaction tx = null)
{
if (conn == null)
conn = Conn;
var cmd = tx == null ? new NpgsqlCommand(sql, conn) : new NpgsqlCommand(sql, conn, tx);
using (cmd)
return await cmd.ExecuteNonQueryAsync();
}
protected async Task<object> ExecuteScalarAsync(string sql, NpgsqlConnection conn = null, NpgsqlTransaction tx = null)
{
if (conn == null)
conn = Conn;
var cmd = tx == null ? new NpgsqlCommand(sql, conn) : new NpgsqlCommand(sql, conn, tx);
using (cmd)
return await cmd.ExecuteScalarAsync();
}
#endif
protected static bool IsSequential(CommandBehavior behavior)
{
return (behavior & CommandBehavior.SequentialAccess) != 0;
}
/// <summary>
/// In PG under 9.1 you can't do SELECT pg_sleep(2) in binary because that function returns void and PG doesn't know
/// how to transfer that. So cast to text server-side.
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
protected static NpgsqlCommand CreateSleepCommand(NpgsqlConnection conn, int seconds)
{
return new NpgsqlCommand(string.Format("SELECT pg_sleep({0}){1}", seconds, conn.PostgreSqlVersion < new Version(9, 1, 0) ? "::TEXT" : ""), conn);
}
#endregion
}
}