Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TechnologyServiceTests.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.MappingProfiles;
  3. using Diligent.WebAPI.Business.Services;
  4. using Diligent.WebAPI.Contracts.DTOs.Technology;
  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 TechnologyServiceTests
  11. {
  12. private readonly IMapper _mapper;
  13. private readonly List<Technology> _technologies = new();
  14. private readonly Technology _technology;
  15. private readonly ILogger<TechnologyService> _logger = Substitute.For<ILogger<TechnologyService>>();
  16. public TechnologyServiceTests()
  17. {
  18. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  19. new List<Profile>
  20. {
  21. new TechnologyMappingProfile()
  22. }));
  23. _mapper = new Mapper(configuration);
  24. _technology = new Technology
  25. {
  26. TechnologyId = 1,
  27. Name = ".NET",
  28. TechnologyType = TechnologyTypes.Backend
  29. };
  30. _technologies.Add(_technology);
  31. }
  32. [Fact]
  33. public async Task GetAll_ShouldReturnListOfTechnologies_Always()
  34. {
  35. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  36. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  37. var result = await tehnologyService.GetAllAsync();
  38. Assert.Equal(result.Count, 1);
  39. }
  40. [Fact]
  41. public async Task GetById_ShouldReturnTechnology_WhenTechnologyExists()
  42. {
  43. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  44. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  45. var result = await tehnologyService.GetByIdAsync(1);
  46. result.Should().BeEquivalentTo(_mapper.Map<TechnologyResponseDto>(_technology));
  47. }
  48. [Fact]
  49. public async Task GetById_ShouldThrowEntityNotFoundException_WhenTechnologyDoesNotExists()
  50. {
  51. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  52. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  53. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await tehnologyService.GetByIdAsync(1000));
  54. }
  55. [Fact]
  56. public async Task GetEntityById_ShouldReturnTechnology_WhenTechnologyExists()
  57. {
  58. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  59. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  60. var result = await tehnologyService.GetEntityByIdAsync(1);
  61. result.Should().BeEquivalentTo(_technology);
  62. }
  63. [Fact]
  64. public async Task GetEntityById_ShouldThrowEntityNotFoundException_WhenTechnologyDoesNotExists()
  65. {
  66. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  67. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  68. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await tehnologyService.GetEntityByIdAsync(1000));
  69. }
  70. [Fact]
  71. public async Task GetEntitiesAsync_ShouldReturnListOfTechnologies_Always()
  72. {
  73. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  74. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  75. var result = await tehnologyService.GetEntitiesAsync(new int[] {1});
  76. Assert.Single(result);
  77. }
  78. }
  79. }