Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ApplicantServiceTests.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using AutoMapper;
  2. using Castle.Core.Logging;
  3. using Diligent.WebAPI.Business.Extensions;
  4. using Diligent.WebAPI.Business.MappingProfiles;
  5. using Diligent.WebAPI.Business.Services;
  6. using Diligent.WebAPI.Contracts.DTOs.Ad;
  7. using Diligent.WebAPI.Contracts.DTOs.Applicant;
  8. using Diligent.WebAPI.Contracts.Exceptions;
  9. using Diligent.WebAPI.Data.Entities;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.Extensions.Logging;
  12. using static Diligent.WebAPI.Data.Entities.Applicant;
  13. namespace Diligent.WebAPI.Tests.Services
  14. {
  15. public class ApplicantServiceTests
  16. {
  17. private readonly IMapper _mapper;
  18. private ILogger<ApplicantService> _logger = Substitute.For<ILogger<ApplicantService>>();
  19. private readonly HttpContext _httpContext;
  20. private readonly List<Applicant> _applicants;
  21. private readonly List<Ad> _ads;
  22. public ApplicantServiceTests()
  23. {
  24. _applicants = MockData.GetListOfApplicants();
  25. _ads = MockData.GetListOfAds();
  26. // configure mapper
  27. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  28. new List<Profile>
  29. {
  30. new ApplicantMappingProfile(),
  31. new AdMappingProfile(),
  32. new SelectionProcessMappingProfile()
  33. }));
  34. _mapper = new Mapper(configuration);
  35. _httpContext = new DefaultHttpContext();
  36. }
  37. [Fact]
  38. public async Task GetFilteredApplicants_ShouldReturnListOfApplicants_Always()
  39. {
  40. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  41. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  42. var filterDto = new ApplicantFilterDto();
  43. var result = await applicantService.GetFilteredApplicants(filterDto);
  44. Assert.Equal(_applicants.Count,result.Total);
  45. }
  46. [Fact]
  47. public async Task GetById_ShouldReturnApplicant_WhenApplicantExist()
  48. {
  49. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  50. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  51. var result = await applicantService.GetById(1);
  52. result.Should().BeEquivalentTo(_mapper.Map<ApplicantViewDto>(_applicants[0]));
  53. }
  54. [Fact]
  55. public async Task GetById_ShouldThrowEntityNotFooundException_WhenApplicantDontExist()
  56. {
  57. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  58. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  59. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.GetById(1000));
  60. }
  61. //[Fact]
  62. //public async Task CreateApplicant_ShouldAddEntityIntoDatabase_Always()
  63. //{
  64. // var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  65. // ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  66. // ApplicantCreateDto applicantCreateDto = new()
  67. // {
  68. // ApplicationChannel = "Facebook",
  69. // BitBucketLink = null,
  70. // CV = "link",
  71. // Email = "some@mail.com",
  72. // Experience = 1,
  73. // FirstName = "Meris",
  74. // LastName = "Ahmatovic",
  75. // GithubLink = null,
  76. // LinkedlnLink = null,
  77. // PhoneNumber = "432424",
  78. // Position = ".NET Developer",
  79. // TypeOfEmployment = TypesOfEmployment.Intership.ToString()
  80. // };
  81. // await applicantService.CreateApplicant(applicantCreateDto);
  82. // var filterDto = new ApplicantFilterDto
  83. // {
  84. // CurrentPage = 1,
  85. // PageSize = 4
  86. // };
  87. // var result = await applicantService.GetFilteredApplicants(filterDto);
  88. // Assert.Equal(2, result.Total);
  89. //}
  90. [Fact]
  91. public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist()
  92. {
  93. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  94. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  95. await applicantService.DeleteApplicant(1);
  96. var filterDto = MockData.GetApplicantFilters();
  97. var applicants = await applicantService.GetFilteredApplicants(filterDto);
  98. Assert.Empty(applicants.Items);
  99. }
  100. [Fact]
  101. public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDontExist()
  102. {
  103. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  104. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  105. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000));
  106. }
  107. //[Fact]
  108. //public async Task UpdateApplicant_ShouldUpdateApplicant_WhenApplicantExist()
  109. //{
  110. // var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  111. // ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  112. // ApplicantUpdateDto applicantUpdateDto = new()
  113. // {
  114. // ApplicationChannel = "Instagram",
  115. // BitBucketLink = null,
  116. // CV = "link",
  117. // Email = "some@mail.com",
  118. // Experience = 1,
  119. // FirstName = "Dzenis",
  120. // LastName = "Hadzifejzovic",
  121. // GithubLink = null,
  122. // LinkedlnLink = null,
  123. // PhoneNumber = "432424",
  124. // Position = "React Developer"
  125. // };
  126. // await applicantService.UpdateApplicant(1, applicantUpdateDto);
  127. // var applicant = await applicantService.GetById(1);
  128. // Assert.Equal(applicant.Position, applicantUpdateDto.Position);
  129. //}
  130. [Fact]
  131. public async Task GetAllAdsApplicants_ShouldReturnListOfAdApplicants_Always()
  132. {
  133. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  134. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  135. var filterDto = MockData.GetApplicantFilters();
  136. var result = await applicantService.GetAllAdsApplicants(filterDto);
  137. result.Should().BeEquivalentTo(_mapper.Map<List<AdApplicantsViewDto>>(_ads));
  138. }
  139. [Fact]
  140. public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists()
  141. {
  142. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  143. ApplicantService applicantService = new(databaseContext, _mapper, _logger);
  144. var result = await applicantService.GetApplicantWithSelectionProcessesById(1);
  145. var processes = result.SelectionProcesses;
  146. processes.Should().HaveCount(3);
  147. result.Should().BeEquivalentTo(_mapper.Map<ApplicantViewDto>(_applicants[0]));
  148. }
  149. }
  150. }