-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathTestDatabaseFacade.cs
More file actions
119 lines (103 loc) · 3.5 KB
/
Copy pathTestDatabaseFacade.cs
File metadata and controls
119 lines (103 loc) · 3.5 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
namespace tSQLtCLR
{
class TestDatabaseFacade : IDisposable
{
private SqlConnection connection;
private SqlString infoMessage;
Boolean disposed = false;
public TestDatabaseFacade()
{
connect();
}
public void Dispose()
{
if (!disposed)
{
disconnect();
disposed = true;
}
GC.SuppressFinalize(this);
}
public SqlString InfoMessage
{
get { return infoMessage; }
}
private void connect()
{
connection = new SqlConnection();
connection.ConnectionString = "Context Connection=true;";
connection.Open();
}
private void disconnect()
{
connection.Dispose();
}
public String ServerName
{
get {
SqlDataReader reader = executeCommand("SELECT SERVERPROPERTY('ServerName');");
reader.Read();
String serverName = reader.GetString(0);
reader.Close();
return serverName;
}
}
public String DatabaseName
{
get { return connection.Database; }
}
public SqlDataReader executeCommand(SqlString Command)
{
infoMessage = SqlString.Null;
connection.InfoMessage += OnInfoMessage;
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = Command.ToString();
SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
return dataReader;
}
protected void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
{
if (infoMessage.IsNull)
{
infoMessage = "";
}
infoMessage += args.Message + "\r\n";
}
public void assertEquals(String expectedString, String actualString)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "tSQLt.AssertEqualsString";
cmd.Parameters.AddWithValue("Expected", expectedString);
cmd.Parameters.AddWithValue("Actual", actualString);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
public void failTestCaseAndThrowException(String failureMessage)
{
// tSQLt.Fail throws an exception which is uncaught and passed upwards
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "tSQLt.Fail";
cmd.Parameters.AddWithValue("Message0", failureMessage);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
public void logCapturedOutput(SqlString text)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "tSQLt.LogCapturedOutput";
cmd.Parameters.AddWithValue("text", text);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
}
}