Добавлены тесты репозиториев + исправлен возврат некорректного ValueKind
All checks were successful
Unit tests / test (push) Successful in 1m0s

This commit is contained in:
Шибалкин Александр 2025-01-17 13:54:26 +05:00
parent 2a0fa0f3f9
commit 457579c88d
8 changed files with 132 additions and 4 deletions

View File

@ -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<IActionResult> Add(Guid setpointKey, object newValue, CancellationToken token)
{
var userId = User.GetUserId<Guid>();
await setpointRepository.Add(setpointKey, newValue, userId, token);
await setpointRepository.Add(setpointKey, (JsonElement)newValue, userId, token);
return CreatedAtAction(nameof(Add), true);
}

View File

@ -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; }

View File

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Testcontainers" Version="4.1.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.1.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.extensibility.core" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DD.Persistence.Database.Postgres\DD.Persistence.Database.Postgres.csproj" />
<ProjectReference Include="..\DD.Persistence.Repository\DD.Persistence.Repository.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>

View File

@ -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<PersistencePostgresContext>()
.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<PersistencePostgresContext>()
.UseNpgsql(dbContainer.GetConnectionString()).Options);
await forumDbContext.Database.MigrateAsync();
}
public async Task DisposeAsync() => await dbContainer.DisposeAsync();
}

View File

@ -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<RepositoryTestFixture>
{
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<JsonElement>()
.ValueKind.ShouldBe(JsonValueKind.Number);
}
private JsonElement GetJsonFromObject(object value)
{
var jsonString = JsonSerializer.Serialize(value);
var doc = JsonDocument.Parse(jsonString);
return doc.RootElement;
}
}

View File

@ -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()
{

View File

@ -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

View File

@ -1,4 +1,5 @@
using DD.Persistence.Models;
using System.Text.Json;
namespace DD.Persistence.Repositories;
@ -58,5 +59,5 @@ public interface ISetpointRepository
/// <returns></returns>
/// to do
/// id User учесть в соответствующем методе репозитория
Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token);
Task Add(Guid setpointKey, JsonElement newValue, Guid idUser, CancellationToken token);
}