persistence/Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs

56 lines
1.6 KiB
C#

using System.Net;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Client;
using Persistence.Client.Clients;
using Xunit;
namespace Persistence.IntegrationTests.Controllers;
public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrationTest
where TEntity : class
where TDto : class, new()
{
private ITimeSeriesClient<TDto> timeSeriesClient;
public TimeSeriesBaseControllerTest(WebAppFactoryFixture factory) : base(factory)
{
dbContext.CleanupDbSet<TEntity>();
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
timeSeriesClient = persistenceClientFactory.GetClient<ITimeSeriesClient<TDto>>();
}
public async Task InsertRangeSuccess(TDto dto)
{
//arrange
var expected = dto.Adapt<TDto>();
//act
var response = await timeSeriesClient.InsertRangeAsync(new TDto[] { expected });
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
}
public async Task GetSuccess(DateTimeOffset beginDate, DateTimeOffset endDate, TEntity entity)
{
//arrange
var dbset = dbContext.Set<TEntity>();
dbset.Add(entity);
dbContext.SaveChanges();
var response = await timeSeriesClient.GetAsync(beginDate, endDate);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Single(response.Content);
}
}