Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AdServiceTests.cs 9.3KB

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