Skip to content

Commit 0fb2dff

Browse files
authored
Merge pull request #144 from MV10/issue_142
fixes #142 now all tests drop tables upon completion
2 parents bcc396e + 11da563 commit 0fb2dff

File tree

7 files changed

+114
-82
lines changed

7 files changed

+114
-82
lines changed

test/Serilog.Sinks.MSSqlServer.Tests/Configuration/Microsoft.Extensions.Configuration/ConfigurationExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public void ConnectionStringByName()
4343
.CreateLogger();
4444

4545
// should not throw
46+
47+
Log.CloseAndFlush();
48+
DatabaseFixture.DropTable();
4649
}
4750

4851
[Fact]
@@ -51,21 +54,21 @@ public void ColumnOptionsFromConfigSection()
5154
var standardNames = new List<string> { "CustomMessage", "CustomMessageTemplate", "CustomLevel", "CustomTimeStamp", "CustomException", "CustomProperties" };
5255

5356
var configSection = TestConfiguration().GetSection(ColumnOptionsSection);
54-
var logTableName = $"{DatabaseFixture.LogTableName}Custom";
5557

5658
var loggerConfiguration = new LoggerConfiguration();
5759
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
5860
connectionString: DatabaseFixture.LogEventsConnectionString,
59-
tableName: logTableName,
61+
tableName: DatabaseFixture.LogTableName,
6062
autoCreateSqlTable: true,
6163
columnOptionsSection: configSection)
6264
.CreateLogger();
65+
Log.CloseAndFlush();
6366

6467
// from test TableCreatedWithCustomNames in CustomStandardColumnNames class
6568
using(var conn = new SqlConnection(DatabaseFixture.MasterConnectionString))
6669
{
6770
conn.Execute($"use {DatabaseFixture.Database}");
68-
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'");
71+
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{DatabaseFixture.LogTableName}'");
6972
var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray();
7073

7174
foreach(var column in standardNames)
@@ -75,6 +78,8 @@ public void ColumnOptionsFromConfigSection()
7578

7679
infoSchema.Should().Contain(columns => columns.ColumnName == "Id");
7780
}
81+
82+
DatabaseFixture.DropTable();
7883
}
7984
}
8085
}

test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNames.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ public void CustomIdColumn()
2323
options.Id.ColumnName = customIdName;
2424

2525
// act
26-
var logTableName = $"{DatabaseFixture.LogTableName}CustomId";
27-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
26+
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
2827

2928
// assert
3029
using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString))
3130
{
3231
conn.Execute($"use {DatabaseFixture.Database}");
33-
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'");
32+
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{DatabaseFixture.LogTableName}'");
3433
var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray();
3534

3635
infoSchema.Should().Contain(columns => columns.ColumnName == customIdName);
@@ -39,9 +38,11 @@ public void CustomIdColumn()
3938
// verify Id column has identity property
4039
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
4140
{
42-
var isIdentity = conn.Query<IdentityQuery>($"SELECT COLUMNPROPERTY(object_id('{logTableName}'), '{customIdName}', 'IsIdentity') AS IsIdentity");
41+
var isIdentity = conn.Query<IdentityQuery>($"SELECT COLUMNPROPERTY(object_id('{DatabaseFixture.LogTableName}'), '{customIdName}', 'IsIdentity') AS IsIdentity");
4342
isIdentity.Should().Contain(i => i.IsIdentity == 1);
4443
}
44+
45+
DatabaseFixture.DropTable();
4546
}
4647

4748
internal class IdentityQuery
@@ -56,15 +57,14 @@ public void DefaultIdColumn()
5657
var options = new ColumnOptions();
5758

5859
// act
59-
var logTableName = $"{DatabaseFixture.LogTableName}DefaultId";
60-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
60+
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
6161

