Skip to content

Commit 3fb046f

Browse files
committed
Added unit tests for SqlClient wrapper classes.
1 parent 77a9e25 commit 3fb046f

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
#if NET452
3+
using System.Data.SqlClient;
4+
#else
5+
using Microsoft.Data.SqlClient;
6+
#endif
7+
using Serilog.Sinks.MSSqlServer.Platform.SqlClient;
8+
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
9+
using Xunit;
10+
11+
namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Platform.SqlClient
12+
{
13+
[Trait(TestCategory.TraitName, TestCategory.Unit)]
14+
public class SqlBulkCopyWrapperTests
15+
{
16+
[Fact]
17+
public void InitializeThrowsIfSqlBulkCopyIsNull()
18+
{
19+
Assert.Throws<ArgumentNullException>(() => new SqlBulkCopyWrapper(null));
20+
}
21+
22+
[Fact]
23+
public void AddSqlBulkCopyColumnMappingDoesNotThrow()
24+
{
25+
// Arrange
26+
using (var connection = new SqlConnection())
27+
{
28+
using (var sqlBulkCopy = new SqlBulkCopy(connection))
29+
{
30+
using (var sut = new SqlBulkCopyWrapper(sqlBulkCopy))
31+
{
32+
// Act (should not throw)
33+
sut.AddSqlBulkCopyColumnMapping("Column", "Column");
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
#if NET452
3+
using System.Data.SqlClient;
4+
#else
5+
using Microsoft.Data.SqlClient;
6+
#endif
7+
using Xunit;
8+
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
9+
using Serilog.Sinks.MSSqlServer.Platform.SqlClient;
10+
11+
namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Platform.SqlClient
12+
{
13+
[Trait(TestCategory.TraitName, TestCategory.Unit)]
14+
public class SqlCommandWrapperTests
15+
{
16+
[Fact]
17+
public void InitializeThrowsIfSqlCommandIsNull()
18+
{
19+
Assert.Throws<ArgumentNullException>(() => new SqlCommandWrapper(null));
20+
}
21+
22+
[Fact]
23+
public void AddParameterDoesNotThrow()
24+
{
25+
// Arrange
26+
using (var sqlCommand = new SqlCommand())
27+
{
28+
using (var sut = new SqlCommandWrapper(sqlCommand))
29+
{
30+
// Act (should not throw)
31+
sut.AddParameter("Parameter", "Value");
32+
}
33+
}
34+
}
35+
}
36+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#if NET452
2+
using System.Data.SqlClient;
3+
#else
4+
using Microsoft.Data.SqlClient;
5+
#endif
6+
using Xunit;
7+
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
8+
using Serilog.Sinks.MSSqlServer.Platform.SqlClient;
9+
using System;
10+
11+
namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Platform.SqlClient
12+
{
13+
[Trait(TestCategory.TraitName, TestCategory.Unit)]
14+
public class SqlConnectionWrapperTests
15+
{
16+
[Fact]
17+
public void InitializeThrowsIfCalledWithAuthenticationTokenOnDotNetFramework452ButNotOnOtherTargets()
18+
{
19+
#if NET452
20+
Assert.Throws<InvalidOperationException>(() => new SqlConnectionWrapper(DatabaseFixture.LogEventsConnectionString, "AuthenticationToken"));
21+
#else
22+
// Should not throw
23+
new SqlConnectionWrapper(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Master", "AuthenticationToken");
24+
#endif
25+
}
26+
27+
[Fact]
28+
public void CreateCommandReturnsSqlCommandWrapper()
29+
{
30+
// Arrange
31+
using (var sut = new SqlConnectionWrapper(DatabaseFixture.LogEventsConnectionString, null))
32+
{
33+
// Act
34+
var result = sut.CreateCommand();
35+
36+
// Assert
37+
Assert.NotNull(result);
38+
}
39+
}
40+
41+
[Fact]
42+
public void CreateCommandWithParameterReturnsSqlCommandWrapper()
43+
{
44+
// Arrange
45+
using (var sut = new SqlConnectionWrapper(DatabaseFixture.LogEventsConnectionString, null))
46+
{
47+
// Act
48+
var result = sut.CreateCommand("CommandText");
49+
50+
// Assert
51+
Assert.NotNull(result);
52+
Assert.Equal("CommandText", result.CommandText);
53+
}
54+
}
55+
56+
[Fact]
57+
public void CreateSqlBulkCopyReturnsSqlBulkCopyWrapper()
58+
{
59+
// Arrange
60+
using (var sut = new SqlConnectionWrapper(DatabaseFixture.LogEventsConnectionString, null))
61+
{
62+
// Act
63+
var result = sut.CreateSqlBulkCopy(false, "DestinationTableName");
64+
65+
// Assert
66+
Assert.NotNull(result);
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)