From 457579c88d95b2fb24428e0d034936ed32c8d4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A8=D0=B8=D0=B1=D0=B0=D0=BB=D0=BA=D0=B8=D0=BD=20=D0=90?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Fri, 17 Jan 2025 13:54:26 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D1=80=D0=B5?= =?UTF-8?q?=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D0=B5=D0=B2=20?= =?UTF-8?q?+=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=82=20=D0=BD=D0=B5=D0=BA?= =?UTF-8?q?=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?ValueKind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/SetpointController.cs | 3 +- DD.Persistence.Database/Entity/Setpoint.cs | 3 +- .../DD.Persistence.Repository.Test.csproj | 30 ++++++++++ .../RepositoryTestFixture.cs | 32 +++++++++++ .../SetpointRepositoryShould.cs | 56 +++++++++++++++++++ .../Repositories/SetpointRepository.cs | 3 +- DD.Persistence.sln | 6 ++ .../Repositories/ISetpointRepository.cs | 3 +- 8 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 DD.Persistence.Repository.Test/DD.Persistence.Repository.Test.csproj create mode 100644 DD.Persistence.Repository.Test/RepositoryTestFixture.cs create mode 100644 DD.Persistence.Repository.Test/SetpointRepositoryShould.cs diff --git a/DD.Persistence.API/Controllers/SetpointController.cs b/DD.Persistence.API/Controllers/SetpointController.cs index e3b8b14..1d4c285 100644 --- a/DD.Persistence.API/Controllers/SetpointController.cs +++ b/DD.Persistence.API/Controllers/SetpointController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using DD.Persistence.Models; using DD.Persistence.Repositories; using System.Net; +using System.Text.Json; namespace DD.Persistence.API.Controllers; @@ -104,7 +105,7 @@ public class SetpointController : ControllerBase, ISetpointApi public async Task Add(Guid setpointKey, object newValue, CancellationToken token) { var userId = User.GetUserId(); - await setpointRepository.Add(setpointKey, newValue, userId, token); + await setpointRepository.Add(setpointKey, (JsonElement)newValue, userId, token); return CreatedAtAction(nameof(Add), true); } diff --git a/DD.Persistence.Database/Entity/Setpoint.cs b/DD.Persistence.Database/Entity/Setpoint.cs index 94eca3f..75bca4f 100644 --- a/DD.Persistence.Database/Entity/Setpoint.cs +++ b/DD.Persistence.Database/Entity/Setpoint.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json; namespace DD.Persistence.Database.Model { @@ -10,7 +11,7 @@ namespace DD.Persistence.Database.Model public Guid Key { get; set; } [Column(TypeName = "jsonb"), Comment("Значение уставки")] - public required object Value { get; set; } + public required JsonElement Value { get; set; } [Comment("Дата создания уставки")] public DateTimeOffset Created { get; set; } diff --git a/DD.Persistence.Repository.Test/DD.Persistence.Repository.Test.csproj b/DD.Persistence.Repository.Test/DD.Persistence.Repository.Test.csproj new file mode 100644 index 0000000..4f29d86 --- /dev/null +++ b/DD.Persistence.Repository.Test/DD.Persistence.Repository.Test.csproj @@ -0,0 +1,30 @@ + + + + net9.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + + + + + diff --git a/DD.Persistence.Repository.Test/RepositoryTestFixture.cs b/DD.Persistence.Repository.Test/RepositoryTestFixture.cs new file mode 100644 index 0000000..81e3694 --- /dev/null +++ b/DD.Persistence.Repository.Test/RepositoryTestFixture.cs @@ -0,0 +1,32 @@ + +using DD.Persistence.Database; +using DD.Persistence.Database.Model; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; +using Testcontainers.PostgreSql; +using Xunit; + +namespace DD.Persistence.Repository.Test; + +public class RepositoryTestFixture : IAsyncLifetime +{ + public readonly PostgreSqlContainer dbContainer = new PostgreSqlBuilder().Build(); + + + public PersistencePostgresContext GetDbContext() => new(new DbContextOptionsBuilder() + .UseNpgsql(dbContainer.GetConnectionString()).Options); + + public IMemoryCache GetMemoryCache() => new MemoryCache(new MemoryCacheOptions()); + + public virtual async Task InitializeAsync() + { + await dbContainer.StartAsync(); + var forumDbContext = new PersistencePostgresContext(new DbContextOptionsBuilder() + .UseNpgsql(dbContainer.GetConnectionString()).Options); + + await forumDbContext.Database.MigrateAsync(); + } + + public async Task DisposeAsync() => await dbContainer.DisposeAsync(); +} + diff --git a/DD.Persistence.Repository.Test/SetpointRepositoryShould.cs b/DD.Persistence.Repository.Test/SetpointRepositoryShould.cs new file mode 100644 index 0000000..6b05ff3 --- /dev/null +++ b/DD.Persistence.Repository.Test/SetpointRepositoryShould.cs @@ -0,0 +1,56 @@ +using DD.Persistence.Database.Model; +using DD.Persistence.Repository.Repositories; +using Shouldly; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace DD.Persistence.Repository.Test; +public class SetpointRepositoryShould : IClassFixture +{ + private readonly RepositoryTestFixture fixture; + private readonly PersistencePostgresContext context; + private readonly SetpointRepository sut; + + public SetpointRepositoryShould(RepositoryTestFixture fixture) + { + this.fixture = fixture; + context = fixture.GetDbContext(); + sut = new SetpointRepository(context); + } + + [Fact] + public async Task ReturnValueKindNumber() + { + var id = Guid.NewGuid(); + var value = GetJsonFromObject(22); + await sut.Add(id, value, Guid.NewGuid(), CancellationToken.None); + + var t = fixture.dbContainer.GetConnectionString(); + //act + var result = await sut.GetCurrent([id], CancellationToken.None); + + + //assert + result.ShouldNotBeNull(); + result.ShouldNotBeEmpty(); + + var setpoint = result.First(); + + setpoint.Value.ShouldNotBeNull(); + setpoint + .Value.ShouldBeOfType() + .ValueKind.ShouldBe(JsonValueKind.Number); + } + + private JsonElement GetJsonFromObject(object value) + { + var jsonString = JsonSerializer.Serialize(value); + var doc = JsonDocument.Parse(jsonString); + return doc.RootElement; + } + +} diff --git a/DD.Persistence.Repository/Repositories/SetpointRepository.cs b/DD.Persistence.Repository/Repositories/SetpointRepository.cs index 57cb564..321d881 100644 --- a/DD.Persistence.Repository/Repositories/SetpointRepository.cs +++ b/DD.Persistence.Repository/Repositories/SetpointRepository.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using DD.Persistence.Database.Model; using DD.Persistence.Models; using DD.Persistence.Repositories; +using System.Text.Json; namespace DD.Persistence.Repository.Repositories { @@ -93,7 +94,7 @@ namespace DD.Persistence.Repository.Repositories return dtos; } - public async Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token) + public async Task Add(Guid setpointKey, JsonElement newValue, Guid idUser, CancellationToken token) { var entity = new Setpoint() { diff --git a/DD.Persistence.sln b/DD.Persistence.sln index 5fb5aee..22c99b2 100644 --- a/DD.Persistence.sln +++ b/DD.Persistence.sln @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DD.Persistence.App", "DD.Pe EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Persistence.Models", "DD.Persistence.Models\DD.Persistence.Models.csproj", "{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Persistence.Repository.Test", "DD.Persistence.Repository.Test\DD.Persistence.Repository.Test.csproj", "{08B03623-A1C9-482F-B60E-09F293E04999}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Debug|Any CPU.Build.0 = Debug|Any CPU {698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Release|Any CPU.ActiveCfg = Release|Any CPU {698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Release|Any CPU.Build.0 = Release|Any CPU + {08B03623-A1C9-482F-B60E-09F293E04999}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {08B03623-A1C9-482F-B60E-09F293E04999}.Debug|Any CPU.Build.0 = Debug|Any CPU + {08B03623-A1C9-482F-B60E-09F293E04999}.Release|Any CPU.ActiveCfg = Release|Any CPU + {08B03623-A1C9-482F-B60E-09F293E04999}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DD.Persistence/Repositories/ISetpointRepository.cs b/DD.Persistence/Repositories/ISetpointRepository.cs index 0af805d..016a6c5 100644 --- a/DD.Persistence/Repositories/ISetpointRepository.cs +++ b/DD.Persistence/Repositories/ISetpointRepository.cs @@ -1,4 +1,5 @@ using DD.Persistence.Models; +using System.Text.Json; namespace DD.Persistence.Repositories; @@ -58,5 +59,5 @@ public interface ISetpointRepository /// /// to do /// id User учесть в соответствующем методе репозитория - Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token); + Task Add(Guid setpointKey, JsonElement newValue, Guid idUser, CancellationToken token); }