Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
75 changes: 75 additions & 0 deletions ExtractorUtils.Test/unit/CallbackUtilTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
using Cognite.Extractor.Utils;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace ExtractorUtils.Test.Unit
{
/// <summary>
/// Test double for CogniteDestination that doesn't require CogniteSdk.Client
/// </summary>
internal class TestCogniteDestination : CogniteDestination
{
public TestCogniteDestination()
: base(null!, new NullLogger<CogniteDestination>(), new CogniteConfig { Project = "test-project" })
{
}
}

public class CallbackUtilTest
{
private static CogniteDestination GetCogniteDestination()
{
return new TestCogniteDestination();
}

[Fact]
public async Task TryCallWithMissingConfiguration_ReturnsFalseAndLogsWarning()
{
// Arrange
var destination = GetCogniteDestination();
var mockLogger = new Mock<ILogger>();
var config = new FunctionCallConfig(); // No ExternalId or Id
var wrapper = new FunctionCallWrapper<string>(destination, config, mockLogger.Object);

// Act
var result = await wrapper.TryCall("test-argument", CancellationToken.None);

// Assert
Assert.False(result);
mockLogger.Verify(
x => x.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Missing function configuration")),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
}

[Theory]
[InlineData("test-function-id", null)]
[InlineData(null, 12345L)]
[InlineData("test-function-id", 12345L)]
public async Task TryCallWithValidConfiguration_ReturnsTrue(string? externalId, long? id)
{
// Arrange
var destination = GetCogniteDestination();
var config = new FunctionCallConfig { ExternalId = externalId, Id = id };
var wrapper = new FunctionCallWrapper<string>(destination, config, null);

// Act
// Note: The stub destination always returns true for function calls.
// If the underlying function's behavior changes, this test will need to be updated.
var result = await wrapper.TryCall("test-argument", CancellationToken.None);

// Assert
Assert.True(result);
}
}
}
108 changes: 107 additions & 1 deletion ExtractorUtils.Test/unit/LoggingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Xunit;
using Cognite.Extractor.Logging;
using Cognite.Extractor.Utils;
using Serilog;

namespace ExtractorUtils.Test.Unit
{
Expand Down Expand Up @@ -129,5 +128,112 @@ public static void TestLogLevel()
logger.LogTrace("This is a trace message with {SideEffect}", sideEffect);
Assert.Equal(4, sideEffect.logCount); // should not be evaluated
}

[Theory]
[InlineData("console")]
[InlineData("file")]
[InlineData("trace-listener")]
public void TestLogger_WithDifferentLogTypes(string logType)
{
var services = new ServiceCollection();

switch (logType)
{
case "console":
services.AddSingleton(new LoggerConfig
{
Console = new ConsoleConfig { Level = "information" }
});
break;
case "file":
services.AddSingleton(new LoggerConfig
{
File = new FileConfig { Level = "warning", Path = "test.log" }
});
break;
case "trace-listener":
services.AddSingleton(new LoggerConfig
{
TraceListener = new TraceListenerConfig { Level = "error" }
});
break;
}
services.AddLogger();

{
using var provider = services.BuildServiceProvider();
var logger = provider.GetRequiredService<ILogger<LoggingTest>>();

Assert.NotNull(logger);
} // provider is disposed here

// Cleanup test.log file if created
if (File.Exists("test.log"))
{
File.Delete("test.log");
}
}

[Theory]
[InlineData("console", "debug")]
[InlineData("console", "information")]
[InlineData("file", "warning")]
[InlineData("trace-listener", "error")]
public void TestLogger_WithTypeAndLevel(string configType, string level)
{
var services = new ServiceCollection();

// Configure logger based on the specified type and level
var loggerConfig = new LoggerConfig();
switch (configType)
{
case "console":
loggerConfig.Console = new ConsoleConfig { Level = level };
break;
case "file":
loggerConfig.File = new FileConfig { Level = level, Path = "test.log" };
break;
case "trace-listener":
loggerConfig.TraceListener = new TraceListenerConfig { Level = level };
break;
}

services.AddSingleton(loggerConfig);
services.AddLogger();

{
using var provider = services.BuildServiceProvider();
var logger = provider.GetRequiredService<ILogger<LoggingTest>>();

// Assert logger is configured
Assert.NotNull(logger);

// Verify the configuration was applied
var retrievedConfig = provider.GetRequiredService<LoggerConfig>();
Assert.NotNull(retrievedConfig);

switch (configType)
{
case "console":
Assert.NotNull(retrievedConfig.Console);
Assert.Equal(level, retrievedConfig.Console.Level);
break;
case "file":
Assert.NotNull(retrievedConfig.File);
Assert.Equal(level, retrievedConfig.File.Level);
break;
case "trace-listener":
Assert.NotNull(retrievedConfig.TraceListener);
Assert.Equal(level, retrievedConfig.TraceListener.Level);
break;
}
} // provider is disposed here

// Cleanup test.log file if created
if (File.Exists("test.log"))
{
File.Delete("test.log");
}
}
}
}
Loading