Добавить тестирование Setpoint API

This commit is contained in:
Roman Efremov 2024-11-18 11:32:57 +05:00
parent ef72d985be
commit 90c62b9ede
6 changed files with 98 additions and 5 deletions

View File

@ -17,9 +17,9 @@ public abstract class BaseIntegrationTest : IClassFixture<WebAppFactoryFixture>,
protected BaseIntegrationTest(WebAppFactoryFixture factory) protected BaseIntegrationTest(WebAppFactoryFixture factory)
{ {
//scope = factory.Services.CreateScope(); scope = factory.Services.CreateScope();
//dbContext = scope.ServiceProvider.GetRequiredService<PersistenceDbContext>(); dbContext = scope.ServiceProvider.GetRequiredService<PersistenceDbContext>();
} }
public void Dispose() public void Dispose()

View File

@ -0,0 +1,17 @@
using Persistence.Models;
using Refit;
namespace Persistence.IntegrationTests.Clients
{
public interface ISetpointClient
{
[Post("/api/Setpoint/history")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistoryAsync(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment);
[Post("/api/Setpoint/log")]
Task<IApiResponse<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLogAsync(IEnumerable<Guid> setpoitKeys);
[Post("/api/Setpoint/save")]
Task<IApiResponse<int>> SaveAsync(Guid setpointKey, object newValue);
}
}

View File

@ -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<ISetpointClient>(string.Empty);
}
[Fact]
public async Task GetHistoryAsync_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
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>()
{
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);
}
}
}

View File

@ -24,6 +24,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Persistence.API\Persistence.API.csproj" /> <ProjectReference Include="..\Persistence.API\Persistence.API.csproj" />
<ProjectReference Include="..\Persistence.Database.Postgres\Persistence.Database.Postgres.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -8,6 +8,7 @@ using Persistence.API;
using Refit; using Refit;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text.Json; using System.Text.Json;
using Persistence.Database.Postgres;
namespace Persistence.IntegrationTests; namespace Persistence.IntegrationTests;
public class WebAppFactoryFixture : WebApplicationFactory<Startup> public class WebAppFactoryFixture : WebApplicationFactory<Startup>
@ -51,7 +52,7 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>
var scopedServices = scope.ServiceProvider; var scopedServices = scope.ServiceProvider;
var dbContext = scopedServices.GetRequiredService<PersistenceDbContext>(); var dbContext = scopedServices.GetRequiredService<PersistenceDbContext>();
//dbContext.Database.EnsureCreatedAndMigrated(); dbContext.Database.EnsureCreatedAndMigrated();
//dbContext.Deposits.AddRange(Data.Defaults.Deposits); //dbContext.Deposits.AddRange(Data.Defaults.Deposits);
dbContext.SaveChanges(); dbContext.SaveChanges();
}); });

View File

@ -60,9 +60,8 @@ namespace Persistence.Repository.Repositories
return result; return result;
} }
catch(Exception ex) catch (Exception)
{ {
var t = ex.Message;
return 0; return 0;
} }
} }