using System.Net; using Mapster; using Microsoft.Extensions.DependencyInjection; using Persistence.Client; using Persistence.Client.Clients; using Persistence.Database.Model; using Xunit; namespace Persistence.IntegrationTests.Controllers; public abstract class TimeSeriesBaseControllerTest : BaseIntegrationTest where TEntity : class, ITimestampedData, new() where TDto : class, new() { private ITimeSeriesClient timeSeriesClient; public TimeSeriesBaseControllerTest(WebAppFactoryFixture factory) : base(factory) { dbContext.CleanupDbSet(); var scope = factory.Services.CreateScope(); var persistenceClientFactory = scope.ServiceProvider .GetRequiredService(); timeSeriesClient = persistenceClientFactory.GetClient>(); } public async Task InsertRangeSuccess(TDto dto) { //arrange var expected = dto.Adapt(); //act var response = await timeSeriesClient.InsertRange(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(); dbset.Add(entity); dbContext.SaveChanges(); var response = await timeSeriesClient.Get(beginDate, endDate); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Single(response.Content); } public async Task GetDatesRangeSuccess(TEntity entity) { //arrange var datesRangeExpected = 30; var entity2 = entity.Adapt(); entity2.Date = entity.Date.AddDays(datesRangeExpected); var dbset = dbContext.Set(); dbset.Add(entity); dbset.Add(entity2); dbContext.SaveChanges(); var response = await timeSeriesClient.GetDatesRange(); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); var datesRangeActual = (response.Content.To - response.Content.From).Days; Assert.Equal(datesRangeExpected, datesRangeActual); } public async Task GetResampledDataSuccess(TEntity entity) { //arrange var approxPointsCount = 10; var differenceBetweenStartAndEndDays = 50; var entities = new List(); for (var i = 1; i <= differenceBetweenStartAndEndDays; i++) { var entity2 = entity.Adapt(); entity2.Date = entity.Date.AddDays(i - 1); entities.Add(entity2); } var dbset = dbContext.Set(); dbset.AddRange(entities); dbContext.SaveChanges(); var response = await timeSeriesClient.GetResampledData(entity.Date.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); var ratio = entities.Count() / approxPointsCount; if (ratio > 1) { var expectedResampledCount = entities .Where((_, index) => index % ratio == 0) .Count(); Assert.Equal(expectedResampledCount, response.Content.Count()); } else { Assert.Equal(entities.Count(), response.Content.Count()); } } }