Adds the test suites for the library path templates and centralizes their resolution in the domain service

This commit is contained in:
BinaryAlley committed 2026-10-05 20:15:00 +03:00
1 parent ca4f308b0a
commit 7ed6f05632
52 files changed
+4724 -40

No files matched your search

@@ -3,6 +3,7 @@ using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
@@ -28,6 +29,11 @@ public class LibraryConfigurationTests
_context = new LuminaDbContext(new DbContextOptionsBuilder<LuminaDbContext>().UseSqlite("Data Source=:memory:").Options);
}
/// <summary>
/// Gets the design-time model, which retains configuration such as column order that the read-optimized runtime model omits.
/// </summary>
private IModel DesignTimeModel => _context.GetService<IDesignTimeModel>().Model;
[Fact]
public void Configure_WhenApplied_ShouldSetTableNameAndKey()
{
@@ -64,6 +70,22 @@ public class LibraryConfigurationTests
Assert.Equal(true, downloadMetadataProperty.GetDefaultValue());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigurePathTemplateFingerprint()
{
// Arrange
IEntityType entityType = DesignTimeModel.FindEntityType(typeof(LibraryEntity))!;
// Act
IProperty fingerprintProperty = entityType.GetProperty(nameof(LibraryEntity.PathTemplateFingerprint));
// Assert
Assert.True(fingerprintProperty.IsNullable);
Assert.Equal(typeof(string), fingerprintProperty.ClrType);
Assert.Equal(64, fingerprintProperty.GetMaxLength());
Assert.Equal(11, fingerprintProperty.GetColumnOrder());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureOwnedContentLocations()
{
@@ -71,15 +93,78 @@ public class LibraryConfigurationTests
IEntityType entityType = _context.Model.FindEntityType(typeof(LibraryEntity))!;
// Act
List<IEntityType> ownedTypes = [.. _context.Model.GetEntityTypes().Where(ownedType => ownedType.IsOwned() && ownedType.FindOwnership()?.PrincipalEntityType.Name == entityType.Name)];
IEntityType contentLocationType = Assert.Single(
_context.Model.GetEntityTypes(),
ownedType => ownedType.IsOwned()
&& ownedType.ClrType == typeof(LibraryContentLocationEntity)
&& ownedType.FindOwnership()?.PrincipalEntityType.Name == entityType.Name);
// Assert
IEntityType contentLocationType = Assert.Single(ownedTypes);
Assert.Equal(typeof(LibraryContentLocationEntity), contentLocationType.ClrType);
Assert.Equal("LibraryContentLocations", contentLocationType.GetTableName());
Assert.Equal(260, contentLocationType.GetProperty(nameof(LibraryContentLocationEntity.Path)).GetMaxLength());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureOwnedPathTemplateParts()
{
// Arrange
IEntityType entityType = _context.Model.FindEntityType(typeof(LibraryEntity))!;
// Act
IEntityType partEntityType = Assert.Single(
_context.Model.GetEntityTypes(),
ownedType => ownedType.IsOwned()
&& ownedType.ClrType == typeof(LibraryPathTemplatePartEntity)
&& ownedType.FindOwnership()?.PrincipalEntityType.Name == entityType.Name);
IForeignKey ownershipForeignKey = partEntityType.FindOwnership()!;
// Assert
Assert.Equal("LibraryPathTemplateParts", partEntityType.GetTableName());
Assert.Equal(["Id"], partEntityType.FindPrimaryKey()!.Properties.Select(property => property.Name));
Assert.Equal(typeof(LibraryEntity), ownershipForeignKey.PrincipalEntityType.ClrType);
Assert.Equal(["LibraryId"], ownershipForeignKey.Properties.Select(property => property.Name));
Assert.True(ownershipForeignKey.IsRequired);
IIndex uniqueIndex = Assert.Single(
partEntityType.GetIndexes(),
index => index.Properties.Select(property => property.Name).SequenceEqual(["LibraryId", nameof(LibraryPathTemplatePartEntity.Position)]));
Assert.True(uniqueIndex.IsUnique);
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureOwnedPathTemplatePartColumns()
{
// Arrange
IEntityType partEntityType = DesignTimeModel.GetEntityTypes()
.Single(entityType => entityType.IsOwned()
&& entityType.ClrType == typeof(LibraryPathTemplatePartEntity)
&& entityType.GetTableName() == "LibraryPathTemplateParts");
// Act
IProperty positionProperty = partEntityType.GetProperty(nameof(LibraryPathTemplatePartEntity.Position));
IProperty kindProperty = partEntityType.GetProperty(nameof(LibraryPathTemplatePartEntity.Kind));
IProperty representationProperty = partEntityType.GetProperty(nameof(LibraryPathTemplatePartEntity.Representation));
IProperty isOptionalProperty = partEntityType.GetProperty(nameof(LibraryPathTemplatePartEntity.IsOptional));
// Assert
Assert.Equal("Position", positionProperty.GetColumnName());
Assert.False(positionProperty.IsNullable);
Assert.Equal(typeof(LibraryPathPartKind), kindProperty.ClrType);
Assert.Equal(typeof(string), kindProperty.GetTypeMapping().Converter!.ProviderClrType);
Assert.Equal("TEXT", kindProperty.GetColumnType());
Assert.Equal(32, kindProperty.GetMaxLength());
Assert.False(kindProperty.IsNullable);
Assert.Equal(512, representationProperty.GetMaxLength());
Assert.False(representationProperty.IsNullable);
Assert.Equal(typeof(bool), isOptionalProperty.ClrType);
Assert.False(isOptionalProperty.IsNullable);
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureRelationships()
{
@@ -111,4 +196,23 @@ public class LibraryConfigurationTests
Assert.False(createdOnUtcProperty.IsNullable);
Assert.Null(updatedOnUtcProperty.GetDefaultValue());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureAuditColumnOrders()
{
// Arrange
IEntityType entityType = DesignTimeModel.FindEntityType(typeof(LibraryEntity))!;
// Act
IProperty createdOnUtcProperty = entityType.GetProperty(nameof(LibraryEntity.CreatedOnUtc));
IProperty createdByProperty = entityType.GetProperty(nameof(LibraryEntity.CreatedBy));
IProperty updatedOnUtcProperty = entityType.GetProperty(nameof(LibraryEntity.UpdatedOnUtc));
IProperty updatedByProperty = entityType.GetProperty(nameof(LibraryEntity.UpdatedBy));
// Assert
Assert.Equal(12, createdOnUtcProperty.GetColumnOrder());
Assert.Equal(13, createdByProperty.GetColumnOrder());
Assert.Equal(14, updatedOnUtcProperty.GetColumnOrder());
Assert.Equal(15, updatedByProperty.GetColumnOrder());
}
}
@@ -8,6 +8,7 @@ using Lumina.DataAccess.Core.Repositories.Libraries;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
@@ -29,6 +30,7 @@ public class LibraryRepositoryTests
private readonly LuminaDbContext _mockContext;
private readonly LibraryRepository _sut;
private readonly LibraryEntityFixture _libraryEntityFixture = new();
private readonly LibraryPathTemplatePartEntityFixture _libraryPathTemplatePartEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="LibraryRepositoryTests"/> class.
@@ -230,4 +232,307 @@ public class LibraryRepositoryTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.LibraryNotFound, result.FirstError);
}
[Fact]
public async Task InsertAsync_WhenLibraryHasPathTemplateParts_ShouldAddLibraryAndItsPartsToContext()
{
// Arrange
LibraryPathTemplatePartEntity firstPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity secondPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Separator, representation: "/", isOptional: false);
LibraryPathTemplatePartEntity thirdPart = _libraryPathTemplatePartEntityFixture.Create(position: 2, kind: LibraryPathPartKind.Series, representation: "{0}", isOptional: true);
LibraryEntity library = _libraryEntityFixture.Create(
contentLocations: [],
pathTemplateParts: [firstPart, secondPart, thirdPart],
pathTemplateFingerprint: "path-template-fingerprint");
// Act
Result<Created> result = await _sut.InsertAsync(library, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Created, result.Value);
EntityEntry<LibraryEntity>? addedLibrary = _mockContext.ChangeTracker.Entries<LibraryEntity>()
.FirstOrDefault(entityEntry => entityEntry.State == EntityState.Added && entityEntry.Entity.Id == library.Id);
Assert.NotNull(addedLibrary);
Assert.Equal("path-template-fingerprint", addedLibrary!.Entity.PathTemplateFingerprint);
List<EntityEntry<LibraryPathTemplatePartEntity>> partEntries = [.. _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.Where(entityEntry => entityEntry.State == EntityState.Added)];
Assert.Equal(3, partEntries.Count);
Assert.Equal([0, 1, 2], partEntries.Select(entityEntry => entityEntry.Entity.Position).OrderBy(position => position));
LibraryPathTemplatePartEntity storedFirstPart = partEntries.Single(entityEntry => entityEntry.Entity.Position == 0).Entity;
Assert.Equal(LibraryPathPartKind.Artist, storedFirstPart.Kind);
Assert.Equal("{0}", storedFirstPart.Representation);
Assert.False(storedFirstPart.IsOptional);
LibraryPathTemplatePartEntity storedSecondPart = partEntries.Single(entityEntry => entityEntry.Entity.Position == 1).Entity;
Assert.Equal(LibraryPathPartKind.Separator, storedSecondPart.Kind);
Assert.Equal("/", storedSecondPart.Representation);
Assert.False(storedSecondPart.IsOptional);
LibraryPathTemplatePartEntity storedThirdPart = partEntries.Single(entityEntry => entityEntry.Entity.Position == 2).Entity;
Assert.Equal(LibraryPathPartKind.Series, storedThirdPart.Kind);
Assert.Equal("{0}", storedThirdPart.Representation);
Assert.True(storedThirdPart.IsOptional);
}
[Fact]
public async Task UpdateAsync_WhenPathTemplatePartRepresentationChanges_ShouldReplaceThatPart()
{
// Arrange
LibraryPathTemplatePartEntity keptPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity replacedPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{0}", isOptional: false);
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [keptPart, replacedPart]);
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryPathTemplatePartEntity replacementPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{1}", isOptional: false);
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [keptPart, replacementPart]);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
EntityEntry<LibraryPathTemplatePartEntity>? replacedEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, replacedPart));
Assert.NotNull(replacedEntry);
Assert.Equal(EntityState.Deleted, replacedEntry!.State);
EntityEntry<LibraryPathTemplatePartEntity>? keptEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, keptPart));
Assert.NotNull(keptEntry);
Assert.Equal(EntityState.Unchanged, keptEntry!.State);
Result<LibraryEntity?> retrievedResult = await _sut.GetByIdAsync(library.Id, cancellationToken: CancellationToken.None);
Assert.False(retrievedResult.IsFailure);
LibraryEntity retrievedLibrary = Assert.IsType<LibraryEntity>(retrievedResult.Value);
Assert.Equal(2, retrievedLibrary.PathTemplateParts.Count);
Assert.Equal("{0}", retrievedLibrary.PathTemplateParts.Single(part => part.Position == 0).Representation);
Assert.Equal("{1}", retrievedLibrary.PathTemplateParts.Single(part => part.Position == 1).Representation);
}
[Fact]
public async Task UpdateAsync_WhenPathTemplatePartKindChanges_ShouldReplaceThatPart()
{
// Arrange
LibraryPathTemplatePartEntity keptPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity replacedPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{0}", isOptional: false);
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [keptPart, replacedPart]);
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryPathTemplatePartEntity replacementPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Series, representation: "{0}", isOptional: false);
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [keptPart, replacementPart]);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
EntityEntry<LibraryPathTemplatePartEntity>? replacedEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, replacedPart));
Assert.NotNull(replacedEntry);
Assert.Equal(EntityState.Deleted, replacedEntry!.State);
Result<LibraryEntity?> retrievedResult = await _sut.GetByIdAsync(library.Id, cancellationToken: CancellationToken.None);
LibraryEntity retrievedLibrary = Assert.IsType<LibraryEntity>(retrievedResult.Value);
Assert.Equal(LibraryPathPartKind.Series, retrievedLibrary.PathTemplateParts.Single(part => part.Position == 1).Kind);
}
[Fact]
public async Task UpdateAsync_WhenPathTemplatePartIsOptionalChanges_ShouldReplaceThatPart()
{
// Arrange
LibraryPathTemplatePartEntity keptPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity replacedPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Series, representation: "{0}", isOptional: false);
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [keptPart, replacedPart]);
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryPathTemplatePartEntity replacementPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Series, representation: "{0}", isOptional: true);
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [keptPart, replacementPart]);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
EntityEntry<LibraryPathTemplatePartEntity>? replacedEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, replacedPart));
Assert.NotNull(replacedEntry);
Assert.Equal(EntityState.Deleted, replacedEntry!.State);
Result<LibraryEntity?> retrievedResult = await _sut.GetByIdAsync(library.Id, cancellationToken: CancellationToken.None);
LibraryEntity retrievedLibrary = Assert.IsType<LibraryEntity>(retrievedResult.Value);
Assert.True(retrievedLibrary.PathTemplateParts.Single(part => part.Position == 1).IsOptional);
}
[Fact]
public async Task UpdateAsync_WhenPathTemplatePartPositionIsRemoved_ShouldRemoveThatPart()
{
// Arrange
LibraryPathTemplatePartEntity firstPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity removedPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity thirdPart = _libraryPathTemplatePartEntityFixture.Create(position: 2, kind: LibraryPathPartKind.Series, representation: "{0}", isOptional: true);
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [firstPart, removedPart, thirdPart]);
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [firstPart, thirdPart]);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
EntityEntry<LibraryPathTemplatePartEntity>? removedEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, removedPart));
Assert.NotNull(removedEntry);
Assert.Equal(EntityState.Deleted, removedEntry!.State);
Result<LibraryEntity?> retrievedResult = await _sut.GetByIdAsync(library.Id, cancellationToken: CancellationToken.None);
LibraryEntity retrievedLibrary = Assert.IsType<LibraryEntity>(retrievedResult.Value);
Assert.Equal(2, retrievedLibrary.PathTemplateParts.Count);
Assert.Equal([0, 2], retrievedLibrary.PathTemplateParts.Select(part => part.Position).OrderBy(position => position));
}
[Fact]
public async Task UpdateAsync_WhenPathTemplatePartPositionIsAdded_ShouldAddThatPart()
{
// Arrange
LibraryPathTemplatePartEntity firstPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity secondPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{0}", isOptional: false);
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [firstPart, secondPart]);
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryPathTemplatePartEntity addedPart = _libraryPathTemplatePartEntityFixture.Create(position: 2, kind: LibraryPathPartKind.Series, representation: "{0}", isOptional: true);
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [firstPart, secondPart, addedPart]);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
EntityEntry<LibraryPathTemplatePartEntity>? addedEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, addedPart));
Assert.NotNull(addedEntry);
Assert.Equal(EntityState.Added, addedEntry!.State);
Result<LibraryEntity?> retrievedResult = await _sut.GetByIdAsync(library.Id, cancellationToken: CancellationToken.None);
LibraryEntity retrievedLibrary = Assert.IsType<LibraryEntity>(retrievedResult.Value);
Assert.Equal(3, retrievedLibrary.PathTemplateParts.Count);
Assert.Equal([0, 1, 2], retrievedLibrary.PathTemplateParts.Select(part => part.Position).OrderBy(position => position));
LibraryPathTemplatePartEntity storedAddedPart = retrievedLibrary.PathTemplateParts.Single(part => part.Position == 2);
Assert.Equal(LibraryPathPartKind.Series, storedAddedPart.Kind);
Assert.Equal("{0}", storedAddedPart.Representation);
Assert.True(storedAddedPart.IsOptional);
}
[Fact]
public async Task UpdateAsync_WhenPathTemplatePartsAreUnchanged_ShouldKeepExistingParts()
{
// Arrange
LibraryPathTemplatePartEntity firstPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity secondPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{0}", isOptional: false);
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [firstPart, secondPart]);
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryPathTemplatePartEntity equivalentFirstPart = _libraryPathTemplatePartEntityFixture.Create(position: 0, kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
LibraryPathTemplatePartEntity equivalentSecondPart = _libraryPathTemplatePartEntityFixture.Create(position: 1, kind: LibraryPathPartKind.Title, representation: "{0}", isOptional: false);
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [equivalentFirstPart, equivalentSecondPart]);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
Assert.DoesNotContain(
_mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>(),
entityEntry => entityEntry.State == EntityState.Added || entityEntry.State == EntityState.Deleted);
EntityEntry<LibraryPathTemplatePartEntity>? firstEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, firstPart));
Assert.NotNull(firstEntry);
Assert.Equal(EntityState.Unchanged, firstEntry!.State);
EntityEntry<LibraryPathTemplatePartEntity>? secondEntry = _mockContext.ChangeTracker.Entries<LibraryPathTemplatePartEntity>()
.SingleOrDefault(entityEntry => ReferenceEquals(entityEntry.Entity, secondPart));
Assert.NotNull(secondEntry);
Assert.Equal(EntityState.Unchanged, secondEntry!.State);
Result<LibraryEntity?> retrievedResult = await _sut.GetByIdAsync(library.Id, cancellationToken: CancellationToken.None);
LibraryEntity retrievedLibrary = Assert.IsType<LibraryEntity>(retrievedResult.Value);
Assert.Equal(2, retrievedLibrary.PathTemplateParts.Count);
Assert.Same(firstPart, retrievedLibrary.PathTemplateParts.Single(part => part.Position == 0));
Assert.Same(secondPart, retrievedLibrary.PathTemplateParts.Single(part => part.Position == 1));
}
[Fact]
public async Task UpdateAsync_WhenPathTemplateFingerprintChanges_ShouldUpdateIt()
{
// Arrange
LibraryEntity library = _libraryEntityFixture.Create(contentLocations: [], pathTemplateParts: [], pathTemplateFingerprint: "old-fingerprint");
_mockContext.Libraries.Add(library);
await _mockContext.SaveChangesAsync();
LibraryEntity updatedLibrary = _libraryEntityFixture.Create(
id: library.Id,
userId: library.UserId,
contentLocations: [],
pathTemplateParts: [],
pathTemplateFingerprint: "new-fingerprint");
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedLibrary, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
LibraryEntity? retrievedLibrary = await _mockContext.Libraries.FindAsync(library.Id);
Assert.NotNull(retrievedLibrary);
Assert.Equal("new-fingerprint", retrievedLibrary!.PathTemplateFingerprint);
}
}