Adds Data Access Layer unit tests

This commit is contained in:
BinaryAlley committed 2024-08-28 21:50:55 +03:00
1 parent 46b646c9da
commit a2721e39cb
12 files changed
+418 -8

No files matched your search

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books;
internal class BookRepositoryTests
{
}
@@ -0,0 +1,114 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.DataAccess.Core.Repositories.Books;
using Lumina.DataAccess.Core.Repositories.Common.Factory;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Common.Factory;
/// <summary>
/// Contains unit tests for the <see cref="RepositoryDictionary"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RepositoryDictionaryTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFixture _fixture;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="RepositoryDictionaryTests"/> class.
/// </summary>
public RepositoryDictionaryTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Add_WhenValidRepositoryItemIsAdded_ShouldIncreaseCount()
{
// Arrange
var bookRepository = _fixture.Create<BookRepository>();
var repositoryDictionary = new RepositoryDictionary();
// Act
repositoryDictionary.Add<IBookRepository>(bookRepository);
// Assert
repositoryDictionary.Count.Should().Be(1);
}
[Fact]
public void Clear_WhenCalled_ShouldRemoveAllItems()
{
// Arrange
var bookRepository = _fixture.Create<BookRepository>();
var repositoryDictionary = new RepositoryDictionary();
repositoryDictionary.Add<IBookRepository>(bookRepository);
// Act
repositoryDictionary.Clear();
// Assert
repositoryDictionary.Count.Should().Be(0);
}
[Fact]
public void Get_WhenRepositoryExists_ShouldReturnCorrectRepository()
{
// Arrange
var expected = _fixture.Create<BookRepository>();
var repositoryDictionary = new RepositoryDictionary();
repositoryDictionary.Add<IBookRepository>(expected);
// Act
var actual = repositoryDictionary.Get<IBookRepository>(typeof(BookRepository));
// Assert
actual.Should().BeSameAs(expected);
}
[Fact]
public void Add_WhenNullValueIsProvided_ShouldThrowArgumentException()
{
// Arrange
var repositoryDictionary = new RepositoryDictionary();
// Act & Assert
Action act = () => repositoryDictionary.Add<IBookRepository>(null!);
act.Should().Throw<ArgumentException>().WithMessage("Value cannot be null!");
}
[Fact]
public void Add_WhenDuplicateValueIsAdded_ShouldThrowArgumentException()
{
// Arrange
var bookRepository = _fixture.Create<BookRepository>();
var repositoryDictionary = new RepositoryDictionary();
repositoryDictionary.Add<IBookRepository>(bookRepository);
// Act & Assert
Action act = () => repositoryDictionary.Add<IBookRepository>(bookRepository);
act.Should().Throw<ArgumentException>().WithMessage("Duplicate values are not allowed!");
}
[Fact]
public void Add_WhenNonRepositoryValueIsAdded_ShouldThrowArgumentException()
{
// Arrange
var nonRepository = new object();
var repositoryDictionary = new RepositoryDictionary();
// Act & Assert
Action act = () => repositoryDictionary.Add(nonRepository);
act.Should().Throw<ArgumentException>().WithMessage("Value must implement IRepository interface!");
}
#endregion
}
@@ -0,0 +1,52 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.DataAccess.Common.DependencyInjection;
using Lumina.DataAccess.Core.Repositories.Common.Factory;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Common.Factory;
/// <summary>
/// Contains unit tests for the <see cref="RepositoryFactory"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RepositoryFactoryTests
{
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void CreateRepository_WhenCalled_ShouldReturnCorrectRepository()
{
// Arrange
var services = new ServiceCollection();
DataAccessLayerServices.AddDataAccessLayerServices(services);
var serviceProvider = services.BuildServiceProvider();
var repositoryFactory = new RepositoryFactory(serviceProvider);
// Act
var result = repositoryFactory.CreateRepository<IBookRepository>();
// Assert
result.Should().NotBeNull();
result.Should().BeAssignableTo<IBookRepository>();
}
[Fact]
public void CreateRepository_WhenUnregisteredTypeRequested_ShouldThrowException()
{
// Arrange
var services = new ServiceCollection();
DataAccessLayerServices.AddDataAccessLayerServices(services);
var serviceProvider = services.BuildServiceProvider();
var repositoryFactory = new RepositoryFactory(serviceProvider);
// Act & Assert
Action act = () => repositoryFactory.CreateRepository<IUnregisteredRepository>();
act.Should().Throw<InvalidOperationException>().WithMessage("No service for type 'Lumina.DataAccess.UnitTests.Core.Repositories.Common.Factory.IUnregisteredRepository' has been registered.");
}
#endregion
}
public interface IUnregisteredRepository { }
@@ -0,0 +1,140 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.DataAccess.Common.DependencyInjection;
using Lumina.DataAccess.Core.Repositories.Common.Factory;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.UoW;
/// <summary>
/// Contains unit tests for the <see cref="UnitOfWork"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class UnitOfWorkTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFixture _fixture;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="UnitOfWorkTests"/> class.
/// </summary>
public UnitOfWorkTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_fixture.Customize<LuminaDbContext>(composer => composer.FromFactory(() => {
var options = new DbContextOptionsBuilder<LuminaDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
return Substitute.ForPartsOf<LuminaDbContext>(options);
}));
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void ResetRepositories_WhenCalled_ShouldClearAllRepositories()
{
// Arrange
var services = new ServiceCollection();
DataAccessLayerServices.AddDataAccessLayerServices(services);
var serviceProvider = services.BuildServiceProvider();
var unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
// Act
unitOfWork.ResetRepositories();
// Assert
unitOfWork.Repositories.Count.Should().Be(0);
}
[Fact]
public void AddRepositories_WhenCalled_ShouldAddRepositories()
{
// Arrange
var services = new ServiceCollection();
DataAccessLayerServices.AddDataAccessLayerServices(services);
var serviceProvider = services.BuildServiceProvider();
var unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
int initialCount = unitOfWork.Repositories.Count;
// Act
unitOfWork.ResetRepositories();
unitOfWork.AddRepositories();
// Assert
unitOfWork.Repositories.Count.Should().BeGreaterThan(0);
unitOfWork.Repositories.Count.Should().Be(initialCount);
}
[Fact]
public void Constructor_WhenCalled_ShouldAddRepositories()
{
// Arrange
var repositoryFactory = _fixture.Create<IRepositoryFactory>();
var dbContext = _fixture.Create<LuminaDbContext>();
// Act
var unitOfWork = new UnitOfWork(repositoryFactory, dbContext);
// Assert
unitOfWork.Repositories.Count.Should().BeGreaterThan(0);
}
[Fact]
public void GetRepository_WhenRepositoryNotAdded_ShouldReturnNull()
{
// Arrange
var services = new ServiceCollection();
DataAccessLayerServices.AddDataAccessLayerServices(services);
var serviceProvider = services.BuildServiceProvider();
var unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
unitOfWork.ResetRepositories();
// Act
var result = unitOfWork.GetRepository<IBookRepository>();
// Assert
result.Should().BeNull();
}
[Fact]
public void GetRepository_WhenRepositoriesAdded_ShouldReturnRepositories()
{
// Arrange
var services = new ServiceCollection();
DataAccessLayerServices.AddDataAccessLayerServices(services);
var serviceProvider = services.BuildServiceProvider();
var unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
// Act & Assert
unitOfWork.GetRepository<IBookRepository>().Should().NotBeNull();
}
[Fact]
public async Task SaveChangesAsync_WhenCalled_ShouldCallDbContextSaveChangesAsync()
{
// Arrange
var repositoryFactory = _fixture.Create<IRepositoryFactory>();
var dbContext = _fixture.Create<LuminaDbContext>();
var unitOfWork = new UnitOfWork(repositoryFactory, dbContext);
var cancellationToken = new CancellationToken();
// Act
await unitOfWork.SaveChangesAsync(cancellationToken);
// Assert
await dbContext.Received(1).SaveChangesAsync(cancellationToken);
}
#endregion
}