Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AdServiceTests.cs 9.5KB

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