From 9d08a36c79461aef0f0dc03403bb21d967cc9333 Mon Sep 17 00:00:00 2001 From: prtsie Date: Wed, 18 Sep 2024 19:45:38 +0300 Subject: [PATCH] Repository tests --- SchengenVisaApi/VisaApiTests/Collections.cs | 7 + .../Database/InMemoryContextProvider.cs | 10 +- .../Repositories/ApplicantsRepositoryTests.cs | 15 +- .../Generic/GenericRepositoryTests.cs | 42 +-- .../Repositories/UsersRepositoryTests.cs | 92 ++++++ .../VisaApplicationsRepositoryTests.cs | 266 ++++++++++++++++++ .../Fakers/VisaApplications/PastVisaFaker.cs | 29 ++ .../Fakers/VisaApplications/PastVisitFaker.cs | 29 ++ .../PermissionToDestCountryFaker.cs | 20 ++ .../VisaApplications/ReentryPermitFaker.cs | 22 ++ .../VisaApplications/VisaApplicationFaker.cs | 59 +++- SchengenVisaApi/VisaApiTests/GlobalUsings.cs | 1 - 12 files changed, 563 insertions(+), 29 deletions(-) create mode 100644 SchengenVisaApi/VisaApiTests/Collections.cs create mode 100644 SchengenVisaApi/VisaApiTests/Database/Repositories/UsersRepositoryTests.cs create mode 100644 SchengenVisaApi/VisaApiTests/Database/Repositories/VisaApplicationsRepositoryTests.cs create mode 100644 SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisaFaker.cs create mode 100644 SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisitFaker.cs create mode 100644 SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PermissionToDestCountryFaker.cs create mode 100644 SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/ReentryPermitFaker.cs delete mode 100644 SchengenVisaApi/VisaApiTests/GlobalUsings.cs diff --git a/SchengenVisaApi/VisaApiTests/Collections.cs b/SchengenVisaApi/VisaApiTests/Collections.cs new file mode 100644 index 0000000..61c1979 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Collections.cs @@ -0,0 +1,7 @@ +namespace VisaApi +{ + public static class Collections + { + public const string ContextUsingTestCollection = "ContextUsingTestCollection"; + } +} diff --git a/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs b/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs index 4d33789..90e805a 100644 --- a/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs +++ b/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs @@ -11,6 +11,14 @@ namespace VisaApi.Database .ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning)) .Options; - public static DbContext GetDbContext() => new(opts); + public static DbContext GetDbContext() + { + var result = new DbContext(opts); + + result.Database.EnsureDeleted(); + result.Database.EnsureCreated(); + + return result; + } } } diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs index 02057a9..c95a302 100644 --- a/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/ApplicantsRepositoryTests.cs @@ -7,11 +7,16 @@ using Infrastructure.Database.Applicants.Repositories.Exceptions; using VisaApi.Fakers.Applicants; using VisaApi.Fakers.Common; using VisaApi.Services; +using Xunit; namespace VisaApi.Database.Repositories { + [Collection(Collections.ContextUsingTestCollection)] public class ApplicantsRepositoryTests { + private static UserFaker userFaker = new(); + private static ApplicantFaker applicantFaker = new(GetDateTimeProvider()); + /// Returns /// Database context /// Repository @@ -51,8 +56,8 @@ namespace VisaApi.Database.Repositories { await using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); - var user = new UserFaker().Generate(); - var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); + var user = userFaker.Generate(); + var applicant = applicantFaker.Generate(); applicant.UserId = user.Id; await context.AddAsync(user); await repository.AddAsync(applicant, CancellationToken.None); @@ -93,8 +98,8 @@ namespace VisaApi.Database.Repositories { await using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); - var user = new UserFaker().Generate(); - var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); + var user = userFaker.Generate(); + var applicant = applicantFaker.Generate(); applicant.UserId = user.Id; await context.AddAsync(user); await repository.AddAsync(applicant, CancellationToken.None); @@ -135,7 +140,7 @@ namespace VisaApi.Database.Repositories { await using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); - var user = new UserFaker().Generate(); + var user = userFaker.Generate(); var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); applicant.UserId = user.Id; await context.AddAsync(user); diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs index ca68f59..b7da95d 100644 --- a/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs @@ -3,9 +3,11 @@ using Domains.Users; using FluentAssertions; using Infrastructure.Database; using Infrastructure.Database.Generic; +using Xunit; namespace VisaApi.Database.Repositories.Generic; +[Collection(Collections.ContextUsingTestCollection)] public class GenericRepositoryTests { /// Returns @@ -15,21 +17,21 @@ public class GenericRepositoryTests /// Test for method that should return empty collection if nothing added [Fact] - public async Task GetAllForEmptyShouldReturnEmpty() + public void GetAllForEmptyShouldReturnEmpty() { - await using var context = InMemoryContextProvider.GetDbContext(); + using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); - var result = await repository.GetAllAsync(CancellationToken.None); + var result = repository.GetAllAsync(CancellationToken.None).Result; result.Should().BeEmpty(); } /// Test for method that should return collection with added entities [Fact] - public async Task GetAllForNotEmptyShouldReturnEntities() + public void GetAllForNotEmptyShouldReturnEntities() { - await using var context = InMemoryContextProvider.GetDbContext(); + using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); User[] users = [ @@ -38,49 +40,49 @@ public class GenericRepositoryTests ]; foreach (var user in users) { - await repository.AddAsync(user, CancellationToken.None); + repository.AddAsync(user, CancellationToken.None).Wait(); } - await context.SaveChangesAsync(); + context.SaveChanges(); - var result = await repository.GetAllAsync(CancellationToken.None); + var result = repository.GetAllAsync(CancellationToken.None).Result; - result.Should().OnlyContain(user => users.Contains(user)); + result.Should().Contain(users).And.HaveSameCount(users); } /// Test for method that should return existing entity [Fact] - public async Task GetByIdForExistingShouldReturnEntity() + public void GetByIdForExistingShouldReturnEntity() { - await using var context = InMemoryContextProvider.GetDbContext(); + using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); var user = new User { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin }; - await repository.AddAsync(user, CancellationToken.None); + repository.AddAsync(user, CancellationToken.None).Wait(); - await context.SaveChangesAsync(); + context.SaveChanges(); - var result = await repository.GetByIdAsync(user.Id, CancellationToken.None); + var result = repository.GetByIdAsync(user.Id, CancellationToken.None).Result; result.Should().Be(user); } /// Test for method that should throw exception for not found entity [Fact] - public async Task GetByIdForNotExistingShouldThrow() + public void GetByIdForNotExistingShouldThrow() { - await using var context = InMemoryContextProvider.GetDbContext(); + using var context = InMemoryContextProvider.GetDbContext(); var repository = GetRepository(context); - await context.SaveChangesAsync(); + context.SaveChanges(); EntityNotFoundByIdException? result = null; try { - await repository.GetByIdAsync(Guid.NewGuid(), CancellationToken.None); + repository.GetByIdAsync(Guid.NewGuid(), CancellationToken.None).Wait(); } - catch (Exception e) + catch (AggregateException e) { - result = e as EntityNotFoundByIdException; + result = e.InnerException as EntityNotFoundByIdException; } result.Should().NotBeNull(); diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/UsersRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/UsersRepositoryTests.cs new file mode 100644 index 0000000..82ad536 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/UsersRepositoryTests.cs @@ -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.Database.Repositories +{ + [Collection(Collections.ContextUsingTestCollection)] + public class UsersRepositoryTests + { + private UserFaker userFaker = new(); + + /// Returns + /// Database context + /// Repository + private static IUsersRepository GetRepository(DbContext context) + => new UsersRepository(context, context); + + /// + /// Test for method that should return null for not existing email + /// + [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(); + } + + /// + /// Test for method that should return entity for existing email + /// + [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); + } + + /// + /// Test for method that should return empty from empty db + /// + [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(); + } + + /// + /// Test for method that should return entities from not empty db + /// + [Fact] + private async Task GetAllOfRoleForNotEmptyShouldReturnEntities() + { + await using var context = InMemoryContextProvider.GetDbContext(); + var repository = GetRepository(context); + var users = new List(); + 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); + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/VisaApplicationsRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/VisaApplicationsRepositoryTests.cs new file mode 100644 index 0000000..2709178 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/VisaApplicationsRepositoryTests.cs @@ -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.Database.Repositories +{ + [Collection(Collections.ContextUsingTestCollection)] + public class VisaApplicationsRepositoryTests + { + private UserFaker userFaker = new(); + private ApplicantFaker applicantFaker = new(GetDateTimeProvider()); + private VisaApplicationFaker applicationFaker = new(GetDateTimeProvider()); + + /// Returns + /// Database context + /// Repository + private static IVisaApplicationsRepository GetRepository(DbContext context) + => new VisaApplicationsRepository(context, context); + + /// Returns + private static IDateTimeProvider GetDateTimeProvider() => new TestDateTimeProvider(); + + /// + /// Test for method that should return empty if no applications added + /// + [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(); + } + + /// + /// Test for method that should return added entities + /// + [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(); + 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); + } + + /// + /// Test for method that should throw exception for not existing entities + /// + [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(); + } + + /// + /// Test for method that should throw exception for not existing applicant + /// + [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(); + } + + /// + /// Test for method that should throw exception for not existing application + /// + [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(); + } + + /// + /// Test for method + /// that should throw exception for not accessible application + /// + [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(); + } + + /// + /// Test for method + /// that should return application for valid identifiers + /// + [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); + } + + /// + /// Test for method that should return empty from empty db + /// + [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(); + } + + /// + /// Test for method that should return pending applications from not empty db + /// + [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); + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisaFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisaFaker.cs new file mode 100644 index 0000000..3b827c2 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisaFaker.cs @@ -0,0 +1,29 @@ +using ApplicationLayer.InfrastructureServicesInterfaces; +using Bogus; +using Domains.VisaApplicationDomain; + +namespace VisaApi.Fakers.VisaApplications +{ + /// + /// Generates past visas + /// + public sealed class PastVisaFaker : Faker + { + private IDateTimeProvider dateTimeProvider; + + public PastVisaFaker(IDateTimeProvider dateTimeProvider) + { + this.dateTimeProvider = dateTimeProvider; + + RuleFor(pv => pv.Name, f => f.Random.Words()); + } + + public PastVisa GenerateValid() + { + var result = Generate(); + result.IssueDate = dateTimeProvider.Now().AddDays(Random.Shared.Next(11, 900)); + result.ExpirationDate = result.IssueDate.AddDays(Random.Shared.Next(1, 11)); + return result; + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisitFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisitFaker.cs new file mode 100644 index 0000000..df32675 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PastVisitFaker.cs @@ -0,0 +1,29 @@ +using ApplicationLayer.InfrastructureServicesInterfaces; +using Bogus; +using Domains.VisaApplicationDomain; + +namespace VisaApi.Fakers.VisaApplications +{ + /// + /// Generates past visas + /// + public sealed class PastVisitFaker : Faker + { + private IDateTimeProvider dateTimeProvider; + + public PastVisitFaker(IDateTimeProvider dateTimeProvider) + { + this.dateTimeProvider = dateTimeProvider; + + RuleFor(pv => pv.DestinationCountry, f => f.Address.Country()); + } + + public PastVisit GenerateValid() + { + var result = Generate(); + result.StartDate = dateTimeProvider.Now().AddDays(Random.Shared.Next(11, 900)); + result.EndDate = result.StartDate.AddDays(Random.Shared.Next(1, 11)); + return result; + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PermissionToDestCountryFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PermissionToDestCountryFaker.cs new file mode 100644 index 0000000..f79b31c --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/PermissionToDestCountryFaker.cs @@ -0,0 +1,20 @@ +using ApplicationLayer.InfrastructureServicesInterfaces; +using Bogus; +using Domains.VisaApplicationDomain; + +namespace VisaApi.Fakers.VisaApplications +{ + /// + /// Generates permissions to destination Country + /// + public sealed class PermissionToDestCountryFaker : Faker + { + public PermissionToDestCountryFaker(IDateTimeProvider dateTimeProvider) + { + RuleFor(p => p.Issuer, f => f.Company.CompanyName()); + + RuleFor(p => p.ExpirationDate, + f => f.Date.Future(4, dateTimeProvider.Now())); + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/ReentryPermitFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/ReentryPermitFaker.cs new file mode 100644 index 0000000..6ea3009 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/ReentryPermitFaker.cs @@ -0,0 +1,22 @@ +using ApplicationLayer.InfrastructureServicesInterfaces; +using Bogus; +using Domains; +using Domains.VisaApplicationDomain; + +namespace VisaApi.Fakers.VisaApplications +{ + /// + /// Generates re-entry permissions + /// + public sealed class ReentryPermitFaker : Faker + { + public ReentryPermitFaker(IDateTimeProvider dateTimeProvider) + { + RuleFor(p => p.Number, + f => f.Random.String(ConfigurationConstraints.ReentryPermitNumberLength)); + + RuleFor(p => p.ExpirationDate, + f => f.Date.Future(4, dateTimeProvider.Now())); + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/VisaApplicationFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/VisaApplicationFaker.cs index 38316f7..7514fa3 100644 --- a/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/VisaApplicationFaker.cs +++ b/SchengenVisaApi/VisaApiTests/Fakers/VisaApplications/VisaApplicationFaker.cs @@ -1,10 +1,65 @@ -namespace VisaApi.Fakers.VisaApplications +using ApplicationLayer.InfrastructureServicesInterfaces; +using Bogus; +using Domains; +using Domains.ApplicantDomain; +using Domains.VisaApplicationDomain; + +namespace VisaApi.Fakers.VisaApplications { /// /// Generates visa applications /// - public class VisaApplicationFaker + public sealed class VisaApplicationFaker : Faker { + private static ReentryPermitFaker reentryPermitFaker = null!; + private static PermissionToDestCountryFaker permissionToDestCountryFaker = null!; + public VisaApplicationFaker(IDateTimeProvider dateTimeProvider) + { + reentryPermitFaker = new(dateTimeProvider); + permissionToDestCountryFaker = new(dateTimeProvider); + var pastVisaFaker = new PastVisaFaker(dateTimeProvider); + var pastVisitFaker = new PastVisitFaker(dateTimeProvider); + + RuleFor(va => va.Status, f => f.Random.Enum()); + + RuleFor(va => va.DestinationCountry, f => f.Address.Country()); + + RuleFor(va => va.PastVisas, + f => f.PickRandom(pastVisaFaker.Generate(3), f.Random.Int(0, 3)).ToList()); + + RuleFor(va => va.PastVisits, + f => f.PickRandom(pastVisitFaker.Generate(3), f.Random.Int(0, 3)).ToList()); + + RuleFor(va => va.VisaCategory, f => f.Random.Enum()); + + RuleFor(va => va.ForGroup, f => f.Random.Bool()); + + RuleFor(va => va.RequestedNumberOfEntries, + f => f.Random.Enum()); + + RuleFor(va => va.RequestDate, dateTimeProvider.Now); + + RuleFor(va => va.ValidDaysRequested, + f => f.Random.Int(1, ConfigurationConstraints.MaxValidDays)); + } + + public VisaApplication GenerateValid(Applicant applicant) + { + var result = Generate(); + + result.ApplicantId = applicant.Id; + if (applicant.IsNonResident) + { + result.ReentryPermit = reentryPermitFaker.Generate(); + } + + if (result.VisaCategory is VisaCategory.Transit) + { + result.PermissionToDestCountry = permissionToDestCountryFaker.Generate(); + } + + return result; + } } } diff --git a/SchengenVisaApi/VisaApiTests/GlobalUsings.cs b/SchengenVisaApi/VisaApiTests/GlobalUsings.cs deleted file mode 100644 index c802f44..0000000 --- a/SchengenVisaApi/VisaApiTests/GlobalUsings.cs +++ /dev/null @@ -1 +0,0 @@ -global using Xunit;