#region ========================================================================= USING ===================================================================================== using Nemera.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary; using Nemera.DataAccess.Core.UoW; using Nemera.Domain.SharedKernel.Common.Enums.MediaContributors; 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; /// /// Contains unit tests for the class. /// [ExcludeFromCodeCoverage] public class TrackContributorConfigurationTests : IDisposable { private readonly NemeraDbContext _context; /// /// Initializes a new instance of the class. /// public TrackContributorConfigurationTests() { _context = new NemeraDbContext(new DbContextOptionsBuilder().UseSqlite("Data Source=:memory:").Options); } [Fact] public void Configure_WhenApplied_ShouldSetTableNameAndKey() { // Arrange IEntityType entityType = _context.Model.FindEntityType(typeof(TrackContributorEntity))!; // Act string? tableName = entityType.GetTableName(); IReadOnlyList keyProperties = entityType.FindPrimaryKey()!.Properties; // Assert Assert.Equal("TrackContributors", tableName); Assert.Equal(["Id"], keyProperties.Select(property => property.Name)); } [Fact] public void Configure_WhenApplied_ShouldNotGenerateTheIdValue() { // Arrange IEntityType entityType = _context.Model.FindEntityType(typeof(TrackContributorEntity))!; // Act IProperty idProperty = entityType.GetProperty(nameof(TrackContributorEntity.Id)); // Assert Assert.Equal(ValueGenerated.Never, idProperty.ValueGenerated); } [Fact] public void Configure_WhenApplied_ShouldConfigureScalarProperties() { // Arrange IEntityType entityType = _context.Model.FindEntityType(typeof(TrackContributorEntity))!; // Act IProperty trackIdProperty = entityType.GetProperty(nameof(TrackContributorEntity.TrackId)); IProperty mediaContributorIdProperty = entityType.GetProperty(nameof(TrackContributorEntity.MediaContributorId)); IProperty roleProperty = entityType.GetProperty(nameof(TrackContributorEntity.Role)); // Assert Assert.False(trackIdProperty.IsNullable); Assert.False(mediaContributorIdProperty.IsNullable); Assert.False(roleProperty.IsNullable); Assert.Equal(typeof(MediaContributorRole), roleProperty.ClrType); Assert.Equal("TEXT", roleProperty.GetColumnType()); Assert.Equal(50, roleProperty.GetMaxLength()); } [Fact] public void Configure_WhenApplied_ShouldConfigureUniqueIndexAndContributorLookupIndex() { // Arrange IEntityType entityType = _context.Model.FindEntityType(typeof(TrackContributorEntity))!; // Act List indexes = [.. entityType.GetIndexes()]; // Assert IIndex uniquenessIndex = Assert.Single(indexes.Where(index => index.Properties.Select(property => property.Name).SequenceEqual([nameof(TrackContributorEntity.TrackId), nameof(TrackContributorEntity.MediaContributorId), nameof(TrackContributorEntity.Role)]))); Assert.True(uniquenessIndex.IsUnique); Assert.Contains(indexes, index => index.Properties.Select(property => property.Name).SequenceEqual([nameof(TrackContributorEntity.MediaContributorId)])); } [Fact] public void Configure_WhenApplied_ShouldConfigureAuditProperties() { // Arrange IEntityType entityType = _context.Model.FindEntityType(typeof(TrackContributorEntity))!; // Act IProperty createdOnUtcProperty = entityType.GetProperty(nameof(TrackContributorEntity.CreatedOnUtc)); IProperty createdByProperty = entityType.GetProperty(nameof(TrackContributorEntity.CreatedBy)); IProperty updatedOnUtcProperty = entityType.GetProperty(nameof(TrackContributorEntity.UpdatedOnUtc)); // Assert Assert.False(createdOnUtcProperty.IsNullable); Assert.False(createdByProperty.IsNullable); Assert.True(updatedOnUtcProperty.IsNullable); } /// /// Disposes the database context. /// public void Dispose() { _context.Dispose(); } }