Skip to content

Commit

Permalink
Test(core): CNX-8745 - Added unit test for logger (#3147)
Browse files Browse the repository at this point in the history
* Logger unit tests

* fixed test to reset console stream on teardown

* cleaned up tests a little
  • Loading branch information
JR-Morgan authored Jan 18, 2024
1 parent c47f870 commit af09435
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 11 deletions.
6 changes: 0 additions & 6 deletions Core/Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleApp", "Examples\ExampleApp.csproj", "{73BBCC3C-E649-4977-A34A-7CF424D638E6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDBTransport", "Transports\MongoDBTransport\MongoDBTransport.csproj", "{4450B0E9-D663-4F13-AF96-6A41B1A3B5C9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Transports", "Transports", "{424ECC59-E693-4678-AFAC-C00F2D8AE735}"
Expand All @@ -34,10 +32,6 @@ Global
{CB85D2B2-9837-40B8-91DB-3C0636B1174D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB85D2B2-9837-40B8-91DB-3C0636B1174D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB85D2B2-9837-40B8-91DB-3C0636B1174D}.Release|Any CPU.Build.0 = Release|Any CPU
{73BBCC3C-E649-4977-A34A-7CF424D638E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73BBCC3C-E649-4977-A34A-7CF424D638E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73BBCC3C-E649-4977-A34A-7CF424D638E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73BBCC3C-E649-4977-A34A-7CF424D638E6}.Release|Any CPU.Build.0 = Release|Any CPU
{4450B0E9-D663-4F13-AF96-6A41B1A3B5C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4450B0E9-D663-4F13-AF96-6A41B1A3B5C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4450B0E9-D663-4F13-AF96-6A41B1A3B5C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
5 changes: 4 additions & 1 deletion Core/Tests/Speckle.Core.Tests.Unit/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ namespace Speckle.Core.Tests.Unit;
[SetUpFixture]
public class SetUp
{
public static SpeckleLogConfiguration TestLogConfig { get; } =
new(logToFile: false, logToSeq: false, logToSentry: false);

[OneTimeSetUp]
public void BeforeAll()
{
SpeckleLog.Initialize("Core", "Testing", new SpeckleLogConfiguration(logToFile: false, logToSeq: false));
SpeckleLog.Initialize("Core", "Testing", TestLogConfig);
SpeckleLog.Logger.Information("Initialized logger for testing");
}
}
Expand Down
131 changes: 131 additions & 0 deletions Core/Tests/Speckle.Core.Tests.Unit/Logging/SpeckleLogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using NUnit.Framework;
using Serilog.Context;
using Serilog.Core;
using Serilog.Events;
using Speckle.Core.Logging;

namespace Speckle.Core.Tests.Unit.Logging;

[TestOf(typeof(SpeckleLog))]
public class SpeckleLogTests : IDisposable
{
private StringWriter _stdOut;

[SetUp]
public virtual void Setup()
{
_stdOut = new StringWriter();
Console.SetOut(_stdOut);
}

[TearDown]
public void TearDown()
{
_stdOut?.Dispose();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
}

[Test]
[TestCase(LogEventLevel.Fatal, true)]
[TestCase(LogEventLevel.Error, true)]
[TestCase(LogEventLevel.Warning, true)]
[TestCase(LogEventLevel.Information, true)]
[TestCase(LogEventLevel.Debug, true)]
[TestCase(LogEventLevel.Verbose, false)]
public void LoggerWrites_WithLogEventLevel(LogEventLevel logLevel, bool expectLog)
{
const string TEMPLATE = "My log message";

SpeckleLog.Logger.Write(logLevel, TEMPLATE);

string result = _stdOut.ToString();

if (expectLog)
{
Assert.That(result, Contains.Substring(TEMPLATE));
}
else
{
Assert.That(result, Is.Empty);
}
}

[Test]
public void LoggerWrites_PositionalProperties()
{
const string PROP_NAME = "myProp";
const string TEMPLATE = $"My log message with positional prop {{{PROP_NAME}}}";
const string TARGET_VALUE = "my amazing value";
SpeckleLog.Logger.Warning(TEMPLATE, TARGET_VALUE);

string result = _stdOut.ToString();
Assert.That(result, Does.Contain(TARGET_VALUE));
Assert.That(result, Does.Not.Contain(PROP_NAME));
}

[Test]
public void LoggerWrites_ContextProperties()
{
const string PROP_NAME = "myProp";
const string TEMPLATE = $"My log message with context prop {{{PROP_NAME}}}";
const string TARGET_VALUE = "my amazing value";

SpeckleLog.Logger.ForContext(PROP_NAME, TARGET_VALUE).Warning(TEMPLATE);

string result = _stdOut.ToString();
Assert.That(result, Does.Contain(TARGET_VALUE));
Assert.That(result, Does.Not.Contain(PROP_NAME));
}

[Test]
public void LoggerWrites_ScopedProperties()
{
const string PROP_NAME = "myProp";
const string TEMPLATE = $"My log message with scoped prop {{{PROP_NAME}}}";
const string TARGET_VALUE = "my amazing value";

using var d1 = LogContext.PushProperty(PROP_NAME, TARGET_VALUE);

SpeckleLog.Logger.Warning(TEMPLATE);

string result = _stdOut.ToString();
Assert.That(result, Does.Contain(TARGET_VALUE));
Assert.That(result, Does.Not.Contain(PROP_NAME));
}

[Test]
[TestCase(true)]
[TestCase(false)]
public void CreateConfiguredLogger_WritesToConsole_ToConsole(bool shouldWrite)
{
const string TEST_MESSAGE = "This is my test message";

SpeckleLogConfiguration config =
new(logToConsole: shouldWrite, logToSeq: false, logToSentry: false, logToFile: false);
using var logger = SpeckleLog.CreateConfiguredLogger("My Test Host App!!", null, config);

logger.Fatal(TEST_MESSAGE);

string result = _stdOut.ToString();
if (shouldWrite)
{
Assert.That(result, Does.Contain(TEST_MESSAGE));
}
else
{
Assert.That(result, Is.Empty);
}
}

public void Dispose()
{
_stdOut?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,4 @@
<ProjectReference Include="..\..\Core\Core.csproj" />
<ProjectReference Include="..\..\Transports\DiskTransport\DiskTransport.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Logging\" />
</ItemGroup>
</Project>

0 comments on commit af09435

Please sign in to comment.