mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Adds Book repository unit tests
This commit is contained in:
1 parent
a3e956a165
commit
1ee12d74a4
11 files changed
+649
-12
No files matched your search
@@ -0,0 +1,39 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Contains extension methods for the <see cref="Faker"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static class BogusExtensions
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly"/> random data generation support to the <see cref="Faker"/> class.
|
||||
/// </summary>
|
||||
/// <param name="faker">The faker instance to extend.</param>
|
||||
/// <returns>A random<see cref="DateOnly"/> instance.</returns>
|
||||
public static DateOnly DateOnly(this Faker faker)
|
||||
{
|
||||
return System.DateOnly.FromDateTime(faker.Date.Past());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly"/> random data generation within a specified interval support to the <see cref="Faker"/> class.
|
||||
/// </summary>
|
||||
/// <param name="faker">The faker instance to extend.</param>
|
||||
/// <param name="start">The start of the interval.</param>
|
||||
/// <param name="end">The end of the interval.</param>
|
||||
/// <returns>A random<see cref="DateOnly"/> instance.</returns>
|
||||
public static DateOnly DateOnlyBetween(this Faker faker, DateOnly start, DateOnly end)
|
||||
{
|
||||
var startDateTime = start.ToDateTime(TimeOnly.MinValue);
|
||||
var endDateTime = end.ToDateTime(TimeOnly.MinValue);
|
||||
return System.DateOnly.FromDateTime(faker.Date.Between(startDateTime, endDateTime));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture.Kernel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a specimen builder for the <see cref="DateOnly"/> type.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DateOnlySpecimenBuilder : ISpecimenBuilder
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly"/> support to the AutoFixture library.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to process.</param>
|
||||
/// <param name="context">The context within which the specimen is created.</param>
|
||||
/// <returns>A specimen based on the request.</returns>
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (request is Type type && type == typeof(DateOnly))
|
||||
return DateOnly.FromDateTime(DateTime.Now);
|
||||
return new NoSpecimen();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture.Kernel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a specimen builder for the <see cref="DateOnly"/> type.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class NullableDateOnlySpecimenBuilder : ISpecimenBuilder
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly?"/> support to the AutoFixture library.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to process.</param>
|
||||
/// <param name="context">The context within which the specimen is created.</param>
|
||||
/// <returns>A specimen based on the request.</returns>
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (request is Type type && type == typeof(DateOnly?))
|
||||
return (DateOnly?)DateOnly.FromDateTime(DateTime.Now);
|
||||
return new NoSpecimen();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+189
-7
@@ -1,11 +1,193 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.Models.Books;
|
||||
using Lumina.Application.Common.Models.Common;
|
||||
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.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books;
|
||||
|
||||
internal class BookRepositoryTests
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="BookRepository"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookRepositoryTests
|
||||
{
|
||||
}
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly BookRepository _sut;
|
||||
private readonly BookDtoFixture _bookDtoFixture;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public BookRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new BookRepository(_mockContext);
|
||||
_bookDtoFixture = new BookDtoFixture();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenBookDoesNotExist_ShouldAddBookToContextAndReturnCreated()
|
||||
{
|
||||
// Arrange
|
||||
var bookDto = _bookDtoFixture.CreateBookDto();
|
||||
|
||||
// Act
|
||||
var result = await _sut.InsertAsync(bookDto, 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);
|
||||
addedBook.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenBookAlreadyExists_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
var bookDto = _bookDtoFixture.CreateBookDto();
|
||||
|
||||
_mockContext.Books.Add(bookDto);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await _sut.InsertAsync(bookDto, 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}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenExistingTagsFound_ShouldReplaceTagsWithExistingOnes()
|
||||
{
|
||||
// Arrange
|
||||
var existingTag = new TagDto("Existing");
|
||||
_mockContext.Set<TagDto>().Add(existingTag);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
var bookDto = _bookDtoFixture.CreateBookDto();
|
||||
bookDto.Tags = [new("Existing"), new("New")];
|
||||
|
||||
// Act
|
||||
var result = await _sut.InsertAsync(bookDto, 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);
|
||||
addedBook.Should().NotBeNull();
|
||||
var addedBookEntity = addedBook!.Entity;
|
||||
addedBookEntity.Tags.Should().HaveCount(2);
|
||||
addedBookEntity.Tags.Should().Contain(t => t.Name == "Existing" && t == existingTag);
|
||||
addedBookEntity.Tags.Should().Contain(t => t.Name == "New" && t != existingTag);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenExistingGenresFound_ShouldReplaceGenresWithExistingOnes()
|
||||
{
|
||||
// Arrange
|
||||
var existingGenre = new GenreDto("Existing");
|
||||
_mockContext.Set<GenreDto>().Add(existingGenre);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
var bookDto = _bookDtoFixture.CreateBookDto();
|
||||
bookDto.Genres = [new("Existing"), new("New")];
|
||||
|
||||
// Act
|
||||
var result = await _sut.InsertAsync(bookDto, 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);
|
||||
addedBook.Should().NotBeNull();
|
||||
var addedBookEntity = addedBook!.Entity;
|
||||
addedBookEntity.Genres.Should().HaveCount(2);
|
||||
addedBookEntity.Genres.Should().Contain(g => g.Name == "Existing" && g == existingGenre);
|
||||
addedBookEntity.Genres.Should().Contain(g => g.Name == "New" && g != existingGenre);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_ShouldReturnAllBooks()
|
||||
{
|
||||
// Arrange
|
||||
var books = new List<BookDto>
|
||||
{
|
||||
_bookDtoFixture.CreateBookDto(),
|
||||
_bookDtoFixture.CreateBookDto(),
|
||||
_bookDtoFixture.CreateBookDto()
|
||||
};
|
||||
_mockContext.Books.AddRange(books);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().HaveCount(3);
|
||||
result.Value.Should().BeEquivalentTo(books);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_WhenNoBooksExist_ShouldReturnEmptyList()
|
||||
{
|
||||
// Act
|
||||
var result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_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)];
|
||||
_mockContext.Books.Add(book);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().HaveCount(1);
|
||||
var retrievedBook = result.Value.First();
|
||||
retrievedBook.Tags.Should().HaveCount(2);
|
||||
retrievedBook.Genres.Should().HaveCount(2);
|
||||
retrievedBook.ISBNs.Should().HaveCount(2);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
using AutoFixture;
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.Models.Books;
|
||||
using Lumina.Application.Common.Models.Common;
|
||||
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;
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="BookDto"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookDtoFixture
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly Fixture _fixture;
|
||||
private readonly Random _random = new();
|
||||
private readonly Faker _faker;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookDtoFixture"/> class.
|
||||
/// </summary>
|
||||
public BookDtoFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
_fixture.Customizations.Add(new DateOnlySpecimenBuilder());
|
||||
_fixture.Customizations.Add(new NullableDateOnlySpecimenBuilder());
|
||||
_faker = new Faker();
|
||||
ConfigureCustomTypes();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Creates a random valid BookDto.
|
||||
/// </summary>
|
||||
/// <returns>The created BookDto.</returns>
|
||||
public BookDto CreateBookDto()
|
||||
{
|
||||
var releaseYear = _random.Next(2000, 2010);
|
||||
var reReleaseYear = _random.Next(2010, 2020);
|
||||
|
||||
return new Faker<BookDto>()
|
||||
.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)))
|
||||
.RuleFor(x => x.Description, f => f.Random.String2(f.Random.Number(1, 2000)))
|
||||
.RuleFor(x => x.OriginalReleaseDate, _faker.DateOnlyBetween(new DateOnly(releaseYear, 1, 1), new DateOnly(releaseYear, 12, 31)))
|
||||
.RuleFor(x => x.OriginalReleaseYear, releaseYear)
|
||||
.RuleFor(x => x.ReReleaseDate, _faker.DateOnlyBetween(new DateOnly(reReleaseYear, 1, 1), new DateOnly(reReleaseYear, 12, 31)))
|
||||
.RuleFor(x => x.ReReleaseYear, reReleaseYear)
|
||||
.RuleFor(x => x.ReleaseCountry, f => f.Random.String2(2))
|
||||
.RuleFor(x => x.ReleaseVersion, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.LanguageCode, f => f.Random.String2(2))
|
||||
.RuleFor(x => x.LanguageName, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.LanguageNativeName, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.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.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())
|
||||
.RuleFor(x => x.Edition, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.VolumeNumber, _random.Next(1, 3))
|
||||
.RuleFor(x => x.ASIN, f => f.Random.String2(10))
|
||||
.RuleFor(x => x.GoodreadsId, _random.Next(100000, 500000).ToString())
|
||||
.RuleFor(x => x.LCCN, CreateLCCN)
|
||||
.RuleFor(x => x.OCLCNumber, CreateOCLCNumber)
|
||||
.RuleFor(x => x.OpenLibraryId, CreateOpenLibraryId)
|
||||
.RuleFor(x => x.LibraryThingId, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.GoogleBooksId, CreateGoogleBooksId)
|
||||
.RuleFor(x => x.BarnesAndNobleId, f => f.Random.String2(10, "0123456789"))
|
||||
.RuleFor(x => x.AppleBooksId, f => $"id{f.Random.Number(1, 999999)}")
|
||||
.RuleFor(x => x.ISBNs, f => CreateIsbns(f.Random.Number(1, 5)))
|
||||
.RuleFor(x => x.Ratings, f => CreateBookRatings(f.Random.Number(1, 5)))
|
||||
.RuleFor(x => x.Created, f => f.Date.Past())
|
||||
.RuleFor(x => x.Updated, f => f.Date.Recent())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
private List<TagDto> CreateTags(int count)
|
||||
{
|
||||
return new Faker<TagDto>()
|
||||
.CustomInstantiator(f => new TagDto(f.Random.String2(f.Random.Number(1, 50))))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
private List<GenreDto> CreateGenres(int count)
|
||||
{
|
||||
return new Faker<GenreDto>()
|
||||
.CustomInstantiator(f => new GenreDto(f.Random.String2(f.Random.Number(1, 50))))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
private string CreateLCCN(Faker f)
|
||||
{
|
||||
var letters = new string(Enumerable.Range(0, f.Random.Number(0, 3))
|
||||
.Select(_ => f.Random.Char('a', 'z'))
|
||||
.ToArray());
|
||||
var digits = f.Random.String2(f.Random.Number(8, 10), "0123456789");
|
||||
return letters + digits;
|
||||
}
|
||||
|
||||
private string CreateOCLCNumber(Faker f)
|
||||
{
|
||||
string[] prefixes = ["ocm", "ocn", "on", "(OCoLC)"];
|
||||
var prefix = f.Random.ArrayElement(prefixes);
|
||||
string number = prefix switch
|
||||
{
|
||||
"ocm" => f.Random.String2(8, "0123456789"),
|
||||
"ocn" => f.Random.String2(f.Random.Number(9, 11), "0123456789"),
|
||||
"on" => f.Random.String2(10, "0123456789"),
|
||||
"(OCoLC)" => f.Random.String2(f.Random.Number(8, 10), "0123456789"),
|
||||
_ => f.Random.String2(f.Random.Number(8, 10), "0123456789")
|
||||
};
|
||||
return prefix + number;
|
||||
}
|
||||
|
||||
private string CreateOpenLibraryId(Faker f)
|
||||
{
|
||||
var firstDigit = f.Random.Number(1, 9);
|
||||
var remainingDigits = f.Random.String2(f.Random.Number(0, 6), "0123456789");
|
||||
var suffix = f.Random.ArrayElement(new[] { 'A', 'M', 'W' });
|
||||
return $"OL{firstDigit}{remainingDigits}{suffix}";
|
||||
}
|
||||
|
||||
private string CreateGoogleBooksId(Faker f)
|
||||
{
|
||||
const string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
return new string(Enumerable.Repeat(validChars, 12)
|
||||
.Select(s => s[f.Random.Number(validChars.Length - 1)])
|
||||
.ToArray());
|
||||
}
|
||||
|
||||
private List<IsbnDto> CreateIsbns(int count)
|
||||
{
|
||||
return new Faker<IsbnDto>()
|
||||
.CustomInstantiator(f => new IsbnDto(CreateIsbn(f), f.PickRandom<IsbnFormat>()))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
private string CreateIsbn(Faker f)
|
||||
{
|
||||
var isIsbn13 = f.Random.Bool();
|
||||
if (isIsbn13)
|
||||
{
|
||||
var prefix = f.Random.Bool() ? "978" : "979";
|
||||
var group = f.Random.Number(0, 99999).ToString().PadLeft(5, '0');
|
||||
var publisher = f.Random.Number(0, 999999).ToString().PadLeft(6, '0');
|
||||
var title = f.Random.Number(0, 99).ToString().PadLeft(2, '0');
|
||||
string isbn = $"{prefix}{group.Substring(0, 1)}{publisher}{title}";
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 12; i++)
|
||||
sum += (i % 2 == 0 ? 1 : 3) * int.Parse(isbn[i].ToString());
|
||||
int checkDigit = (10 - (sum % 10)) % 10;
|
||||
return $"{prefix}-{group.Substring(0, 1)}-{publisher}-{title}-{checkDigit}";
|
||||
}
|
||||
else
|
||||
{
|
||||
var digits = new int[9];
|
||||
for (int i = 0; i < 9; i++)
|
||||
digits[i] = f.Random.Number(0, 9);
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 9; i++)
|
||||
sum += (10 - i) * digits[i];
|
||||
int checkDigit = (11 - (sum % 11)) % 11;
|
||||
string checkChar = checkDigit == 10 ? "X" : checkDigit.ToString();
|
||||
return $"{digits[0]}-{digits[1]}{digits[2]}-{digits[3]}{digits[4]}{digits[5]}{digits[6]}{digits[7]}{digits[8]}-{checkChar}";
|
||||
}
|
||||
}
|
||||
|
||||
private List<BookRatingDto> CreateBookRatings(int count)
|
||||
{
|
||||
return new Faker<BookRatingDto>()
|
||||
.CustomInstantiator(f => new BookRatingDto(
|
||||
f.Random.Decimal(1, 5),
|
||||
5,
|
||||
f.PickRandom<BookRatingSource>(),
|
||||
f.Random.Number(1, 1000)
|
||||
))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
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.Create<decimal>(),
|
||||
_fixture.Create<decimal>(),
|
||||
_fixture.Create<BookRatingSource>(),
|
||||
_fixture.Create<int>()
|
||||
));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -12,10 +12,12 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.18.1" />
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
|
||||
<PackageReference Include="Bogus" Version="35.6.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="EntityFrameworkCore.Testing.NSubstitute" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.5.3" />
|
||||
|
||||
Reference in new issue
Block a user