forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBase.cs
More file actions
128 lines (109 loc) · 4.92 KB
/
TestBase.cs
File metadata and controls
128 lines (109 loc) · 4.92 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
#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.Data;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Npgsql.Logging;
using Npgsql.Tests.Util.Logging;
using NUnit.Framework;
namespace Npgsql.Tests
{
public abstract class TestBase
{
/// <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 =>
_connectionString ?? (_connectionString = Environment.GetEnvironmentVariable("NPGSQL_TEST_DB") ?? DefaultConnectionString);
string _connectionString;
static bool _loggingSetUp;
protected static TestLoggerSink TestLoggerSink { get; } = new TestLoggerSink();
/// <summary>
/// Unless the NPGSQL_TEST_DB environment variable is defined, this is used as the connection string for the
/// test database.
/// </summary>
const string DefaultConnectionString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests";
#region Setup / Teardown
[SetUp]
public void Setup()
{
TestLoggerSink.Clear();
}
[OneTimeSetUp]
public virtual void TestFixtureSetup()
{
if (!_loggingSetUp)
SetupLogging();
}
protected virtual void SetupLogging()
{
NpgsqlLogManager.LoggerFactory = new LoggerFactory();
NpgsqlLogManager.LoggerFactory.AddProvider(new TestLoggerProvider(TestLoggerSink));
var logLevelText = Environment.GetEnvironmentVariable("NPGSQL_TEST_LOGGING");
if (logLevelText != null)
{
LogLevel logLevel;
if (!Enum.TryParse(logLevelText, true, out logLevel))
throw new ArgumentOutOfRangeException($"Invalid loglevel in NPGSQL_TEST_LOGGING: {logLevelText}");
NpgsqlLogManager.LoggerFactory.AddConsole((text, level) => level >= logLevel);
NpgsqlLogManager.IsParameterLoggingEnabled = true;
}
_loggingSetUp = true;
}
#endregion
#region Utilities for use by tests
protected NpgsqlConnection OpenConnection(string connectionString = null)
{
if (connectionString == null)
connectionString = ConnectionString;
var conn = new NpgsqlConnection(connectionString);
try
{
conn.Open();
}
catch (PostgresException e)
{
if (e.SqlState == "3D000")
TestUtil.IgnoreExceptOnBuildServer("Please create a database npgsql_tests, owned by user npgsql_tests");
else if (e.SqlState == "28P01")
TestUtil.IgnoreExceptOnBuildServer("Please create a user npgsql_tests as follows: create user npgsql_tests with password 'npgsql_tests'");
else
throw;
}
return conn;
}
protected NpgsqlConnection OpenConnection(NpgsqlConnectionStringBuilder csb)
=> OpenConnection(csb.ToString());
protected static bool IsSequential(CommandBehavior behavior)
=> (behavior & CommandBehavior.SequentialAccess) != 0;
// 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.
protected static NpgsqlCommand CreateSleepCommand(NpgsqlConnection conn, int seconds = 1000)
=> new NpgsqlCommand($"SELECT pg_sleep({seconds}){(conn.PostgreSqlVersion < new Version(9, 1, 0) ? "::TEXT" : "")}", conn);
#endregion
}
}