tests for ApplicationsRepository

This commit is contained in:
2024-09-17 22:23:16 +03:00
parent 6cff609168
commit 34f76bb258

View File

@@ -11,11 +11,18 @@ namespace VisaApi.Database.Repositories
{
public class ApplicantsRepositoryTests
{
/// <summary> Returns <see cref="IApplicantsRepository"/> </summary>
/// <param name="context"> Database context </param>
/// <returns>Repository</returns>
private static IApplicantsRepository GetRepository(DbContext context)
=> new ApplicantsRepository(context, context);
/// <summary> Returns <see cref="IDateTimeProvider"/> </summary>
private static IDateTimeProvider GetDateTimeProvider() => new TestDateTimeProvider();
/// <summary>
/// Test for <see cref="IApplicantsRepository.FindByUserIdAsync"/> method that should throw exception for not existing entity
/// </summary>
[Fact]
private async Task FindByUserIdForNotExistingShouldThrow()
{
@@ -35,6 +42,9 @@ namespace VisaApi.Database.Repositories
result.Should().NotBeNull();
}
/// <summary>
/// Test for <see cref="IApplicantsRepository.FindByUserIdAsync"/> method that should return existing entity
/// </summary>
[Fact]
private async Task FindByUserIdForExistingShouldReturnApplicant()
{
@@ -51,5 +61,89 @@ namespace VisaApi.Database.Repositories
result.Should().BeEquivalentTo(applicant);
}
/// <summary>
/// Test for <see cref="IApplicantsRepository.GetApplicantIdByUserId"/> method that should throw exception for not existing entity
/// </summary>
[Fact]
private async Task GetApplicantIdByUserIdForNotExistingShouldThrow()
{
await using var context = InMemoryContextProvider.GetDbContext();
var repository = GetRepository(context);
ApplicantNotFoundByUserIdException? result = null;
try
{
await repository.GetApplicantIdByUserId(Guid.NewGuid(), CancellationToken.None);
}
catch (Exception e)
{
result = e as ApplicantNotFoundByUserIdException;
}
result.Should().NotBeNull();
}
/// <summary>
/// Test for <see cref="IApplicantsRepository.GetApplicantIdByUserId"/> method that should return existing entity's identifier
/// </summary>
[Fact]
private async Task GetApplicantIdByUserIdForExistingShouldReturnApplicant()
{
await using var context = InMemoryContextProvider.GetDbContext();
var repository = GetRepository(context);
var user = new UserFaker().Generate();
var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate();
applicant.UserId = user.Id;
await context.AddAsync(user);
await repository.AddAsync(applicant, CancellationToken.None);
await context.SaveChangesAsync();
var result = await repository.GetApplicantIdByUserId(user.Id, CancellationToken.None);
result.Should().Be(applicant.Id);
}
/// <summary>
/// Test for <see cref="IApplicantsRepository.IsApplicantNonResidentByUserId"/> method that should throw exception for not existing entity
/// </summary>
[Fact]
private async Task IsApplicantNonResidentByUserIdForNotExistingShouldThrow()
{
await using var context = InMemoryContextProvider.GetDbContext();
var repository = GetRepository(context);
ApplicantNotFoundByUserIdException? result = null;
try
{
await repository.IsApplicantNonResidentByUserId(Guid.NewGuid(), CancellationToken.None);
}
catch (Exception e)
{
result = e as ApplicantNotFoundByUserIdException;
}
result.Should().NotBeNull();
}
/// <summary>
/// Test for <see cref="IApplicantsRepository.IsApplicantNonResidentByUserId"/> method that should return existing entity's IsNonResident property
/// </summary>
[Fact]
private async Task IsApplicantNonResidentByUserIdForExistingShouldReturnApplicant()
{
await using var context = InMemoryContextProvider.GetDbContext();
var repository = GetRepository(context);
var user = new UserFaker().Generate();
var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate();
applicant.UserId = user.Id;
await context.AddAsync(user);
await repository.AddAsync(applicant, CancellationToken.None);
await context.SaveChangesAsync();
var result = await repository.IsApplicantNonResidentByUserId(user.Id, CancellationToken.None);
result.Should().Be(applicant.IsNonResident);
}
}
}