6262
// assert
6363
var idColumnName = "Id";
6464
using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString))
6565
{
6666
conn.Execute($"use {DatabaseFixture.Database}");
67-
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'");
67+
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{DatabaseFixture.LogTableName}'");
6868
var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray();
6969

7070
infoSchema.Should().Contain(columns => columns.ColumnName == idColumnName);
@@ -73,9 +73,11 @@ public void DefaultIdColumn()
7373
// verify Id column has identity property
7474
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
7575
{
76-
var isIdentity = conn.Query<IdentityQuery>($"SELECT COLUMNPROPERTY(object_id('{logTableName}'), '{idColumnName}', 'IsIdentity') AS IsIdentity");
76+
var isIdentity = conn.Query<IdentityQuery>($"SELECT COLUMNPROPERTY(object_id('{DatabaseFixture.LogTableName}'), '{idColumnName}', 'IsIdentity') AS IsIdentity");
7777
isIdentity.Should().Contain(i => i.IsIdentity == 1);
7878
}
79+
80+
DatabaseFixture.DropTable();
7981
}
8082

8183
[Fact]
@@ -93,14 +95,13 @@ public void TableCreatedWithCustomNames()
9395
options.Properties.ColumnName = "CustomProperties";
9496

9597
// act
96-
var logTableName = $"{DatabaseFixture.LogTableName}Custom";
97-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
98+
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
9899

99100
// assert
100101
using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString))
101102
{
102103
conn.Execute($"use {DatabaseFixture.Database}");
103-
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'");
104+
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{DatabaseFixture.LogTableName}'");
104105
var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray();
105106

106107
foreach (var column in standardNames)
@@ -110,6 +111,8 @@ public void TableCreatedWithCustomNames()
110111

111112
infoSchema.Should().Contain(columns => columns.ColumnName == "Id");
112113
}
114+
115+
DatabaseFixture.DropTable();
113116
}
114117

115118
[Fact]
@@ -120,21 +123,22 @@ public void TableCreatedWithDefaultNames()
120123
var standardNames = new List<string> { "Message", "MessageTemplate", "Level", "TimeStamp", "Exception", "Properties" };
121124

122125
// act
123-
var logTableName = $"{DatabaseFixture.LogTableName}DefaultStandard";
124-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, logTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
126+
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
125127

126128
// assert
127129
using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString))
128130
{
129131
conn.Execute($"use {DatabaseFixture.Database}");
130-
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{logTableName}'");
132+
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{DatabaseFixture.LogTableName}'");
131133
var infoSchema = logEvents as InfoSchema[] ?? logEvents.ToArray();
132134

133135
foreach (var column in standardNames)
134136
{
135137
infoSchema.Should().Contain(columns => columns.ColumnName == column);
136138
}
137139
}
140+
141+
DatabaseFixture.DropTable();
138142
}
139143

140144
[Fact]
@@ -151,11 +155,10 @@ public void WriteEventToCustomStandardColumns()
151155
options.Properties.ColumnName = "CustomProperties";
152156
options.Id.ColumnName = "CustomId";
153157

154-
var logTableName = $"{DatabaseFixture.LogTableName}CustomEvent";
155158
var loggerConfiguration = new LoggerConfiguration();
156159
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
157160
connectionString: DatabaseFixture.LogEventsConnectionString,
158-
tableName: logTableName,
161+
tableName: DatabaseFixture.LogTableName,
159162
autoCreateSqlTable: true,
160163
columnOptions: options)
161164
.CreateLogger();
@@ -171,10 +174,12 @@ public void WriteEventToCustomStandardColumns()
171174
// assert
172175
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
173176
{
174-
var logEvents = conn.Query<CustomStandardLogColumns>($"SELECT * FROM {logTableName}");
177+
var logEvents = conn.Query<CustomStandardLogColumns>($"SELECT * FROM {DatabaseFixture.LogTableName}");
175178

176179
logEvents.Should().Contain(e => e.CustomMessage.Contains(loggingInformationMessage));
177180
}
181+
182+
DatabaseFixture.DropTable();
178183
}
179184

180185
[Fact]
@@ -207,6 +212,8 @@ public void WriteEventToDefaultStandardColumns()
207212

208213
logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage));
209214
}
215+
216+
DatabaseFixture.DropTable();
210217
}
211218

212219
[Fact]
@@ -223,11 +230,10 @@ public void AuditEventToCustomStandardColumns()
223230
options.Properties.ColumnName = "CustomProperties";
224231
options.Id.ColumnName = "CustomId";
225232

