add more tests
Some checks failed
SonarQube / Build and analyze (push) Failing after 2m56s
Unit Tests / Run Tests (push) Failing after 2m28s

This commit is contained in:
Leonid Pershin
2025-10-20 07:02:12 +03:00
parent af9773e7d6
commit 1647fe19d3
12 changed files with 3714 additions and 21 deletions

View File

@@ -1,6 +1,7 @@
using ChatBot.Data;
using ChatBot.Services;
using ChatBot.Tests.TestUtilities;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -44,4 +45,214 @@ public class DatabaseInitializationServiceTests : UnitTestBase
// If we reach here, the method completed successfully
Assert.True(true);
}
[Fact]
public async Task StartAsync_ShouldLogCorrectInformation_WhenStopping()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act
await service.StopAsync(CancellationToken.None);
// Assert
loggerMock.Verify(
x =>
x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>(
(v, t) => v.ToString()!.Contains("Database initialization service stopped")
),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
),
Times.Once
);
}
[Fact]
public async Task StartAsync_ShouldHandleCancellationToken()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
var cts = new CancellationTokenSource();
cts.Cancel(); // Cancel immediately
// Setup service provider to throw when CreateScope is called
serviceProviderMock
.Setup(x => x.GetService(typeof(IServiceScopeFactory)))
.Returns((IServiceScopeFactory)null!);
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act & Assert
var act = async () => await service.StartAsync(cts.Token);
await act.Should().ThrowAsync<InvalidOperationException>();
}
[Fact]
public async Task StartAsync_ShouldLogStartingMessage()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
// Setup service provider to throw when CreateScope is called
serviceProviderMock
.Setup(x => x.GetService(typeof(IServiceScopeFactory)))
.Returns((IServiceScopeFactory)null!);
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act & Assert
var act = async () => await service.StartAsync(CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>();
loggerMock.Verify(
x =>
x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>(
(v, t) => v.ToString()!.Contains("Starting database initialization...")
),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
),
Times.Once
);
}
[Fact]
public async Task StartAsync_ShouldThrowExceptionWhenServiceProviderFails()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
// Setup service provider to throw when CreateScope is called
serviceProviderMock
.Setup(x => x.GetService(typeof(IServiceScopeFactory)))
.Returns((IServiceScopeFactory)null!);
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act & Assert
var act = async () => await service.StartAsync(CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>();
// Verify that starting message was logged
loggerMock.Verify(
x =>
x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>(
(v, t) => v.ToString()!.Contains("Starting database initialization...")
),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
),
Times.Once
);
}
[Fact]
public async Task StartAsync_ShouldHandleOperationCanceledException()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
var cts = new CancellationTokenSource();
cts.Cancel(); // Cancel immediately
// Setup service provider to throw when CreateScope is called
serviceProviderMock
.Setup(x => x.GetService(typeof(IServiceScopeFactory)))
.Returns((IServiceScopeFactory)null!);
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act & Assert
var act = async () => await service.StartAsync(cts.Token);
await act.Should().ThrowAsync<InvalidOperationException>();
}
[Fact]
public async Task StartAsync_ShouldHandleGeneralException()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
// Setup service provider to throw when CreateScope is called
serviceProviderMock
.Setup(x => x.GetService(typeof(IServiceScopeFactory)))
.Returns((IServiceScopeFactory)null!);
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act & Assert
var act = async () => await service.StartAsync(CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>();
}
[Fact]
public async Task StartAsync_ShouldThrowExceptionWithServiceProviderError()
{
// Arrange
var serviceProviderMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
// Setup service provider to throw when CreateScope is called
serviceProviderMock
.Setup(x => x.GetService(typeof(IServiceScopeFactory)))
.Returns((IServiceScopeFactory)null!);
var service = new DatabaseInitializationService(
serviceProviderMock.Object,
loggerMock.Object
);
// Act & Assert
var act = async () => await service.StartAsync(CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>();
// Verify that starting message was logged
loggerMock.Verify(
x =>
x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>(
(v, t) => v.ToString()!.Contains("Starting database initialization...")
),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
),
Times.Once
);
}
}