using AsbCloudApp.Data; using AsbCloudDb.Model; using AsbCloudWebApi.IntegrationTests.Clients; using System.Net; using System.Reflection; using AsbCloudApp.Requests; using AsbCloudWebApi.IntegrationTests.Data; using Mapster; using Microsoft.EntityFrameworkCore; using Refit; using Xunit; namespace AsbCloudWebApi.IntegrationTests.Controllers; public class WellOperationControllerTest : BaseIntegrationTest { private IWellOperationClient client; public WellOperationControllerTest(WebAppFactoryFixture factory) : base(factory) { client = factory.GetAuthorizedHttpClient(string.Empty); dbContext.CleanupDbSet(); } /// /// Успешное добавление операций (без предварительной очистки данных) /// /// [Fact] public async Task InsertRange_returns_success() { //arrange var well = await dbContext.Wells.FirstAsync(); var entity = CreateWellOperation(well.Id); var dtos = new[] { entity.Adapt() }; //act var response = await client.InsertRangeAsync(well.Id, 1, false, dtos); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } /// /// Успешное добавление операций (с предварительной очисткой данных) /// /// [Fact] public async Task InsertRangeWithDeleteBefore_returns_success() { //arrange var well = await dbContext.Wells.FirstAsync(); var entity = CreateWellOperation(well.Id); var dtos = new[] { entity.Adapt() }; //act var response = await client.InsertRangeAsync(well.Id, 1, true, dtos); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } /// /// Успешное обновление операции /// /// [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(); //act var response = await client.UpdateAsync(well.Id, entity.Id, dto, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } /// /// Получение плановых операций /// /// [Fact] public async Task GetPageOperationsPlanAsync_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(); var timezoneOffset = TimeSpan.FromHours(well.Timezone.Hours); dto.DateStart = dto.DateStart.ToOffset(timezoneOffset); var request = new WellOperationRequestBase { OperationType = WellOperation.IdOperationTypePlan }; //act var response = await client.GetPageOperationsPlanAsync(well.Id, request, CancellationToken.None); //assert Assert.NotNull(response.Content); Assert.Single(response.Content.Items); var actualDto = response.Content.Items.First(); var excludeProps = new[] { nameof(WellOperationDto.LastUpdateDate) }; MatchHelper.Match(dto, actualDto, excludeProps); } [Fact] public async Task ImportPlanDefaultExcelFileAsync_returns_success() { //arrange var stream = Assembly.GetExecutingAssembly().GetFileCopyStream("WellOperationsPlan.xlsx"); var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); memoryStream.Position = 0; var well = await dbContext.Wells.FirstAsync(); //act var streamPart = new StreamPart(memoryStream, "WellOperations.xlsx", "application/octet-stream"); var response = await client.ImportPlanDefaultExcelFileAsync(well.Id, new[] { streamPart }, CancellationToken.None); //assert Assert.NotNull(response.Content); Assert.Equal(4, response.Content.Count()); Assert.True(response.Content.All(w => Math.Abs(w.DateStart.Offset.Hours - Defaults.Timezone.Hours) < 0.1)); } 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 }; }