Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions tests/NLog.Database.Tests/DatabaseTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,232 @@ public void DbFactoryConnectionStringTest2(string csInConn, string csInDbTarget,
Assert.Equal(csExpected, cs);
}

[Fact]
public void DbConnectionFactoryNullTest()
{
Assert.Throws<ArgumentNullException>(() => new DatabaseTarget((Func<IDbConnection>)null));
}

[Fact]
public void DbConnectionFactoryReturnsNullTest()
{
// Clear log
MockDbConnection.ClearLog();
var exceptions = new List<Exception>();

// Arrange
var dt = new DatabaseTarget(() => null)
{
CommandText = "not important",
ConnectionString = "FooBar",
};
var logFactory = new LogFactory().Setup().LoadConfiguration(cfg => cfg.Configuration.AddRuleForAllLevels(dt)).LogFactory;

try
{
LogManager.ThrowExceptions = false;
dt.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
}
finally
{
LogManager.ThrowExceptions = true;
logFactory.Shutdown();
}

// Assert
Assert.Single(exceptions);
Assert.NotNull(exceptions[0]);
Assert.Equal("Creation of DbConnection failed", exceptions[0].Message);
}

[Fact]
public void DbConnectionFactoryThrowsTest()
{
// Clear log
MockDbConnection.ClearLog();
var exceptions = new List<Exception>();

// Arrange
var dt = new DatabaseTarget(() => throw new InvalidOperationException("Factory failure"))
{
CommandText = "not important",
ConnectionString = "FooBar",
};
var logFactory = new LogFactory().Setup().LoadConfiguration(cfg => cfg.Configuration.AddRuleForAllLevels(dt)).LogFactory;

try
{
LogManager.ThrowExceptions = false;
dt.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
}
finally
{
LogManager.ThrowExceptions = true;
logFactory.Shutdown();
}

// Assert
Assert.Single(exceptions);
Assert.NotNull(exceptions[0]);
Assert.Equal("Factory failure", exceptions[0].Message);
}

[Fact]
public void DbConnectionFactorySimpleWriteTest()
{
// Clear log
MockDbConnection.ClearLog();

// Arrange
var dt = new DatabaseTarget(() => new MockDbConnection())
{
CommandText = "INSERT INTO FooBar VALUES('${message}')",
ConnectionString = "FooBar",
};
new LogFactory().Setup().LoadConfiguration(cfg => cfg.Configuration.AddRuleForAllLevels(dt));

var exceptions = new List<Exception>();
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add));
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg2").WithContinuation(exceptions.Add));
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg3").WithContinuation(exceptions.Add));
foreach (var ex in exceptions)
{
Assert.Null(ex);
}

// Assert
string expectedLog = @"Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg1')
Close()
Dispose()
Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg2')
Close()
Dispose()
Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg3')
Close()
Dispose()
";

AssertLog(expectedLog);
}

[Fact]
public void DbConnectionFactoryBatchedWriteTest()
{
// Clear log
MockDbConnection.ClearLog();

// Arrange
var dt = new DatabaseTarget(() => new MockDbConnection())
{
CommandText = "INSERT INTO FooBar VALUES('${message}')",
ConnectionString = "FooBar",
};
new LogFactory().Setup().LoadConfiguration(cfg => cfg.Configuration.AddRuleForAllLevels(dt));

var exceptions = new List<Exception>();
var events = new[]
{
new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger", "msg2").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger", "msg3").WithContinuation(exceptions.Add),
};
dt.WriteAsyncLogEvents(events);
foreach (var ex in exceptions)
{
Assert.Null(ex);
}

// Assert
string expectedLog = @"Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg1')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg2')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg3')
Close()
Dispose()
";

AssertLog(expectedLog);
}

[Fact]
public void DbConnectionFactoryKeepConnectionTest()
{
// Clear log
MockDbConnection.ClearLog();

// Arrange
var dt = new DatabaseTarget(() => new MockDbConnection())
{
CommandText = "INSERT INTO FooBar VALUES('${message}')",
ConnectionString = "FooBar",
KeepConnection = true,
};
var logFactory = new LogFactory().Setup().LoadConfiguration(cfg => cfg.Configuration.AddRuleForAllLevels(dt)).LogFactory;

var exceptions = new List<Exception>();
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add));
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg2").WithContinuation(exceptions.Add));
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg3").WithContinuation(exceptions.Add));
foreach (var ex in exceptions)
{
Assert.Null(ex);
}

// Assert
string expectedLog = @"Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg1')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg2')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg3')
";

AssertLog(expectedLog);

MockDbConnection.ClearLog();
logFactory.Shutdown();
expectedLog = @"Close()
Dispose()
";

AssertLog(expectedLog);
}

[Fact]
public void DbConnectionFactoryWithParametersWriteTest()
{
// Clear log
MockDbConnection.ClearLog();

// Arrange
var dt = new DatabaseTarget(() => new MockDbConnection())
{
CommandText = "INSERT INTO FooBar VALUES(@msg)",
ConnectionString = "FooBar",
Parameters =
{
new DatabaseParameterInfo("@msg", "${message}", p => p.DbType = DbType.String),
},
};
new LogFactory().Setup().LoadConfiguration(cfg => cfg.Configuration.AddRuleForAllLevels(dt));

var exceptions = new List<Exception>();
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add));
foreach (var ex in exceptions)
{
Assert.Null(ex);
}

// Assert
var log = MockDbConnection.Log;
Assert.Contains("Open('FooBar').", log);
Assert.Contains("Parameter #0 Name=@msg", log);
Assert.Contains("Parameter #0 DbType=String", log);
Assert.Contains("Parameter #0 Value=\"msg1\"", log);
Assert.Contains("ExecuteNonQuery: INSERT INTO FooBar VALUES(@msg)", log);
}

private static void AssertLog(string expectedLog)
{
Assert.Equal(expectedLog.Replace("\r", ""), MockDbConnection.Log.Replace("\r", ""));
Expand Down