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

200 lines
5.7 KiB
C#
Raw Normal View History

using System.Net;
using System.Reflection;
using AsbCloudApp.Data.WellOperation;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
using AsbCloudWebApi.IntegrationTests.Clients;
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.WellOperations;
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, false, dtos);
2024-03-11 15:19:50 +05:00
//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, true, dtos);
2024-03-11 15:19:50 +05:00
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
/// <summary>
/// Успешное обновление операций
2024-03-11 15:19:50 +05:00
/// </summary>
/// <returns></returns>
[Fact]
public async Task UpdateRangeAsync_returns_success()
2024-03-11 15:19:50 +05:00
{
//arrange
var well = await dbContext.Wells.FirstAsync();
var entity = CreateWellOperation(well.Id);
dbContext.WellOperations.Add(entity);
await dbContext.SaveChangesAsync();
var dtos = new[] { entity.Adapt<WellOperationDto>() };
2024-03-11 15:19:50 +05:00
//act
var response = await client.UpdateRangeAsync(well.Id, dtos);
2024-03-11 15:19:50 +05:00
//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);
dto.LastUpdateDate = dto.LastUpdateDate?.ToOffset(timezoneOffset);
var request = new WellOperationRequest
{
OperationType = WellOperation.IdOperationTypePlan
};
//act
var response = await client.GetPageOperationsPlanAsync(well.Id, request);
//assert
Assert.Equal(response.StatusCode, HttpStatusCode.OK);
Assert.NotNull(response.Content);
Assert.Single(response.Content.Items);
2024-03-11 15:19:50 +05:00
var actualDto = response.Content.Items.First();
MatchHelper.Match(dto, actualDto);
}
[Theory]
[InlineData(WellOperation.IdOperationTypePlan, "PlanWellOperations.xlsx")]
[InlineData(WellOperation.IdOperationTypeFact, "FactWellOperations.xlsx")]
public async Task ParseAsync_returns_success(int idType, string fileName)
{
//arrange
var well = await dbContext.Wells.FirstAsync();
var expectedDto = new WellOperationDto
{
IdWell = well.Id,
IdWellSectionType = 2,
IdCategory = WellOperationCategory.IdSlide,
IdPlan = null,
CategoryInfo = "Доп.инфо",
IdType = idType,
DepthStart = 10.0,
DepthEnd = 20.0,
DateStart = new DateTimeOffset(new DateTime(2023, 1, 10), TimeSpan.FromHours(well.Timezone.Hours)),
DurationHours = 1.0,
Comment = "123",
};
var stream = Assembly.GetExecutingAssembly().GetFileCopyStream(fileName);
2024-03-11 15:19:50 +05:00
var streamPart = new StreamPart(stream, fileName, "application/octet-stream");
//act
var response = await client.ParseAsync(well.Id, idType, streamPart);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
var actualDto = response.Content.Item.First().Item;
MatchHelper.Match(expectedDto, actualDto);
}
[Theory]
[InlineData(WellOperation.IdOperationTypePlan)]
[InlineData(WellOperation.IdOperationTypeFact)]
public async Task ExportAsync_returns_success(int idType)
{
//arrange
var well = await dbContext.Wells.FirstAsync();
var entity = CreateWellOperation(well.Id, idType);
dbContext.WellOperations.Add(entity);
await dbContext.SaveChangesAsync();
//act
var response = await client.ExportAsync(well.Id, idType);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("application/octet-stream", response.ContentHeaders?.ContentType?.MediaType);
Assert.True(response.ContentHeaders?.ContentLength > 0);
}
2024-03-11 15:19:50 +05:00
private static WellOperation CreateWellOperation(int idWell, int idType = WellOperation.IdOperationTypePlan) =>
2024-03-11 15:19:50 +05:00
new()
{
IdWell = idWell,
IdWellSectionType = 2,
IdCategory = WellOperationCategory.IdSlide,
2024-03-11 15:19:50 +05:00
IdPlan = null,
CategoryInfo = "Доп.инфо",
LastUpdateDate = new DateTimeOffset(new DateTime(2023, 1, 10)).ToUniversalTime(),
IdType = idType,
2024-03-11 15:19:50 +05:00
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,
2024-03-11 15:19:50 +05:00
};
}