Radarr/src/NzbDrone.Core.Test/InstrumentationTests/DatabaseTargetFixture.cs

126 lines
3.4 KiB
C#
Raw Normal View History

using System;
2016-06-10 22:53:55 +00:00
using System.Threading;
2013-02-23 20:09:44 +00:00
using FluentAssertions;
using NLog;
using NUnit.Framework;
2013-08-31 01:42:30 +00:00
using NzbDrone.Common.Instrumentation;
2013-11-28 07:08:40 +00:00
using NzbDrone.Core.Datastore.Migration.Framework;
2013-02-23 20:09:44 +00:00
using NzbDrone.Core.Instrumentation;
2016-06-10 22:53:55 +00:00
using NzbDrone.Core.MediaFiles;
2013-02-23 20:09:44 +00:00
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.InstrumentationTests
{
[TestFixture]
2013-03-24 04:16:00 +00:00
public class DatabaseTargetFixture : DbTest<DatabaseTarget, Log>
2013-02-23 20:09:44 +00:00
{
private static string _uniqueMessage;
Logger _logger;
2016-12-09 06:54:15 +00:00
protected override MigrationType MigrationType => MigrationType.Log;
2013-03-27 04:03:02 +00:00
2013-02-23 20:09:44 +00:00
[SetUp]
public void Setup()
{
Mocker.Resolve<ILogRepository, LogRepository>();
Mocker.Resolve<DatabaseTarget>().Register();
2013-04-12 23:28:05 +00:00
LogManager.ReconfigExistingLoggers();
2014-12-17 07:12:26 +00:00
_logger = NzbDroneLogger.GetLogger(this);
2013-02-23 20:09:44 +00:00
2014-12-17 07:12:26 +00:00
_uniqueMessage = "Unique message: " + Guid.NewGuid();
2013-02-23 20:09:44 +00:00
}
[Test]
public void write_log()
{
_logger.Info(_uniqueMessage);
2019-09-04 20:38:02 +00:00
Thread.Sleep(1000);
2016-06-10 22:53:55 +00:00
2013-02-23 20:09:44 +00:00
StoredModel.Message.Should().Be(_uniqueMessage);
VerifyLog(StoredModel, LogLevel.Info);
}
[Test]
public void write_long_log()
{
var message = string.Empty;
2013-02-23 20:09:44 +00:00
for (int i = 0; i < 100; i++)
{
message += Guid.NewGuid();
}
_logger.Info(message);
2019-09-04 20:38:02 +00:00
Thread.Sleep(1000);
2016-06-10 22:53:55 +00:00
2013-02-23 20:09:44 +00:00
StoredModel.Message.Should().HaveLength(message.Length);
StoredModel.Message.Should().Be(message);
VerifyLog(StoredModel, LogLevel.Info);
}
[Test]
public void write_log_exception()
{
var ex = new InvalidOperationException("Fake Exception");
2016-02-11 21:13:42 +00:00
_logger.Error(ex, _uniqueMessage);
2013-02-23 20:09:44 +00:00
2019-09-04 20:38:02 +00:00
Thread.Sleep(1000);
2013-02-23 20:09:44 +00:00
VerifyLog(StoredModel, LogLevel.Error);
StoredModel.Message.Should().Be(_uniqueMessage + ": " + ex.Message);
StoredModel.ExceptionType.Should().Be(ex.GetType().ToString());
StoredModel.Exception.Should().Be(ex.ToString());
ExceptionVerification.ExpectedErrors(1);
}
[Test]
public void exception_log_with_no_message_should_use_exceptions_message()
{
var ex = new InvalidOperationException("Fake Exception");
_uniqueMessage = string.Empty;
2013-02-23 20:09:44 +00:00
2016-02-11 21:13:42 +00:00
_logger.Error(ex, _uniqueMessage);
2013-02-23 20:09:44 +00:00
2019-09-04 20:38:02 +00:00
Thread.Sleep(1000);
2016-06-10 22:53:55 +00:00
2013-02-23 20:09:44 +00:00
StoredModel.Message.Should().Be(ex.Message);
VerifyLog(StoredModel, LogLevel.Error);
ExceptionVerification.ExpectedErrors(1);
}
[Test]
public void null_string_as_arg_should_not_fail()
{
var epFile = new MovieFile();
2014-07-23 23:43:54 +00:00
_logger.Debug("File {0} no longer exists on disk. removing from database.", epFile.RelativePath);
2013-02-23 20:09:44 +00:00
2019-09-04 20:38:02 +00:00
Thread.Sleep(1000);
2016-06-10 22:53:55 +00:00
2014-07-23 23:43:54 +00:00
epFile.RelativePath.Should().BeNull();
2013-02-23 20:09:44 +00:00
}
[TearDown]
public void Teardown()
{
Mocker.Resolve<DatabaseTarget>().UnRegister();
}
2013-02-23 20:09:44 +00:00
private void VerifyLog(Log logItem, LogLevel level)
{
logItem.Time.Should().BeWithin(TimeSpan.FromSeconds(2));
2013-08-31 02:13:13 +00:00
logItem.Logger.Should().Be(this.GetType().Name);
2013-02-23 20:09:44 +00:00
logItem.Level.Should().Be(level.Name);
_logger.Name.Should().EndWith(logItem.Logger);
}
}
}