Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AdServiceTests.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 GetByIdEntity_ShouldReturnAd_WhenAdExists()
  94. {
  95. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  96. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  97. var result = await adService.GetByIdEntityAsync(1);
  98. result.Should().BeEquivalentTo(_ad);
  99. }
  100. [Fact]
  101. public async Task GetByIdEntity_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.GetByIdEntityAsync(1000));
  106. }
  107. [Fact]
  108. public async Task GetAdDetailsById_ShouldReturnAd_WhenAdExists()
  109. {
  110. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  111. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  112. var result = await adService.GetAdDetailsByIdAsync(1);
  113. result.Should().BeEquivalentTo(_mapper.Map<AdDetailsResponseDto>(_ad));
  114. }
  115. [Fact]
  116. public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  117. {
  118. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  119. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  120. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetAdDetailsByIdAsync(1000));
  121. }
  122. [Fact]
  123. public async Task GetArchiveAds_ShouldReturnListOfAds_WhenCalled()
  124. {
  125. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  126. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  127. var result = await adService.GetArchiveAds();
  128. //result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
  129. Assert.Equal(result.Count, 1);
  130. }
  131. [Fact]
  132. public async Task GetFilteredAds_ShouldReturnListOfAds_WhenCalled()
  133. {
  134. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  135. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  136. var result = await adService.GetFilteredAdsAsync(new AdFilterDto
  137. {
  138. MinExperience = 0,
  139. MaxExperience = 10,
  140. EmploymentType = "Intership",
  141. WorkHour = "FullTime",
  142. Technologies = new string[]
  143. {
  144. ".NET"
  145. }
  146. });
  147. Assert.Equal(result.Count, 1);
  148. }
  149. [Fact]
  150. public async Task GetAllWithCountAsync_ShouldReturnListOfAds_WhenCalled()
  151. {
  152. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  153. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  154. var result = await adService.GetAllWithCountAsync();
  155. Assert.Equal(result.Count, 1);
  156. }
  157. [Fact]
  158. public async Task ArchiveAd_ShouldThrowEntityNotFoundException_WhenAdDontExist()
  159. {
  160. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  161. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  162. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.ArchiveAdAsync(99));
  163. }
  164. [Fact]
  165. public async Task ArchiveAd_ShouldChangeExpiryDate_WhenAdExist()
  166. {
  167. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  168. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  169. await adService.ArchiveAdAsync(2);
  170. Assert.Equal(_ads[1].ExpiredAt.Date, DateTime.Now.Date);
  171. }
  172. [Fact]
  173. public async Task ImportAd_ShouldAddAdInDatabase_WhenCalled()
  174. {
  175. AdCreateDto adCreateDto = new AdCreateDto
  176. {
  177. Title = "Vue Developer",
  178. MinimumExperience = 0,
  179. CreatedAt = DateTime.Now,
  180. ExpiredAt = DateTime.Now.AddDays(30),
  181. KeyResponsibilities = "KR|KR",
  182. Requirements = "R|R|R",
  183. Offer = "O|O",
  184. WorkHour = "FullTime",
  185. EmploymentType = "Intership",
  186. TechnologiesIds = new List<int> { 3 }
  187. };
  188. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  189. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  190. var ad = await adService.ImportAsync(adCreateDto);
  191. var ads = await adService.GetAllAsync();
  192. Assert.Equal(ads.Count, 2);
  193. }
  194. [Fact]
  195. public async Task CreateAd_ShouldAddAdIntoDatabase_WhenCalled()
  196. {
  197. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  198. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  199. _technologyService.GetEntityByIdAsync(Arg.Any<int>()).Returns(new Technology
  200. {
  201. TechnologyId = 3,
  202. Name = "Vue"
  203. });
  204. AdCreateDto adCreateDto = new AdCreateDto
  205. {
  206. Title = "Vue Developer",
  207. MinimumExperience = 0,
  208. CreatedAt = DateTime.Now,
  209. ExpiredAt = DateTime.Now.AddDays(30),
  210. KeyResponsibilities = "KR|KR",
  211. Requirements = "R|R|R",
  212. Offer = "O|O",
  213. WorkHour = "FullTime",
  214. EmploymentType = "Intership",
  215. TechnologiesIds = new List<int> { 3 }
  216. };
  217. await adService.CreateAsync(adCreateDto);
  218. var ads = await adService.GetAllAsync();
  219. Assert.Equal(2, ads.Count);
  220. }
  221. [Fact]
  222. public async Task UpdateAd_ShouldUpdateAd_WhenAdExists()
  223. {
  224. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  225. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  226. var updateForAd = new AdUpdateDto
  227. {
  228. Title = "Vue Developer",
  229. MinimumExperience = 0,
  230. CreatedAt = DateTime.Now,
  231. ExpiredAt = DateTime.Now.AddDays(30),
  232. KeyResponsibilities = "KR|KR",
  233. Requirements = "R|R|R",
  234. Offer = "O|O",
  235. WorkHour = "FullTime",
  236. EmploymentType = "Intership",
  237. };
  238. await adService.UpdateAsync(1, updateForAd);
  239. var ads = await adService.GetAllAsync();
  240. Assert.Equal(ads[0].Title, "Vue Developer");
  241. }
  242. [Fact]
  243. public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  244. {
  245. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  246. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  247. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.UpdateAsync(1000, new AdUpdateDto
  248. {
  249. Title = "Vue Developer",
  250. MinimumExperience = 0,
  251. CreatedAt = DateTime.Now,
  252. ExpiredAt = DateTime.Now.AddDays(30),
  253. KeyResponsibilities = "KR|KR",
  254. Requirements = "R|R|R",
  255. Offer = "O|O",
  256. WorkHour = "FullTime",
  257. EmploymentType = "Intership",
  258. }));
  259. }
  260. [Fact]
  261. public async Task DeleteAd_ShouldDeleteAdFromDatabase_WhenAdExists()
  262. {
  263. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  264. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  265. await adService.DeleteAsync(1);
  266. var ads = await adService.GetAllAsync();
  267. Assert.Equal(0, ads.Count);
  268. }
  269. [Fact]
  270. public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
  271. {
  272. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  273. AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
  274. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.DeleteAsync(1000));
  275. }
  276. }
  277. }