DD.WellWorkover.Cloud/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs

165 lines
4.6 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
2024-01-26 09:49:00 +05:00
using AsbCloudDb.Model;
using AsbCloudWebApi.IntegrationTests.Clients;
using System.Net;
using System.Reflection;
using AsbCloudApp.Requests;
using AsbCloudWebApi.IntegrationTests.Data;
2024-03-11 15:19:50 +05:00
using Mapster;
using Microsoft.EntityFrameworkCore;
using Refit;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests.Controllers;
public class WellOperationControllerTest : BaseIntegrationTest
{
2024-03-11 15:19:50 +05:00
private IWellOperationClient client;
2024-03-11 15:19:50 +05:00
public WellOperationControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
2024-03-11 15:19:50 +05:00
client = factory.GetAuthorizedHttpClient<IWellOperationClient>(string.Empty);
2024-03-11 15:19:50 +05:00
dbContext.CleanupDbSet<WellOperation>();
}
/// <summary>
/// Успешное добавление операций (без предварительной очистки данных)
/// </summary>
/// <returns></returns>
[Fact]
public async Task InsertRange_returns_success()
{
//arrange
var well = await dbContext.Wells.FirstAsync();
var entity = CreateWellOperation(well.Id);
var dtos = new[] { entity.Adapt<WellOperationDto>() };
//act
var response = await client.InsertRangeAsync(well.Id, 1, false, dtos);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
/// <summary>
/// Успешное добавление операций (с предварительной очисткой данных)
/// </summary>
/// <returns></returns>
[Fact]
public async Task InsertRangeWithDeleteBefore_returns_success()
{
//arrange
var well = await dbContext.Wells.FirstAsync();
var entity = CreateWellOperation(well.Id);
var dtos = new[] { entity.Adapt<WellOperationDto>() };
//act
var response = await client.InsertRangeAsync(well.Id, 1, true, dtos);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
/// <summary>
/// Успешное обновление операции
/// </summary>
/// <returns></returns>
[Fact]
public async Task UpdateAsync_returns_success()
{
//arrange
var well = await dbContext.Wells.FirstAsync();
var entity = CreateWellOperation(well.Id);
dbContext.WellOperations.Add(entity);
await dbContext.SaveChangesAsync();
var dto = entity.Adapt<WellOperationDto>();
//act
var response = await client.UpdateAsync(well.Id, entity.Id, dto, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
/// <summary>
/// Получение плановых операций
/// </summary>
/// <returns></returns>
[Fact]
public async Task GetPageOperationsPlanAsync_returns_success()
{
//arrange
2024-03-11 15:19:50 +05:00
var well = await dbContext.Wells.FirstAsync();
var entity = CreateWellOperation(well.Id);
dbContext.WellOperations.Add(entity);
await dbContext.SaveChangesAsync();
var dto = entity.Adapt<WellOperationDto>();
var timezoneOffset = TimeSpan.FromHours(well.Timezone.Hours);
dto.DateStart = dto.DateStart.ToOffset(timezoneOffset);
var request = new WellOperationRequestBase
{
OperationType = WellOperation.IdOperationTypePlan
};
//act
2024-03-11 15:19:50 +05:00
var response = await client.GetPageOperationsPlanAsync(well.Id, request, CancellationToken.None);
//assert
Assert.NotNull(response.Content);
Assert.Single(response.Content.Items);
2024-03-11 15:19:50 +05:00
var actualDto = response.Content.Items.First();
2024-03-11 15:19:50 +05:00
var excludeProps = new[]
{
nameof(WellOperationDto.LastUpdateDate)
};
MatchHelper.Match(dto, actualDto, excludeProps);
}
[Fact]
public async Task ImportPlanDefaultExcelFileAsync_returns_success()
{
//arrange
2024-03-11 15:19:50 +05:00
var stream = Assembly.GetExecutingAssembly().GetFileCopyStream("WellOperationsPlan.xlsx");
var memoryStream = new MemoryStream();
2024-03-11 15:19:50 +05:00
await stream.CopyToAsync(memoryStream);
memoryStream.Position = 0;
2024-03-11 15:19:50 +05:00
var well = await dbContext.Wells.FirstAsync();
//act
var streamPart = new StreamPart(memoryStream, "WellOperations.xlsx", "application/octet-stream");
2024-03-11 15:19:50 +05:00
var response = await client.ImportPlanDefaultExcelFileAsync(well.Id, new[] { streamPart }, CancellationToken.None);
//assert
Assert.NotNull(response.Content);
Assert.Equal(4, response.Content.Count());
2024-03-11 15:19:50 +05:00
Assert.True(response.Content.All(w => Math.Abs(w.DateStart.Offset.Hours - Defaults.Timezone.Hours) < 0.1));
}
2024-03-11 15:19:50 +05:00
private static WellOperation CreateWellOperation(int idWell) =>
new()
{
IdWell = idWell,
IdWellSectionType = 2,
LastUpdateDate = DateTimeOffset.UtcNow,
IdCategory = 5000,
IdPlan = null,
CategoryInfo = "1",
IdType = 0,
DepthStart = 10.0,
DepthEnd = 20.0,
DateStart = new DateTimeOffset(new DateTime(2023, 1, 10), TimeSpan.FromHours(Defaults.Timezone.Hours)).ToUniversalTime(),
DurationHours = 1.0,
Comment = "1",
IdUser = 1
};
}