1
1
using System ;
2
2
using Microsoft . Extensions . Configuration ;
3
3
using Moq ;
4
+ using Serilog . Configuration ;
4
5
using Serilog . Formatting ;
5
6
using Serilog . Sinks . MSSqlServer . Configuration . Factories ;
6
7
using Xunit ;
@@ -45,10 +46,9 @@ public void MSSqlServerCallsApplyMicrosoftExtensionsConfigurationGetConnectionSt
45
46
}
46
47
47
48
[ Fact ]
48
- public void MSSqlServerCallsSinkFactoryWithConnectionStringFromConfig ( )
49
+ public void MSSqlServerCallsSinkFactoryWithConnectionStringFromMicrosoftConfigExtensions ( )
49
50
{
50
51
// Arrange
51
- const string inputConnectionString = "TestConnectionString" ;
52
52
const string configConnectionString = "TestConnectionStringFromConfig" ;
53
53
_applyMicrosoftExtensionsConfigurationMock . Setup ( c => c . GetConnectionString ( It . IsAny < string > ( ) , It . IsAny < IConfiguration > ( ) ) )
54
54
. Returns ( configConnectionString ) ;
@@ -57,16 +57,14 @@ public void MSSqlServerCallsSinkFactoryWithConnectionStringFromConfig()
57
57
58
58
// Act
59
59
_loggerConfiguration . WriteTo . MSSqlServerInternal (
60
- connectionString : inputConnectionString ,
60
+ connectionString : "TestConnectionString" ,
61
61
tableName : "TestTableName" ,
62
62
appConfiguration : appConfigurationMock . Object ,
63
63
applySystemConfiguration : _applySystemConfigurationMock . Object ,
64
64
applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
65
65
sinkFactory : sinkFactoryMock . Object ) ;
66
66
67
67
// Assert
68
- _applyMicrosoftExtensionsConfigurationMock . Verify ( c => c . GetConnectionString ( inputConnectionString , appConfigurationMock . Object ) ,
69
- Times . Once ) ;
70
68
sinkFactoryMock . Verify ( f => f . Create ( configConnectionString , It . IsAny < string > ( ) , It . IsAny < int > ( ) , It . IsAny < TimeSpan > ( ) ,
71
69
It . IsAny < IFormatProvider > ( ) , It . IsAny < bool > ( ) , It . IsAny < ColumnOptions > ( ) , It . IsAny < string > ( ) , It . IsAny < ITextFormatter > ( ) ) , Times . Once ) ;
72
70
}
@@ -114,10 +112,9 @@ public void MSSqlServerCallsApplyMicrosoftExtensionsConfigurationGetColumnOption
114
112
}
115
113
116
114
[ Fact ]
117
- public void MSSqlServerCallsSinkFactoryWithColumnOptionsFromConfig ( )
115
+ public void MSSqlServerCallsSinkFactoryWithColumnOptionsFromMicrosoftConfigExtensions ( )
118
116
{
119
117
// Arrange
120
- var inputColumnOptions = new ColumnOptions ( ) ;
121
118
var configColumnOptions = new ColumnOptions ( ) ;
122
119
var columnOptionsSectionMock = new Mock < IConfigurationSection > ( ) ;
123
120
_applyMicrosoftExtensionsConfigurationMock . Setup ( c => c . ConfigureColumnOptions ( It . IsAny < ColumnOptions > ( ) , It . IsAny < IConfigurationSection > ( ) ) )
@@ -128,15 +125,13 @@ public void MSSqlServerCallsSinkFactoryWithColumnOptionsFromConfig()
128
125
_loggerConfiguration . WriteTo . MSSqlServerInternal (
129
126
connectionString : "TestConnectionString" ,
130
127
tableName : "TestTableName" ,
131
- columnOptions : inputColumnOptions ,
128
+ columnOptions : new ColumnOptions ( ) ,
132
129
columnOptionsSection : columnOptionsSectionMock . Object ,
133
130
applySystemConfiguration : _applySystemConfigurationMock . Object ,
134
131
applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
135
132
sinkFactory : sinkFactoryMock . Object ) ;
136
133
137
134
// Assert
138
- _applyMicrosoftExtensionsConfigurationMock . Verify ( c => c . ConfigureColumnOptions ( inputColumnOptions , columnOptionsSectionMock . Object ) ,
139
- Times . Once ) ;
140
135
sinkFactoryMock . Verify ( f => f . Create ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < int > ( ) , It . IsAny < TimeSpan > ( ) ,
141
136
It . IsAny < IFormatProvider > ( ) , It . IsAny < bool > ( ) , configColumnOptions , It . IsAny < string > ( ) , It . IsAny < ITextFormatter > ( ) ) , Times . Once ) ;
142
137
}
@@ -160,6 +155,138 @@ public void MSSqlServerDoesNotCallApplyMicrosoftExtensionsConfigurationGetColumn
160
155
Times . Never ) ;
161
156
}
162
157
158
+ [ Fact ]
159
+ public void MSSqlServerCallsApplySystemConfigurationGetConnectionString ( )
160
+ {
161
+ // Arrange
162
+ const string inputConnectionString = "TestConnectionString" ;
163
+ _applySystemConfigurationMock . Setup ( c => c . GetSinkConfigurationSection ( It . IsAny < string > ( ) ) )
164
+ . Returns ( new MSSqlServerConfigurationSection ( ) ) ;
165
+ var sinkFactoryMock = new Mock < IMSSqlServerSinkFactory > ( ) ;
166
+
167
+ // Act
168
+ _loggerConfiguration . WriteTo . MSSqlServerInternal (
169
+ connectionString : inputConnectionString ,
170
+ tableName : "TestTableName" ,
171
+ applySystemConfiguration : _applySystemConfigurationMock . Object ,
172
+ applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
173
+ sinkFactory : sinkFactoryMock . Object ) ;
174
+
175
+ // Assert
176
+ _applySystemConfigurationMock . Verify ( c => c . GetConnectionString ( inputConnectionString ) ,
177
+ Times . Once ) ;
178
+ }
179
+
180
+ [ Fact ]
181
+ public void MSSqlServerCallsSinkFactoryWithConnectionStringFromSystemConfig ( )
182
+ {
183
+ // Arrange
184
+ const string configConnectionString = "TestConnectionStringFromConfig" ;
185
+ _applySystemConfigurationMock . Setup ( c => c . GetSinkConfigurationSection ( It . IsAny < string > ( ) ) )
186
+ . Returns ( new MSSqlServerConfigurationSection ( ) ) ;
187
+ _applySystemConfigurationMock . Setup ( c => c . GetConnectionString ( It . IsAny < string > ( ) ) )
188
+ . Returns ( configConnectionString ) ;
189
+ var sinkFactoryMock = new Mock < IMSSqlServerSinkFactory > ( ) ;
190
+
191
+ // Act
192
+ _loggerConfiguration . WriteTo . MSSqlServerInternal (
193
+ connectionString : "TestConnectionString" ,
194
+ tableName : "TestTableName" ,
195
+ applySystemConfiguration : _applySystemConfigurationMock . Object ,
196
+ applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
197
+ sinkFactory : sinkFactoryMock . Object ) ;
198
+
199
+ // Assert
200
+ sinkFactoryMock . Verify ( f => f . Create ( configConnectionString , It . IsAny < string > ( ) , It . IsAny < int > ( ) , It . IsAny < TimeSpan > ( ) ,
201
+ It . IsAny < IFormatProvider > ( ) , It . IsAny < bool > ( ) , It . IsAny < ColumnOptions > ( ) , It . IsAny < string > ( ) , It . IsAny < ITextFormatter > ( ) ) , Times . Once ) ;
202
+ }
203
+
204
+ [ Fact ]
205
+ public void MSSqlServerDoesNotCallApplySystemConfigurationGetConnectionStringWhenNotUsingSystemConfig ( )
206
+ {
207
+ // Arrange
208
+ var sinkFactoryMock = new Mock < IMSSqlServerSinkFactory > ( ) ;
209
+
210
+ // Act
211
+ _loggerConfiguration . WriteTo . MSSqlServerInternal (
212
+ connectionString : "TestConnectionString" ,
213
+ tableName : "TestTableName" ,
214
+ applySystemConfiguration : _applySystemConfigurationMock . Object ,
215
+ applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
216
+ sinkFactory : sinkFactoryMock . Object ) ;
217
+
218
+ // Assert
219
+ _applySystemConfigurationMock . Verify ( c => c . GetConnectionString ( It . IsAny < string > ( ) ) , Times . Never ) ;
220
+ }
221
+
222
+ [ Fact ]
223
+ public void MSSqlServerCallsApplySystemConfigurationGetColumnOptions ( )
224
+ {
225
+ // Arrange
226
+ var columnOptions = new ColumnOptions ( ) ;
227
+ var systemConfigSection = new MSSqlServerConfigurationSection ( ) ;
228
+ _applySystemConfigurationMock . Setup ( c => c . GetSinkConfigurationSection ( It . IsAny < string > ( ) ) )
229
+ . Returns ( systemConfigSection ) ;
230
+ var sinkFactoryMock = new Mock < IMSSqlServerSinkFactory > ( ) ;
231
+
232
+ // Act
233
+ _loggerConfiguration . WriteTo . MSSqlServerInternal (
234
+ connectionString : "TestConnectionString" ,
235
+ tableName : "TestTableName" ,
236
+ columnOptions : columnOptions ,
237
+ applySystemConfiguration : _applySystemConfigurationMock . Object ,
238
+ applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
239
+ sinkFactory : sinkFactoryMock . Object ) ;
240
+
241
+ // Assert
242
+ _applySystemConfigurationMock . Verify ( c => c . ConfigureColumnOptions ( systemConfigSection , columnOptions ) ,
243
+ Times . Once ) ;
244
+ }
245
+
246
+ [ Fact ]
247
+ public void MSSqlServerCallsSinkFactoryWithColumnOptionsFromSystemConfig ( )
248
+ {
249
+ // Arrange
250
+ var configColumnOptions = new ColumnOptions ( ) ;
251
+ _applySystemConfigurationMock . Setup ( c => c . GetSinkConfigurationSection ( It . IsAny < string > ( ) ) )
252
+ . Returns ( new MSSqlServerConfigurationSection ( ) ) ;
253
+ _applySystemConfigurationMock . Setup ( c => c . ConfigureColumnOptions ( It . IsAny < MSSqlServerConfigurationSection > ( ) , It . IsAny < ColumnOptions > ( ) ) )
254
+ . Returns ( configColumnOptions ) ;
255
+ var sinkFactoryMock = new Mock < IMSSqlServerSinkFactory > ( ) ;
256
+
257
+ // Act
258
+ _loggerConfiguration . WriteTo . MSSqlServerInternal (
259
+ connectionString : "TestConnectionString" ,
260
+ tableName : "TestTableName" ,
261
+ columnOptions : new ColumnOptions ( ) ,
262
+ applySystemConfiguration : _applySystemConfigurationMock . Object ,
263
+ applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
264
+ sinkFactory : sinkFactoryMock . Object ) ;
265
+
266
+ // Assert
267
+ sinkFactoryMock . Verify ( f => f . Create ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < int > ( ) , It . IsAny < TimeSpan > ( ) ,
268
+ It . IsAny < IFormatProvider > ( ) , It . IsAny < bool > ( ) , configColumnOptions , It . IsAny < string > ( ) , It . IsAny < ITextFormatter > ( ) ) , Times . Once ) ;
269
+ }
270
+
271
+ [ Fact ]
272
+ public void MSSqlServerDoesNotCallApplySystemConfigurationGetColumnOptionsWhenNotUsingSystemConfig ( )
273
+ {
274
+ // Arrange
275
+ var sinkFactoryMock = new Mock < IMSSqlServerSinkFactory > ( ) ;
276
+
277
+ // Act
278
+ _loggerConfiguration . WriteTo . MSSqlServerInternal (
279
+ connectionString : "TestConnectionString" ,
280
+ tableName : "TestTableName" ,
281
+ applySystemConfiguration : _applySystemConfigurationMock . Object ,
282
+ applyMicrosoftExtensionsConfiguration : _applyMicrosoftExtensionsConfigurationMock . Object ,
283
+ sinkFactory : sinkFactoryMock . Object ) ;
284
+
285
+ // Assert
286
+ _applySystemConfigurationMock . Verify ( c => c . ConfigureColumnOptions ( It . IsAny < MSSqlServerConfigurationSection > ( ) , It . IsAny < ColumnOptions > ( ) ) ,
287
+ Times . Never ) ;
288
+ }
289
+
163
290
[ Fact ]
164
291
public void MSSqlServerCallsSinkFactoryWithSuppliedParameters ( )
165
292
{
0 commit comments