mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 23:01:10 +00:00
156 lines
6.4 KiB
C#
156 lines
6.4 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Nemera.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
|
|
using Nemera.DataAccess.Core.UoW;
|
|
using Nemera.Domain.SharedKernel.Common.Enums.AudioLibrary;
|
|
using Nemera.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
#endregion
|
|
|
|
namespace Nemera.DataAccess.UnitTests.Common.Configuration;
|
|
|
|
/// <summary>
|
|
/// Contains unit tests for the <see cref="MusicArtworkConfiguration"/> class.
|
|
/// </summary>
|
|
[ExcludeFromCodeCoverage]
|
|
public class MusicArtworkConfigurationTests : IDisposable
|
|
{
|
|
private readonly NemeraDbContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MusicArtworkConfigurationTests"/> class.
|
|
/// </summary>
|
|
public MusicArtworkConfigurationTests()
|
|
{
|
|
_context = new NemeraDbContext(new DbContextOptionsBuilder<NemeraDbContext>().UseSqlite("Data Source=:memory:").Options);
|
|
}
|
|
|
|
[Fact]
|
|
public void Configure_WhenApplied_ShouldSetTableNameAndKey()
|
|
{
|
|
// Arrange
|
|
IEntityType entityType = _context.Model.FindEntityType(typeof(MusicArtworkEntity))!;
|
|
|
|
// Act
|
|
string? tableName = entityType.GetTableName();
|
|
IReadOnlyList<IProperty> keyProperties = entityType.FindPrimaryKey()!.Properties;
|
|
|
|
// Assert
|
|
Assert.Equal("MusicArtwork", tableName);
|
|
Assert.Equal(["Id"], keyProperties.Select(property => property.Name));
|
|
}
|
|
|
|
[Fact]
|
|
public void Configure_WhenApplied_ShouldNotGenerateTheIdValue()
|
|
{
|
|
// Arrange
|
|
IEntityType entityType = _context.Model.FindEntityType(typeof(MusicArtworkEntity))!;
|
|
|
|
// Act
|
|
IProperty idProperty = entityType.GetProperty(nameof(MusicArtworkEntity.Id));
|
|
|
|
// Assert
|
|
Assert.Equal(ValueGenerated.Never, idProperty.ValueGenerated);
|
|
}
|
|
|
|
[Fact]
|
|
public void Configure_WhenApplied_ShouldConfigureScalarProperties()
|
|
{
|
|
// Arrange
|
|
IEntityType entityType = _context.Model.FindEntityType(typeof(MusicArtworkEntity))!;
|
|
|
|
// Act
|
|
IProperty ownerTypeProperty = entityType.GetProperty(nameof(MusicArtworkEntity.OwnerType));
|
|
IProperty ownerIdProperty = entityType.GetProperty(nameof(MusicArtworkEntity.OwnerId));
|
|
IProperty artworkTypeProperty = entityType.GetProperty(nameof(MusicArtworkEntity.ArtworkType));
|
|
IProperty ordinalProperty = entityType.GetProperty(nameof(MusicArtworkEntity.Ordinal));
|
|
IProperty fileNameProperty = entityType.GetProperty(nameof(MusicArtworkEntity.FileName));
|
|
IProperty contentHashProperty = entityType.GetProperty(nameof(MusicArtworkEntity.ContentHash));
|
|
IProperty statusProperty = entityType.GetProperty(nameof(MusicArtworkEntity.Status));
|
|
IProperty providerProperty = entityType.GetProperty(nameof(MusicArtworkEntity.Provider));
|
|
IProperty lastUpdateUtcProperty = entityType.GetProperty(nameof(MusicArtworkEntity.LastUpdateUtc));
|
|
|
|
// Assert
|
|
Assert.False(ownerTypeProperty.IsNullable);
|
|
Assert.Equal(typeof(MusicArtworkOwnerType), ownerTypeProperty.ClrType);
|
|
Assert.Equal("TEXT", ownerTypeProperty.GetColumnType());
|
|
Assert.Equal(50, ownerTypeProperty.GetMaxLength());
|
|
Assert.False(ownerIdProperty.IsNullable);
|
|
Assert.False(artworkTypeProperty.IsNullable);
|
|
Assert.Equal(typeof(ArtworkType), artworkTypeProperty.ClrType);
|
|
Assert.Equal("TEXT", artworkTypeProperty.GetColumnType());
|
|
Assert.Equal(50, artworkTypeProperty.GetMaxLength());
|
|
Assert.False(ordinalProperty.IsNullable);
|
|
Assert.True(fileNameProperty.IsNullable);
|
|
Assert.Equal(2048, fileNameProperty.GetMaxLength());
|
|
Assert.False(contentHashProperty.IsNullable);
|
|
Assert.False(statusProperty.IsNullable);
|
|
Assert.Equal(typeof(ArtworkStatus), statusProperty.ClrType);
|
|
Assert.Equal("TEXT", statusProperty.GetColumnType());
|
|
Assert.Equal(20, statusProperty.GetMaxLength());
|
|
Assert.True(providerProperty.IsNullable);
|
|
Assert.Equal(100, providerProperty.GetMaxLength());
|
|
Assert.True(lastUpdateUtcProperty.IsNullable);
|
|
}
|
|
|
|
[Fact]
|
|
public void Configure_WhenApplied_ShouldConfigureUniqueOwnerArtworkIndex()
|
|
{
|
|
// Arrange
|
|
IEntityType entityType = _context.Model.FindEntityType(typeof(MusicArtworkEntity))!;
|
|
|
|
// Act
|
|
List<IIndex> indexes = [.. entityType.GetIndexes()];
|
|
|
|
// Assert
|
|
IIndex uniqueIndex = Assert.Single(indexes, index => index.IsUnique);
|
|
Assert.Equal(
|
|
[nameof(MusicArtworkEntity.OwnerType), nameof(MusicArtworkEntity.OwnerId), nameof(MusicArtworkEntity.ArtworkType), nameof(MusicArtworkEntity.Ordinal)],
|
|
uniqueIndex.Properties.Select(property => property.Name));
|
|
}
|
|
|
|
[Fact]
|
|
public void Configure_WhenApplied_ShouldConfigureIndexOnStatus()
|
|
{
|
|
// Arrange
|
|
IEntityType entityType = _context.Model.FindEntityType(typeof(MusicArtworkEntity))!;
|
|
|
|
// Act
|
|
List<IIndex> indexes = [.. entityType.GetIndexes()];
|
|
|
|
// Assert
|
|
Assert.Contains(indexes, index => index.Properties.Select(property => property.Name).SequenceEqual([nameof(MusicArtworkEntity.Status)]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Configure_WhenApplied_ShouldConfigureAuditProperties()
|
|
{
|
|
// Arrange
|
|
IEntityType entityType = _context.Model.FindEntityType(typeof(MusicArtworkEntity))!;
|
|
|
|
// Act
|
|
IProperty createdOnUtcProperty = entityType.GetProperty(nameof(MusicArtworkEntity.CreatedOnUtc));
|
|
IProperty createdByProperty = entityType.GetProperty(nameof(MusicArtworkEntity.CreatedBy));
|
|
IProperty updatedOnUtcProperty = entityType.GetProperty(nameof(MusicArtworkEntity.UpdatedOnUtc));
|
|
IProperty updatedByProperty = entityType.GetProperty(nameof(MusicArtworkEntity.UpdatedBy));
|
|
|
|
// Assert
|
|
Assert.False(createdOnUtcProperty.IsNullable);
|
|
Assert.False(createdByProperty.IsNullable);
|
|
Assert.True(updatedOnUtcProperty.IsNullable);
|
|
Assert.True(updatedByProperty.IsNullable);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disposes the database context.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
_context.Dispose();
|
|
}
|
|
}
|