Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SelectionLevelsServiceTests.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.DTOs.Applicant;
  6. using Diligent.WebAPI.Contracts.DTOs.SelectionLevel;
  7. using Diligent.WebAPI.Contracts.Exceptions;
  8. using Diligent.WebAPI.Data.Entities;
  9. using Microsoft.Extensions.Logging;
  10. namespace Diligent.WebAPI.Tests.Services
  11. {
  12. public class SelectionLevelsServiceTests
  13. {
  14. private readonly IMapper _mapper;
  15. private readonly List<SelectionLevel> _levels;
  16. private ILogger<SelectionLevelService> _logger = Substitute.For<ILogger<SelectionLevelService>>();
  17. private readonly SelectionLevel _selectionLevel;
  18. public SelectionLevelsServiceTests()
  19. {
  20. _selectionLevel = new SelectionLevel
  21. {
  22. Id = 1,
  23. Name = "HR intervju",
  24. SelectionProcesses = new List<SelectionProcess>()
  25. };
  26. // configure mapper
  27. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  28. new List<Profile>
  29. {
  30. new SelectionLevelMappingProfile(),
  31. new SelectionProcessMappingProfile(),
  32. }));
  33. _mapper = new Mapper(configuration);
  34. }
  35. [Fact]
  36. public async Task GetAll_ShouldReturnListOfLevels_Always()
  37. {
  38. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  39. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  40. var result = await service.GetAllAsync();
  41. result.Should().HaveCount(4);
  42. }
  43. [Fact]
  44. public async Task GetFilteredData_ShouldReturnListOfLevels_Always()
  45. {
  46. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  47. databaseContext.SelectionLevels.First().SelectionProcesses = new List<SelectionProcess>
  48. {
  49. new SelectionProcess{ Id = 1, Status = "Obrađen", Date = DateTime.Now},
  50. new SelectionProcess{ Id = 2, Status = "Obrađen", Date = DateTime.Now.AddMonths(-1)},
  51. new SelectionProcess{ Id = 3, Status = "Čeka na zakazivanje", Date = DateTime.Now},
  52. new SelectionProcess{ Id = 4, Status = "Čeka na zakazivanje", Date = DateTime.Now.AddMonths(-1)},
  53. };
  54. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  55. var filter = new SelectionProcessFilterDto
  56. {
  57. DateStart = DateTime.Now.AddDays(-1),
  58. DateEnd = DateTime.Now.AddDays(1),
  59. Statuses = new string[]{ "Obrađen", "Zakazan" }
  60. };
  61. var result = service.GetFilteredLevelsAsync(filter);
  62. result.Should().HaveCount(4);
  63. result.First().SelectionProcesses.Should().HaveCount(1);
  64. }
  65. [Fact]
  66. public async Task GetById_ShouldReturnLevel_WhenLevelExist()
  67. {
  68. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  69. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  70. var result = await service.GetByIdAsync(1);
  71. result.Should().BeEquivalentTo(_mapper.Map<SelectionLevelResposneDto>(_selectionLevel));
  72. }
  73. [Fact]
  74. public async Task GetById_ShouldThrowEntityNotFooundException_WhenLevelDoesnotExist()
  75. {
  76. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  77. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  78. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await service.GetByIdAsync(1000));
  79. }
  80. [Fact]
  81. public async Task GetByIdEntity_ShouldReturnLevel_WhenLevelExist()
  82. {
  83. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  84. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  85. var result = await service.GetByIdEntity(1);
  86. result.Id.Should().Be(_selectionLevel.Id);
  87. result.Name.Should().Be(_selectionLevel.Name);
  88. }
  89. [Fact]
  90. public async Task GetByIdEntity_ShouldThrowEntityNotFooundException_WhenLevelDoesnotExist()
  91. {
  92. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  93. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  94. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await service.GetByIdEntity(1000));
  95. }
  96. [Fact]
  97. public async Task GetCountByLevels_ShouldReturnListOfLevels_Always()
  98. {
  99. var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
  100. databaseContext.SelectionLevels.First().SelectionProcesses = new List<SelectionProcess>
  101. {
  102. new SelectionProcess{ Id = 1, Status = "Obrađen", Date = DateTime.Now},
  103. new SelectionProcess{ Id = 2, Status = "Zakazan", Date = DateTime.Now.AddMonths(-1)},
  104. new SelectionProcess{ Id = 3, Status = "Čeka na zakazivanje", Date = DateTime.Now},
  105. new SelectionProcess{ Id = 4, Status = "Čeka na zakazivanje", Date = DateTime.Now.AddMonths(-1)},
  106. };
  107. SelectionLevelService service = new(databaseContext, _mapper, _logger);
  108. var statues = new List<string> { "Obrađen", "Zakazan" };
  109. var result = await service.GetCountByLevels(statues);
  110. result.Should().HaveCount(4);
  111. result.First().CountAll.Should().Be(4);
  112. result.First().CountDone.Should().Be(2);
  113. }
  114. }
  115. }