#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;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class RoleConfigurationTests
{
private readonly NemeraDbContext _context;
///
/// Initializes a new instance of the class.
///
public RoleConfigurationTests()
{
_context = new NemeraDbContext(new DbContextOptionsBuilder().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 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());
}
}