Skip to content

Commit 449ad5b

Browse files
author
Colin Young
committed
Integration test to verify database is created with custom column names
1 parent cdd7999 commit 449ad5b

File tree

4 files changed

+919
-332
lines changed

4 files changed

+919
-332
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using Dapper;
6+
using Xunit;
7+
using FluentAssertions;
8+
9+
namespace Serilog.Sinks.MSSqlServer.Tests
10+
{
11+
public class CustomStandardColumnNames : IClassFixture<DatabaseFixture>
12+
{
13+
public class InfoSchema
14+
{
15+
public string ColumnName { get; set; }
16+
}
17+
18+
[Fact]
19+
public void TableCreatedWithCustomNames()
20+
{
21+
// arrange
22+
var options = new ColumnOptions();
23+
24+
options.Store.ToList().ForEach(c =>
25+
{
26+
options.Store.Remove(c.Key);
27+
options.Store.Add(c.Key, $"Custom{c.Key.ToString()}");
28+
});
29+
30+
// act
31+
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options);
32+
33+
// assert
34+
using (var conn = new SqlConnection(DatabaseFixture.MasterConnectionString))
35+
{
36+
conn.Execute($"use {DatabaseFixture.Database}");
37+
var logEvents = conn.Query<InfoSchema>($@"SELECT COLUMN_NAME AS ColumnName FROM {DatabaseFixture.Database}.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{DatabaseFixture.LogTableName}'");
38+
var infoSchemata = logEvents as InfoSchema[] ?? logEvents.ToArray();
39+
40+
foreach (var column in options.Store.Values)
41+
{
42+
Console.WriteLine($"Testing {column}");
43+
infoSchemata.Should().Contain(columns => columns.ColumnName == column);
44+
}
45+
}
46+
}
47+
48+
[Fact]
49+
public void TableCreatedWithDefaultNames()
50+
{
51+
52+
}
53+
}
54+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Linq;
4+
using Dapper;
5+
6+
namespace Serilog.Sinks.MSSqlServer.Tests
7+
{
8+
public class DatabaseFixture : IDisposable
9+
{
10+
public static string Database => "LogTest";
11+
public static string LogTableName => "LogEvents";
12+
13+
private const string CreateLogEventsDatabase = @"
14+
EXEC ('CREATE DATABASE [{0}] ON PRIMARY
15+
(NAME = [{0}],
16+
FILENAME =''{1}'',
17+
SIZE = 25MB,
18+
MAXSIZE = 50MB,
19+
FILEGROWTH = 5MB )')";
20+
21+
private static readonly string DatabaseFileNameQuery = $@"SELECT CONVERT(VARCHAR(255), SERVERPROPERTY('instancedefaultdatapath')) + '{Database}.mdf' AS Name";
22+
private static readonly string DropLogEventsDatabase = $@"
23+
ALTER DATABASE [{Database}]
24+
SET SINGLE_USER
25+
WITH ROLLBACK IMMEDIATE
26+
DROP DATABASE [{Database}]
27+
";
28+
29+
public static string MasterConnectionString => @"Data Source=(LocalDb)\v11.0;Initial Catalog=Master;Integrated Security=True";
30+
public static string LogEventsConnectionString => $@"Data Source=(LocalDb)\v11.0;Initial Catalog={Database};Integrated Security=True";
31+
32+
public class FileName
33+
{
34+
public string Name { get; set; }
35+
}
36+
37+
public DatabaseFixture()
38+
{
39+
CreateDatabase();
40+
}
41+
42+
public void Dispose()
43+
{
44+
DeleteDatabase();
45+
}
46+
47+
private static void DeleteDatabase()
48+
{
49+
using (var conn = new SqlConnection(MasterConnectionString))
50+
{
51+
conn.Open();
52+
var databases = conn.Query("select name from sys.databases");
53+
54+
//if (databases.Any(d => d.name == Database)) conn.Execute(DropLogEventsDatabase);
55+
}
56+
}
57+
58+
private static void CreateDatabase()
59+
{
60+
DeleteDatabase();
61+
62+
using (var conn = new SqlConnection(MasterConnectionString))
63+
{
64+
conn.Open();
65+
// ReSharper disable once PossibleNullReferenceException
66+
var filename = conn.Query<FileName>(DatabaseFileNameQuery).FirstOrDefault().Name;
67+
var createDatabase = string.Format(CreateLogEventsDatabase, Database, filename);
68+
69+
conn.Execute(createDatabase);
70+
}
71+
}
72+
}
73+
}

test/Serilog.Sinks.MSSqlServer.Tests/project.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
"dependencies": {
55
"Serilog.Sinks.MSSqlServer": { "target": "project" },
6-
"xunit": "2.1.0",
7-
"dotnet-test-xunit": "1.0.0-rc2-build10025"
6+
"xunit": "2.2.0-beta2-build3300",
7+
"dotnet-test-xunit": "2.2.0-preview2-build1029",
8+
"FluentAssertions": "4.13.0",
9+
"Dapper.StrongName": "1.50.2"
810
},
911

1012
"buildOptions": {

0 commit comments

Comments
 (0)