Pruebas Unitarias Persona
Pruebas Unitarias Persona
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MarkCRM.Domain.Models;
using MarkCRM.Domain.Enums;
using MarkCRM.Domain.Exceptions.StageException;
namespace MarkCRM.Domain.Test
{
[TestClass]
public class PersonModelTests
{
private TimeZoneInfo boliviaTimeZone;
private DateTime currentDateTime;
public PersonModelTests()
{
boliviaTimeZone =
TimeZoneInfo.FindSystemTimeZoneById("America/La_Paz");
currentDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,
boliviaTimeZone);
}
[TestMethod]
public void ValidateCreatePersonWithValidData()
{
// Arrange
int id = 1;
string name = "John";
string fatherLastName = "Doe";
string motherLastName = "Smith";
string email = "[email protected]";
string phone = "123456789";
string address = "123 Main St";
EstadoPersona state = EstadoPersona.Activo;
string dni = "12345678";
int gender = 1;
int nationality = 1;
int religion = 1;
int created_by = 1;
DateTime created_at = currentDateTime;
DateTime lastModified = currentDateTime;
int lastModifiedBy = 1;
// Act
PersonModel person = CreatePersonModel(id, name, state, gender,
nationality, created_at, lastModified,
dni, fatherLastName,
motherLastName, email, phone, address,
religion, created_by,
lastModifiedBy);
// Assert
Assert.IsNotNull(person);
Assert.AreEqual(id, person.Id);
Assert.AreEqual(name, person.Name);
Assert.AreEqual(fatherLastName, person.FatherLastName);
Assert.AreEqual(motherLastName, person.MotherLastName);
Assert.AreEqual(email, person.Email);
Assert.AreEqual(phone, person.Phone);
Assert.AreEqual(address, person.Address);
Assert.AreEqual(state, person.State);
Assert.AreEqual(dni, person.Dni);
Assert.AreEqual(gender, person.Gender);
Assert.AreEqual(nationality, person.Nationality);
Assert.AreEqual(religion, person.Religion);
Assert.AreEqual(created_at, person.CreatedAt);
Assert.AreEqual(created_by, person.CreatedBy);
Assert.AreEqual(lastModified, person.LastModifiedAt);
Assert.AreEqual(lastModifiedBy, person.LastModifiedBy);
}
[TestMethod]
[DataRow("")]
[DataRow(" ")]
[DataRow("a")]
public void ValidateCreatePersonWithInvalidName(string name)
{
// Act + Assert
Assert.ThrowsException<InvalidFieldException>(() =>
CreatePersonModel(1, name, EstadoPersona.Activo, 1, 1, currentDateTime,
currentDateTime));
}
[TestMethod]
[DataRow("123456-V")] // DNI válido con letra V
[DataRow("654321-R")] // DNI válido con letra R
[DataRow("789012-M")] // DNI válido con letra M
public void ValidateCreatePersonWithValidDniWithLetter(string dni)
{
// Arrange
// Act
PersonModel person = CreatePersonModel(1, "John", EstadoPersona.Activo,
1, 1, currentDateTime, currentDateTime,
dni: dni);
// Assert
Assert.IsNotNull(person);
Assert.AreEqual(dni, person.Dni);
}
[TestMethod]
[DataRow("123456-")] // DNI inválido sin letra después del guion
[DataRow("123456-VR")] // DNI inválido con más de una letra después del
guion
[DataRow("123-456-V")] // DNI inválido con guion en el lugar incorrecto
public void ValidateCreatePersonWithInvalidDniWithLetter(string dni)
{
// Act + Assert
Assert.ThrowsException<InvalidDniException>(() => CreatePersonModel(1,
"John", EstadoPersona.Activo, 1, 1, currentDateTime, currentDateTime,
dni: dni));
}
[TestMethod]
[DataRow("invalid_email")]
[DataRow("john.doe@")]
[DataRow("john.doe@example")]
public void ValidateCreatePersonWithInvalidEmail(string email)
{
// Act + Assert
Assert.ThrowsException<InvalidFieldException>(() =>
CreatePersonModel(1, "John", EstadoPersona.Activo, 1, 1, currentDateTime,
currentDateTime,
email: email));
}
[TestMethod]
[DataRow("12345")] // DNI inválido por longitud incorrecta
[DataRow("1234567890123")] // DNI inválido por longitud excesiva
public void ValidateCreatePersonWithInvalidDniLength(string dni)
{
// Act + Assert
Assert.ThrowsException<InvalidFieldException>(() =>
CreatePersonModel(1, "John", EstadoPersona.Activo, 1, 1, currentDateTime,
currentDateTime,
dni: dni));
}
[TestMethod]
[DataRow("A123456")] // DNI inválido por formato incorrecto
public void ValidateCreatePersonWithInvalidDniFormat(string dni)
{
// Act + Assert
Assert.ThrowsException<InvalidDniException>(() => CreatePersonModel(1,
"John", EstadoPersona.Activo, 1, 1, currentDateTime, currentDateTime,
dni: dni));
}
[TestMethod]
public void ValidateUpdateNameWithValidName()
{
// Arrange
PersonModel person = CreatePersonModel(1, "John", EstadoPersona.Activo,
1, 1, currentDateTime, currentDateTime);
string newName = "Jane";
// Act
person.UpdateName(newName);
// Assert
Assert.AreEqual(newName, person.Name);
}
[TestMethod]
[DataRow("")]
[DataRow(" ")]
[DataRow("a")]
public void ValidateUpdateNameWithInvalidName(string name)
{
// Arrange
PersonModel person = CreatePersonModel(1, "John", EstadoPersona.Activo,
1, 1, currentDateTime, currentDateTime);
// Act + Assert
Assert.ThrowsException<InvalidFieldException>(() =>
person.UpdateName(name));
}
}
}