226-
var logTableName = $"{DatabaseFixture.LogTableName}AuditCustomEvent";
227233
var loggerConfiguration = new LoggerConfiguration();
228234
Log.Logger = loggerConfiguration.AuditTo.MSSqlServer(
229235
connectionString: DatabaseFixture.LogEventsConnectionString,
230-
tableName: logTableName,
236+
tableName: DatabaseFixture.LogTableName,
231237
autoCreateSqlTable: true,
232238
columnOptions: options)
233239
.CreateLogger();
@@ -243,10 +249,12 @@ public void AuditEventToCustomStandardColumns()
243249
// assert
244250
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
245251
{
246-
var logEvents = conn.Query<CustomStandardLogColumns>($"SELECT * FROM {logTableName}");
252+
var logEvents = conn.Query<CustomStandardLogColumns>($"SELECT * FROM {DatabaseFixture.LogTableName}");
247253

248254
logEvents.Should().Contain(e => e.CustomMessage.Contains(loggingInformationMessage));
249255
}
256+
257+
DatabaseFixture.DropTable();
250258
}
251259

252260
[Fact]
@@ -277,6 +285,8 @@ public void AuditEventToDefaultStandardColumns()
277285

278286
logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage));
279287
}
288+
289+
DatabaseFixture.DropTable();
280290
}
281291
}
282292
}

test/Serilog.Sinks.MSSqlServer.Tests/DatabaseFixture.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,24 @@ public DatabaseFixture()
3939
{
4040
CreateDatabase();
4141
}
42-
42+
4343
public void Dispose()
4444
{
4545
DeleteDatabase();
4646
}
4747

48+
public static void DropTable(string tableName = null)
49+
{
50+
try
51+
{
52+
using (var conn = new SqlConnection(LogEventsConnectionString))
53+
{
54+
conn.Execute($"DROP TABLE {(string.IsNullOrEmpty(tableName) ? LogTableName : tableName)};");
55+
}
56+
}
57+
catch { }
58+
}
59+
4860
private static void DeleteDatabase()
4961
{
5062
using (var conn = new SqlConnection(MasterConnectionString))

test/Serilog.Sinks.MSSqlServer.Tests/LevelAsEnum.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ public class LevelAsEnum
1616
public void CanStoreLevelAsEnum()
1717
{
1818
// arrange
19-
const string tableName = "LogEventsLevelAsEnum";
2019
var loggerConfiguration = new LoggerConfiguration();
2120
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
2221
connectionString: DatabaseFixture.LogEventsConnectionString,
23-
tableName: tableName,
22+
tableName: DatabaseFixture.LogTableName,
2423
autoCreateSqlTable: true,
2524
batchPostingLimit: 1,
2625
period: TimeSpan.FromSeconds(10),
@@ -38,21 +37,22 @@ public void CanStoreLevelAsEnum()
3837
// assert
3938
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
4039
{
41-
var logEvents = conn.Query<EnumLevelStandardLogColumns>($"SELECT Message, Level FROM {tableName}");
40+
var logEvents = conn.Query<EnumLevelStandardLogColumns>($"SELECT Message, Level FROM {DatabaseFixture.LogTableName}");
4241

4342
logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage) && e.Level == 2);
4443
}
44+
45+
DatabaseFixture.DropTable();
4546
}
4647

4748
[Fact]
4849
public void CanStoreLevelAsString()
4950
{
5051
// arrange
51-
const string tableName = "LogEventsLevelAsString";
5252
var loggerConfiguration = new LoggerConfiguration();
5353
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
5454
connectionString: DatabaseFixture.LogEventsConnectionString,
55-
tableName: tableName,
55+
tableName: DatabaseFixture.LogTableName,
5656
autoCreateSqlTable: true,
5757
batchPostingLimit: 1,
5858
period: TimeSpan.FromSeconds(10),
@@ -70,21 +70,22 @@ public void CanStoreLevelAsString()
7070
// assert
7171
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
7272
{
73-
var logEvents = conn.Query<StringLevelStandardLogColumns>($"SELECT Message, Level FROM {tableName}");
73+
var logEvents = conn.Query<StringLevelStandardLogColumns>($"SELECT Message, Level FROM {DatabaseFixture.LogTableName}");
7474

7575
logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage) && e.Level == LogEventLevel.Information.ToString());
7676
}
77+
78+
DatabaseFixture.DropTable();
7779
}
7880

