From 34f76bb258554ce42c5847542118376b878e8dbb Mon Sep 17 00:00:00 2001 From: prtsie Date: Tue, 17 Sep 2024 22:23:16 +0300 Subject: [PATCH] tests for ApplicationsRepository --- .../Repositories/ApplicantsRepositoryTests.cs | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs index da47582..8444290 100644 --- a/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs @@ -11,11 +11,18 @@ namespace VisaApi.Database.Repositories { public class ApplicantsRepositoryTests { + /// Returns + /// Database context + /// Repository private static IApplicantsRepository GetRepository(DbContext context) => new ApplicantsRepository(context, context); + /// Returns private static IDateTimeProvider GetDateTimeProvider() => new TestDateTimeProvider(); + /// + /// Test for method that should throw exception for not existing entity + /// [Fact] private async Task FindByUserIdForNotExistingShouldThrow() { @@ -35,6 +42,9 @@ namespace VisaApi.Database.Repositories result.Should().NotBeNull(); } + /// + /// Test for method that should return existing entity + /// [Fact] private async Task FindByUserIdForExistingShouldReturnApplicant() { @@ -51,5 +61,89 @@ namespace VisaApi.Database.Repositories result.Should().BeEquivalentTo(applicant); } + + /// + /// Test for method that should throw exception for not existing entity + /// + [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(); + } + + /// + /// Test for method that should return existing entity's identifier + /// + [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); + } + + /// + /// Test for method that should throw exception for not existing entity + /// + [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(); + } + + /// + /// Test for method that should return existing entity's IsNonResident property + /// + [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); + } } }