Repository tests
This commit is contained in:
		
							
								
								
									
										7
									
								
								SchengenVisaApi/VisaApiTests/Collections.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								SchengenVisaApi/VisaApiTests/Collections.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | namespace VisaApi | ||||||
|  | { | ||||||
|  |     public static class Collections | ||||||
|  |     { | ||||||
|  |         public const string ContextUsingTestCollection = "ContextUsingTestCollection"; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -11,6 +11,14 @@ namespace VisaApi.Database | |||||||
|             .ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning)) |             .ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning)) | ||||||
|             .Options; |             .Options; | ||||||
|  |  | ||||||
|         public static DbContext GetDbContext() => new(opts); |         public static DbContext GetDbContext() | ||||||
|  |         { | ||||||
|  |             var result = new DbContext(opts); | ||||||
|  |  | ||||||
|  |             result.Database.EnsureDeleted(); | ||||||
|  |             result.Database.EnsureCreated(); | ||||||
|  |  | ||||||
|  |             return result; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,11 +7,16 @@ using Infrastructure.Database.Applicants.Repositories.Exceptions; | |||||||
| using VisaApi.Fakers.Applicants; | using VisaApi.Fakers.Applicants; | ||||||
| using VisaApi.Fakers.Common; | using VisaApi.Fakers.Common; | ||||||
| using VisaApi.Services; | using VisaApi.Services; | ||||||
|  | using Xunit; | ||||||
|  |  | ||||||
| namespace VisaApi.Database.Repositories | namespace VisaApi.Database.Repositories | ||||||
| { | { | ||||||
|  |     [Collection(Collections.ContextUsingTestCollection)] | ||||||
|     public class ApplicantsRepositoryTests |     public class ApplicantsRepositoryTests | ||||||
|     { |     { | ||||||
|  |         private static UserFaker userFaker = new(); | ||||||
|  |         private static ApplicantFaker applicantFaker = new(GetDateTimeProvider()); | ||||||
|  |  | ||||||
|         /// <summary> Returns <see cref="IApplicantsRepository"/> </summary> |         /// <summary> Returns <see cref="IApplicantsRepository"/> </summary> | ||||||
|         /// <param name="context"> Database context </param> |         /// <param name="context"> Database context </param> | ||||||
|         /// <returns>Repository</returns> |         /// <returns>Repository</returns> | ||||||
| @@ -51,8 +56,8 @@ namespace VisaApi.Database.Repositories | |||||||
|         { |         { | ||||||
|             await using var context = InMemoryContextProvider.GetDbContext(); |             await using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|             var repository = GetRepository(context); |             var repository = GetRepository(context); | ||||||
|             var user = new UserFaker().Generate(); |             var user = userFaker.Generate(); | ||||||
|             var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); |             var applicant = applicantFaker.Generate(); | ||||||
|             applicant.UserId = user.Id; |             applicant.UserId = user.Id; | ||||||
|             await context.AddAsync(user); |             await context.AddAsync(user); | ||||||
|             await repository.AddAsync(applicant, CancellationToken.None); |             await repository.AddAsync(applicant, CancellationToken.None); | ||||||
| @@ -93,8 +98,8 @@ namespace VisaApi.Database.Repositories | |||||||
|         { |         { | ||||||
|             await using var context = InMemoryContextProvider.GetDbContext(); |             await using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|             var repository = GetRepository(context); |             var repository = GetRepository(context); | ||||||
|             var user = new UserFaker().Generate(); |             var user = userFaker.Generate(); | ||||||
|             var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); |             var applicant = applicantFaker.Generate(); | ||||||
|             applicant.UserId = user.Id; |             applicant.UserId = user.Id; | ||||||
|             await context.AddAsync(user); |             await context.AddAsync(user); | ||||||
|             await repository.AddAsync(applicant, CancellationToken.None); |             await repository.AddAsync(applicant, CancellationToken.None); | ||||||
| @@ -135,7 +140,7 @@ namespace VisaApi.Database.Repositories | |||||||
|         { |         { | ||||||
|             await using var context = InMemoryContextProvider.GetDbContext(); |             await using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|             var repository = GetRepository(context); |             var repository = GetRepository(context); | ||||||
|             var user = new UserFaker().Generate(); |             var user = userFaker.Generate(); | ||||||
|             var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); |             var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate(); | ||||||
|             applicant.UserId = user.Id; |             applicant.UserId = user.Id; | ||||||
|             await context.AddAsync(user); |             await context.AddAsync(user); | ||||||
|   | |||||||
| @@ -3,9 +3,11 @@ using Domains.Users; | |||||||
| using FluentAssertions; | using FluentAssertions; | ||||||
| using Infrastructure.Database; | using Infrastructure.Database; | ||||||
| using Infrastructure.Database.Generic; | using Infrastructure.Database.Generic; | ||||||
|  | using Xunit; | ||||||
|  |  | ||||||
| namespace VisaApi.Database.Repositories.Generic; | namespace VisaApi.Database.Repositories.Generic; | ||||||
|  |  | ||||||
|  | [Collection(Collections.ContextUsingTestCollection)] | ||||||
| public class GenericRepositoryTests | public class GenericRepositoryTests | ||||||
| { | { | ||||||
|     /// <summary> Returns <see cref="GenericRepository{T}"/> </summary> |     /// <summary> Returns <see cref="GenericRepository{T}"/> </summary> | ||||||
| @@ -15,21 +17,21 @@ public class GenericRepositoryTests | |||||||
|  |  | ||||||
|     /// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return empty collection if nothing added </summary> |     /// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return empty collection if nothing added </summary> | ||||||
|     [Fact] |     [Fact] | ||||||
|     public async Task GetAllForEmptyShouldReturnEmpty() |     public void GetAllForEmptyShouldReturnEmpty() | ||||||
|     { |     { | ||||||
|         await using var context = InMemoryContextProvider.GetDbContext(); |         using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|         var repository = GetRepository(context); |         var repository = GetRepository(context); | ||||||
|  |  | ||||||
|         var result = await repository.GetAllAsync(CancellationToken.None); |         var result = repository.GetAllAsync(CancellationToken.None).Result; | ||||||
|  |  | ||||||
|         result.Should().BeEmpty(); |         result.Should().BeEmpty(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return collection with added entities </summary> |     /// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return collection with added entities </summary> | ||||||
|     [Fact] |     [Fact] | ||||||
|     public async Task GetAllForNotEmptyShouldReturnEntities() |     public void GetAllForNotEmptyShouldReturnEntities() | ||||||
|     { |     { | ||||||
|         await using var context = InMemoryContextProvider.GetDbContext(); |         using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|         var repository = GetRepository(context); |         var repository = GetRepository(context); | ||||||
|         User[] users = |         User[] users = | ||||||
|         [ |         [ | ||||||
| @@ -38,49 +40,49 @@ public class GenericRepositoryTests | |||||||
|         ]; |         ]; | ||||||
|         foreach (var user in users) |         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); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should return existing entity </summary> |     /// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should return existing entity </summary> | ||||||
|     [Fact] |     [Fact] | ||||||
|     public async Task GetByIdForExistingShouldReturnEntity() |     public void GetByIdForExistingShouldReturnEntity() | ||||||
|     { |     { | ||||||
|         await using var context = InMemoryContextProvider.GetDbContext(); |         using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|         var repository = GetRepository(context); |         var repository = GetRepository(context); | ||||||
|         var user = new User { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin }; |         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); |         result.Should().Be(user); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should throw exception for not found entity </summary> |     /// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should throw exception for not found entity </summary> | ||||||
|     [Fact] |     [Fact] | ||||||
|     public async Task GetByIdForNotExistingShouldThrow() |     public void GetByIdForNotExistingShouldThrow() | ||||||
|     { |     { | ||||||
|         await using var context = InMemoryContextProvider.GetDbContext(); |         using var context = InMemoryContextProvider.GetDbContext(); | ||||||
|         var repository = GetRepository(context); |         var repository = GetRepository(context); | ||||||
|  |  | ||||||
|         await context.SaveChangesAsync(); |         context.SaveChanges(); | ||||||
|  |  | ||||||
|         EntityNotFoundByIdException<User>? result = null; |         EntityNotFoundByIdException<User>? result = null; | ||||||
|         try |         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<User>; |             result = e.InnerException as EntityNotFoundByIdException<User>; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         result.Should().NotBeNull(); |         result.Should().NotBeNull(); | ||||||
|   | |||||||
| @@ -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(); | ||||||
|  |  | ||||||
|  |         /// <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.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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,29 @@ | |||||||
|  | using ApplicationLayer.InfrastructureServicesInterfaces; | ||||||
|  | using Bogus; | ||||||
|  | using Domains.VisaApplicationDomain; | ||||||
|  |  | ||||||
|  | namespace VisaApi.Fakers.VisaApplications | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Generates past visas | ||||||
|  |     /// </summary> | ||||||
|  |     public sealed class PastVisaFaker : Faker<PastVisa> | ||||||
|  |     { | ||||||
|  |         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; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,29 @@ | |||||||
|  | using ApplicationLayer.InfrastructureServicesInterfaces; | ||||||
|  | using Bogus; | ||||||
|  | using Domains.VisaApplicationDomain; | ||||||
|  |  | ||||||
|  | namespace VisaApi.Fakers.VisaApplications | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Generates past visas | ||||||
|  |     /// </summary> | ||||||
|  |     public sealed class PastVisitFaker : Faker<PastVisit> | ||||||
|  |     { | ||||||
|  |         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; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,20 @@ | |||||||
|  | using ApplicationLayer.InfrastructureServicesInterfaces; | ||||||
|  | using Bogus; | ||||||
|  | using Domains.VisaApplicationDomain; | ||||||
|  |  | ||||||
|  | namespace VisaApi.Fakers.VisaApplications | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Generates permissions to destination Country | ||||||
|  |     /// </summary> | ||||||
|  |     public sealed class PermissionToDestCountryFaker : Faker<PermissionToDestCountry> | ||||||
|  |     { | ||||||
|  |         public PermissionToDestCountryFaker(IDateTimeProvider dateTimeProvider) | ||||||
|  |         { | ||||||
|  |             RuleFor(p => p.Issuer, f => f.Company.CompanyName()); | ||||||
|  |  | ||||||
|  |             RuleFor(p => p.ExpirationDate, | ||||||
|  |                 f => f.Date.Future(4, dateTimeProvider.Now())); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,22 @@ | |||||||
|  | using ApplicationLayer.InfrastructureServicesInterfaces; | ||||||
|  | using Bogus; | ||||||
|  | using Domains; | ||||||
|  | using Domains.VisaApplicationDomain; | ||||||
|  |  | ||||||
|  | namespace VisaApi.Fakers.VisaApplications | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Generates re-entry permissions | ||||||
|  |     /// </summary> | ||||||
|  |     public sealed class ReentryPermitFaker : Faker<ReentryPermit> | ||||||
|  |     { | ||||||
|  |         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())); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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 | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Generates visa applications |     /// Generates visa applications | ||||||
|     /// </summary> |     /// </summary> | ||||||
|     public class VisaApplicationFaker |     public sealed class VisaApplicationFaker : Faker<VisaApplication> | ||||||
|     { |     { | ||||||
|  |         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<ApplicationStatus>()); | ||||||
|  |  | ||||||
|  |             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<VisaCategory>()); | ||||||
|  |  | ||||||
|  |             RuleFor(va => va.ForGroup, f => f.Random.Bool()); | ||||||
|  |  | ||||||
|  |             RuleFor(va => va.RequestedNumberOfEntries, | ||||||
|  |                 f => f.Random.Enum<RequestedNumberOfEntries>()); | ||||||
|  |  | ||||||
|  |             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; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1 +0,0 @@ | |||||||
| global using Xunit; |  | ||||||
		Reference in New Issue
	
	Block a user