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());
}
}