Skip to content

Commit 58c1135

Browse files
committed
* Added some unit tests for SqlLogEventWriter
* Added wrappers for SqlClientClasses to increase testablility (SqlConnectionWrapper, SqlCommandWrapper).
1 parent 102f660 commit 58c1135

File tree

13 files changed

+227
-29
lines changed

13 files changed

+227
-29
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.Common;
4+
5+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
6+
{
7+
internal interface ISqlCommandWrapper : IDisposable
8+
{
9+
CommandType CommandType { get; set; }
10+
DbParameterCollection Parameters { get; }
11+
string CommandText { get; set; }
12+
13+
int ExecuteNonQuery();
14+
}
15+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System.Data.SqlClient;
2-
3-
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
1+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
42
{
53
internal interface ISqlConnectionFactory
64
{
7-
SqlConnection Create();
5+
ISqlConnectionWrapper Create();
86
}
97
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Threading.Tasks;
4+
5+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
6+
{
7+
internal interface ISqlConnectionWrapper : IDisposable
8+
{
9+
SqlConnection SqlConnection { get; }
10+
11+
void Open();
12+
Task OpenAsync();
13+
ISqlCommandWrapper CreateCommand();
14+
}
15+
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlBulkBatchWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public async Task WriteBatch(IEnumerable<LogEvent> events, DataTable dataTable)
4444
{
4545
await cn.OpenAsync().ConfigureAwait(false);
4646
using (var copy = _disableTriggers
47-
? new SqlBulkCopy(cn)
48-
: new SqlBulkCopy(cn, SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers, null)
47+
? new SqlBulkCopy(cn.SqlConnection)
48+
: new SqlBulkCopy(cn.SqlConnection, SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers, null)
4949
)
5050
{
5151
copy.DestinationTableName = string.Format(CultureInfo.InvariantCulture, "[{0}].[{1}]", _schemaName, _tableName);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.Common;
4+
using System.Data.SqlClient;
5+
6+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
7+
{
8+
internal class SqlCommandWrapper : ISqlCommandWrapper
9+
{
10+
private readonly SqlCommand _sqlCommand;
11+
private bool _disposedValue;
12+
13+
public SqlCommandWrapper(SqlCommand sqlCommand)
14+
{
15+
_sqlCommand = sqlCommand ?? throw new ArgumentNullException(nameof(sqlCommand));
16+
}
17+
18+
public CommandType CommandType
19+
{
20+
get => _sqlCommand.CommandType;
21+
set => _sqlCommand.CommandType = value;
22+
}
23+
24+
public DbParameterCollection Parameters => _sqlCommand.Parameters;
25+
26+
public string CommandText
27+
{
28+
get => _sqlCommand.CommandText;
29+
set => _sqlCommand.CommandText = value;
30+
}
31+
32+
public int ExecuteNonQuery() =>
33+
_sqlCommand.ExecuteNonQuery();
34+
35+
protected virtual void Dispose(bool disposing)
36+
{
37+
if (!_disposedValue)
38+
{
39+
_sqlCommand.Dispose();
40+
_disposedValue = true;
41+
}
42+
}
43+
44+
public void Dispose()
45+
{
46+
Dispose(disposing: true);
47+
GC.SuppressFinalize(this);
48+
}
49+
}
50+
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlConnectionFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public SqlConnectionFactory(string connectionString, IAzureManagedServiceAuthent
2020
?? throw new ArgumentNullException(nameof(azureManagedServiceAuthenticator));
2121
}
2222

23-
public SqlConnection Create()
23+
public ISqlConnectionWrapper Create()
2424
{
2525
var sqlConnection = new SqlConnection(_connectionString);
26-
2726
_azureManagedServiceAuthenticator.SetAuthenticationToken(sqlConnection);
2827

29-
return sqlConnection;
28+
var sqlConnectionWrapper = new SqlConnectionWrapper(sqlConnection);
29+
return sqlConnectionWrapper;
3030
}
3131
}
3232
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Threading.Tasks;
4+
5+
namespace Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform
6+
{
7+
internal class SqlConnectionWrapper : ISqlConnectionWrapper
8+
{
9+
private readonly SqlConnection _sqlConnection;
10+
private bool _disposedValue;
11+
12+
public SqlConnectionWrapper(SqlConnection sqlConnection)
13+
{
14+
_sqlConnection = sqlConnection ?? throw new ArgumentNullException(nameof(sqlConnection));
15+
}
16+
17+
public SqlConnection SqlConnection => _sqlConnection;
18+
19+
public void Open()
20+
{
21+
_sqlConnection.Open();
22+
}
23+
24+
public async Task OpenAsync()
25+
{
26+
await _sqlConnection.OpenAsync().ConfigureAwait(false);
27+
}
28+
29+
public ISqlCommandWrapper CreateCommand()
30+
{
31+
var sqlCommand = _sqlConnection.CreateCommand();
32+
return new SqlCommandWrapper(sqlCommand);
33+
}
34+
35+
protected virtual void Dispose(bool disposing)
36+
{
37+
if (!_disposedValue)
38+
{
39+
_sqlConnection.Dispose();
40+
_disposedValue = true;
41+
}
42+
}
43+
44+
public void Dispose()
45+
{
46+
Dispose(disposing: true);
47+
GC.SuppressFinalize(this);
48+
}
49+
}
50+
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlLogEventWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void WriteEvent(LogEvent logEvent)
3434
using (var connection = _sqlConnectionFactory.Create())
3535
{
3636
connection.Open();
37-
using (SqlCommand command = connection.CreateCommand())
37+
using (ISqlCommandWrapper command = connection.CreateCommand())
3838
{
3939
command.CommandType = CommandType.Text;
4040

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlTableCreator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void CreateTable(DataTable dataTable)
3535
using (var conn = _sqlConnectionFactory.Create())
3636
{
3737
var sql = _sqlCreateTableWriter.GetSqlFromDataTable(_schemaName, _tableName, dataTable, _columnOptions);
38-
using (var cmd = new SqlCommand(sql, conn))
38+
using (var cmd = new SqlCommand(sql, conn.SqlConnection))
3939
{
4040
conn.Open();
4141
cmd.ExecuteNonQuery();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Platform
2+
{
3+
public class SqlBulkBatchWriterTests
4+
{
5+
}
6+
}

0 commit comments

Comments
 (0)