persistence/Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs

235 lines
6.0 KiB
C#

using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Client;
using Persistence.Client.Clients;
using Persistence.Database.Entity;
using Persistence.Models;
using Xunit;
namespace Persistence.IntegrationTests.Controllers;
public class WitsDataControllerTest : BaseIntegrationTest
{
private IWitsDataClient witsDataClient;
public WitsDataControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
witsDataClient = persistenceClientFactory.GetClient<IWitsDataClient>();
}
[Fact]
public async Task GetDatesRangeAsync_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
var discriminatorId = 1;
//act
var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
}
[Fact]
public async Task GetPart_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
var discriminatorId = 1;
var dateBegin = DateTimeOffset.UtcNow;
var take = 1;
//act
var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task InsertRange_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
//act
await AddRange();
}
[Fact]
public async Task GetValuesForGraph_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
var discriminatorId = 1;
var dateFrom = DateTimeOffset.UtcNow;
var dateTo = DateTimeOffset.UtcNow;
var approxPointCount = 12;
//act
var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task GetDatesRangeAsync_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
var dtos = await AddRange();
var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
//act
var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
var expectedDateFrom = dtos
.Select(e => e.Timestamped)
.Min()
.ToString("dd.MM.yyyy-HH:mm:ss");
var actualDateFrom = response.Content.From.DateTime
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedDateFrom, actualDateFrom);
var expectedDateTo = dtos
.Select(e => e.Timestamped)
.Max()
.ToString("dd.MM.yyyy-HH:mm:ss");
var actualDateTo = response.Content.To.DateTime
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedDateTo, actualDateTo);
}
[Fact]
public async Task GetPart_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
var dtos = await AddRange();
var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
var dateBegin = dtos.FirstOrDefault()!.Timestamped;
var take = 1;
//act
var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(take, response.Content.Count());
var expectedDto = dtos.FirstOrDefault();
var actualDto = response.Content.FirstOrDefault();
Assert.Equal(expectedDto?.DiscriminatorId, actualDto?.DiscriminatorId);
var expectedValueDto = expectedDto?.Values.FirstOrDefault();
var actualValueDto = actualDto?.Values.FirstOrDefault();
Assert.Equal(expectedValueDto?.ItemId, actualValueDto?.ItemId);
Assert.Equal(expectedValueDto?.RecordId, actualValueDto?.RecordId);
}
[Fact]
public async Task GetValuesForGraph_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
var dtos = await AddRange(37);
var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
var dateFrom = dtos.Select(e => e.Timestamped).Min();
var dateTo = dtos.Select(e => e.Timestamped).Max();
var approxPointCount = 12;
//act
var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal(approxPointCount, response.Content.Count());
}
[Fact]
public async Task AddRange_returns_BadRequest()
{
//arrange
var dtos = new List<WitsDataDto>()
{
new WitsDataDto()
{
DiscriminatorId = -1, // < 0
Timestamped = DateTimeOffset.UtcNow,
Values = new List<WitsValueDto>()
{
new WitsValueDto()
{
RecordId = -1, // < 0
ItemId = 101, // > 100
Value = string.Empty
}
}
}
};
//act
var response = await witsDataClient.AddRange(dtos, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
private async Task<IEnumerable<WitsDataDto>> AddRange(int countToCreate = 10)
{
var dtos = new List<WitsDataDto>();
var timestamped = DateTimeOffset.UtcNow;
for (var i = 0; i < countToCreate; i++)
{
var random = new Random();
dtos.Add(new WitsDataDto()
{
DiscriminatorId = 1,
Timestamped = timestamped.AddSeconds(i),
Values = new List<WitsValueDto>()
{
new WitsValueDto()
{
RecordId = i + 1,
ItemId = i + 1,
Value = random.Next(1, 100)
}
}
});
}
//act
var response = await witsDataClient.AddRange(dtos, CancellationToken.None);
//assert
var count = dtos.SelectMany(e => e.Values).Count();
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.Equal(count, response.Content);
return dtos;
}
}