|
1 |
| -namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Platform |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Globalization; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Moq; |
| 7 | +using Serilog.Debugging; |
| 8 | +using Serilog.Events; |
| 9 | +using Serilog.Sinks.MSSqlServer.Output; |
| 10 | +using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform; |
| 11 | +using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.SqlClient; |
| 12 | +using Serilog.Sinks.MSSqlServer.Tests.TestUtils; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Platform |
2 | 16 | {
|
3 | 17 | public class SqlBulkBatchWriterTests
|
4 | 18 | {
|
| 19 | + private const string _tableName = "TestTableName"; |
| 20 | + private const string _schemaName = "TestSchemaName"; |
| 21 | + private readonly Mock<ISqlConnectionFactory> _sqlConnectionFactoryMock; |
| 22 | + private readonly Mock<ILogEventDataGenerator> _logEventDataGeneratorMock; |
| 23 | + private readonly Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock; |
| 24 | + private readonly Mock<ISqlBulkCopyWrapper> _sqlBulkCopyWrapper; |
| 25 | + private readonly DataTable _dataTable; |
| 26 | + private readonly SqlBulkBatchWriter _sut; |
| 27 | + |
| 28 | + public SqlBulkBatchWriterTests() |
| 29 | + { |
| 30 | + _sqlConnectionFactoryMock = new Mock<ISqlConnectionFactory>(); |
| 31 | + _logEventDataGeneratorMock = new Mock<ILogEventDataGenerator>(); |
| 32 | + _sqlConnectionWrapperMock = new Mock<ISqlConnectionWrapper>(); |
| 33 | + _sqlBulkCopyWrapper = new Mock<ISqlBulkCopyWrapper>(); |
| 34 | + |
| 35 | + _sqlConnectionFactoryMock.Setup(f => f.Create()).Returns(_sqlConnectionWrapperMock.Object); |
| 36 | + _sqlConnectionWrapperMock.Setup(c => c.CreateSqlBulkCopy(It.IsAny<bool>(), It.IsAny<string>())).Returns(_sqlBulkCopyWrapper.Object); |
| 37 | + |
| 38 | + _dataTable = new DataTable(_tableName); |
| 39 | + |
| 40 | + _sut = new SqlBulkBatchWriter(_tableName, _schemaName, false, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void InitializeWithoutTableNameThrows() |
| 45 | + { |
| 46 | + Assert.Throws<ArgumentNullException>(() => new SqlBulkBatchWriter(null, _schemaName, false, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object)); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void InitializeWithoutSchemaNameThrows() |
| 51 | + { |
| 52 | + Assert.Throws<ArgumentNullException>(() => new SqlBulkBatchWriter(_tableName, null, false, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object)); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void InitializeWithoutSqlConnectionFactoryThrows() |
| 57 | + { |
| 58 | + Assert.Throws<ArgumentNullException>(() => new SqlBulkBatchWriter(_tableName, _schemaName, false, null, _logEventDataGeneratorMock.Object)); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void InitializeWithoutLogEventDataGeneratorThrows() |
| 63 | + { |
| 64 | + Assert.Throws<ArgumentNullException>(() => new SqlBulkBatchWriter(_tableName, _schemaName, false, _sqlConnectionFactoryMock.Object, null)); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task WriteBatchCallsLogEventDataGeneratorGetColumnsAndValuesForEachLogEvent() |
| 69 | + { |
| 70 | + // Arrange |
| 71 | + var logEvents = CreateLogEvents(); |
| 72 | + |
| 73 | + // Act |
| 74 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 75 | + |
| 76 | + // Assert |
| 77 | + _logEventDataGeneratorMock.Verify(c => c.GetColumnsAndValues(logEvents[0]), Times.Once); |
| 78 | + _logEventDataGeneratorMock.Verify(c => c.GetColumnsAndValues(logEvents[1]), Times.Once); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public async Task WriteBatchCallsSqlConnectionFactoryCreate() |
| 83 | + { |
| 84 | + // Arrange |
| 85 | + var logEvents = CreateLogEvents(); |
| 86 | + |
| 87 | + // Act |
| 88 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 89 | + |
| 90 | + // Assert |
| 91 | + _sqlConnectionFactoryMock.Verify(f => f.Create(), Times.Once); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task WriteBatchCallsSqlConnectionWrapperOpenAsync() |
| 96 | + { |
| 97 | + // Arrange |
| 98 | + var logEvents = CreateLogEvents(); |
| 99 | + |
| 100 | + // Act |
| 101 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 102 | + |
| 103 | + // Assert |
| 104 | + _sqlConnectionWrapperMock.Verify(c => c.OpenAsync(), Times.Once); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public async Task WriteBatchCallsSqlConnectionWrappeCreateSqlBulkCopy() |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + var logEvents = CreateLogEvents(); |
| 112 | + var expectedDestinationTableName = string.Format(CultureInfo.InvariantCulture, "[{0}].[{1}]", _schemaName, _tableName); |
| 113 | + |
| 114 | + // Act |
| 115 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 116 | + |
| 117 | + // Assert |
| 118 | + _sqlConnectionWrapperMock.Verify(c => c.CreateSqlBulkCopy(false, expectedDestinationTableName), Times.Once); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task WriteBatchCallsSqlConnectionWrappeCreateSqlBulkCopyWithDisableTriggersTrue() |
| 123 | + { |
| 124 | + // Arrange |
| 125 | + var logEvents = CreateLogEvents(); |
| 126 | + var expectedDestinationTableName = string.Format(CultureInfo.InvariantCulture, "[{0}].[{1}]", _schemaName, _tableName); |
| 127 | + var sut = new SqlBulkBatchWriter(_tableName, _schemaName, true, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object); |
| 128 | + |
| 129 | + // Act |
| 130 | + await sut.WriteBatch(logEvents, _dataTable); |
| 131 | + |
| 132 | + // Assert |
| 133 | + _sqlConnectionWrapperMock.Verify(c => c.CreateSqlBulkCopy(true, expectedDestinationTableName), Times.Once); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public async Task WriteBatchCallsSqlBulkCopyWrapperAddSqlBulkCopyColumnMappingForEachColumn() |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + const string column1Name = "Colum1"; |
| 141 | + const string column2Name = "Colum2"; |
| 142 | + var logEvents = CreateLogEvents(); |
| 143 | + _dataTable.Columns.Add(new DataColumn(column1Name)); |
| 144 | + _dataTable.Columns.Add(new DataColumn(column2Name)); |
| 145 | + |
| 146 | + // Act |
| 147 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 148 | + |
| 149 | + // Assert |
| 150 | + _sqlBulkCopyWrapper.Verify(c => c.AddSqlBulkCopyColumnMapping(column1Name, column1Name), Times.Once); |
| 151 | + _sqlBulkCopyWrapper.Verify(c => c.AddSqlBulkCopyColumnMapping(column2Name, column2Name), Times.Once); |
| 152 | + } |
| 153 | + |
| 154 | + [Fact] |
| 155 | + public async Task WriteBatchCallsSqlBulkCopyWrapperWriteToServerAsync() |
| 156 | + { |
| 157 | + // Arrange |
| 158 | + var logEvents = CreateLogEvents(); |
| 159 | + |
| 160 | + // Act |
| 161 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 162 | + |
| 163 | + // Assert |
| 164 | + _sqlBulkCopyWrapper.Verify(c => c.WriteToServerAsync(_dataTable), Times.Once); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public async Task WriteBatchClearsDataTable() |
| 169 | + { |
| 170 | + // Arrange |
| 171 | + var logEvents = CreateLogEvents(); |
| 172 | + |
| 173 | + // Act |
| 174 | + await _sut.WriteBatch(logEvents, _dataTable); |
| 175 | + |
| 176 | + // Assert |
| 177 | + Assert.Empty(_dataTable.Rows); |
| 178 | + } |
| 179 | + |
| 180 | + [Fact] |
| 181 | + public void WriteBatchWritesSelfLogAndRethrowsIfSqlConnectionFactoryCreateThrows() |
| 182 | + { |
| 183 | + // Arrange |
| 184 | + var selfLogWritten = false; |
| 185 | + SelfLog.Enable(w => selfLogWritten = true); |
| 186 | + _sqlConnectionFactoryMock.Setup(f => f.Create()).Callback(() => throw new Exception()); |
| 187 | + var logEvents = CreateLogEvents(); |
| 188 | + |
| 189 | + // Act + assert |
| 190 | + Assert.ThrowsAsync<Exception>(() => _sut.WriteBatch(logEvents, _dataTable)); |
| 191 | + Assert.True(selfLogWritten); |
| 192 | + } |
| 193 | + |
| 194 | + [Fact] |
| 195 | + public void WriteBatchWritesSelfLogAndRethrowsIfSqlConnectionOpenAsyncThrows() |
| 196 | + { |
| 197 | + // Arrange |
| 198 | + var selfLogWritten = false; |
| 199 | + SelfLog.Enable(w => selfLogWritten = true); |
| 200 | + _sqlConnectionWrapperMock.Setup(c => c.OpenAsync()).Callback(() => throw new Exception()); |
| 201 | + var logEvents = CreateLogEvents(); |
| 202 | + |
| 203 | + // Act + assert |
| 204 | + Assert.ThrowsAsync<Exception>(() => _sut.WriteBatch(logEvents, _dataTable)); |
| 205 | + Assert.True(selfLogWritten); |
| 206 | + } |
| 207 | + |
| 208 | + [Fact] |
| 209 | + public void WriteBatchWritesSelfLogAndRethrowsIfSqlBulkCopyWriterAddSqlBulkCopyColumnMappingThrows() |
| 210 | + { |
| 211 | + // Arrange |
| 212 | + var selfLogWritten = false; |
| 213 | + SelfLog.Enable(w => selfLogWritten = true); |
| 214 | + _sqlBulkCopyWrapper.Setup(c => c.AddSqlBulkCopyColumnMapping(It.IsAny<string>(), It.IsAny<string>())) |
| 215 | + .Callback(() => throw new Exception()); |
| 216 | + var logEvents = CreateLogEvents(); |
| 217 | + _dataTable.Columns.Add(new DataColumn("ColumnName")); |
| 218 | + |
| 219 | + // Act + assert |
| 220 | + Assert.ThrowsAsync<Exception>(() => _sut.WriteBatch(logEvents, _dataTable)); |
| 221 | + Assert.True(selfLogWritten); |
| 222 | + } |
| 223 | + |
| 224 | + [Fact] |
| 225 | + public void WriteBatchWritesSelfLogAndRethrowsIfSqlBulkCopyWriterWriteToServerAsyncThrows() |
| 226 | + { |
| 227 | + // Arrange |
| 228 | + var selfLogWritten = false; |
| 229 | + SelfLog.Enable(w => selfLogWritten = true); |
| 230 | + _sqlBulkCopyWrapper.Setup(c => c.WriteToServerAsync(It.IsAny<DataTable>())) |
| 231 | + .Callback(() => throw new Exception()); |
| 232 | + var logEvents = CreateLogEvents(); |
| 233 | + |
| 234 | + // Act + assert |
| 235 | + Assert.ThrowsAsync<Exception>(() => _sut.WriteBatch(logEvents, _dataTable)); |
| 236 | + Assert.True(selfLogWritten); |
| 237 | + } |
| 238 | + |
| 239 | + private static List<LogEvent> CreateLogEvents() |
| 240 | + { |
| 241 | + var logEvents = new List<LogEvent>(); |
| 242 | + logEvents.Add(TestLogEventHelper.CreateLogEvent()); |
| 243 | + logEvents.Add(TestLogEventHelper.CreateLogEvent()); |
| 244 | + return logEvents; |
| 245 | + } |
5 | 246 | }
|
6 | 247 | }
|
0 commit comments