Files
Nemera/tests/UnitTests/Nemera.DataAccess.UnitTests/Common/Configuration/RoleConfigurationTests.cs
T

74 lines
2.5 KiB
C#

#region ========================================================================= USING =====================================================================================
using Nemera.Application.Common.DataAccess.Entities.Authorization;
using Nemera.DataAccess.Core.UoW;
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="RoleConfiguration"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RoleConfigurationTests
{
private readonly NemeraDbContext _context;
/// <summary>
/// Initializes a new instance of the <see cref="RoleConfigurationTests"/> class.
/// </summary>
public RoleConfigurationTests()
{
_context = new NemeraDbContext(new DbContextOptionsBuilder<NemeraDbContext>().UseSqlite("Data Source=:memory:").Options);
}
[Fact]
public void Configure_WhenApplied_ShouldSetTableNameAndKey()
{
// Arrange
IEntityType entityType = _context.Model.FindEntityType(typeof(RoleEntity))!;
// Act
string? tableName = entityType.GetTableName();
IReadOnlyList<IProperty> keyProperties = entityType.FindPrimaryKey()!.Properties;
// Assert
Assert.Equal("Roles", tableName);
Assert.Equal(["Id"], keyProperties.Select(property => property.Name));
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureRoleNameAsRequired()
{
// Arrange
IEntityType entityType = _context.Model.FindEntityType(typeof(RoleEntity))!;
// Act
IProperty roleNameProperty = entityType.GetProperty(nameof(RoleEntity.RoleName));
// Assert
Assert.False(roleNameProperty.IsNullable);
Assert.Equal(100, roleNameProperty.GetMaxLength());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureAuditProperties()
{
// Arrange
IEntityType entityType = _context.Model.FindEntityType(typeof(RoleEntity))!;
// Act
IProperty createdOnUtcProperty = entityType.GetProperty(nameof(RoleEntity.CreatedOnUtc));
IProperty updatedOnUtcProperty = entityType.GetProperty(nameof(RoleEntity.UpdatedOnUtc));
// Assert
Assert.False(createdOnUtcProperty.IsNullable);
Assert.Null(updatedOnUtcProperty.GetDefaultValue());
}
}