7981
[Fact]
8082
public void AuditCanStoreLevelAsEnum()
8183
{
8284
// arrange
83-
const string tableName = "AuditLogEventsLevelAsEnum";
8485
var loggerConfiguration = new LoggerConfiguration();
8586
Log.Logger = loggerConfiguration.AuditTo.MSSqlServer(
8687
connectionString: DatabaseFixture.LogEventsConnectionString,
87-
tableName: tableName,
88+
tableName: DatabaseFixture.LogTableName,
8889
autoCreateSqlTable: true,
8990
columnOptions: new ColumnOptions { Level = { StoreAsEnum = true } })
9091
.CreateLogger();
@@ -100,21 +101,22 @@ public void AuditCanStoreLevelAsEnum()
100101
// assert
101102
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
102103
{
103-
var logEvents = conn.Query<EnumLevelStandardLogColumns>($"SELECT Message, Level FROM {tableName}");
104+
var logEvents = conn.Query<EnumLevelStandardLogColumns>($"SELECT Message, Level FROM {DatabaseFixture.LogTableName}");
104105

105106
logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage) && e.Level == 2);
106107
}
108+
109+
DatabaseFixture.DropTable();
107110
}
108111

109112
[Fact]
110113
public void AuditCanStoreLevelAsString()
111114
{
112115
// arrange
113-
const string tableName = "LogEventsLevelAsString";
114116
var loggerConfiguration = new LoggerConfiguration();
115117
Log.Logger = loggerConfiguration.AuditTo.MSSqlServer(
116118
connectionString: DatabaseFixture.LogEventsConnectionString,
117-
tableName: tableName,
119+
tableName: DatabaseFixture.LogTableName,
118120
autoCreateSqlTable: true,
119121
columnOptions: new ColumnOptions { Level = { StoreAsEnum = false } })
120122
.CreateLogger();
@@ -130,10 +132,12 @@ public void AuditCanStoreLevelAsString()
130132
// assert
131133
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
132134
{
133-
var logEvents = conn.Query<StringLevelStandardLogColumns>($"SELECT Message, Level FROM {tableName}");
135+
var logEvents = conn.Query<StringLevelStandardLogColumns>($"SELECT Message, Level FROM {DatabaseFixture.LogTableName}");
134136

135137
logEvents.Should().Contain(e => e.Message.Contains(loggingInformationMessage) && e.Level == LogEventLevel.Information.ToString());
136138
}
139+
140+
DatabaseFixture.DropTable();
137141
}
138142
}
139143

test/Serilog.Sinks.MSSqlServer.Tests/TestMiscFeatures.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal class LogEventColumns
2020
public void LogEventExcludeAdditionalProperties()
2121
{
2222
// arrange
23-
const string tableName = "LogEventExcludeProps";
2423
var columnOptions = new ColumnOptions()
2524
{
2625
AdditionalDataColumns = new List<DataColumn>
@@ -36,7 +35,7 @@ public void LogEventExcludeAdditionalProperties()
3635
.WriteTo.MSSqlServer
3736
(
3837
connectionString: DatabaseFixture.LogEventsConnectionString,
39-
tableName: tableName,
38+
tableName: DatabaseFixture.LogTableName,
4039
columnOptions: columnOptions,
4140
autoCreateSqlTable: true,
4241
batchPostingLimit: 1,
@@ -55,12 +54,13 @@ public void LogEventExcludeAdditionalProperties()
5554
// assert
5655
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
5756
{
58-
var logEvents = conn.Query<LogEventColumns>($"SELECT LogEvent from {tableName}");
57+
var logEvents = conn.Query<LogEventColumns>($"SELECT LogEvent from {DatabaseFixture.LogTableName}");
5958

6059
logEvents.Should().Contain(e => e.LogEvent.Contains("AValue"));
6160
logEvents.Should().NotContain(e => e.LogEvent.Contains("BValue"));
6261
}
6362

63+
DatabaseFixture.DropTable();
6464
}
6565
}
6666
}

0 commit comments

Comments
 (0)