mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Adds Application layer unit tests for file system browser dialog
This commit is contained in:
1 parent
1f5322fa1d
commit
0e3e688eb7
79 files changed
+5484
-91
No files matched your search
+16
@@ -0,0 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Mediator;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Behaviors.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="ValidationBehavior"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValidationBehaviorFixture : IRequest<ErrorOr<ValidationBehaviorTestResponse>>
|
||||
{
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Behaviors.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture test class for validation behavior responses.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValidationBehaviorTestResponse
|
||||
{
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Lumina.Application.Common.Behaviors;
|
||||
using Lumina.Application.UnitTests.Common.Behaviors.Fixtures;
|
||||
using Mediator;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ValidationBehavior"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValidationBehaviorTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly IValidator<ValidationBehaviorFixture> _mockValidator;
|
||||
private readonly MessageHandlerDelegate<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> _nextDelegate;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationBehaviorTests"/> class.
|
||||
/// </summary>
|
||||
public ValidationBehaviorTests()
|
||||
{
|
||||
_mockValidator = Substitute.For<IValidator<ValidationBehaviorFixture>>();
|
||||
_nextDelegate = Substitute.For<MessageHandlerDelegate<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>>>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public async Task Handle_WhenValidatorIsNull_ShouldInvokeNextDelegate()
|
||||
{
|
||||
// Arrange
|
||||
ValidationBehavior<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> behavior = new();
|
||||
ValidationBehaviorFixture request = new();
|
||||
ValidationBehaviorTestResponse expectedResponse = new();
|
||||
_nextDelegate.Invoke(request, Arg.Any<CancellationToken>()).Returns(new ValueTask<ErrorOr<ValidationBehaviorTestResponse>>(ErrorOrFactory.From(expectedResponse)));
|
||||
|
||||
// Act
|
||||
ErrorOr<ValidationBehaviorTestResponse> result = await behavior.Handle(request, CancellationToken.None, _nextDelegate);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(expectedResponse);
|
||||
await _nextDelegate.Received(1).Invoke(request, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenValidationSucceeds_ShouldInvokeNextDelegate()
|
||||
{
|
||||
// Arrange
|
||||
ValidationBehavior<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> behavior = new(_mockValidator);
|
||||
ValidationBehaviorFixture request = new();
|
||||
ValidationBehaviorTestResponse expectedResponse = new();
|
||||
_mockValidator.ValidateAsync(request, Arg.Any<CancellationToken>()).Returns(new ValidationResult());
|
||||
_nextDelegate.Invoke(request, Arg.Any<CancellationToken>()).Returns(new ValueTask<ErrorOr<ValidationBehaviorTestResponse>>(ErrorOrFactory.From(expectedResponse)));
|
||||
|
||||
// Act
|
||||
ErrorOr<ValidationBehaviorTestResponse> result = await behavior.Handle(request, CancellationToken.None, _nextDelegate);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(expectedResponse);
|
||||
await _mockValidator.Received(1).ValidateAsync(request, Arg.Any<CancellationToken>());
|
||||
await _nextDelegate.Received(1).Invoke(request, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenValidationFails_ShouldReturnErrors()
|
||||
{
|
||||
// Arrange
|
||||
ValidationBehavior<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> behavior = new(_mockValidator);
|
||||
ValidationBehaviorFixture request = new();
|
||||
List<ValidationFailure> validationFailures =
|
||||
[
|
||||
new ValidationFailure("Property1", "Error message 1"),
|
||||
new ValidationFailure("Property2", "Error message 2")
|
||||
];
|
||||
_mockValidator.ValidateAsync(request, Arg.Any<CancellationToken>()).Returns(new ValidationResult(validationFailures));
|
||||
|
||||
// Act
|
||||
ErrorOr<ValidationBehaviorTestResponse> result = await behavior.Handle(request, CancellationToken.None, _nextDelegate);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeTrue();
|
||||
result.Errors.Should().HaveCount(2);
|
||||
result.Errors[0].Type.Should().Be(ErrorType.Validation);
|
||||
result.Errors[0].Description.Should().Be("Error message 1");
|
||||
result.Errors[1].Type.Should().Be(ErrorType.Validation);
|
||||
result.Errors[1].Description.Should().Be("Error message 2");
|
||||
await _mockValidator.Received(1).ValidateAsync(request, Arg.Any<CancellationToken>());
|
||||
await _nextDelegate.DidNotReceive().Invoke(request, Arg.Any<CancellationToken>());
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Converters.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="OptionalJsonConverterFactory"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class OptionalJsonConverterFactoryFixture
|
||||
{
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Converters;
|
||||
using Lumina.Application.UnitTests.Common.Converters.Fixtures;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetFilesQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class OptionalJsonConverterFactoryTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly OptionalJsonConverterFactory _factory;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OptionalJsonConverterFactoryTests"/> class.
|
||||
/// </summary>
|
||||
public OptionalJsonConverterFactoryTests()
|
||||
{
|
||||
_factory = new OptionalJsonConverterFactory();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void CanConvert_WhenTypeIsOptional_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Type typeToConvert = typeof(Optional<string>);
|
||||
|
||||
// Act
|
||||
bool result = _factory.CanConvert(typeToConvert);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanConvert_WhenTypeIsNotOptional_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
Type typeToConvert = typeof(string);
|
||||
|
||||
// Act
|
||||
bool result = _factory.CanConvert(typeToConvert);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanConvert_WhenTypeIsGenericButNotOptional_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
Type typeToConvert = typeof(List<string>);
|
||||
|
||||
// Act
|
||||
bool result = _factory.CanConvert(typeToConvert);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateConverter_WhenTypeIsOptionalString_ShouldReturnCorrectConverterType()
|
||||
{
|
||||
// Arrange
|
||||
Type typeToConvert = typeof(Optional<string>);
|
||||
JsonSerializerOptions options = new();
|
||||
|
||||
// Act
|
||||
JsonConverter converter = _factory.CreateConverter(typeToConvert, options);
|
||||
|
||||
// Assert
|
||||
converter.Should().BeOfType<OptionalJsonConverter<string>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateConverter_WhenTypeIsOptionalInt_ShouldReturnCorrectConverterType()
|
||||
{
|
||||
// Arrange
|
||||
Type typeToConvert = typeof(Optional<int>);
|
||||
JsonSerializerOptions options = new();
|
||||
|
||||
// Act
|
||||
JsonConverter converter = _factory.CreateConverter(typeToConvert, options);
|
||||
|
||||
// Assert
|
||||
converter.Should().BeOfType<OptionalJsonConverter<int>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateConverter_WhenTypeIsOptionalCustomClass_ShouldReturnCorrectConverterType()
|
||||
{
|
||||
// Arrange
|
||||
Type typeToConvert = typeof(Optional<OptionalJsonConverterFactoryFixture>);
|
||||
JsonSerializerOptions options = new();
|
||||
|
||||
// Act
|
||||
JsonConverter converter = _factory.CreateConverter(typeToConvert, options);
|
||||
|
||||
// Assert
|
||||
converter.Should().BeOfType<OptionalJsonConverter<OptionalJsonConverterFactoryFixture>>();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Converters;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="OptionalJsonConverter"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class OptionalJsonConverterTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly JsonSerializerOptions _options;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OptionalJsonConverterTests"/> class.
|
||||
/// </summary>
|
||||
public OptionalJsonConverterTests()
|
||||
{
|
||||
_options = new JsonSerializerOptions();
|
||||
_options.Converters.Add(new OptionalJsonConverter<string>());
|
||||
_options.Converters.Add(new OptionalJsonConverter<int>());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Read_WhenJsonIsNull_ShouldReturnNone()
|
||||
{
|
||||
// Arrange
|
||||
string json = "null";
|
||||
|
||||
// Act
|
||||
Optional<string> result = JsonSerializer.Deserialize<Optional<string>>(json, _options);
|
||||
|
||||
// Assert
|
||||
result.HasValue.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenJsonHasStringValue_ShouldReturnSomeWithCorrectValue()
|
||||
{
|
||||
// Arrange
|
||||
string json = "\"test\"";
|
||||
|
||||
// Act
|
||||
Optional<string> result = JsonSerializer.Deserialize<Optional<string>>(json, _options);
|
||||
|
||||
// Assert
|
||||
result.HasValue.Should().BeTrue();
|
||||
result.Value.Should().Be("test");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenJsonHasIntValue_ShouldReturnSomeWithCorrectValue()
|
||||
{
|
||||
// Arrange
|
||||
string json = "42";
|
||||
|
||||
// Act
|
||||
Optional<int> result = JsonSerializer.Deserialize<Optional<int>>(json, _options);
|
||||
|
||||
// Assert
|
||||
result.HasValue.Should().BeTrue();
|
||||
result.Value.Should().Be(42);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Write_WhenOptionalHasNoValue_ShouldWriteNull()
|
||||
{
|
||||
// Arrange
|
||||
Optional<string> optional = Optional<string>.None();
|
||||
|
||||
// Act
|
||||
string json = JsonSerializer.Serialize(optional, _options);
|
||||
|
||||
// Assert
|
||||
json.Should().Be("null");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Write_WhenOptionalHasStringValue_ShouldWriteCorrectJson()
|
||||
{
|
||||
// Arrange
|
||||
Optional<string> optional = Optional<string>.Some("test");
|
||||
|
||||
// Act
|
||||
string json = JsonSerializer.Serialize(optional, _options);
|
||||
|
||||
// Assert
|
||||
json.Should().Be("\"test\"");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Write_WhenOptionalHasIntValue_ShouldWriteCorrectJson()
|
||||
{
|
||||
// Arrange
|
||||
Optional<int> optional = Optional<int>.Some(42);
|
||||
|
||||
// Act
|
||||
string json = JsonSerializer.Serialize(optional, _options);
|
||||
|
||||
// Assert
|
||||
json.Should().Be("42");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadWrite_WhenSerializingAndDeserializingSomeValue_ShouldPreserveValue()
|
||||
{
|
||||
// Arrange
|
||||
Optional<string> original = Optional<string>.Some("test");
|
||||
|
||||
// Act
|
||||
string json = JsonSerializer.Serialize(original, _options);
|
||||
Optional<string> deserialized = JsonSerializer.Deserialize<Optional<string>>(json, _options);
|
||||
|
||||
// Assert
|
||||
deserialized.Should().Be(original);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadWrite_WhenSerializingAndDeserializingNone_ShouldPreserveNone()
|
||||
{
|
||||
// Arrange
|
||||
Optional<string> original = Optional<string>.None();
|
||||
|
||||
// Act
|
||||
string json = JsonSerializer.Serialize(original, _options);
|
||||
Optional<string> deserialized = JsonSerializer.Deserialize<Optional<string>>(json, _options);
|
||||
|
||||
// Assert
|
||||
deserialized.Should().Be(original);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Mapping.FileManagement;
|
||||
using Lumina.Application.UnitTests.Core.FileManagement.Directories.Fixtures;
|
||||
using Lumina.Contracts.Models.FileManagement;
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
|
||||
using Mapster;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="DirectoryMappingConfig"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DirectoryMappingConfigTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly TypeAdapterConfig _config;
|
||||
private readonly DirectoryMappingConfig _directoryMappingConfig;
|
||||
private readonly FileSystemTreeMappingConfig _fileSystemTreeMappingConfig;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
public DirectoryMappingConfigTests()
|
||||
{
|
||||
_config = new TypeAdapterConfig();
|
||||
_directoryMappingConfig = new DirectoryMappingConfig();
|
||||
_fileSystemTreeMappingConfig = new FileSystemTreeMappingConfig();
|
||||
_directoryMappingConfig.Register(_config);
|
||||
_fileSystemTreeMappingConfig.Register(_config);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Register_WhenMappingDirectoryToFileSystemTreeNodeResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
DirectoryFixture directoryFixture = new();
|
||||
Directory directory = directoryFixture.CreateDirectory();
|
||||
|
||||
// create some child items
|
||||
List<FileSystemItem> childItems =
|
||||
[
|
||||
directoryFixture.CreateDirectory(),
|
||||
directoryFixture.CreateDirectory()
|
||||
];
|
||||
foreach (FileSystemItem item in childItems)
|
||||
directory.AddItem(item);
|
||||
|
||||
// Act
|
||||
FileSystemTreeNodeResponse result = directory.Adapt<FileSystemTreeNodeResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Name.Should().Be(directory.Name);
|
||||
result.Path.Should().Be(directory.Id.Path);
|
||||
result.ItemType.Should().Be(directory.Type);
|
||||
result.IsExpanded.Should().BeFalse();
|
||||
result.ChildrenLoaded.Should().BeFalse();
|
||||
result.Children.Should().NotBeNull();
|
||||
result.Children.Should().HaveCount(childItems.Count);
|
||||
|
||||
// Verify that each child item is correctly mapped
|
||||
for (int i = 0; i < childItems.Count; i++)
|
||||
{
|
||||
result.Children[i].Name.Should().Be(childItems[i].Name);
|
||||
result.Children[i].Path.Should().Be(childItems[i].Id.Path);
|
||||
result.Children[i].ItemType.Should().Be(childItems[i].Type);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingDirectoryToDirectoryResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
DirectoryFixture directoryFixture = new();
|
||||
Directory directory = directoryFixture.CreateDirectory();
|
||||
|
||||
// Act
|
||||
DirectoryResponse result = directory.Adapt<DirectoryResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(directory.Id.Path);
|
||||
result.Name.Should().Be(directory.Name);
|
||||
result.DateCreated.Should().Be(directory.DateCreated.Value);
|
||||
result.DateModified.Should().Be(directory.DateModified.Value);
|
||||
result.Items.Should().BeAssignableTo<List<FileSystemItemModel>>();
|
||||
result.Items.Should().HaveCount(directory.Items.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingDirectoryToFileSystemItemModel_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
DirectoryFixture directoryFixture = new();
|
||||
Directory directory = directoryFixture.CreateDirectory();
|
||||
DateTime beforeMapping = DateTime.Now;
|
||||
|
||||
// Act
|
||||
FileSystemItemModel result = directory.Adapt<FileSystemItemModel>(_config);
|
||||
DateTime afterMapping = DateTime.Now;
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(directory.Id.Path);
|
||||
result.Name.Should().Be(directory.Name);
|
||||
result.DateCreated.Should().BeOnOrAfter(beforeMapping).And.BeOnOrBefore(afterMapping);
|
||||
result.DateModified.Should().BeOnOrAfter(beforeMapping).And.BeOnOrBefore(afterMapping);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Mapping.FileManagement;
|
||||
using Lumina.Application.UnitTests.Core.FileManagement.Files.Fixtures;
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
|
||||
using Mapster;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FileMappingConfig"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileMappingConfigTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly TypeAdapterConfig _config;
|
||||
private readonly FileMappingConfig _fileMappingConfig;
|
||||
private readonly FileSystemTreeMappingConfig _fileSystemTreeMappingConfig;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
public FileMappingConfigTests()
|
||||
{
|
||||
_config = new TypeAdapterConfig();
|
||||
_fileMappingConfig = new FileMappingConfig();
|
||||
_fileSystemTreeMappingConfig = new FileSystemTreeMappingConfig();
|
||||
_fileMappingConfig.Register(_config);
|
||||
_fileSystemTreeMappingConfig.Register(_config);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Register_WhenMappingFileToFileSystemTreeNodeResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileFixture fileFixture = new();
|
||||
File file = fileFixture.CreateFile();
|
||||
|
||||
// Act
|
||||
FileSystemTreeNodeResponse result = file.Adapt<FileSystemTreeNodeResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Name.Should().Be(file.Name);
|
||||
result.Path.Should().Be(file.Id.Path);
|
||||
result.ItemType.Should().Be(file.Type);
|
||||
result.IsExpanded.Should().BeFalse();
|
||||
result.ChildrenLoaded.Should().BeFalse();
|
||||
result.Children.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingFileToFileResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileFixture fileFixture = new();
|
||||
File file = fileFixture.CreateFile();
|
||||
|
||||
// Act
|
||||
FileResponse result = file.Adapt<FileResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(file.Id.Path);
|
||||
result.Name.Should().Be(file.Name);
|
||||
result.DateCreated.Should().Be(file.DateCreated.Value);
|
||||
result.DateModified.Should().Be(file.DateModified.Value);
|
||||
result.Size.Should().Be(file.Size);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingFileToFileResponseWithoutDates_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileFixture fileFixture = new();
|
||||
File file = FileFixture.CreateFileWithoutDates();
|
||||
|
||||
// Act
|
||||
FileResponse result = file.Adapt<FileResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(file.Id.Path);
|
||||
result.Name.Should().Be(file.Name);
|
||||
result.DateCreated.Should().Be(default);
|
||||
result.DateModified.Should().Be(default);
|
||||
result.Size.Should().Be(file.Size);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Mapping.FileManagement;
|
||||
using Lumina.Application.UnitTests.Core.FileManagement.Directories.Fixtures;
|
||||
using Lumina.Application.UnitTests.Core.FileManagement.Files.Fixtures;
|
||||
using Lumina.Contracts.Enums.FileSystem;
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
|
||||
using Mapster;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FileSystemTreeMappingConfig"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileSystemTreeMappingConfigTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly TypeAdapterConfig _config;
|
||||
private readonly FileSystemTreeMappingConfig _fileSystemTreeMappingConfig;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
public FileSystemTreeMappingConfigTests()
|
||||
{
|
||||
_config = new TypeAdapterConfig();
|
||||
_fileSystemTreeMappingConfig = new FileSystemTreeMappingConfig();
|
||||
_fileSystemTreeMappingConfig.Register(_config);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Register_WhenMappingWindowsRootItemToFileSystemTreeNodeResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
WindowsRootItem windowsRoot = WindowsRootItem.Create("C:", "C:").Value;
|
||||
|
||||
// Act
|
||||
FileSystemTreeNodeResponse result = windowsRoot.Adapt<FileSystemTreeNodeResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(windowsRoot.Id.Path);
|
||||
result.Name.Should().Be(windowsRoot.Name);
|
||||
result.ItemType.Should().Be(FileSystemItemType.Root);
|
||||
result.IsExpanded.Should().BeFalse();
|
||||
result.Children.Should().NotBeNull();
|
||||
result.Children.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingFileSystemTreeNodeResponseToWindowsRootItem_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemTreeNodeResponse response = new()
|
||||
{
|
||||
Path = "D:",
|
||||
Name = "D:",
|
||||
ItemType = FileSystemItemType.Root,
|
||||
IsExpanded = false
|
||||
};
|
||||
|
||||
// Act
|
||||
WindowsRootItem result = response.Adapt<WindowsRootItem>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Id.Path.Should().Be(response.Path);
|
||||
result.Name.Should().Be(response.Name);
|
||||
result.Type.Should().Be(FileSystemItemType.Root);
|
||||
result.Status.Should().Be(FileSystemItemStatus.Accessible);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingUnixRootItemToFileSystemTreeNodeResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
UnixRootItem unixRoot = UnixRootItem.Create().Value;
|
||||
|
||||
// Act
|
||||
FileSystemTreeNodeResponse result = unixRoot.Adapt<FileSystemTreeNodeResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(unixRoot.Id.Path);
|
||||
result.Name.Should().Be(unixRoot.Name);
|
||||
result.ItemType.Should().Be(FileSystemItemType.Root);
|
||||
result.IsExpanded.Should().BeFalse();
|
||||
result.ChildrenLoaded.Should().BeFalse();
|
||||
result.Children.Should().NotBeNull();
|
||||
result.Children.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingFileSystemTreeNodeResponseToUnixRootItem_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemTreeNodeResponse response = new()
|
||||
{
|
||||
Path = "/",
|
||||
Name = "/",
|
||||
ItemType = FileSystemItemType.Root,
|
||||
IsExpanded = false
|
||||
};
|
||||
|
||||
// Act
|
||||
UnixRootItem result = response.Adapt<UnixRootItem>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Id.Path.Should().Be("/");
|
||||
result.Name.Should().Be("/");
|
||||
result.Type.Should().Be(FileSystemItemType.Root);
|
||||
result.Status.Should().Be(FileSystemItemStatus.Accessible);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingDirectoryToFileSystemTreeNodeResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
DirectoryFixture directoryFixture = new();
|
||||
Directory directory = directoryFixture.CreateDirectory();
|
||||
directory.AddItem(directoryFixture.CreateDirectory());
|
||||
directory.AddItem(directoryFixture.CreateDirectory());
|
||||
|
||||
// Act
|
||||
FileSystemTreeNodeResponse result = directory.Adapt<FileSystemTreeNodeResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Name.Should().Be(directory.Name);
|
||||
result.Path.Should().Be(directory.Id.Path);
|
||||
result.ItemType.Should().Be(directory.Type);
|
||||
result.IsExpanded.Should().BeFalse();
|
||||
result.ChildrenLoaded.Should().BeFalse();
|
||||
result.Children.Should().NotBeNull();
|
||||
result.Children.Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingFileToFileSystemTreeNodeResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileFixture fileFixture = new();
|
||||
File file = fileFixture.CreateFile();
|
||||
|
||||
// Act
|
||||
FileSystemTreeNodeResponse result = file.Adapt<FileSystemTreeNodeResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Name.Should().Be(file.Name);
|
||||
result.Path.Should().Be(file.Id.Path);
|
||||
result.ItemType.Should().Be(file.Type);
|
||||
result.IsExpanded.Should().BeFalse();
|
||||
result.ChildrenLoaded.Should().BeFalse();
|
||||
result.Children.Should().BeEmpty();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Mapping.FileManagement;
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
|
||||
using Mapster;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PathSegmentMappingConfig"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PathSegmentMappingConfigTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly TypeAdapterConfig _config;
|
||||
private readonly PathSegmentMappingConfig _pathSegmentMappingConfig;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
public PathSegmentMappingConfigTests()
|
||||
{
|
||||
_config = new TypeAdapterConfig();
|
||||
_pathSegmentMappingConfig = new PathSegmentMappingConfig();
|
||||
_pathSegmentMappingConfig.Register(_config);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Register_WhenMappingPathSegmentToPathSegmentResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
ErrorOr<PathSegment> createResult = PathSegment.Create("TestSegment", true, false);
|
||||
createResult.IsError.Should().BeFalse();
|
||||
PathSegment pathSegment = createResult.Value;
|
||||
|
||||
// Act
|
||||
PathSegmentResponse result = pathSegment.Adapt<PathSegmentResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(pathSegment.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Folder", true, false)]
|
||||
[InlineData("File.txt", false, false)]
|
||||
[InlineData("C:", false, true)]
|
||||
public void Register_WhenMappingDifferentPathSegmentTypes_ShouldMapCorrectly(string name, bool isDirectory, bool isDrive)
|
||||
{
|
||||
// Arrange
|
||||
ErrorOr<PathSegment> createResult = PathSegment.Create(name, isDirectory, isDrive);
|
||||
createResult.IsError.Should().BeFalse();
|
||||
PathSegment pathSegment = createResult.Value;
|
||||
|
||||
// Act
|
||||
PathSegmentResponse result = pathSegment.Adapt<PathSegmentResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Path.Should().Be(pathSegment.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingMultiplePathSegments_ShouldMapAllCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
List<PathSegment> pathSegments =
|
||||
[
|
||||
PathSegment.Create("C:", false, true).Value,
|
||||
PathSegment.Create("Users", true, false).Value,
|
||||
PathSegment.Create("Documents", true, false).Value,
|
||||
PathSegment.Create("file.txt", false, false).Value
|
||||
];
|
||||
|
||||
// Act
|
||||
List<PathSegmentResponse> results = pathSegments.Adapt<List<PathSegmentResponse>>(_config);
|
||||
|
||||
// Assert
|
||||
results.Should().NotBeNull();
|
||||
results.Should().HaveCount(pathSegments.Count);
|
||||
for (int i = 0; i < pathSegments.Count; i++)
|
||||
{
|
||||
results[i].Path.Should().Be(pathSegments[i].Name);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Mapping.FileManagement;
|
||||
using Lumina.Contracts.Enums.PhotoLibrary;
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
|
||||
using Mapster;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ThumbnailMappingConfig"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ThumbnailMappingConfigTests
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly TypeAdapterConfig _config;
|
||||
private readonly ThumbnailMappingConfig _thumbnailMappingConfig;
|
||||
private readonly Faker _faker;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
public ThumbnailMappingConfigTests()
|
||||
{
|
||||
_config = new TypeAdapterConfig();
|
||||
_thumbnailMappingConfig = new ThumbnailMappingConfig();
|
||||
_thumbnailMappingConfig.Register(_config);
|
||||
_faker = new Faker();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public void Register_WhenMappingThumbnailToThumbnailResponse_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
ImageType imageType = _faker.PickRandom<ImageType>();
|
||||
byte[] bytes = _faker.Random.Bytes(100);
|
||||
Thumbnail thumbnail = new(imageType, bytes);
|
||||
|
||||
// Act
|
||||
ThumbnailResponse result = thumbnail.Adapt<ThumbnailResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Type.Should().Be(thumbnail.Type);
|
||||
result.Bytes.Should().BeEquivalentTo(thumbnail.Bytes);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ImageType.JPEG)]
|
||||
[InlineData(ImageType.PNG)]
|
||||
[InlineData(ImageType.GIF)]
|
||||
public void Register_WhenMappingDifferentImageTypes_ShouldMapCorrectly(ImageType imageType)
|
||||
{
|
||||
// Arrange
|
||||
byte[] bytes = _faker.Random.Bytes(100);
|
||||
Thumbnail thumbnail = new(imageType, bytes);
|
||||
|
||||
// Act
|
||||
ThumbnailResponse result = thumbnail.Adapt<ThumbnailResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Type.Should().Be(imageType);
|
||||
result.Bytes.Should().BeEquivalentTo(bytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingThumbnailWithEmptyBytes_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
ImageType imageType = _faker.PickRandom<ImageType>();
|
||||
byte[] emptyBytes = [];
|
||||
Thumbnail thumbnail = new(imageType, emptyBytes);
|
||||
|
||||
// Act
|
||||
ThumbnailResponse result = thumbnail.Adapt<ThumbnailResponse>(_config);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Type.Should().Be(imageType);
|
||||
result.Bytes.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_WhenMappingMultipleThumbnails_ShouldMapAllCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
List<Thumbnail> thumbnails =
|
||||
[
|
||||
new Thumbnail(ImageType.JPEG, _faker.Random.Bytes(100)),
|
||||
new Thumbnail(ImageType.PNG, _faker.Random.Bytes(200)),
|
||||
new Thumbnail(ImageType.GIF, _faker.Random.Bytes(150))
|
||||
];
|
||||
|
||||
// Act
|
||||
List<ThumbnailResponse> results = thumbnails.Adapt<List<ThumbnailResponse>>(_config);
|
||||
|
||||
// Assert
|
||||
results.Should().NotBeNull();
|
||||
results.Should().HaveCount(thumbnails.Count);
|
||||
for (int i = 0; i < thumbnails.Count; i++)
|
||||
{
|
||||
results[i].Type.Should().Be(thumbnails[i].Type);
|
||||
results[i].Bytes.Should().BeEquivalentTo(thumbnails[i].Bytes);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user