DD.WellWorkover.Cloud/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs

623 lines
20 KiB
C#
Raw Normal View History

2024-02-21 15:08:51 +05:00
using AsbCloudApp.Requests;
2024-01-20 15:38:37 +05:00
using AsbCloudDb.Model.ProcessMapPlan;
2024-01-19 17:49:09 +05:00
using AsbCloudWebApi.IntegrationTests.Clients;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Net;
2024-02-09 11:33:03 +05:00
using System.Reflection;
using AsbCloudDb.Model.ProcessMaps;
using AsbCloudWebApi.IntegrationTests.Data;
using Refit;
2024-01-19 17:49:09 +05:00
using Xunit;
2024-02-21 15:08:51 +05:00
using AsbCloudApp.Data.ProcessMaps;
2024-01-19 17:49:09 +05:00
2024-02-09 11:33:03 +05:00
namespace AsbCloudWebApi.IntegrationTests.Controllers.ProcessMapPlan;
2024-01-19 17:49:09 +05:00
public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
2024-01-20 15:38:37 +05:00
private readonly ProcessMapPlanDrillingDto dto = new (){
2024-01-19 17:49:09 +05:00
Id = 0,
Creation = new(),
Obsolete = null,
IdState = 0,
IdPrevious = null,
2024-02-09 11:33:03 +05:00
2024-01-19 17:49:09 +05:00
IdWell = 1,
2024-02-09 11:33:03 +05:00
Section = "Кондуктор",
IdWellSectionType = 3,
2024-01-19 17:49:09 +05:00
DepthStart = 0.5,
DepthEnd = 1.5,
IdMode = 1,
Mode = "Ротор",
2024-01-19 17:49:09 +05:00
AxialLoadPlan = 2.718281,
AxialLoadLimitMax = 3.1415926,
DeltaPressurePlan = 4,
DeltaPressureLimitMax = 5,
TopDriveTorquePlan = 6,
TopDriveTorqueLimitMax = 7,
TopDriveSpeedPlan = 8,
TopDriveSpeedLimitMax = 9,
FlowPlan = 10,
FlowLimitMax = 11,
RopPlan = 12,
UsageSaub = 13,
UsageSpin = 14,
Comment = "это тестовая запись",
};
2024-01-20 15:38:37 +05:00
private readonly ProcessMapPlanDrilling entity = new ()
{
Id = 0,
IdAuthor = 1,
IdEditor = null,
Creation = DateTimeOffset.UtcNow,
Obsolete = null,
IdState = AsbCloudDb.Model.ChangeLogAbstract.IdStateActual,
IdPrevious = null,
IdWell = 1,
IdWellSectionType = 1,
DepthStart = 0.5,
DepthEnd = 1.5,
IdMode = 1,
AxialLoadPlan = 2.718281,
AxialLoadLimitMax = 3.1415926,
DeltaPressurePlan = 4,
DeltaPressureLimitMax = 5,
TopDriveTorquePlan = 6,
TopDriveTorqueLimitMax = 7,
TopDriveSpeedPlan = 8,
TopDriveSpeedLimitMax = 9,
FlowPlan = 10,
FlowLimitMax = 11,
RopPlan = 12,
UsageSaub = 13,
UsageSpin = 14,
Comment = "это тестовая запись",
};
2024-01-19 17:49:09 +05:00
2024-02-09 11:33:03 +05:00
private IProcessMapPlanDrillingClient client;
2024-01-19 17:49:09 +05:00
public ProcessMapPlanDrillingControllerTest(WebAppFactoryFixture factory) : base(factory)
{
dbContext.CleanupDbSet<ProcessMapPlanDrilling>();
client = factory.GetAuthorizedHttpClient<IProcessMapPlanDrillingClient>(string.Empty);
2024-01-19 17:49:09 +05:00
}
[Fact]
2024-01-20 15:38:37 +05:00
public async Task InsertRange_returns_success()
2024-01-19 17:49:09 +05:00
{
//arrange
2024-01-20 15:38:37 +05:00
var expected = dto.Adapt<ProcessMapPlanDrillingDto>();
2024-01-19 17:49:09 +05:00
//act
2024-01-20 15:38:37 +05:00
var response = await client.InsertRange(dto.IdWell, new[] { expected });
2024-01-19 17:49:09 +05:00
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
var entity = dbContext
.Set<ProcessMapPlanDrilling>()
2024-01-19 17:49:09 +05:00
.Where(p => p.AxialLoadPlan == dto.AxialLoadPlan)
.Where(p => p.AxialLoadLimitMax == dto.AxialLoadLimitMax)
.Where(p => p.Comment == dto.Comment)
.FirstOrDefault(p => p.IdWell == dto.IdWell);
2024-01-19 17:49:09 +05:00
Assert.NotNull(entity);
var actual = entity.Adapt<ProcessMapPlanDrillingDto>();
2024-01-20 15:38:37 +05:00
Assert.Equal(ProcessMapPlanBase.IdStateActual, actual.IdState);
var excludeProps = new[] {
nameof(ProcessMapPlanDrillingDto.Id),
nameof(ProcessMapPlanDrillingDto.IdState),
nameof(ProcessMapPlanDrillingDto.Author),
2024-01-20 15:38:37 +05:00
nameof(ProcessMapPlanDrillingDto.Creation),
2024-02-09 11:33:03 +05:00
nameof(ProcessMapPlanDrillingDto.Mode),
nameof(ProcessMapPlanDrillingDto.Section)
2024-01-20 15:38:37 +05:00
};
MatchHelper.Match(expected, actual, excludeProps);
}
[Fact]
public async Task InsertRange_returns_BadRequest_for_IdWellSectionType()
{
//arrange
var badDto = dto.Adapt<ProcessMapPlanDrillingDto>();
badDto.IdWellSectionType = int.MaxValue;
//act
var response = await client.InsertRange(dto.IdWell, new[] { badDto });
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
[Fact]
public async Task InsertRange_returns_BadRequest_for_IdMode()
{
//arrange
var badDto = dto.Adapt<ProcessMapPlanDrillingDto>();
badDto.IdMode = int.MaxValue;
//act
var response = await client.InsertRange(dto.IdWell, new[] { badDto });
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
[Fact]
public async Task InsertRange_returns_BadRequest_for_IdWell()
{
//arrange
var badDto = dto.Adapt<ProcessMapPlanDrillingDto>();
badDto.IdWell = int.MaxValue;
//act
var response = await client.InsertRange(dto.IdWell, new[] { badDto });
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
2024-01-20 15:38:37 +05:00
[Fact]
public async Task ClearAndInsertRange_returns_success()
{
// arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
var startTime = DateTimeOffset.UtcNow;
2024-01-20 15:38:37 +05:00
// act
var result = await client.ClearAndInsertRange(entity.IdWell, new ProcessMapPlanDrillingDto[] { dto });
// assert
var doneTime = DateTimeOffset.UtcNow;
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal(2, result.Content);
var count = dbset.Count();
Assert.Equal(2, count);
var oldEntity = dbset.First(p => p.Id == entry.Entity.Id);
Assert.Equal(ProcessMapPlanBase.IdCleared, oldEntity.IdState);
2024-01-20 15:38:37 +05:00
Assert.Equal(1, oldEntity.IdEditor);
Assert.NotNull(oldEntity.Obsolete);
Assert.InRange(oldEntity.Obsolete.Value, startTime, doneTime);
var newEntity = dbset.First(p => p.Id != entry.Entity.Id);
Assert.Equal(ProcessMapPlanBase.IdStateActual, newEntity.IdState);
Assert.Equal(1, newEntity.IdAuthor);
Assert.Null(newEntity.IdEditor);
Assert.Null(newEntity.Obsolete);
Assert.Null(newEntity.IdPrevious);
Assert.InRange(newEntity.Creation, startTime, doneTime);
}
[Fact]
public async Task UpdateOrInsertRange_returns_success()
2024-01-20 15:38:37 +05:00
{
// arrange
var startTime = DateTimeOffset.UtcNow;
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
var dtoUpdate = dto.Adapt<ProcessMapPlanDrillingDto>();
dtoUpdate.Id = entry.Entity.Id;
dtoUpdate.Comment = "nebuchadnezzar";
dtoUpdate.DeltaPressureLimitMax++;
dtoUpdate.DeltaPressurePlan++;
dtoUpdate.FlowPlan++;
dtoUpdate.FlowLimitMax++;
dtoUpdate.RopPlan++;
dtoUpdate.AxialLoadPlan++;
dtoUpdate.AxialLoadLimitMax++;
dtoUpdate.DepthStart++;
dtoUpdate.DepthEnd++;
dtoUpdate.TopDriveSpeedPlan++;
dtoUpdate.TopDriveSpeedLimitMax++;
dtoUpdate.TopDriveTorquePlan++;
dtoUpdate.TopDriveTorqueLimitMax++;
var dtoInsert = dtoUpdate.Adapt<ProcessMapPlanDrillingDto>();
dtoInsert.Id = 0;
dtoInsert.Comment = "nebuchad";
dtoInsert.DeltaPressureLimitMax++;
dtoInsert.DeltaPressurePlan++;
dtoInsert.FlowPlan++;
dtoInsert.FlowLimitMax++;
dtoInsert.RopPlan++;
dtoInsert.AxialLoadPlan++;
dtoInsert.AxialLoadLimitMax++;
dtoInsert.DepthStart++;
dtoInsert.DepthEnd++;
dtoInsert.TopDriveSpeedPlan++;
dtoInsert.TopDriveSpeedLimitMax++;
dtoInsert.TopDriveTorquePlan++;
dtoInsert.TopDriveTorqueLimitMax++;
2024-01-20 15:38:37 +05:00
// act
var result = await client.UpdateOrInsertRange(entity.IdWell, new ProcessMapPlanDrillingDto[] { dtoUpdate, dtoInsert });
2024-01-20 15:38:37 +05:00
// assert
var doneTime = DateTimeOffset.UtcNow;
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal(3, result.Content);
2024-01-20 15:38:37 +05:00
var count = dbset.Count();
Assert.Equal(3, count);
2024-01-20 15:38:37 +05:00
var oldEntity = dbset.First(p => p.Id == entry.Entity.Id);
Assert.Equal(ProcessMapPlanBase.IdStateReplaced, oldEntity.IdState);
Assert.Equal(1, oldEntity.IdEditor);
Assert.NotNull(oldEntity.Obsolete);
Assert.InRange(oldEntity.Obsolete.Value, startTime, doneTime);
var newEntity = dbset.First(p => p.Comment == dtoUpdate.Comment);
2024-01-20 15:38:37 +05:00
Assert.Equal(ProcessMapPlanBase.IdStateActual, newEntity.IdState);
Assert.Equal(1, newEntity.IdAuthor);
Assert.Null(newEntity.IdEditor);
Assert.Null(newEntity.Obsolete);
Assert.Equal(oldEntity.Id, newEntity.IdPrevious);
Assert.InRange(newEntity.Creation, startTime, doneTime);
var expected = dtoUpdate.Adapt<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
var excludeProps = new[] {
nameof(ProcessMapPlanDrilling.Id),
nameof(ProcessMapPlanDrilling.Author),
nameof(ProcessMapPlanDrilling.IdAuthor),
nameof(ProcessMapPlanDrilling.Editor),
nameof(ProcessMapPlanDrilling.Creation),
2024-01-20 15:38:37 +05:00
};
MatchHelper.Match(expected, newEntity!, excludeProps);
}
[Fact]
public async Task DeleteRange_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
var startTime = DateTimeOffset.UtcNow;
2024-01-20 15:38:37 +05:00
//act
var response = await client.DeleteRange(dto.IdWell, new[] { entry.Entity.Id });
//assert
var doneTime = DateTimeOffset.UtcNow;
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
var actual = dbContext
.Set<ProcessMapPlanDrilling>()
.FirstOrDefault(p => p.Id == entry.Entity.Id);
2024-01-20 15:38:37 +05:00
Assert.NotNull(actual);
Assert.Equal(ProcessMapPlanBase.IdStateDeleted, actual.IdState);
Assert.Equal(1, actual.IdEditor);
Assert.NotNull(actual.Obsolete);
Assert.InRange(actual.Obsolete.Value, startTime, doneTime);
}
[Fact]
public async Task Clear_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
var startTime = DateTimeOffset.UtcNow;
//act
var response = await client.Clear(dto.IdWell);
//assert
var doneTime = DateTimeOffset.UtcNow;
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
var actual = dbContext
.Set<ProcessMapPlanDrilling>()
.FirstOrDefault(p => p.Id == entry.Entity.Id);
Assert.NotNull(actual);
Assert.Equal(ProcessMapPlanBase.IdCleared, actual.IdState);
Assert.Equal(1, actual.IdEditor);
Assert.NotNull(actual.Obsolete);
Assert.InRange(actual.Obsolete.Value, startTime, doneTime);
}
2024-01-20 15:38:37 +05:00
[Fact]
public async Task GetDatesChange_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
var entity2 = entity.Adapt<ProcessMapPlanDrilling>();
entity2.Creation = entity.Creation.AddDays(1);
dbset.Add(entity);
dbset.Add(entity2);
dbContext.SaveChanges();
var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours;
var offset = TimeSpan.FromHours(timezoneHours);
var dates = new[] { entity.Creation, entity2.Creation }
.Select(d => d.ToOffset(offset))
.Select(d => new DateOnly(d.Year, d.Month, d.Day));
//act
var response = await client.GetDatesChange(dto.IdWell);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal(2, response.Content.Count());
Assert.All(response.Content, d => dates.Contains(d));
}
[Fact]
public async Task Get_all_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.Add(entity);
var entityDeleted = entity.Adapt<ProcessMapPlanDrilling>();
entityDeleted.Creation = entity.Creation.AddDays(-1);
entityDeleted.Obsolete = entity.Creation;
entityDeleted.IdState = ProcessMapPlanBase.IdStateDeleted;
entityDeleted.IdEditor = 1;
dbset.Add(entityDeleted);
dbContext.SaveChanges();
//act
2024-01-22 13:16:39 +05:00
var request = new ProcessMapPlanBaseRequest();
2024-01-20 15:38:37 +05:00
var response = await client.Get(dto.IdWell, request);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal(2, response.Content.Count());
}
[Fact]
public async Task Get_actual_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
dbset.Add(entity);
var entityDeleted = entity.Adapt<ProcessMapPlanDrilling>();
entityDeleted.Creation = entity.Creation.AddDays(-1);
entityDeleted.Obsolete = entity.Creation;
entityDeleted.IdState = ProcessMapPlanBase.IdStateDeleted;
entityDeleted.IdEditor = 1;
entityDeleted.Comment = "nothing";
dbset.Add(entityDeleted);
dbContext.SaveChanges();
//act
2024-01-22 13:16:39 +05:00
var request = new ProcessMapPlanBaseRequest {
2024-01-20 15:38:37 +05:00
Moment = new DateTimeOffset(3000, 1, 1, 0, 0, 0, 0, TimeSpan.Zero)
};
var response = await client.Get(dto.IdWell, request);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Single(response.Content);
var actual = response.Content.First()!;
var expected = entity.Adapt<ProcessMapPlanDrillingDto>()!;
var excludeProps = new[] {
nameof(ProcessMapPlanDrillingDto.Id),
nameof(ProcessMapPlanDrillingDto.Author),
2024-01-20 15:38:37 +05:00
nameof(ProcessMapPlanDrillingDto.Creation),
};
MatchHelper.Match(expected, actual, excludeProps);
}
[Fact]
public async Task Get_at_moment_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-21 09:38:07 +05:00
var now = DateTimeOffset.UtcNow;
2024-01-20 15:38:37 +05:00
var entityDeleted = entity.Adapt<ProcessMapPlanDrilling>();
2024-01-21 09:38:07 +05:00
entityDeleted.Creation = now;
entityDeleted.Obsolete = now.AddMinutes(1);
2024-01-20 15:38:37 +05:00
entityDeleted.IdState = ProcessMapPlanBase.IdStateDeleted;
entityDeleted.IdEditor = 1;
entityDeleted.Comment = "nothing";
dbset.Add(entityDeleted);
var entityDeleted2 = entity.Adapt<ProcessMapPlanDrilling>();
2024-01-21 09:38:07 +05:00
entityDeleted2.Creation = now.AddMinutes(1);
entityDeleted2.Obsolete = now.AddMinutes(2);
2024-01-20 15:38:37 +05:00
entityDeleted2.IdState = ProcessMapPlanBase.IdStateDeleted;
entityDeleted2.IdEditor = 1;
entityDeleted2.Comment = "nothing";
dbset.Add(entityDeleted2);
dbContext.SaveChanges();
//act
var request = new ProcessMapPlanBaseRequest
{
2024-01-21 09:38:07 +05:00
Moment = now.AddMinutes(0.5),
2024-01-20 15:38:37 +05:00
};
var response = await client.Get(dto.IdWell, request);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
2024-01-22 13:16:39 +05:00
Assert.Single(response.Content);
2024-01-20 15:38:37 +05:00
}
[Fact]
public async Task Get_section_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
dbset.Add(entity);
var entity2 = entity.Adapt<ProcessMapPlanDrilling>();
entity2.IdWellSectionType = 2;
entity2.Comment = "IdWellSectionType = 2";
dbset.Add(entity2);
dbContext.SaveChanges();
//act
var request = new ProcessMapPlanBaseRequest
{
IdWellSectionType = 2,
};
var response = await client.Get(dto.IdWell, request);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Single(response.Content);
var actual = response.Content.First()!;
var expected = entity2.Adapt<ProcessMapPlanDrillingDto>()!;
var excludeProps = new[] {
nameof(ProcessMapPlanDrillingDto.Id),
nameof(ProcessMapPlanDrillingDto.Author),
2024-01-20 15:38:37 +05:00
nameof(ProcessMapPlanDrillingDto.Creation),
};
MatchHelper.Match(expected, actual, excludeProps);
}
[Fact]
public async Task Get_updated_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
2024-01-20 15:38:37 +05:00
dbset.Add(entity);
var entity2 = entity.Adapt<ProcessMapPlanDrilling>();
entity2.Creation = entity.Creation.AddHours(1);
entity2.Comment = "IdWellSectionType = 2";
dbset.Add(entity2);
var entity3 = entity.Adapt<ProcessMapPlanDrilling>();
entity3.Obsolete = entity.Creation.AddHours(1);
entity3.Comment = "IdWellSectionType = 3";
dbset.Add(entity3);
dbContext.SaveChanges();
var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours;
var offset = TimeSpan.FromHours(timezoneHours);
var updateFrom = entity.Creation.ToOffset(offset).AddHours(0.5);
//act
var request = new ProcessMapPlanBaseRequest
{
UpdateFrom = updateFrom,
};
var response = await client.Get(dto.IdWell, request);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal(2, response.Content.Count());
var actual = response.Content
.First(p => p.Comment == entity2.Comment);
2024-01-19 17:49:09 +05:00
2024-01-20 15:38:37 +05:00
var expected = entity2.Adapt<ProcessMapPlanDrillingDto>();
2024-01-19 17:49:09 +05:00
var excludeProps = new[] {
nameof(ProcessMapPlanDrillingDto.Id),
nameof(ProcessMapPlanDrillingDto.Author),
2024-01-19 17:49:09 +05:00
nameof(ProcessMapPlanDrillingDto.Creation),
};
MatchHelper.Match(expected, actual, excludeProps);
}
2024-02-09 11:33:03 +05:00
[Fact]
public async Task Parse_returns_success()
{
//arrange
const string fileName = "ProcessMapPlanDrillingValid.xlsx";
var stream = Assembly.GetExecutingAssembly().GetFileCopyStream(fileName);
//act
var streamPart = new StreamPart(stream, fileName, "application/octet-stream");
var response = await client.Parse(Defaults.Wells[0].Id, new[] { streamPart });
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var parserResult = response.Content;
Assert.NotNull(parserResult);
Assert.Single(parserResult.Item);
Assert.True(parserResult.IsValid);
var row = parserResult.Item.First();
var dtoActual = row.Item;
Assert.True(row.IsValid);
var excludeProps = new[] { nameof(ProcessMapPlanDrillingDto.IdWell) };
MatchHelper.Match(dto, dtoActual, excludeProps);
}
[Fact]
public async Task Parse_returns_success_for_result_with_warnings()
{
//arrange
const string fileName = "ProcessMapPlanDrillingInvalid.xlsx";
var stream = Assembly.GetExecutingAssembly().GetFileCopyStream(fileName);
//act
var streamPart = new StreamPart(stream, fileName, "application/octet-stream");
var response = await client.Parse(Defaults.Wells[0].Id, new[] { streamPart });
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var parserResult = response.Content;
Assert.NotNull(parserResult);
Assert.False(parserResult.IsValid);
Assert.Single(parserResult.Warnings);
Assert.Single(parserResult.Item);
var row = parserResult.Item.First();
Assert.False(row.IsValid);
Assert.Equal(2, row.Warnings.Count());
}
2024-01-19 17:49:09 +05:00
}