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

ApplicantServiceTests.cs 6.9KB

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