mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 22:01:10 +00:00
Adds Data Access Layer unit tests
This commit is contained in:
1 parent
46b646c9da
commit
a2721e39cb
12 files changed
+418
-8
No files matched your search
+90
@@ -0,0 +1,90 @@
|
||||
#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.Books;
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Common.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="DataAccessLayerServices"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DataAccessLayerServicesTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly IFixture _fixture;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DataAccessLayerServicesTests"/> class.
|
||||
/// </summary>
|
||||
public DataAccessLayerServicesTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void AddDataAccessLayerServices_WhenBasePathExists_ShouldAddServices()
|
||||
{
|
||||
// Arrange
|
||||
var services = new ServiceCollection();
|
||||
var basePath = Path.GetTempPath(); // Use a real existing path
|
||||
|
||||
// Act
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
|
||||
// Assert
|
||||
services.Should().Contain(sd => sd.ServiceType == typeof(LuminaDbContext));
|
||||
services.Should().Contain(sd => sd.ServiceType == typeof(IUnitOfWork) && sd.ImplementationType == typeof(UnitOfWork));
|
||||
services.Should().Contain(sd => sd.ServiceType == typeof(IRepositoryFactory) && sd.ImplementationType == typeof(RepositoryFactory));
|
||||
services.Should().Contain(sd => sd.ServiceType == typeof(IBookRepository) && sd.ImplementationType == typeof(BookRepository));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDataAccessLayerServices_ShouldRegisterDbContextWithCorrectConnectionString()
|
||||
{
|
||||
// Arrange
|
||||
var services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
|
||||
// Assert
|
||||
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LuminaDbContext>));
|
||||
descriptor.Should().NotBeNull();
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var context = serviceProvider.GetService<LuminaDbContext>();
|
||||
|
||||
context.Should().NotBeNull();
|
||||
context!.Database.GetDbConnection().ConnectionString.Should().Contain("Lumina.db");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDataAccessLayerServices_WhenCalled_ShouldRegisterRepositories()
|
||||
{
|
||||
// Arrange
|
||||
var services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
var repository = serviceProvider.GetService<IBookRepository>();
|
||||
repository.Should().NotBeNull();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+11
@@ -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
|
||||
{
|
||||
}
|
||||
+114
@@ -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
|
||||
}
|
||||
+52
@@ -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
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Utilities;
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Utilities.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="OptionsBuilderFluentValidationUtilitiesTests"/> class.
|
||||
+3
-2
@@ -6,6 +6,7 @@ using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using Lumina.Infrastructure.Common.Utilities;
|
||||
using Lumina.Infrastructure.Common.Validation;
|
||||
using Lumina.Infrastructure.UnitTests.Common.Utilities.Fixtures;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
@@ -25,7 +26,7 @@ public class OptionsBuilderFluentValidationUtilitiesTests
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FluentValidationOptionsTests"/> class.
|
||||
/// Initializes a new instance of the <see cref="OptionsBuilderFluentValidationUtilitiesTests"/> class.
|
||||
/// </summary>
|
||||
public OptionsBuilderFluentValidationUtilitiesTests()
|
||||
{
|
||||
@@ -33,7 +34,7 @@ public class OptionsBuilderFluentValidationUtilitiesTests
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ================================================================= METHODS ===================================================================================
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void ValidateFluently_WhenCalled_ShouldRegisterFluentValidationOptions()
|
||||
{
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Validation;
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Validation.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FluentValidationOptionsTests"/> class.
|
||||
@@ -10,5 +10,5 @@ namespace Lumina.Infrastructure.UnitTests.Common.Validation;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FluentValidationOptionsFixture
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
+2
-1
@@ -8,6 +8,7 @@ using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using Lumina.Infrastructure.Common.Validation;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Lumina.Infrastructure.UnitTests.Common.Validation.Fixtures;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Validation;
|
||||
@@ -32,7 +33,7 @@ public class FluentValidationOptionsTests
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ================================================================= METHODS ===================================================================================
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Validate_WhenMatchingName_ShouldValidateOptions()
|
||||
{
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ public class CommonSettingsModelValidatorTests
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ================================================================= METHODS ===================================================================================
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void CommonSettingsModelValidator_WhenThemeProvided_ShouldNotHaveValidationError()
|
||||
{
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ public class DatabaseSettingsModelValidatorTests
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ================================================================= METHODS ===================================================================================
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void DatabaseSettingsModelValidator_WhenDefaultConnectionProvided_ShouldNotHaveValidationError()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user