Implements directory browser dialog, part 1: file system treeview

This commit is contained in:
BinaryAlley committed 2024-09-15 13:07:53 +03:00
1 parent 4a53348013
commit 4e4ecbbfca
318 files changed
+6012 -1184

No files matched your search

@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture.Kernel;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture.Kernel;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -5,17 +5,20 @@ using ErrorOr;
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Common;
using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands.Fixtures;
using Lumina.Domain.Common.Enums;
using Lumina.Contracts.Enums.BookLibrary;
using Lumina.Contracts.Models.Common;
using Lumina.Contracts.Models.WrittenContentLibrary.BookLibrary;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate;
using MapsterMapper;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
@@ -62,10 +65,10 @@ public class AddBookCommandHandlerTests
{
// Arrange
var bookCommand = _commandBookFixture.CreateCommandBook();
var bookDto = _fixture.Create<BookDto>();
var bookModel = _fixture.Create<BookModel>();
_mockMapper.Map<BookDto>(Arg.Any<Book>()).Returns(bookDto);
_mockBookRepository.InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>())
_mockMapper.Map<BookModel>(Arg.Any<Book>()).Returns(bookModel);
_mockBookRepository.InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
// Act
@@ -74,7 +77,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeOfType<Book>();
await _mockBookRepository.Received(1).InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.Received(1).InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -91,7 +94,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.TitleCannotBeEmpty.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -100,10 +103,10 @@ public class AddBookCommandHandlerTests
{
// Arrange
var bookCommand = _commandBookFixture.CreateCommandBook();
var bookDto = _fixture.Create<BookDto>();
var bookModel = _fixture.Create<BookModel>();
_mockMapper.Map<BookDto>(Arg.Any<Book>()).Returns(bookDto);
_mockBookRepository.InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>())
_mockMapper.Map<BookModel>(Arg.Any<Book>()).Returns(bookModel);
_mockBookRepository.InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>())
.Returns(Errors.WrittenContent.BookAlreadyExists);
// Act
@@ -112,7 +115,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(Errors.WrittenContent.BookAlreadyExists);
await _mockBookRepository.Received(1).InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.Received(1).InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -121,7 +124,7 @@ public class AddBookCommandHandlerTests
{
// Arrange
var bookCommand = _commandBookFixture.CreateCommandBook();
bookCommand = bookCommand with { ISBNs = [new IsbnDto("invalid", IsbnFormat.Isbn13)] };
bookCommand = bookCommand with { ISBNs = [new IsbnModel("invalid", IsbnFormat.Isbn13)] };
// Act
var result = await _sut.Handle(bookCommand, CancellationToken.None);
@@ -129,7 +132,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.WrittenContent.InvalidIsbn13Format.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -138,7 +141,7 @@ public class AddBookCommandHandlerTests
{
// Arrange
var bookCommand = _commandBookFixture.CreateCommandBook();
bookCommand = bookCommand with { Ratings = [new BookRatingDto(-1, 5, null, null)] };
bookCommand = bookCommand with { Ratings = [new BookRatingModel(-1, 5, null, null)] };
// Act
var result = await _sut.Handle(bookCommand, CancellationToken.None);
@@ -146,7 +149,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.RatingValueMustBePositive.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -159,7 +162,7 @@ public class AddBookCommandHandlerTests
{
Metadata = bookCommand.Metadata with
{
Genres = [new GenreDto("")]
Genres = [new GenreModel("")]
}
};
@@ -169,7 +172,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.GenreNameCannotBeEmpty.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -182,7 +185,7 @@ public class AddBookCommandHandlerTests
{
Metadata = bookCommand.Metadata with
{
Tags = [new TagDto("")]
Tags = [new TagModel("")]
}
};
@@ -192,7 +195,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.TagNameCannotBeEmpty.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -205,7 +208,7 @@ public class AddBookCommandHandlerTests
{
Metadata = bookCommand.Metadata with
{
ReleaseInfo = new ReleaseInfoDto(
ReleaseInfo = new ReleaseInfoModel(
OriginalReleaseDate: new DateOnly(2025, 1, 1),
ReReleaseDate: new DateOnly(2024, 1, 1),
OriginalReleaseYear: null,
@@ -222,7 +225,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.ReReleaseDateCannotBeEarlierThanOriginalReleaseDate.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -235,7 +238,7 @@ public class AddBookCommandHandlerTests
{
Metadata = bookCommand.Metadata with
{
Language = new LanguageInfoDto("", "English", null)
Language = new LanguageInfoModel("", "English", null)
}
};
@@ -245,7 +248,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.LanguageCodeCannotBeEmpty.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -258,7 +261,7 @@ public class AddBookCommandHandlerTests
{
Metadata = bookCommand.Metadata with
{
OriginalLanguage = new LanguageInfoDto("", "English", null)
OriginalLanguage = new LanguageInfoModel("", "English", null)
}
};
@@ -268,7 +271,7 @@ public class AddBookCommandHandlerTests
// Assert
result.IsError.Should().BeTrue();
result.Errors.Should().Contain(e => e.Code == Errors.Metadata.LanguageCodeCannotBeEmpty.Code);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookDto>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookModel>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
#endregion
@@ -1,13 +1,13 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using FluentValidation.TestHelper;
using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
using Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands.Fixtures;
using System.Diagnostics.CodeAnalysis;
using Lumina.Contracts.Enums.BookLibrary;
using Lumina.Domain.Common.Errors;
using Bogus;
using Lumina.Domain.Common.Enums;
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Common;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
@@ -1,13 +1,17 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using Bogus;
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Common;
using Lumina.Application.Common.Models.Contributors;
using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Enums;
using Lumina.Contracts.Enums.BookLibrary;
using Lumina.Contracts.Models.Common;
using Lumina.Contracts.Models.MediaContributors;
using Lumina.Contracts.Models.WrittenContentLibrary;
using Lumina.Contracts.Models.WrittenContentLibrary.BookLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands.Fixtures;
@@ -47,8 +51,8 @@ public class AddBookCommandFixture
var releaseYear = _random.Next(2000, 2010);
var reReleaseYear = _random.Next(2010, 2020);
var releaseInfo = new Faker<ReleaseInfoDto>()
.CustomInstantiator(f => new ReleaseInfoDto(
var releaseInfo = new Faker<ReleaseInfoModel>()
.CustomInstantiator(f => new ReleaseInfoModel(
default,
default,
default,
@@ -64,42 +68,42 @@ public class AddBookCommandFixture
.RuleFor(x => x.ReleaseVersion, f => f.Random.String2(f.Random.Number(1, 50)))
.Generate();
var genre = new Faker<GenreDto>()
.CustomInstantiator(f => new GenreDto(
var genre = new Faker<GenreModel>()
.CustomInstantiator(f => new GenreModel(
default!
))
.RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50)));
var tag = new Faker<TagDto>()
.CustomInstantiator(f => new TagDto(
var tag = new Faker<TagModel>()
.CustomInstantiator(f => new TagModel(
default!
))
.RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50)));
var mediaContributorRole = new Faker<MediaContributorRoleDto>()
.CustomInstantiator(f => new MediaContributorRoleDto(
var mediaContributorRole = new Faker<MediaContributorRoleModel>()
.CustomInstantiator(f => new MediaContributorRoleModel(
default!,
default!
))
.RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50)))
.RuleFor(e => e.Category, f => f.Random.String2(f.Random.Number(1, 50)));
var mediaContributorName = new Faker<MediaContributorNameDto>()
.CustomInstantiator(f => new MediaContributorNameDto(
var mediaContributorName = new Faker<MediaContributorNameModel>()
.CustomInstantiator(f => new MediaContributorNameModel(
default!,
default!
))
.RuleFor(e => e.DisplayName, f => f.Random.String2(f.Random.Number(1, 50)))
.RuleFor(e => e.LegalName, f => f.Random.String2(f.Random.Number(1, 50)));
var mediaContributor = new Faker<MediaContributorDto>()
.CustomInstantiator(f => new MediaContributorDto(
var mediaContributor = new Faker<MediaContributorModel>()
.CustomInstantiator(f => new MediaContributorModel(
default!,
default!
))
.RuleFor(e => e.Name, mediaContributorName)
.RuleFor(e => e.Role, mediaContributorRole);
var rating = new Faker<BookRatingDto>()
.CustomInstantiator(f => new BookRatingDto(
var rating = new Faker<BookRatingModel>()
.CustomInstantiator(f => new BookRatingModel(
default,
default,
default,
@@ -110,8 +114,8 @@ public class AddBookCommandFixture
.RuleFor(e => e.Source, _fixture.Create<BookRatingSource>())
.RuleFor(e => e.VoteCount, _random.Next(1, 1000));
var language = new Faker<LanguageInfoDto>()
.CustomInstantiator(f => new LanguageInfoDto(
var language = new Faker<LanguageInfoModel>()
.CustomInstantiator(f => new LanguageInfoModel(
default!,
default!,
default
@@ -120,8 +124,8 @@ public class AddBookCommandFixture
.RuleFor(e => e.LanguageCode, f => f.Random.String2(2))
.RuleFor(e => e.NativeName, f => f.Random.String2(f.Random.Number(1, 50)));
var originalLanguage = new Faker<LanguageInfoDto>()
.CustomInstantiator(f => new LanguageInfoDto(
var originalLanguage = new Faker<LanguageInfoModel>()
.CustomInstantiator(f => new LanguageInfoModel(
default!,
default!,
default
@@ -130,8 +134,8 @@ public class AddBookCommandFixture
.RuleFor(e => e.LanguageCode, f => f.Random.String2(2))
.RuleFor(e => e.NativeName, f => f.Random.String2(f.Random.Number(1, 50)));
var metadata = new Faker<WrittenContentMetadataDto>()
.CustomInstantiator(f => new WrittenContentMetadataDto(
var metadata = new Faker<WrittenContentMetadataModel>()
.CustomInstantiator(f => new WrittenContentMetadataModel(
default!,
default,
default,
@@ -154,8 +158,8 @@ public class AddBookCommandFixture
.RuleFor(x => x.Publisher, f => f.Random.String2(f.Random.Number(1, 100)))
.RuleFor(x => x.PageCount, _random.Next(100, 300));
var isbn = new Faker<IsbnDto>()
.CustomInstantiator(f => new IsbnDto(
var isbn = new Faker<IsbnModel>()
.CustomInstantiator(f => new IsbnModel(
default!,
default
))
@@ -277,7 +281,7 @@ public class AddBookCommandFixture
var releaseYear = _random.Next(2000, 2010);
var reReleaseYear = _random.Next(2010, 2020);
_fixture.Register(() => new ReleaseInfoDto(
_fixture.Register(() => new ReleaseInfoModel(
_faker.DateOnlyBetween(new DateOnly(releaseYear, 1, 1), new DateOnly(releaseYear, 12, 31)),
releaseYear,
_faker.DateOnlyBetween(new DateOnly(reReleaseYear, 1, 1), new DateOnly(reReleaseYear, 12, 31)),
@@ -285,53 +289,53 @@ public class AddBookCommandFixture
_fixture.Create<string>()[2..],
_fixture.Create<string?>()));
_fixture.Register(() => new GenreDto(
_fixture.Register(() => new GenreModel(
_fixture.Create<string>()
));
_fixture.Register(() => new TagDto(
_fixture.Register(() => new TagModel(
_fixture.Create<string>()
));
_fixture.Register(() => new LanguageInfoDto(
_fixture.Register(() => new LanguageInfoModel(
_fixture.Create<string>(),
_fixture.Create<string>(),
_fixture.Create<string?>()
));
_fixture.Register(() => new IsbnDto(
_fixture.Register(() => new IsbnModel(
_fixture.Create<string>(),
_fixture.Create<IsbnFormat>()
));
_fixture.Register(() => new BookRatingDto(
_fixture.Register(() => new BookRatingModel(
_fixture.Create<decimal>(),
_fixture.Create<decimal>(),
_fixture.Create<BookRatingSource?>(),
_fixture.Create<int?>()
));
_fixture.Register(() => new WrittenContentMetadataDto(
_fixture.Register(() => new WrittenContentMetadataModel(
_fixture.Create<string>()[255..],
LimitStringLength(_fixture.Create<string?>(), 255),
LimitStringLength(_fixture.Create<string?>(), 2000),
_fixture.Create<ReleaseInfoDto>(),
_fixture.Create<List<GenreDto>>(),
_fixture.Create<List<TagDto>>(),
_fixture.Create<LanguageInfoDto?>(),
_fixture.Create<LanguageInfoDto?>(),
_fixture.Create<ReleaseInfoModel>(),
_fixture.Create<List<GenreModel>>(),
_fixture.Create<List<TagModel>>(),
_fixture.Create<LanguageInfoModel?>(),
_fixture.Create<LanguageInfoModel?>(),
_fixture.Create<string?>(),
_fixture.Create<int?>()
));
_fixture.Register(() => new BookSeriesDto(
_fixture.Register(() => new BookSeriesModel(
_fixture.Create<string>()
));
_fixture.Register(() => new MediaContributorRoleDto(
_fixture.Register(() => new MediaContributorRoleModel(
_fixture.Create<string>(),
_fixture.Create<string>()
));
_fixture.Register(() => new MediaContributorNameDto(
_fixture.Register(() => new MediaContributorNameModel(
_fixture.Create<string>(),
_fixture.Create<string?>()
));
_fixture.Register(() => new MediaContributorDto(
_fixture.Create<MediaContributorNameDto>(),
_fixture.Create<MediaContributorRoleDto>()
_fixture.Register(() => new MediaContributorModel(
_fixture.Create<MediaContributorNameModel>(),
_fixture.Create<MediaContributorRoleModel>()
));
}
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
@@ -11,6 +11,8 @@ using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
#endregion
namespace Lumina.DataAccess.UnitTests.Common.DependencyInjection;
@@ -41,7 +43,7 @@ public class DataAccessLayerServicesTests
{
// Arrange
var services = new ServiceCollection();
var basePath = Path.GetTempPath(); // Use a real existing path
var basePath = Path.GetTempPath(); // use a real existing path
// Act
DataAccessLayerServices.AddDataAccessLayerServices(services);
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture.Kernel;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture.Kernel;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -2,15 +2,19 @@
using EntityFrameworkCore.Testing.NSubstitute;
using ErrorOr;
using FluentAssertions;
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Common;
using Lumina.Contracts.Enums.BookLibrary;
using Lumina.Contracts.Models.Common;
using Lumina.Contracts.Models.WrittenContentLibrary.BookLibrary;
using Lumina.DataAccess.Core.Repositories.Books;
using Lumina.DataAccess.Core.UoW;
using Lumina.DataAccess.UnitTests.Core.Repositories.Books.Fixtures;
using Lumina.Domain.Common.Enums;
using Lumina.Domain.Common.Errors;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books;
@@ -24,7 +28,7 @@ public class BookRepositoryTests
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly LuminaDbContext _mockContext;
private readonly BookRepository _sut;
private readonly BookDtoFixture _bookDtoFixture;
private readonly BookModelFixture _bookModelFixture;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -35,7 +39,7 @@ public class BookRepositoryTests
{
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
_sut = new BookRepository(_mockContext);
_bookDtoFixture = new BookDtoFixture();
_bookModelFixture = new BookModelFixture();
}
#endregion
@@ -44,18 +48,18 @@ public class BookRepositoryTests
public async Task InsertAsync_WhenBookDoesNotExist_ShouldAddBookToContextAndReturnCreated()
{
// Arrange
var bookDto = _bookDtoFixture.CreateBookDto();
var bookModel = _bookModelFixture.CreateBookModel();
// Act
var result = await _sut.InsertAsync(bookDto, CancellationToken.None);
var result = await _sut.InsertAsync(bookModel, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().Be(Result.Created);
// Check if the book was added to the context's ChangeTracker
var addedBook = _mockContext.ChangeTracker.Entries<BookDto>()
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == bookDto.Id);
var addedBook = _mockContext.ChangeTracker.Entries<BookModel>()
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == bookModel.Id);
addedBook.Should().NotBeNull();
}
@@ -63,40 +67,40 @@ public class BookRepositoryTests
public async Task InsertAsync_WhenBookAlreadyExists_ShouldReturnError()
{
// Arrange
var bookDto = _bookDtoFixture.CreateBookDto();
var bookModel = _bookModelFixture.CreateBookModel();
_mockContext.Books.Add(bookDto);
_mockContext.Books.Add(bookModel);
await _mockContext.SaveChangesAsync();
// Act
var result = await _sut.InsertAsync(bookDto, CancellationToken.None);
var result = await _sut.InsertAsync(bookModel, CancellationToken.None);
// Assert
result.IsError.Should().BeTrue();
result.FirstError.Should().Be(Errors.WrittenContent.BookAlreadyExists);
_mockContext.ChangeTracker.Entries<BookDto>().Should().HaveCount(1); // Only the existing book should be in the context}
_mockContext.ChangeTracker.Entries<BookModel>().Should().HaveCount(1); // Only the existing book should be in the context}
}
[Fact]
public async Task InsertAsync_WhenExistingTagsFound_ShouldReplaceTagsWithExistingOnes()
{
// Arrange
var existingTag = new TagDto("Existing");
_mockContext.Set<TagDto>().Add(existingTag);
var existingTag = new TagModel("Existing");
_mockContext.Set<TagModel>().Add(existingTag);
await _mockContext.SaveChangesAsync();
var bookDto = _bookDtoFixture.CreateBookDto();
bookDto.Tags = [new("Existing"), new("New")];
var bookModel = _bookModelFixture.CreateBookModel();
bookModel.Tags = [new("Existing"), new("New")];
// Act
var result = await _sut.InsertAsync(bookDto, CancellationToken.None);
var result = await _sut.InsertAsync(bookModel, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().Be(Result.Created);
var addedBook = _mockContext.ChangeTracker.Entries<BookDto>()
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == bookDto.Id);
var addedBook = _mockContext.ChangeTracker.Entries<BookModel>()
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == bookModel.Id);
addedBook.Should().NotBeNull();
var addedBookEntity = addedBook!.Entity;
addedBookEntity.Tags.Should().HaveCount(2);
@@ -108,22 +112,22 @@ public class BookRepositoryTests
public async Task InsertAsync_WhenExistingGenresFound_ShouldReplaceGenresWithExistingOnes()
{
// Arrange
var existingGenre = new GenreDto("Existing");
_mockContext.Set<GenreDto>().Add(existingGenre);
var existingGenre = new GenreModel("Existing");
_mockContext.Set<GenreModel>().Add(existingGenre);
await _mockContext.SaveChangesAsync();
var bookDto = _bookDtoFixture.CreateBookDto();
bookDto.Genres = [new("Existing"), new("New")];
var bookModel = _bookModelFixture.CreateBookModel();
bookModel.Genres = [new("Existing"), new("New")];
// Act
var result = await _sut.InsertAsync(bookDto, CancellationToken.None);
var result = await _sut.InsertAsync(bookModel, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().Be(Result.Created);
var addedBook = _mockContext.ChangeTracker.Entries<BookDto>()
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == bookDto.Id);
var addedBook = _mockContext.ChangeTracker.Entries<BookModel>()
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == bookModel.Id);
addedBook.Should().NotBeNull();
var addedBookEntity = addedBook!.Entity;
addedBookEntity.Genres.Should().HaveCount(2);
@@ -135,11 +139,11 @@ public class BookRepositoryTests
public async Task GetAllAsync_WhenCalled_ShouldReturnAllBooks()
{
// Arrange
var books = new List<BookDto>
var books = new List<BookModel>
{
_bookDtoFixture.CreateBookDto(),
_bookDtoFixture.CreateBookDto(),
_bookDtoFixture.CreateBookDto()
_bookModelFixture.CreateBookModel(),
_bookModelFixture.CreateBookModel(),
_bookModelFixture.CreateBookModel()
};
_mockContext.Books.AddRange(books);
await _mockContext.SaveChangesAsync();
@@ -170,10 +174,10 @@ public class BookRepositoryTests
public async Task GetAllAsync_WhenCalled_ShouldIncludeRelatedEntities()
{
// Arrange
var book = _bookDtoFixture.CreateBookDto();
book.Tags = [new TagDto("Tag1"), new TagDto("Tag2")];
book.Genres = [new GenreDto("Genre1"), new GenreDto("Genre2")];
book.ISBNs = [new IsbnDto("1234567890", IsbnFormat.Isbn10), new IsbnDto("1234567890123", IsbnFormat.Isbn13)];
var book = _bookModelFixture.CreateBookModel();
book.Tags = [new TagModel("Tag1"), new TagModel("Tag2")];
book.Genres = [new GenreModel("Genre1"), new GenreModel("Genre2")];
book.ISBNs = [new IsbnModel("1234567890", IsbnFormat.Isbn10), new IsbnModel("1234567890123", IsbnFormat.Isbn13)];
_mockContext.Books.Add(book);
await _mockContext.SaveChangesAsync();
@@ -1,23 +1,23 @@
using AutoFixture;
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using Bogus;
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Common;
using Lumina.Contracts.Enums.BookLibrary;
using Lumina.Contracts.Models.Common;
using Lumina.Contracts.Models.WrittenContentLibrary.BookLibrary;
using Lumina.DataAccess.UnitTests.Common.Setup;
using Lumina.Domain.Common.Enums;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books.Fixtures;
/// <summary>
/// Fixture class for the <see cref="BookDto"/> class.
/// Fixture class for the <see cref="BookModel"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class BookDtoFixture
public class BookModelFixture
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly Fixture _fixture;
@@ -27,9 +27,9 @@ public class BookDtoFixture
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="BookDtoFixture"/> class.
/// Initializes a new instance of the <see cref="BookModelFixture"/> class.
/// </summary>
public BookDtoFixture()
public BookModelFixture()
{
_fixture = new Fixture();
_fixture.Customizations.Add(new DateOnlySpecimenBuilder());
@@ -41,15 +41,15 @@ public class BookDtoFixture
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid BookDto.
/// Creates a random valid BookModel.
/// </summary>
/// <returns>The created BookDto.</returns>
public BookDto CreateBookDto()
/// <returns>The created BookModel.</returns>
public BookModel CreateBookModel()
{
var releaseYear = _random.Next(2000, 2010);
var reReleaseYear = _random.Next(2010, 2020);
return new Faker<BookDto>()
return new Faker<BookModel>()
.RuleFor(x => x.Id, f => f.Random.Guid())
.RuleFor(x => x.Title, f => f.Random.String2(f.Random.Number(1, 255)))
.RuleFor(x => x.OriginalTitle, f => f.Random.String2(f.Random.Number(1, 255)))
@@ -66,8 +66,8 @@ public class BookDtoFixture
.RuleFor(x => x.OriginalLanguageCode, f => f.Random.String2(2))
.RuleFor(x => x.OriginalLanguageName, f => f.Random.String2(f.Random.Number(1, 50)))
.RuleFor(x => x.OriginalLanguageNativeName, f => f.Random.String2(f.Random.Number(1, 50)))
.RuleFor(x => x.Tags, f => new HashSet<TagDto>(CreateTags(f.Random.Number(1, 5))))
.RuleFor(x => x.Genres, f => new HashSet<GenreDto>(CreateGenres(f.Random.Number(1, 5))))
.RuleFor(x => x.Tags, f => new HashSet<TagModel>(CreateTags(f.Random.Number(1, 5))))
.RuleFor(x => x.Genres, f => new HashSet<GenreModel>(CreateGenres(f.Random.Number(1, 5))))
.RuleFor(x => x.Publisher, f => f.Random.String2(f.Random.Number(1, 100)))
.RuleFor(x => x.PageCount, _random.Next(100, 300))
.RuleFor(x => x.Format, f => f.PickRandom<BookFormat>().ToString())
@@ -89,17 +89,17 @@ public class BookDtoFixture
.Generate();
}
private List<TagDto> CreateTags(int count)
private List<TagModel> CreateTags(int count)
{
return new Faker<TagDto>()
.CustomInstantiator(f => new TagDto(f.Random.String2(f.Random.Number(1, 50))))
return new Faker<TagModel>()
.CustomInstantiator(f => new TagModel(f.Random.String2(f.Random.Number(1, 50))))
.Generate(count);
}
private List<GenreDto> CreateGenres(int count)
private List<GenreModel> CreateGenres(int count)
{
return new Faker<GenreDto>()
.CustomInstantiator(f => new GenreDto(f.Random.String2(f.Random.Number(1, 50))))
return new Faker<GenreModel>()
.CustomInstantiator(f => new GenreModel(f.Random.String2(f.Random.Number(1, 50))))
.Generate(count);
}
@@ -143,10 +143,10 @@ public class BookDtoFixture
.ToArray());
}
private List<IsbnDto> CreateIsbns(int count)
private List<IsbnModel> CreateIsbns(int count)
{
return new Faker<IsbnDto>()
.CustomInstantiator(f => new IsbnDto(CreateIsbn(f), f.PickRandom<IsbnFormat>()))
return new Faker<IsbnModel>()
.CustomInstantiator(f => new IsbnModel(CreateIsbn(f), f.PickRandom<IsbnFormat>()))
.Generate(count);
}
@@ -180,10 +180,10 @@ public class BookDtoFixture
}
}
private List<BookRatingDto> CreateBookRatings(int count)
private List<BookRatingModel> CreateBookRatings(int count)
{
return new Faker<BookRatingDto>()
.CustomInstantiator(f => new BookRatingDto(
return new Faker<BookRatingModel>()
.CustomInstantiator(f => new BookRatingModel(
f.Random.Decimal(1, 5),
5,
f.PickRandom<BookRatingSource>(),
@@ -194,10 +194,10 @@ public class BookDtoFixture
private void ConfigureCustomTypes()
{
_fixture.Register(() => new TagDto(_fixture.Create<string>()));
_fixture.Register(() => new GenreDto(_fixture.Create<string>()));
_fixture.Register(() => new IsbnDto(_fixture.Create<string>(), _fixture.Create<IsbnFormat>()));
_fixture.Register(() => new BookRatingDto(
_fixture.Register(() => new TagModel(_fixture.Create<string>()));
_fixture.Register(() => new GenreModel(_fixture.Create<string>()));
_fixture.Register(() => new IsbnModel(_fixture.Create<string>(), _fixture.Create<IsbnFormat>()));
_fixture.Register(() => new BookRatingModel(
_fixture.Create<decimal>(),
_fixture.Create<decimal>(),
_fixture.Create<BookRatingSource>(),
@@ -205,4 +205,4 @@ public class BookDtoFixture
));
}
#endregion
}
}
@@ -5,6 +5,7 @@ using FluentAssertions;
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
@@ -4,6 +4,7 @@ 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;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -10,7 +10,10 @@ using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.UoW;
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics.CodeAnalysis;
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using FluentAssertions;
@@ -10,6 +9,9 @@ using Lumina.Infrastructure.UnitTests.Common.Utilities.Fixtures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Infrastructure.UnitTests.Common.Utilities;
@@ -4,11 +4,13 @@ using AutoFixture.AutoNSubstitute;
using FluentAssertions;
using FluentValidation;
using FluentValidation.Results;
using Lumina.Infrastructure.Common.Validation;
using Lumina.Infrastructure.UnitTests.Common.Validation.Fixtures;
using Microsoft.Extensions.Options;
using NSubstitute;
using Lumina.Infrastructure.Common.Validation;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Lumina.Infrastructure.UnitTests.Common.Validation.Fixtures;
#endregion
namespace Lumina.Infrastructure.UnitTests.Common.Validation;
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>