mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 05:11:10 +00:00
Replaces reflection-based repository instantiation with unit of work property accessors
This commit is contained in:
1 parent
4fa9eb6e26
commit
fee14afb89
87 files changed
+541
-735
No files matched your search
-6
@@ -1,6 +0,0 @@
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Common.Factory.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Repository fixture interface for testing non registered repositories.
|
||||
/// </summary>
|
||||
public interface IUnregisteredRepository { }
|
||||
-107
@@ -1,107 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.DataAccess.Core.Repositories.Books;
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using System;
|
||||
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
|
||||
{
|
||||
private readonly IFixture _fixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RepositoryDictionaryTests"/> class.
|
||||
/// </summary>
|
||||
public RepositoryDictionaryTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_WhenValidRepositoryItemIsAdded_ShouldIncreaseCount()
|
||||
{
|
||||
// Arrange
|
||||
BookRepository bookRepository = _fixture.Create<BookRepository>();
|
||||
RepositoryDictionary repositoryDictionary = new();
|
||||
|
||||
// Act
|
||||
repositoryDictionary.Add<IBookRepository>(bookRepository);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, repositoryDictionary.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_WhenCalled_ShouldRemoveAllItems()
|
||||
{
|
||||
// Arrange
|
||||
BookRepository bookRepository = _fixture.Create<BookRepository>();
|
||||
RepositoryDictionary repositoryDictionary = new();
|
||||
repositoryDictionary.Add<IBookRepository>(bookRepository);
|
||||
|
||||
// Act
|
||||
repositoryDictionary.Clear();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0, repositoryDictionary.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Get_WhenRepositoryExists_ShouldReturnCorrectRepository()
|
||||
{
|
||||
// Arrange
|
||||
BookRepository expected = _fixture.Create<BookRepository>();
|
||||
RepositoryDictionary repositoryDictionary = new();
|
||||
repositoryDictionary.Add<IBookRepository>(expected);
|
||||
|
||||
// Act
|
||||
IBookRepository actual = repositoryDictionary.Get<IBookRepository>(typeof(BookRepository));
|
||||
|
||||
// Assert
|
||||
Assert.Same(expected, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_WhenNullValueIsProvided_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
RepositoryDictionary repositoryDictionary = new();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentException>(() => repositoryDictionary.Add<IBookRepository>(null!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_WhenDuplicateValueIsAdded_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
BookRepository bookRepository = _fixture.Create<BookRepository>();
|
||||
RepositoryDictionary repositoryDictionary = new();
|
||||
repositoryDictionary.Add<IBookRepository>(bookRepository);
|
||||
|
||||
// Act & Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => repositoryDictionary.Add<IBookRepository>(bookRepository));
|
||||
Assert.Equal("Duplicate values are not allowed!", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_WhenNonRepositoryValueIsAdded_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
object nonRepository = new();
|
||||
RepositoryDictionary repositoryDictionary = new();
|
||||
|
||||
// Act & Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => repositoryDictionary.Add(nonRepository));
|
||||
Assert.Equal("Value must implement IRepository interface!", exception.Message);
|
||||
}
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.DataAccess.Common.DependencyInjection;
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Common.Factory.Fixtures;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
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
|
||||
{
|
||||
[Fact]
|
||||
public void CreateRepository_WhenCalled_ShouldReturnCorrectRepository()
|
||||
{
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
RepositoryFactory repositoryFactory = new(serviceProvider);
|
||||
|
||||
// Act
|
||||
IBookRepository result = repositoryFactory.CreateRepository<IBookRepository>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsAssignableFrom<IBookRepository>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateRepository_WhenUnregisteredTypeRequested_ShouldThrowException()
|
||||
{
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
RepositoryFactory repositoryFactory = new(serviceProvider);
|
||||
|
||||
// Act & Assert
|
||||
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(
|
||||
() => repositoryFactory.CreateRepository<IUnregisteredRepository>());
|
||||
Assert.Equal(
|
||||
"No service for type 'Lumina.DataAccess.UnitTests.Core.Repositories.Common.Factory.Fixtures.IUnregisteredRepository' has been registered.",
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user