add tests
This commit is contained in:
101
ChatBot.Tests/Services/HealthChecks/OllamaHealthCheckTests.cs
Normal file
101
ChatBot.Tests/Services/HealthChecks/OllamaHealthCheckTests.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.Linq;
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Services.HealthChecks;
|
||||
using ChatBot.Services.Interfaces;
|
||||
using ChatBot.Tests.TestUtilities;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using OllamaSharp.Models.Chat;
|
||||
|
||||
namespace ChatBot.Tests.Services.HealthChecks;
|
||||
|
||||
public class OllamaHealthCheckTests : UnitTestBase
|
||||
{
|
||||
private readonly Mock<ILogger<OllamaHealthCheck>> _loggerMock;
|
||||
private readonly Mock<IOllamaClient> _ollamaClientMock;
|
||||
private readonly Mock<IOptions<OllamaSettings>> _optionsMock;
|
||||
private readonly OllamaHealthCheck _healthCheck;
|
||||
|
||||
public OllamaHealthCheckTests()
|
||||
{
|
||||
_loggerMock = TestDataBuilder.Mocks.CreateLoggerMock<OllamaHealthCheck>();
|
||||
_ollamaClientMock = TestDataBuilder.Mocks.CreateOllamaClientMock();
|
||||
|
||||
var ollamaSettings = TestDataBuilder.Configurations.CreateOllamaSettings();
|
||||
_optionsMock = TestDataBuilder.Mocks.CreateOptionsMock(ollamaSettings);
|
||||
|
||||
_healthCheck = new OllamaHealthCheck(_ollamaClientMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnHealthy_WhenOllamaResponds()
|
||||
{
|
||||
// Arrange
|
||||
_ollamaClientMock
|
||||
.Setup(x => x.ListLocalModelsAsync())
|
||||
.ReturnsAsync(
|
||||
new List<OllamaSharp.Models.Model>
|
||||
{
|
||||
new OllamaSharp.Models.Model { Name = "llama3.2" },
|
||||
}
|
||||
);
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Healthy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenOllamaThrowsException()
|
||||
{
|
||||
// Arrange
|
||||
_ollamaClientMock
|
||||
.Setup(x => x.ListLocalModelsAsync())
|
||||
.ThrowsAsync(new Exception("Ollama unavailable"));
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Unhealthy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenOllamaReturnsEmptyResponse()
|
||||
{
|
||||
// Arrange
|
||||
_ollamaClientMock
|
||||
.Setup(x => x.ListLocalModelsAsync())
|
||||
.ReturnsAsync(new List<OllamaSharp.Models.Model>());
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Unhealthy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenOllamaReturnsNullMessage()
|
||||
{
|
||||
// Arrange
|
||||
_ollamaClientMock
|
||||
.Setup(x => x.ListLocalModelsAsync())
|
||||
.ReturnsAsync((IEnumerable<OllamaSharp.Models.Model>)null!);
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Unhealthy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Services.HealthChecks;
|
||||
using ChatBot.Services.Interfaces;
|
||||
using ChatBot.Tests.TestUtilities;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace ChatBot.Tests.Services.HealthChecks;
|
||||
|
||||
public class TelegramBotHealthCheckTests : UnitTestBase
|
||||
{
|
||||
private readonly Mock<ILogger<TelegramBotHealthCheck>> _loggerMock;
|
||||
private readonly Mock<ITelegramBotClientWrapper> _telegramBotClientWrapperMock;
|
||||
private readonly TelegramBotHealthCheck _healthCheck;
|
||||
|
||||
public TelegramBotHealthCheckTests()
|
||||
{
|
||||
_loggerMock = TestDataBuilder.Mocks.CreateLoggerMock<TelegramBotHealthCheck>();
|
||||
_telegramBotClientWrapperMock = new Mock<ITelegramBotClientWrapper>();
|
||||
|
||||
_healthCheck = new TelegramBotHealthCheck(
|
||||
_telegramBotClientWrapperMock.Object,
|
||||
_loggerMock.Object
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnHealthy_WhenTelegramResponds()
|
||||
{
|
||||
// Arrange
|
||||
var botInfo = TestDataBuilder.Mocks.CreateTelegramBot();
|
||||
_telegramBotClientWrapperMock
|
||||
.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(botInfo);
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Healthy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenTelegramThrowsException()
|
||||
{
|
||||
// Arrange
|
||||
_telegramBotClientWrapperMock
|
||||
.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new Exception("Telegram unavailable"));
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Unhealthy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenTelegramReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
_telegramBotClientWrapperMock
|
||||
.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((User)null!);
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Unhealthy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenTelegramReturnsInvalidBot()
|
||||
{
|
||||
// Arrange
|
||||
var invalidBot = new User
|
||||
{
|
||||
Id = 0, // Invalid bot ID
|
||||
Username = null,
|
||||
};
|
||||
|
||||
_telegramBotClientWrapperMock
|
||||
.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(invalidBot);
|
||||
|
||||
// Act
|
||||
var context = new HealthCheckContext();
|
||||
var result = await _healthCheck.CheckHealthAsync(context);
|
||||
|
||||
// Assert
|
||||
result.Status.Should().Be(HealthStatus.Unhealthy);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user