persistence/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs

179 lines
4.7 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 TechMessagesControllerTest : BaseIntegrationTest
{
private readonly ITechMessagesClient techMessagesClient;
public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
techMessagesClient = persistenceClientFactory.GetClient<ITechMessagesClient>();
}
[Fact]
public async Task GetPage_returns_success()
{
//arrange
dbContext.CleanupDbSet<TechMessage>();
var requestDto = new RequestDto()
{
Skip = 1,
Take = 2,
SortSettings = nameof(TechMessageDto.ImportantId)
};
//act
var response = await techMessagesClient.GetPage(requestDto, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content.Items);
Assert.Equal(requestDto.Skip, response.Content.Skip);
Assert.Equal(requestDto.Take, response.Content.Take);
}
[Fact]
public async Task GetPage_AfterSave_returns_success()
{
//arrange
var dtos = await InsertRange();
var dtosCount = dtos.Count();
var requestDto = new RequestDto()
{
Skip = 0,
Take = 2,
SortSettings = nameof(TechMessageDto.ImportantId)
};
//act
var response = await techMessagesClient.GetPage(requestDto, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal(dtosCount, response.Content.Count);
}
[Fact]
public async Task InsertRange_returns_success()
{
await InsertRange();
}
[Fact]
public async Task GetSystems_returns_success()
{
//act
dbContext.CleanupDbSet<TechMessage>();
var response = await techMessagesClient.GetSystems(new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task GetSystems_AfterSave_returns_success()
{
//arrange
var dtos = await InsertRange();
var systems = dtos
.Select(e => e.AutoDrillingSystem)
.Distinct()
.ToArray();
//act
var response = await techMessagesClient.GetSystems(new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
string?[]? content = response.Content?.ToArray();
Assert.Equal(systems, content);
}
[Fact]
public async Task GetStatistics_returns_success()
{
//arrange
dbContext.CleanupDbSet<TechMessage>();
var imortantId = 1;
var autoDrillingSystem = nameof(TechMessageDto.AutoDrillingSystem);
//act
var response = await techMessagesClient.GetStatistics(imortantId, autoDrillingSystem, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(0, response.Content);
}
[Fact]
public async Task GetStatistics_AfterSave_returns_success()
{
//arrange
var imortantId = 1;
var autoDrillingSystem = nameof(TechMessageDto.AutoDrillingSystem);
var dtos = await InsertRange();
var filteredDtos = dtos.Where(e => e.ImportantId == imortantId && e.AutoDrillingSystem == e.AutoDrillingSystem);
//act
var response = await techMessagesClient.GetStatistics(imortantId, autoDrillingSystem, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(filteredDtos.Count(), response.Content);
}
public async Task<IEnumerable<TechMessageDto>> InsertRange()
{
//arrange
var dtos = new List<TechMessageDto>()
{
new TechMessageDto()
{
EventId = Guid.NewGuid(),
ImportantId = 1,
OccurrenceDate = DateTimeOffset.UtcNow,
Depth = 1.11,
MessageText = nameof(TechMessageDto.MessageText),
AutoDrillingSystem = nameof(TechMessageDto.AutoDrillingSystem),
UserId = Guid.NewGuid()
},
new TechMessageDto()
{
EventId = Guid.NewGuid(),
ImportantId = 2,
OccurrenceDate = DateTimeOffset.UtcNow,
Depth = 2.22,
MessageText = nameof(TechMessageDto.MessageText),
AutoDrillingSystem = nameof(TechMessageDto.AutoDrillingSystem),
UserId = Guid.NewGuid()
}
};
//act
var response = await techMessagesClient.InsertRange(dtos, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.Equal(dtos.Count, response.Content);
return dtos;
}
}
}