From 476733590020a1245d37c38f66e847f4c9116f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Mon, 27 Nov 2023 17:44:55 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D0=B0?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Поправил тесты 2. Поправил сервис, репозиторий и контроллер. Сделал небольшой рефакторинг --- .../Repositories/IHelpPageRepository.cs | 11 -- .../Repository/HelpPageRepository.cs | 5 - .../Services/HelpPageService.cs | 2 - .../Services/HelpPageServiceTest.cs | 145 ------------------ .../UnitTests/Services/HelpPageServiceTest.cs | 97 ++++++++++++ .../Controllers/HelpPageController.cs | 22 --- 6 files changed, 97 insertions(+), 185 deletions(-) delete mode 100644 AsbCloudWebApi.Tests/Services/HelpPageServiceTest.cs create mode 100644 AsbCloudWebApi.Tests/UnitTests/Services/HelpPageServiceTest.cs diff --git a/AsbCloudApp/Repositories/IHelpPageRepository.cs b/AsbCloudApp/Repositories/IHelpPageRepository.cs index 82dd96a4..a9e5b8b3 100644 --- a/AsbCloudApp/Repositories/IHelpPageRepository.cs +++ b/AsbCloudApp/Repositories/IHelpPageRepository.cs @@ -20,15 +20,4 @@ public interface IHelpPageRepository : ICrudRepository Task GetOrDefaultByUrlPageAndIdCategoryAsync(string key, int idCategory, CancellationToken cancellationToken); - - /// - /// Проверяет наличие справки для страницы - /// - /// - /// - /// - /// - Task IsExistingAsync(string key, - int idCategory, - CancellationToken cancellationToken); } diff --git a/AsbCloudInfrastructure/Repository/HelpPageRepository.cs b/AsbCloudInfrastructure/Repository/HelpPageRepository.cs index 5742511f..562844bc 100644 --- a/AsbCloudInfrastructure/Repository/HelpPageRepository.cs +++ b/AsbCloudInfrastructure/Repository/HelpPageRepository.cs @@ -31,9 +31,4 @@ public class HelpPageRepository : CrudRepositoryBase, return helpPage.Adapt(); } - - public Task IsExistingAsync(string key, int idCategory, CancellationToken cancellationToken) => - dbContext.HelpPages.AnyAsync(h => h.UrlPage == key && - h.IdCategory == idCategory, - cancellationToken); } diff --git a/AsbCloudInfrastructure/Services/HelpPageService.cs b/AsbCloudInfrastructure/Services/HelpPageService.cs index 0e8f40ee..396ca595 100644 --- a/AsbCloudInfrastructure/Services/HelpPageService.cs +++ b/AsbCloudInfrastructure/Services/HelpPageService.cs @@ -6,7 +6,6 @@ using System.Net; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; -using AsbCloudApp.Exceptions; namespace AsbCloudInfrastructure.Services; @@ -82,7 +81,6 @@ public class HelpPageService : IHelpPageService /// /// /// - /// public async Task<(Stream stream, string fileName)?> GetFileStreamAsync(string pageKey, int idCategory, CancellationToken cancellationToken) diff --git a/AsbCloudWebApi.Tests/Services/HelpPageServiceTest.cs b/AsbCloudWebApi.Tests/Services/HelpPageServiceTest.cs deleted file mode 100644 index 076a5534..00000000 --- a/AsbCloudWebApi.Tests/Services/HelpPageServiceTest.cs +++ /dev/null @@ -1,145 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Data; -using AsbCloudApp.Repositories; -using AsbCloudApp.Services; -using AsbCloudInfrastructure.Services; -using Microsoft.Extensions.Configuration; -using Moq; -using Xunit; - -namespace AsbCloudWebApi.Tests.ServicesTests; - -public class HelpPageServiceTest -{ - private static Dictionary configSettings = new (){ - {"DirectoryNameHelpPageFiles", "helpPages"} - }; - - private static List HelpPages = new() - { - new() - { - Id = 123, - IdCategory = 20000, - Name = "Справка1.pdf", - Size = 54000, - UrlPage = "test" - }, - new() - { - Id = 134, - IdCategory = 20000, - Name = "Справка2.pdf", - Size = 51000, - UrlPage = "test1" - }, - new() - { - Id = 178, - IdCategory = 10000, - Name = "Справка3.pdf", - Size = 49000, - UrlPage = "test2" - } - }; - - private readonly Mock helpPageRepository = new(); - private readonly Mock fileStorageRepository = new(); - - private readonly IHelpPageService helpPageService; - - public HelpPageServiceTest() - { - IConfiguration configuration = new ConfigurationBuilder() - .AddInMemoryCollection(configSettings) - .Build(); - - helpPageService = new HelpPageService(helpPageRepository.Object, - fileStorageRepository.Object, - configuration); - } - - [Fact] - public async Task AddOrUpdateAsync_ShouldReturn_NewHelpPage() - { - //arrange - int idHelpPage = new Random().Next(1, 100); - string urlPage = "test"; - int idCategory = 20000; - string fileName = "test.pdf"; - MemoryStream fileStream = new MemoryStream(Array.Empty()); - - helpPageRepository.Setup(x => x.GetOrDefaultByUrlPageAndIdCategoryAsync(It.IsAny(), - It.IsAny(), It.IsAny())) - .Returns(() => - { - var helpPage = HelpPages.FirstOrDefault(x => - x.UrlPage == urlPage && - x.IdCategory == idCategory); - - return Task.FromResult(helpPage); - }); - - helpPageRepository.Setup(x => x.InsertAsync(It.IsAny(), - It.IsAny())) - .Returns(() => Task.FromResult(idHelpPage)); - - fileStorageRepository.Setup(x => x.SaveFileAsync(It.IsAny(), - It.IsAny(), - It.IsAny())); - - //act - int result = await helpPageService.AddOrUpdateAsync(urlPage, - idCategory, - fileName, - fileStream, - CancellationToken.None); - - //assert - Assert.True(result > 0); - } - - - [Fact] - public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage() - { - //arrange - int idHelpPage = new Random().Next(1, 100); - string urlPage = "test"; - int newIdCategory = 20000; - string newFileName = "test.pdf"; - MemoryStream newFileStream = new MemoryStream(Array.Empty()); - HelpPageDto existingHelpPage = HelpPages.First(x => - x.UrlPage == urlPage && - x.IdCategory == newIdCategory); - - helpPageRepository.Setup(x => x.GetOrDefaultByUrlPageAndIdCategoryAsync(It.IsAny(), - It.IsAny(), It.IsAny())) - .Returns(() => Task.FromResult(existingHelpPage)!); - - helpPageRepository.Setup(x => x.InsertAsync(It.IsAny(), - It.IsAny())) - .Returns(() => Task.FromResult(idHelpPage)); - - fileStorageRepository.Setup(x => x.SaveFileAsync(It.IsAny(), - It.IsAny(), - It.IsAny())); - - //act - await helpPageService.AddOrUpdateAsync(urlPage, - newIdCategory, - newFileName, - newFileStream, - CancellationToken.None); - - //assert - Assert.Equal(newFileName, existingHelpPage.Name); - Assert.Equal(newIdCategory, existingHelpPage.IdCategory); - Assert.Equal(newFileStream.Length, existingHelpPage.Size); - } -} \ No newline at end of file diff --git a/AsbCloudWebApi.Tests/UnitTests/Services/HelpPageServiceTest.cs b/AsbCloudWebApi.Tests/UnitTests/Services/HelpPageServiceTest.cs new file mode 100644 index 00000000..1277a83e --- /dev/null +++ b/AsbCloudWebApi.Tests/UnitTests/Services/HelpPageServiceTest.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; +using AsbCloudApp.Repositories; +using AsbCloudInfrastructure.Services; +using Microsoft.Extensions.Configuration; +using NSubstitute; +using Xunit; + +namespace AsbCloudWebApi.Tests.UnitTests.Services; + +public class HelpPageServiceTest +{ + private const string urlPage = "test"; + private const string fileName = "Справка_для_страницы_test.pdf"; + private const int idCategory = 20000; + + private static Dictionary configSettings = new() + { + { "DirectoryNameHelpPageFiles", "helpPages" } + }; + + private static readonly MemoryStream fileStream = new(Array.Empty()); + + private static readonly HelpPageDto existingHelpPage = new() + { + Id = 178, + IdCategory = idCategory, + UrlPage = "test2", + Name = "Справка_для_страницы_test2.pdf" + }; + + private readonly IHelpPageRepository helpPageRepositoryMock = Substitute.For(); + private readonly IFileStorageRepository fileStorageRepositoryMock = Substitute.For(); + + private readonly HelpPageService helpPageService; + + public HelpPageServiceTest() + { + IConfiguration configuration = new ConfigurationBuilder() + .AddInMemoryCollection(configSettings) + .Build(); + + helpPageRepositoryMock.GetOrDefaultByUrlPageAndIdCategoryAsync(existingHelpPage.UrlPage, existingHelpPage.IdCategory, Arg.Any()) + .Returns(existingHelpPage); + + helpPageRepositoryMock.InsertAsync(Arg.Any(), Arg.Any()) + .ReturnsForAnyArgs(1); + + fileStorageRepositoryMock.SaveFileAsync(Arg.Any(), Arg.Any(), Arg.Any()); + + helpPageService = new HelpPageService(helpPageRepositoryMock, + fileStorageRepositoryMock, + configuration); + } + + [Fact] + public async Task AddOrUpdateAsync_ShouldReturn_AddedHelpPage() + { + //act + var result = await helpPageService.AddOrUpdateAsync(urlPage, + idCategory, + fileName, + fileStream, + CancellationToken.None); + + //assert + await helpPageRepositoryMock.Received().InsertAsync(Arg.Any(), Arg.Any()); + await fileStorageRepositoryMock.Received().SaveFileAsync(Arg.Any(), Arg.Any(), Arg.Any()); + + Assert.NotEqual(existingHelpPage.Id, result); + Assert.NotEqual(urlPage, existingHelpPage.UrlPage); + Assert.NotEqual(fileName, existingHelpPage.Name); + Assert.Equal(idCategory, existingHelpPage.IdCategory); + } + + + [Fact] + public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage() + { + //act + var result = await helpPageService.AddOrUpdateAsync(existingHelpPage.UrlPage, + existingHelpPage.IdCategory, + existingHelpPage.Name, + fileStream, + CancellationToken.None); + + //assert + await helpPageRepositoryMock.Received().UpdateAsync(Arg.Any(), Arg.Any()); + await fileStorageRepositoryMock.Received().SaveFileAsync(Arg.Any(), Arg.Any(), Arg.Any()); + + Assert.Equal(existingHelpPage.Id, result); + } +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/HelpPageController.cs b/AsbCloudWebApi/Controllers/HelpPageController.cs index a90ea46a..aadd3fcc 100644 --- a/AsbCloudWebApi/Controllers/HelpPageController.cs +++ b/AsbCloudWebApi/Controllers/HelpPageController.cs @@ -94,26 +94,4 @@ public class HelpPageController : ControllerBase return File(file.Value.stream, "application/pdf", file.Value.fileName); } - - /// - /// Проверяет наличие справки для страницы - /// - /// Ключ страницы - /// Id категории файла. Допустимое значение параметра: 20000 - /// - /// - [HttpGet("isExisting")] - [ProducesResponseType(typeof(bool), (int)HttpStatusCode.OK)] - public async Task IsExistingAsync( - [Required] string key, - [Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")] - int idCategory, - CancellationToken cancellationToken) - { - var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(key, - idCategory, - cancellationToken); - - return Ok(helpPage != null); - } }