From 90c62b9ede7a8799841fb7919ebc8e7e6d3c89d2 Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Mon, 18 Nov 2024 11:32:57 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20Setpoint=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseIntegrationTest.cs | 4 +- .../Clients/ISetpointClient.cs | 17 +++++ .../Controllers/SetpointControllerTest.cs | 75 +++++++++++++++++++ .../Persistence.IntegrationTests.csproj | 1 + .../WebAppFactoryFixture.cs | 3 +- .../Repositories/SetpointRepository.cs | 3 +- 6 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 Persistence.IntegrationTests/Clients/ISetpointClient.cs create mode 100644 Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs diff --git a/Persistence.IntegrationTests/BaseIntegrationTest.cs b/Persistence.IntegrationTests/BaseIntegrationTest.cs index a0edda9..8a8f764 100644 --- a/Persistence.IntegrationTests/BaseIntegrationTest.cs +++ b/Persistence.IntegrationTests/BaseIntegrationTest.cs @@ -17,9 +17,9 @@ public abstract class BaseIntegrationTest : IClassFixture, protected BaseIntegrationTest(WebAppFactoryFixture factory) { - //scope = factory.Services.CreateScope(); + scope = factory.Services.CreateScope(); - //dbContext = scope.ServiceProvider.GetRequiredService(); + dbContext = scope.ServiceProvider.GetRequiredService(); } public void Dispose() diff --git a/Persistence.IntegrationTests/Clients/ISetpointClient.cs b/Persistence.IntegrationTests/Clients/ISetpointClient.cs new file mode 100644 index 0000000..831f119 --- /dev/null +++ b/Persistence.IntegrationTests/Clients/ISetpointClient.cs @@ -0,0 +1,17 @@ +using Persistence.Models; +using Refit; + +namespace Persistence.IntegrationTests.Clients +{ + public interface ISetpointClient + { + [Post("/api/Setpoint/history")] + Task>> GetHistoryAsync(IEnumerable setpointKeys, DateTimeOffset historyMoment); + + [Post("/api/Setpoint/log")] + Task>>> GetLogAsync(IEnumerable setpoitKeys); + + [Post("/api/Setpoint/save")] + Task> SaveAsync(Guid setpointKey, object newValue); + } +} diff --git a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs new file mode 100644 index 0000000..b972c43 --- /dev/null +++ b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs @@ -0,0 +1,75 @@ +using System.Net; +using Persistence.IntegrationTests.Clients; +using Xunit; + +namespace Persistence.IntegrationTests.Controllers +{ + public class SetpointControllerTest : BaseIntegrationTest + { + private ISetpointClient client; + private class TestObject + { + public string? value1 { get; set; } + public int value2 { get; set; } + public double value3 { get; set; } + } + public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory) + { + client = factory.GetHttpClient(string.Empty); + } + + [Fact] + public async Task GetHistoryAsync_returns_success() + { + //arrange + var setpointKeys = new List() + { + Guid.NewGuid(), + Guid.NewGuid() + }; + var historyMoment = DateTimeOffset.Now.ToUniversalTime(); + + //act + var response = await client.GetHistoryAsync(setpointKeys, historyMoment); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + + [Fact] + public async Task GetLogAsync_returns_success() + { + //arrange + var setpointKeys = new List() + { + Guid.NewGuid(), + Guid.NewGuid() + }; + + //act + var response = await client.GetLogAsync(setpointKeys); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + + [Fact] + public async Task SaveAsync_returns_success() + { + //arrange + var setpointKey = Guid.NewGuid(); + var setpointValue = new TestObject() + { + value1 = "1", + value2 = 2, + value3 = 3.3 + }; + + //act + var response = await client.SaveAsync(setpointKey, setpointValue); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + } +} diff --git a/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj b/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj index 2b793de..912eeda 100644 --- a/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj +++ b/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj @@ -24,6 +24,7 @@ + diff --git a/Persistence.IntegrationTests/WebAppFactoryFixture.cs b/Persistence.IntegrationTests/WebAppFactoryFixture.cs index ea6016d..dd95ac0 100644 --- a/Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -8,6 +8,7 @@ using Persistence.API; using Refit; using System.Net.Http.Headers; using System.Text.Json; +using Persistence.Database.Postgres; namespace Persistence.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory @@ -51,7 +52,7 @@ public class WebAppFactoryFixture : WebApplicationFactory var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); - //dbContext.Database.EnsureCreatedAndMigrated(); + dbContext.Database.EnsureCreatedAndMigrated(); //dbContext.Deposits.AddRange(Data.Defaults.Deposits); dbContext.SaveChanges(); }); diff --git a/Persistence.Repository/Repositories/SetpointRepository.cs b/Persistence.Repository/Repositories/SetpointRepository.cs index 04304ce..ef346e6 100644 --- a/Persistence.Repository/Repositories/SetpointRepository.cs +++ b/Persistence.Repository/Repositories/SetpointRepository.cs @@ -60,9 +60,8 @@ namespace Persistence.Repository.Repositories return result; } - catch(Exception ex) + catch (Exception) { - var t = ex.Message; return 0; } }