Changed file hierarchy for tests
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using DbContext = Infrastructure.Database.DbContext;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database
|
||||
{
|
||||
public static class InMemoryContextProvider
|
||||
{
|
||||
private static DbContextOptions<DbContext> opts = new DbContextOptionsBuilder<DbContext>()
|
||||
.UseInMemoryDatabase("VisaApiDB")
|
||||
.ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning))
|
||||
.Options;
|
||||
|
||||
public static DbContext GetDbContext()
|
||||
{
|
||||
var result = new DbContext(opts);
|
||||
|
||||
result.Database.EnsureDeleted();
|
||||
result.Database.EnsureCreated();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.Applicants.NeededServices;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Applicants.Repositories;
|
||||
using Infrastructure.Database.Applicants.Repositories.Exceptions;
|
||||
using VisaApi.Fakers.Applicants;
|
||||
using VisaApi.Fakers.Common;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories
|
||||
{
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class ApplicantsRepositoryTests
|
||||
{
|
||||
private static UserFaker userFaker = new();
|
||||
private static ApplicantFaker applicantFaker = new(GetDateTimeProvider());
|
||||
|
||||
/// <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()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
ApplicantNotFoundByUserIdException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await repository.FindByUserIdAsync(Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicantNotFoundByUserIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.FindByUserIdAsync"/> method that should return existing entity
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByUserIdForExistingShouldReturnApplicant()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await repository.AddAsync(applicant, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.FindByUserIdAsync(user.Id, CancellationToken.None);
|
||||
|
||||
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 = userFaker.Generate();
|
||||
var applicant = applicantFaker.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 = 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using ApplicationLayer.GeneralExceptions;
|
||||
using Domains.Users;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories.Generic;
|
||||
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class GenericRepositoryTests
|
||||
{
|
||||
/// <summary> Returns <see cref="GenericRepository{T}"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static GenericRepository<User> GetRepository(DbContext context) => new TestGenericRepository(context, context);
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return empty collection if nothing added </summary>
|
||||
[Fact]
|
||||
public void GetAllForEmptyShouldReturnEmpty()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = repository.GetAllAsync(CancellationToken.None).Result;
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return collection with added entities </summary>
|
||||
[Fact]
|
||||
public void GetAllForNotEmptyShouldReturnEntities()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
User[] users =
|
||||
[
|
||||
new() { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin },
|
||||
new() { Email = "bruh@mail.ru", Password = "123", Role = Role.Applicant }
|
||||
];
|
||||
foreach (var user in users)
|
||||
{
|
||||
repository.AddAsync(user, CancellationToken.None).Wait();
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
var result = repository.GetAllAsync(CancellationToken.None).Result;
|
||||
|
||||
result.Should().Contain(users).And.HaveSameCount(users);
|
||||
}
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should return existing entity </summary>
|
||||
[Fact]
|
||||
public void GetByIdForExistingShouldReturnEntity()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = new User { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin };
|
||||
repository.AddAsync(user, CancellationToken.None).Wait();
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
var result = repository.GetByIdAsync(user.Id, CancellationToken.None).Result;
|
||||
|
||||
result.Should().Be(user);
|
||||
}
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should throw exception for not found entity </summary>
|
||||
[Fact]
|
||||
public void GetByIdForNotExistingShouldThrow()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
EntityNotFoundByIdException<User>? result = null;
|
||||
try
|
||||
{
|
||||
repository.GetByIdAsync(Guid.NewGuid(), CancellationToken.None).Wait();
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
result = e.InnerException as EntityNotFoundByIdException<User>;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using Domains.Users;
|
||||
using Infrastructure.Database.Generic;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories.Generic
|
||||
{
|
||||
public class TestGenericRepository(IGenericReader reader, IGenericWriter writer) : GenericRepository<User>(reader, writer);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using ApplicationLayer.Services.AuthServices.NeededServices;
|
||||
using ApplicationLayer.Services.VisaApplications.NeededServices;
|
||||
using Domains.Users;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Users.Repositories;
|
||||
using VisaApi.Fakers.Common;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories
|
||||
{
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class UsersRepositoryTests
|
||||
{
|
||||
private UserFaker userFaker = new();
|
||||
|
||||
/// <summary> Returns <see cref="IVisaApplicationsRepository"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static IUsersRepository GetRepository(DbContext context)
|
||||
=> new UsersRepository(context, context);
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.FindByEmailAsync"/> method that should return null for not existing email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByEmailForNotExistingShouldReturnNull()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = await repository.FindByEmailAsync("email@email.ru", CancellationToken.None);
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.FindByEmailAsync"/> method that should return entity for existing email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByEmailForExistingShouldReturnEntity()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
await repository.AddAsync(user, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.FindByEmailAsync(user.Email, CancellationToken.None);
|
||||
|
||||
result.Should().Be(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.GetAllOfRoleAsync"/> method that should return empty from empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetAllOfRoleForEmptyShouldReturnEmpty()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = await repository.GetAllOfRoleAsync(Role.ApprovingAuthority, CancellationToken.None);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.GetAllOfRoleAsync"/> method that should return entities from not empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetAllOfRoleForNotEmptyShouldReturnEntities()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var users = new List<User>();
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var user = userFaker.Generate();
|
||||
user.Role = Role.ApprovingAuthority;
|
||||
users.Add(user);
|
||||
await repository.AddAsync(user, CancellationToken.None);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetAllOfRoleAsync(Role.ApprovingAuthority, CancellationToken.None);
|
||||
|
||||
result.Should().Contain(users).And.HaveSameCount(users);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.VisaApplications.NeededServices;
|
||||
using Domains.VisaApplicationDomain;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.VisaApplications.Repositories;
|
||||
using Infrastructure.Database.VisaApplications.Repositories.Exceptions;
|
||||
using VisaApi.Fakers.Applicants;
|
||||
using VisaApi.Fakers.Common;
|
||||
using VisaApi.Fakers.VisaApplications;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories
|
||||
{
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class VisaApplicationsRepositoryTests
|
||||
{
|
||||
private UserFaker userFaker = new();
|
||||
private ApplicantFaker applicantFaker = new(GetDateTimeProvider());
|
||||
private VisaApplicationFaker applicationFaker = new(GetDateTimeProvider());
|
||||
|
||||
/// <summary> Returns <see cref="IVisaApplicationsRepository"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static IVisaApplicationsRepository GetRepository(DbContext context)
|
||||
=> new VisaApplicationsRepository(context, context);
|
||||
|
||||
/// <summary> Returns <see cref="IDateTimeProvider"/> </summary>
|
||||
private static IDateTimeProvider GetDateTimeProvider() => new TestDateTimeProvider();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetOfApplicantAsync"/> method that should return empty if no applications added
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetOfApplicantForEmptyShouldReturnEmpty()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetOfApplicantAsync(applicant.Id, CancellationToken.None);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetOfApplicantAsync"/> method that should return added entities
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetOfApplicantForExistingShouldReturnEntities()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
var applications = new List<VisaApplication>();
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var application = applicationFaker.GenerateValid(applicant);
|
||||
applications.Add(application);
|
||||
await context.AddAsync(application);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetOfApplicantAsync(applicant.Id, CancellationToken.None);
|
||||
|
||||
result.Should().Contain(applications).And.HaveSameCount(applications);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method that should throw exception for not existing entities
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(Guid.NewGuid(), Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method that should throw exception for not existing applicant
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingApplicantShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var application = applicationFaker.GenerateValid(applicant);
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(application);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(Guid.NewGuid(), application.Id, CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method that should throw exception for not existing application
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingApplicationShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(applicant.Id, Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method
|
||||
/// that should throw exception for not accessible application
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotAccessibleApplicationShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var otherUser = userFaker.Generate();
|
||||
var otherApplicant = applicantFaker.Generate();
|
||||
otherApplicant.UserId = user.Id;
|
||||
var notAccessibleApplication = applicationFaker.GenerateValid(otherApplicant);
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(otherUser);
|
||||
await context.AddAsync(otherApplicant);
|
||||
await context.AddAsync(notAccessibleApplication);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(applicant.Id, notAccessibleApplication.Id, CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method
|
||||
/// that should return application for valid identifiers
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForValidIdsShouldReturnApplication()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var application = applicationFaker.GenerateValid(applicant);
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(application);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetByApplicantAndApplicationIdAsync(applicant.Id, application.Id, CancellationToken.None);
|
||||
|
||||
result.Should().Be(application);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetPendingApplicationsAsync"/> method that should return empty from empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetPendingApplicationsForEmptyShouldReturnEmpty()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = await repository.GetPendingApplicationsAsync(CancellationToken.None);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetPendingApplicationsAsync"/> method that should return pending applications from not empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetPendingApplicationsForExistingShouldReturnExistingPending()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var applicationPending = applicationFaker.GenerateValid(applicant);
|
||||
applicationPending.Status = ApplicationStatus.Pending;
|
||||
var applicationNotPending = applicationFaker.GenerateValid(applicant);
|
||||
applicationNotPending.Status = ApplicationStatus.Approved;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(applicationPending);
|
||||
await context.AddAsync(applicationNotPending);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetPendingApplicationsAsync(CancellationToken.None);
|
||||
|
||||
result.Should().Contain(applicationPending).And.HaveCount(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user