mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 07:11: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
+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 { }
|
||||
Reference in new issue
Block a user