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(); witsDataClient = persistenceClientFactory.GetClient(); } [Fact] public async Task GetDatesRangeAsync_returns_success() { //arrange dbContext.CleanupDbSet(); var discriminatorId = 1; //act var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); } [Fact] public async Task GetPart_returns_success() { //arrange dbContext.CleanupDbSet(); var discriminatorId = 1; var dateBegin = DateTimeOffset.UtcNow; var take = 1; //act var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, new CancellationToken()); //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(); //act await AddRange(); } [Fact] public async Task GetValuesForGraph_returns_success() { //arrange dbContext.CleanupDbSet(); var discriminatorId = 1; var dateFrom = DateTimeOffset.UtcNow; var dateTo = DateTimeOffset.UtcNow; var approxPointCount = 12; //act var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, new CancellationToken()); //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(); var dtos = await AddRange(); var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId; //act var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, new CancellationToken()); //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) .Min() .ToString("dd.MM.yyyy-HH:mm:ss"); var actualDateTo = response.Content.From.DateTime .ToString("dd.MM.yyyy-HH:mm:ss"); Assert.Equal(expectedDateTo, actualDateTo); } [Fact] public async Task GetPart_AfterSave_returns_success() { //arrange dbContext.CleanupDbSet(); 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, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); Assert.Equal(take, response.Content.Count()); } [Fact] public async Task GetValuesForGraph_AfterSave_returns_success() { //arrange dbContext.CleanupDbSet(); 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, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Equal(approxPointCount, response.Content.Count()); } [Fact] public async Task InsertRange_returns_BadRequest() { //arrange var dtos = new List() { new WitsDataDto() { DiscriminatorId = -1, // < 0 Timestamped = DateTimeOffset.UtcNow, Values = new List() { new WitsValueDto() { RecordId = -1, // < 0 ItemId = 101, // > 100 Value = string.Empty } } } }; //act var response = await witsDataClient.AddRange(dtos, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); } private async Task> AddRange(int countToCreate = 10) { var dtos = new List(); var timestamped = DateTimeOffset.UtcNow; for (var i = 0; i < countToCreate && i < 100; i++) { dtos.Add(new WitsDataDto() { DiscriminatorId = 1, Timestamped = timestamped.AddSeconds(i), Values = new List() { new WitsValueDto() { RecordId = i + 1, ItemId = i + 1, Value = new Random().Next(1, 100) } } }); } //act var response = await witsDataClient.AddRange(dtos, new CancellationToken()); //assert var count = dtos.SelectMany(e => e.Values).Count(); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(count, response.Content); return dtos; } }