mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 21:11:10 +00:00
Normalizes tag and genre names and migrates existing values
This commit is contained in:
1 parent
8a5cb9116e
commit
eaca69bf2a
19 files changed
+4089
-58
No files matched your search
+38
@@ -0,0 +1,38 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.DataAccess.Common.Interceptors;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.IntegrationTests.Common.Interceptors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <see cref="SqliteSharedReferenceNormalizationInterceptor"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class SqliteSharedReferenceNormalizationInterceptorTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(" Horror ", "horror")] // ASCII letters
|
||||
[InlineData(" ÉLECTRO ", "électro")] // non-ASCII Latin letters
|
||||
[InlineData("ЖАНР", "жанр")] // non-Latin letters
|
||||
public async Task ConnectionOpenedAsync_WhenTheConnectionIsOpened_ShouldRegisterTheInvariantNormalizationFunction(string name, string expectedName)
|
||||
{
|
||||
// Arrange
|
||||
using LuminaDbContext context = new(new DbContextOptionsBuilder<LuminaDbContext>().UseSqlite("Data Source=:memory:").Options);
|
||||
|
||||
// Act
|
||||
await context.Database.OpenConnectionAsync();
|
||||
SqliteConnection connection = (SqliteConnection)context.Database.GetDbConnection();
|
||||
await using SqliteCommand command = connection.CreateCommand();
|
||||
command.CommandText = $"SELECT {SqliteSharedReferenceNormalizationInterceptor.NORMALIZE_FUNCTION_NAME}($name);";
|
||||
command.Parameters.AddWithValue("$name", name);
|
||||
object? result = await command.ExecuteScalarAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedName, result);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -212,10 +212,10 @@ public class AddTrackEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory
|
||||
|
||||
// Genres and tags checks.
|
||||
Assert.Equal(
|
||||
metadata.Genres!.Select(genre => genre.Name).OrderBy(name => name),
|
||||
metadata.Genres!.Select(genre => genre.Name!.ToLowerInvariant()).OrderBy(name => name),
|
||||
trackResponse.Metadata.Genres!.Select(genre => genre.Name).OrderBy(name => name));
|
||||
Assert.Equal(
|
||||
metadata.Tags!.Select(tag => tag.Name).OrderBy(name => name),
|
||||
metadata.Tags!.Select(tag => tag.Name!.ToLowerInvariant()).OrderBy(name => name),
|
||||
trackResponse.Metadata.Tags!.Select(tag => tag.Name).OrderBy(name => name));
|
||||
|
||||
// Moods, ISRCs, contributors and ratings checks.
|
||||
|
||||
+4
-4
@@ -133,14 +133,14 @@ public class AddBookEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory>
|
||||
// genres checks
|
||||
Assert.Equal(bookRequest.Metadata.Genres!.Count, bookResponse.Metadata.Genres!.Count);
|
||||
Assert.Equal(
|
||||
bookRequest.Metadata.Genres.Select(genre => genre.Name).OrderBy(x => x),
|
||||
bookResponse.Metadata.Genres.Select(genre => genre.Name).OrderBy(x => x));
|
||||
bookRequest.Metadata.Genres.Select(genre => genre.Name!.ToLowerInvariant()).OrderBy(name => name),
|
||||
bookResponse.Metadata.Genres.Select(genre => genre.Name).OrderBy(name => name));
|
||||
|
||||
// tags checks
|
||||
Assert.Equal(bookRequest.Metadata.Tags!.Count, bookResponse.Metadata.Tags!.Count);
|
||||
Assert.Equal(
|
||||
bookRequest.Metadata.Tags.Select(tag => tag.Name).OrderBy(x => x),
|
||||
bookResponse.Metadata.Tags.Select(tag => tag.Name).OrderBy(x => x));
|
||||
bookRequest.Metadata.Tags.Select(tag => tag.Name!.ToLowerInvariant()).OrderBy(name => name),
|
||||
bookResponse.Metadata.Tags.Select(tag => tag.Name).OrderBy(name => name));
|
||||
|
||||
// book specific properties
|
||||
Assert.Equal(bookRequest.Format, bookResponse.Format);
|
||||
|
||||
+2
-2
@@ -461,8 +461,8 @@ public class GetBooksEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
BookEntity book = _bookEntityFixture.Create(libraryId: libraryId, title: title, path: $"/books/{Guid.NewGuid()}.epub", includeMetadata: false);
|
||||
book.Tags = [_tagEntityFixture.Create(name: $"Tag-{Guid.NewGuid()}")];
|
||||
book.Genres = [_genreEntityFixture.Create(name: $"Genre-{Guid.NewGuid()}")];
|
||||
book.Tags = [_tagEntityFixture.Create(name: $"tag-{Guid.NewGuid()}")];
|
||||
book.Genres = [_genreEntityFixture.Create(name: $"genre-{Guid.NewGuid()}")];
|
||||
book.ISBNs = [_isbnEntityFixture.Create(value: "9780395272237", format: IsbnFormat.Isbn13)];
|
||||
book.Ratings = [_bookRatingEntityFixture.Create(value: 4.5M, maxValue: 5, source: BookRatingSource.Goodreads, voteCount: 100)];
|
||||
dbContext.Books.Add(book);
|
||||
|
||||
+5
-5
@@ -31,7 +31,7 @@ public class GenreDtoMappingTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(genreDto.Name, result.Value.Name);
|
||||
Assert.Equal("rock", result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -50,7 +50,7 @@ public class GenreDtoMappingTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(genreDto.Name, result.Value.Name);
|
||||
Assert.Equal(name.ToLowerInvariant(), result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -92,7 +92,7 @@ public class GenreDtoMappingTests
|
||||
for (int i = 0; i < genreDtos.Count; i++)
|
||||
{
|
||||
Assert.False(resultList[i].IsFailure);
|
||||
Assert.Equal(genreDtos[i].Name, resultList[i].Value.Name);
|
||||
Assert.Equal(genreDtos[i].Name!.ToLowerInvariant(), resultList[i].Value.Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,12 +117,12 @@ public class GenreDtoMappingTests
|
||||
|
||||
List<Result<Genre>> resultList = results.ToList();
|
||||
Assert.False(resultList[0].IsFailure);
|
||||
Assert.Equal("Rock", resultList[0].Value.Name);
|
||||
Assert.Equal("rock", resultList[0].Value.Name);
|
||||
|
||||
Assert.True(resultList[1].IsFailure);
|
||||
|
||||
Assert.False(resultList[2].IsFailure);
|
||||
Assert.Equal("Jazz", resultList[2].Value.Name);
|
||||
Assert.Equal("jazz", resultList[2].Value.Name);
|
||||
|
||||
Assert.True(resultList[3].IsFailure);
|
||||
}
|
||||
|
||||
+4
-4
@@ -81,7 +81,7 @@ public class GenreEntityMappingTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(entity.Name, result.Value.Name);
|
||||
Assert.Equal("fiction", result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -120,11 +120,11 @@ public class GenreEntityMappingTests
|
||||
Assert.NotNull(results);
|
||||
Assert.Equal(entities.Count, results.Count());
|
||||
|
||||
List<Result<Genre>> resultList = results.ToList();
|
||||
List<Result<Genre>> resultList = [.. results];
|
||||
for (int i = 0; i < entities.Count; i++)
|
||||
{
|
||||
Assert.False(resultList[i].IsFailure);
|
||||
Assert.Equal(entities[i].Name, resultList[i].Value.Name);
|
||||
Assert.Equal(entities[i].Name!.ToLowerInvariant(), resultList[i].Value.Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public class GenreEntityMappingTests
|
||||
Assert.NotNull(results);
|
||||
Assert.Equal(entities.Count, results.Count());
|
||||
|
||||
List<GenreDto> resultList = results.ToList();
|
||||
List<GenreDto> resultList = [.. results];
|
||||
for (int i = 0; i < entities.Count; i++)
|
||||
Assert.Equal(entities[i].Name, resultList[i].Name);
|
||||
}
|
||||
|
||||
+15
-15
@@ -23,7 +23,7 @@ public class TagDtoMappingTests
|
||||
public void ToDomainValueObject_WhenMappingValidTagDto_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
TagDto tagDto = _tagDtoFixture.Create(name: "indie");
|
||||
TagDto tagDto = _tagDtoFixture.Create(name: "Indie");
|
||||
|
||||
// Act
|
||||
Result<Tag> result = tagDto.ToDomainValueObject();
|
||||
@@ -31,15 +31,15 @@ public class TagDtoMappingTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(tagDto.Name, result.Value.Name);
|
||||
Assert.Equal("indie", result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("indie")]
|
||||
[InlineData("electronic")]
|
||||
[InlineData("instrumental")]
|
||||
[InlineData("live")]
|
||||
[InlineData("acoustic")]
|
||||
[InlineData("Indie")]
|
||||
[InlineData("Electronic")]
|
||||
[InlineData("Instrumental")]
|
||||
[InlineData("Live")]
|
||||
[InlineData("Acoustic")]
|
||||
public void ToDomainValueObject_WhenMappingDifferentValidTagDtos_ShouldMapCorrectly(string name)
|
||||
{
|
||||
// Arrange
|
||||
@@ -51,7 +51,7 @@ public class TagDtoMappingTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(tagDto.Name, result.Value.Name);
|
||||
Assert.Equal(name.ToLowerInvariant(), result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -76,10 +76,10 @@ public class TagDtoMappingTests
|
||||
// Arrange
|
||||
List<TagDto> tagDtos =
|
||||
[
|
||||
_tagDtoFixture.Create(name: "indie"),
|
||||
_tagDtoFixture.Create(name: "electronic"),
|
||||
_tagDtoFixture.Create(name: "instrumental"),
|
||||
_tagDtoFixture.Create(name: "live")
|
||||
_tagDtoFixture.Create(name: "Indie"),
|
||||
_tagDtoFixture.Create(name: "Electronic"),
|
||||
_tagDtoFixture.Create(name: "Instrumental"),
|
||||
_tagDtoFixture.Create(name: "Live")
|
||||
];
|
||||
|
||||
// Act
|
||||
@@ -93,7 +93,7 @@ public class TagDtoMappingTests
|
||||
for (int i = 0; i < tagDtos.Count; i++)
|
||||
{
|
||||
Assert.False(resultList[i].IsFailure);
|
||||
Assert.Equal(tagDtos[i].Name, resultList[i].Value.Name);
|
||||
Assert.Equal(tagDtos[i].Name!.ToLowerInvariant(), resultList[i].Value.Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,9 +103,9 @@ public class TagDtoMappingTests
|
||||
// Arrange
|
||||
List<TagDto> tagDtos =
|
||||
[
|
||||
_tagDtoFixture.Create(name: "indie"),
|
||||
_tagDtoFixture.Create(name: "Indie"),
|
||||
_tagDtoFixture.Create(name: ""),
|
||||
_tagDtoFixture.Create(name: "electronic"),
|
||||
_tagDtoFixture.Create(name: "Electronic"),
|
||||
_tagDtoFixture.Create(name: " ")
|
||||
];
|
||||
|
||||
|
||||
+2
-2
@@ -81,7 +81,7 @@ public class TagEntityMappingTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(entity.Name, result.Value.Name);
|
||||
Assert.Equal("fantasy", result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -124,7 +124,7 @@ public class TagEntityMappingTests
|
||||
for (int i = 0; i < entities.Count; i++)
|
||||
{
|
||||
Assert.False(resultList[i].IsFailure);
|
||||
Assert.Equal(entities[i].Name, resultList[i].Value.Name);
|
||||
Assert.Equal(entities[i].Name!.ToLowerInvariant(), resultList[i].Value.Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+54
@@ -95,6 +95,45 @@ public class SharedReferenceResolverTests
|
||||
Assert.Same(firstTag, resolveResult.Value["jazz"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_WhenIncomingNamesDifferOnlyByCasing_ShouldCollapseThemToASingleInstance()
|
||||
{
|
||||
// Arrange
|
||||
TagEntity firstTag = _tagEntityFixture.Create(name: "Horror");
|
||||
TagEntity secondTag = _tagEntityFixture.Create(name: "horror");
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyDictionary<string, TagEntity>> resolveResult = await SharedReferenceResolver.ResolveAsync(_mockContext, [firstTag, secondTag], Errors.Metadata.TagNameCannotBeEmpty, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(resolveResult.IsFailure);
|
||||
Assert.Single(resolveResult.Value);
|
||||
Assert.Same(firstTag, resolveResult.Value["horror"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_WhenAStoredNameDiffersOnlyByCasing_ShouldReturnTheStoredInstance()
|
||||
{
|
||||
// Arrange
|
||||
TagEntity storedTag = _tagEntityFixture.Create(name: "horror");
|
||||
_mockContext.Set<TagEntity>().Add(storedTag);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Detach the stored row, so that the resolution cannot take the tracked shortcut and must read it from the storage medium.
|
||||
_mockContext.ChangeTracker.Clear();
|
||||
TagEntity incomingTag = _tagEntityFixture.Create(name: "Horror");
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyDictionary<string, TagEntity>> resolveResult = await SharedReferenceResolver.ResolveAsync(_mockContext, [incomingTag], Errors.Metadata.TagNameCannotBeEmpty, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(resolveResult.IsFailure);
|
||||
TagEntity resolvedTag = resolveResult.Value["horror"];
|
||||
// A different instance than the incoming one proves the stored row was reused instead of the incoming one being inserted.
|
||||
Assert.NotSame(incomingTag, resolvedTag);
|
||||
Assert.Equal("horror", resolvedTag.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_WhenThereAreNoIncomingEntities_ShouldReturnAnEmptyDictionary()
|
||||
{
|
||||
@@ -137,6 +176,21 @@ public class SharedReferenceResolverTests
|
||||
Assert.Same(firstTag, normalizedTag);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Normalize_WhenANameDiffersOnlyByCasingFromTheResolvedName_ShouldMapToTheResolvedInstance()
|
||||
{
|
||||
// Arrange
|
||||
TagEntity resolvedTag = _tagEntityFixture.Create(name: "horror");
|
||||
TagEntity incomingTag = _tagEntityFixture.Create(name: "Horror");
|
||||
Result<IReadOnlyDictionary<string, TagEntity>> resolveResult = await SharedReferenceResolver.ResolveAsync(_mockContext, [resolvedTag], Errors.Metadata.TagNameCannotBeEmpty, CancellationToken.None);
|
||||
|
||||
// Act
|
||||
IReadOnlyCollection<TagEntity> normalizedTags = SharedReferenceResolver.Normalize([incomingTag], resolveResult.Value);
|
||||
|
||||
// Assert
|
||||
Assert.Same(resolvedTag, Assert.Single(normalizedTags));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileAsync_WhenANameIsNew_ShouldAddItToTheTrackedCollection()
|
||||
{
|
||||
|
||||
@@ -17,14 +17,14 @@ public class GenreTests
|
||||
private readonly GenreFixture _genreFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithValidName_ShouldCreateGenreWithTrimmedName()
|
||||
public void Create_WhenCalledWithValidName_ShouldCreateGenreWithTrimmedLowercaseInvariantName()
|
||||
{
|
||||
// Act
|
||||
Result<Genre> result = Genre.Create(" Science Fiction ");
|
||||
Result<Genre> result = Genre.Create(" Science FICTION ");
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("Science Fiction", result.Value.Name);
|
||||
Assert.Equal("science fiction", result.Value.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -55,6 +55,20 @@ public class GenreTests
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithNamesDifferingOnlyByCasing_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Genre firstGenre = _genreFixture.Create(name: "Science Fiction");
|
||||
Genre secondGenre = _genreFixture.Create(name: "science fiction");
|
||||
|
||||
// Act
|
||||
bool result = firstGenre.Equals(secondGenre);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentName_ShouldReturnFalse()
|
||||
{
|
||||
@@ -79,6 +93,6 @@ public class GenreTests
|
||||
string result = genre.ToString();
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Science Fiction", result);
|
||||
Assert.Equal("science fiction", result);
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,10 @@ public class TagTests
|
||||
private readonly TagFixture _tagFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithValidName_ShouldCreateTagWithTrimmedName()
|
||||
public void Create_WhenCalledWithValidName_ShouldCreateTagWithTrimmedLowercaseInvariantName()
|
||||
{
|
||||
// Act
|
||||
Result<Tag> result = Tag.Create(" bestseller ");
|
||||
Result<Tag> result = Tag.Create(" BeStSeLLeR ");
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
@@ -55,6 +55,20 @@ public class TagTests
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithNamesDifferingOnlyByCasing_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Tag firstTag = _tagFixture.Create(name: "BestSeller");
|
||||
Tag secondTag = _tagFixture.Create(name: "bestseller");
|
||||
|
||||
// Act
|
||||
bool result = firstTag.Equals(secondTag);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentName_ShouldReturnFalse()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user