Moves repository entities to Application layer for joint access

This commit is contained in:
BinaryAlley committed 2024-11-04 19:40:11 +02:00
1 parent baf9854355
commit 7321fb89e2
120 files changed
+2606 -784

No files matched your search

@@ -0,0 +1,93 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Application.Core.MediaLibrary.Management.Commands.AddLibrary;
using Lumina.Contracts.Requests.MediaLibrary.Management;
using Lumina.Domain.Common.Enums.MediaLibrary;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.Management;
/// <summary>
/// Contains unit tests for the <see cref="AddMediaLibraryRequestMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class AddMediaLibraryRequestMappingTests
{
[Fact]
public void ToCommand_WhenMappingValidRequest_ShouldMapCorrectly()
{
// Arrange
AddLibraryRequest request = new(
Guid.NewGuid(),
"My Library",
LibraryType.Book,
["C:/Books", "D:/Media/Books"]
);
// Act
AddLibraryCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.UserId.Should().Be(request.UserId);
result.Title.Should().Be(request.Title);
result.LibraryType.Should().Be(request.LibraryType);
result.ContentLocations.Should().BeEquivalentTo(request.ContentLocations);
}
[Theory]
[InlineData(LibraryType.Book)]
[InlineData(LibraryType.Movie)]
[InlineData(LibraryType.TvShow)]
[InlineData(LibraryType.Music)]
public void ToCommand_WhenMappingDifferentLibraryTypes_ShouldMapCorrectly(LibraryType libraryType)
{
// Arrange
AddLibraryRequest request = new(
Guid.NewGuid(),
"My Library",
libraryType,
["C:/Media"]
);
// Act
AddLibraryCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.UserId.Should().Be(request.UserId);
result.Title.Should().Be(request.Title);
result.LibraryType.Should().Be(libraryType);
result.ContentLocations.Should().BeEquivalentTo(request.ContentLocations);
}
[Fact]
public void ToCommand_WhenMappingMultipleContentLocations_ShouldMapAllCorrectly()
{
// Arrange
string[] contentLocations =
[
"C:/Media/Books",
"D:/Books",
"E:/Digital Library/Books",
"F:/Reading Material"
];
AddLibraryRequest request = new(
Guid.NewGuid(),
"My Library",
LibraryType.Book,
contentLocations
);
// Act
AddLibraryCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.ContentLocations.Should().BeEquivalentTo(contentLocations);
}
}
@@ -0,0 +1,132 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Lumina.Domain.Common.Enums.MediaLibrary;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.Management;
/// <summary>
/// Contains unit tests for the <see cref="LibraryEntityMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LibraryEntityMappingTests
{
[Fact]
public void ToResponse_WhenMappingValidLibraryEntity_ShouldMapCorrectly()
{
// Arrange
LibraryEntity entity = new()
{
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Title = "My Library",
LibraryType = LibraryType.Book,
ContentLocations =
[
new() { Path = "C:/Books" },
new() { Path = "D:/Media/Books" }
],
Created = DateTime.UtcNow,
Updated = null
};
// Act
LibraryResponse result = entity.ToResponse();
// Assert
result.Should().NotBeNull();
result.Id.Should().Be(entity.Id);
result.UserId.Should().Be(entity.UserId);
result.Title.Should().Be(entity.Title);
result.LibraryType.Should().Be(entity.LibraryType);
result.ContentLocations.Should().BeEquivalentTo(entity.ContentLocations.Select(l => l.Path));
result.Created.Should().Be(entity.Created);
result.Updated.Should().Be(entity.Updated);
}
[Theory]
[InlineData(LibraryType.Book)]
[InlineData(LibraryType.Movie)]
[InlineData(LibraryType.TvShow)]
[InlineData(LibraryType.Music)]
public void ToResponse_WhenMappingDifferentLibraryTypes_ShouldMapCorrectly(LibraryType libraryType)
{
// Arrange
LibraryEntity entity = new()
{
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Title = "My Library",
LibraryType = libraryType,
ContentLocations = [new() { Path = "C:/Media" }],
Created = DateTime.UtcNow,
Updated = null
};
// Act
LibraryResponse result = entity.ToResponse();
// Assert
result.Should().NotBeNull();
result.LibraryType.Should().Be(libraryType);
}
[Fact]
public void ToResponse_WhenMappingMultipleContentLocations_ShouldMapAllCorrectly()
{
// Arrange
LibraryEntity entity = new()
{
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Title = "My Library",
LibraryType = LibraryType.Book,
ContentLocations =
[
new() { Path = "C:/Media/Books" },
new() { Path = "D:/Books" },
new() { Path = "E:/Digital Library/Books" },
new() { Path = "F:/Reading Material" }
],
Created = DateTime.UtcNow,
Updated = null
};
// Act
LibraryResponse result = entity.ToResponse();
// Assert
result.Should().NotBeNull();
result.ContentLocations.Should().BeEquivalentTo(entity.ContentLocations.Select(l => l.Path));
}
[Fact]
public void ToResponse_WhenMappingWithUpdatedDateTime_ShouldMapCorrectly()
{
// Arrange
DateTime updated = DateTime.UtcNow.AddDays(-1);
LibraryEntity entity = new()
{
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Title = "My Library",
LibraryType = LibraryType.Book,
ContentLocations = [new() { Path = "C:/Books" }],
Created = DateTime.UtcNow,
Updated = updated
};
// Act
LibraryResponse result = entity.ToResponse();
// Assert
result.Should().NotBeNull();
result.Updated.Should().Be(updated);
}
}
@@ -0,0 +1,123 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.Mapping.Common.Metadata;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Common;
using Lumina.Domain.Common.Enums.MediaLibrary;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.Management;
/// <summary>
/// Contains unit tests for the <see cref="LibraryMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LibraryMappingTests
{
[Fact]
public void ToRepositoryEntity_WhenMappingValidLibrary_ShouldMapCorrectly()
{
// Arrange
ErrorOr<Library> libraryResult = Library.Create(
Guid.NewGuid(),
"Test Library",
LibraryType.Book,
["C:/Books", "D:/Media/Books"]
);
Library library = libraryResult.Value;
// Act
LibraryEntity result = library.ToRepositoryEntity();
// Assert
result.Should().NotBeNull();
result.Id.Should().Be(library.Id.Value);
result.UserId.Should().Be(library.UserId.Value);
result.Title.Should().Be(library.Title);
result.LibraryType.Should().Be(library.LibraryType);
result.ContentLocations.Select(l => l.Path)
.Should().BeEquivalentTo(library.ContentLocations.Select(l => l.Path));
result.Created.Should().Be(library.Created);
result.Updated.Should().Be(library.Updated.HasValue ? library.Updated : null);
}
[Fact]
public void ToRepositoryEntity_WhenMappingLibraryWithEmptyContentLocations_ShouldMapCorrectly()
{
// Arrange
ErrorOr<Library> libraryResult = Library.Create(
Guid.NewGuid(),
"Empty Library",
LibraryType.Book,
[]
);
Library library = libraryResult.Value;
// Act
LibraryEntity result = library.ToRepositoryEntity();
// Assert
result.Should().NotBeNull();
result.ContentLocations.Should().BeEmpty();
}
[Theory]
[InlineData(LibraryType.Book)]
[InlineData(LibraryType.Movie)]
[InlineData(LibraryType.TvShow)]
[InlineData(LibraryType.Music)]
public void ToRepositoryEntity_WhenMappingDifferentLibraryTypes_ShouldMapCorrectly(LibraryType libraryType)
{
// Arrange
ErrorOr<Library> libraryResult = Library.Create(
Guid.NewGuid(),
"Test Library",
libraryType,
["C:/Media"]
);
Library library = libraryResult.Value;
// Act
LibraryEntity result = library.ToRepositoryEntity();
// Assert
result.Should().NotBeNull();
result.LibraryType.Should().Be(libraryType);
}
[Fact]
public void ToRepositoryEntity_WhenMappingMultipleContentLocations_ShouldMapAllCorrectly()
{
// Arrange
List<string> contentLocations =
[
"C:/Media/Books",
"D:/Books",
"E:/Digital Library/Books",
"F:/Reading Material"
];
ErrorOr<Library> libraryResult = Library.Create(
Guid.NewGuid(),
"Test Library",
LibraryType.Book,
contentLocations
);
Library library = libraryResult.Value;
// Act
LibraryEntity result = library.ToRepositoryEntity();
// Assert
result.Should().NotBeNull();
result.ContentLocations.Select(l => l.Path)
.Should().BeEquivalentTo(library.ContentLocations.Select(l => l.Path));
}
}