Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.MappingProfiles;
  3. using Diligent.WebAPI.Business.Services;
  4. using Diligent.WebAPI.Contracts.DTOs.Ad;
  5. using Diligent.WebAPI.Contracts.Exceptions;
  6. using Diligent.WebAPI.Data.Entities;
  7. using Microsoft.Extensions.Logging;
  8. namespace Diligent.WebAPI.Tests.Services
  9. {
  10. public class AdServiceTests
  11. {
  12. private readonly IMapper _mapper;
  13. private readonly List<Ad> _ads = new List<Ad>();
  14. private readonly Ad _ad;
  15. private ITechnologyService _technologyService = Substitute.For<ITechnologyService>();
  16. private ILogger<AdService> _logger = Substitute.For<ILogger<AdService>>();
  17. private readonly List<Technology> _technologies = new List<Technology>();
  18. public AdServiceTests()
  19. {
  20. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  21. new List<Profile>
  22. {
  23. new AdMappingProfile(),
  24. new TechnologyMappingProfile()
  25. }));
  26. _mapper = new Mapper(configuration);
  27. _ad = new Ad
  28. {
  29. Id = 1,
  30. Title = "React Developer",
  31. MinimumExperience = 0,
  32. CreatedAt = DateTime.Now,
  33. ExpiredAt = DateTime.Now.AddDays(30),
  34. KeyResponsibilities = "KR|KR",
  35. Requirements = "R|R|R",
  36. Offer = "O|O",
  37. WorkHour = WorkHours.FullTime,
  38. EmploymentType = EmploymentTypes.Intership,
  39. Technologies = new List<Technology>
  40. {
  41. new Technology { TechnologyId = 1, Name = "React", TechnologyType = TechnologyTypes.Backend}
  42. }
  43. };
  44. _ads.Add(_ad);
  45. _ads.Add(new Ad
  46. {
  47. Id = 2,
  48. Title = ".NET Developer",
  49. MinimumExperience = 0,
  50. CreatedAt = DateTime.Now.AddDays(-2),
  51. ExpiredAt = DateTime.Now.AddDays(-1),
  52. KeyResponsibilities = "KR|KR",
  53. Requirements = "R|R|R",
  54. Offer = "O|O",
  55. WorkHour = WorkHours.FullTime,
  56. EmploymentType = EmploymentTypes.Intership,
  57. Technologies = new List<Technology>
  58. {
  59. new Technology { TechnologyId = 2, Name = ".NET", TechnologyType = TechnologyTypes.Frontend}
  60. }
  61. });
  62. _technologies.Add(new Technology
  63. {
  64. TechnologyId = 1,
  65. Name = ".NET"
  66. });
  67. }
  68. [Fact]
  69. public async Task GetAll_ShouldReturnListOfAds_WhenCalled()
  70. {
  71. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  72. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  73. var result = await adService.GetAllAsync();
  74. //result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
  75. Assert.Equal(result.Count, 1);
  76. }
  77. [Fact]
  78. public async Task GetById_ShouldReturnAd_WhenAdExists()
  79. {
  80. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  81. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  82. var result = await adService.GetByIdAsync(1);
  83. result.Should().BeEquivalentTo(_mapper.Map<AdResponseDto>(_ad));
  84. }
  85. [Fact]
  86. public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  87. {
  88. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  89. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  90. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetByIdAsync(1000));
  91. }
  92. [Fact]
  93. public async Task GetAdDetailsById_ShouldReturnAd_WhenAdExists()
  94. {
  95. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  96. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  97. var result = await adService.GetAdDetailsByIdAsync(1);
  98. result.Should().BeEquivalentTo(_mapper.Map<AdDetailsResponseDto>(_ad));
  99. }
  100. [Fact]
  101. public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  102. {
  103. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  104. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  105. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetAdDetailsByIdAsync(1000));
  106. }
  107. [Fact]
  108. public async Task GetArchiveAds_ShouldReturnListOfAds_WhenCalled()
  109. {
  110. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  111. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  112. var result = await adService.GetArchiveAds();
  113. //result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
  114. Assert.Equal(result.Count, 1);
  115. }
  116. [Fact]
  117. public async Task GetFilteredAds_ShouldReturnListOfAds_WhenCalled()
  118. {
  119. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  120. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  121. var result = await adService.GetFilteredAdsAsync(new AdFilterDto
  122. {
  123. MinExperience = 0,
  124. MaxExperience = 10,
  125. EmploymentType = "Intership",
  126. WorkHour = "FullTime",
  127. Technologies = new string[]
  128. {
  129. ".NET"
  130. }
  131. });
  132. Assert.Equal(result.Count, 1);
  133. }
  134. [Fact]
  135. public async Task CreateAd_ShouldAddAdIntoDatabase_WhenCalled()
  136. {
  137. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  138. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  139. _technologyService.GetEntityByIdAsync(Arg.Any<int>()).Returns(new Technology
  140. {
  141. TechnologyId = 3,
  142. Name = "Vue"
  143. });
  144. AdCreateDto adCreateDto = new AdCreateDto
  145. {
  146. Title = "Vue Developer",
  147. MinimumExperience = 0,
  148. CreatedAt = DateTime.Now,
  149. ExpiredAt = DateTime.Now.AddDays(30),
  150. KeyResponsibilities = "KR|KR",
  151. Requirements = "R|R|R",
  152. Offer = "O|O",
  153. WorkHour = "FullTime",
  154. EmploymentType = "Intership",
  155. TechnologiesIds = new List<int> { 3 }
  156. };
  157. await adService.CreateAsync(adCreateDto);
  158. var ads = await adService.GetAllAsync();
  159. Assert.Equal(2, ads.Count);
  160. }
  161. [Fact]
  162. public async Task UpdateAd_ShouldUpdateAd_WhenAdExists()
  163. {
  164. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  165. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  166. var updateForAd = new AdUpdateDto
  167. {
  168. Title = "Vue Developer",
  169. MinimumExperience = 0,
  170. CreatedAt = DateTime.Now,
  171. ExpiredAt = DateTime.Now.AddDays(30),
  172. KeyResponsibilities = "KR|KR",
  173. Requirements = "R|R|R",
  174. Offer = "O|O",
  175. WorkHour = "FullTime",
  176. EmploymentType = "Intership",
  177. };
  178. await adService.UpdateAsync(1, updateForAd);
  179. var ads = await adService.GetAllAsync();
  180. Assert.Equal(ads[0].Title, "Vue Developer");
  181. }
  182. [Fact]
  183. public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  184. {
  185. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  186. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  187. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.UpdateAsync(1000, new AdUpdateDto
  188. {
  189. Title = "Vue Developer",
  190. MinimumExperience = 0,
  191. CreatedAt = DateTime.Now,
  192. ExpiredAt = DateTime.Now.AddDays(30),
  193. KeyResponsibilities = "KR|KR",
  194. Requirements = "R|R|R",
  195. Offer = "O|O",
  196. WorkHour = "FullTime",
  197. EmploymentType = "Intership",
  198. }));
  199. }
  200. [Fact]
  201. public async Task DeleteAd_ShouldDeleteAdFromDatabase_WhenAdExists()
  202. {
  203. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  204. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  205. await adService.DeleteAsync(1);
  206. var ads = await adService.GetAllAsync();
  207. Assert.Equal(0, ads.Count);
  208. }
  209. [Fact]
  210. public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  211. {
  212. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  213. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  214. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.DeleteAsync(1000));
  215. }
  216. }
  217. }