Интеграционный тест (InsertRangeAsync)

This commit is contained in:
Olga Nemt 2024-01-25 15:56:34 +05:00
parent eaa10e129a
commit ff208a6aa8
5 changed files with 133 additions and 73 deletions

View File

@ -6,7 +6,7 @@ namespace AsbCloudWebApi.IntegrationTests;
public abstract class BaseIntegrationTest : IClassFixture<WebAppFactoryFixture> public abstract class BaseIntegrationTest : IClassFixture<WebAppFactoryFixture>
{ {
private readonly IServiceScope scope; protected readonly IServiceScope scope;
protected readonly IAsbCloudDbContext dbContext; protected readonly IAsbCloudDbContext dbContext;

View File

@ -1,14 +1,13 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMapPlan;
using AsbCloudApp.Requests;
using Refit; using Refit;
namespace AsbCloudWebApi.IntegrationTests.Clients; namespace AsbCloudWebApi.IntegrationTests.Clients;
public interface IWellOperationClient public interface IWellOperationClient
{ {
private const string BaseRoute = "/api/wellOperation"; private const string BaseRoute = "/api/well/{idWell}/wellOperations";
[Post(BaseRoute + "/{idType}/{deleteBeforeInsert}")]
Task<IApiResponse<int>> InsertRangeAsync(int idWell, int idType, bool deleteBeforeInsert, [Body] IEnumerable<WellOperationDto> dtos);
[Post(BaseRoute)]
Task<IApiResponse<int>> InsertRange(int idWell, [Body] IEnumerable<WellOperationDto> dtos);
} }

View File

@ -1,8 +1,4 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMapPlan;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
using AsbCloudDb.Model.ProcessMaps;
using AsbCloudWebApi.IntegrationTests.Clients; using AsbCloudWebApi.IntegrationTests.Clients;
using System.Net; using System.Net;
using Xunit; using Xunit;
@ -12,104 +8,147 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers;
public class WellOperationControllerTest : BaseIntegrationTest public class WellOperationControllerTest : BaseIntegrationTest
{ {
private readonly int idWell = 4; private static int idWell = 1;
private readonly WellOperationDto[] dtos = new WellOperationDto[] private readonly WellOperationDto[] dtos = new WellOperationDto[]
{ {
new WellOperationDto() new WellOperationDto()
{ {
Id = 2,
IdWell = idWell,
IdType = 1,
DateStart = DateTimeOffset.Now,
CategoryInfo = "1",
CategoryName = "1",
Comment = "1",
Day = 1,
DepthEnd = 20,
DepthStart = 10,
DurationHours = 1,
IdCategory = 5000,
IdParentCategory = null,
IdPlan = null,
IdUser = 1,
IdWellSectionType = 1,
LastUpdateDate = DateTimeOffset.Now,
NptHours = 1,
WellSectionTypeName = null,
UserName = null
}
};
}, private readonly WellOperationDto[] dtosWithError = new WellOperationDto[]
{
new WellOperationDto() new WellOperationDto()
{ {
Id = 3,
IdWell = idWell,
IdType = 1,
DateStart = DateTimeOffset.Now,
CategoryInfo = "1",
CategoryName = "1",
Comment = "1",
Day = 1,
DepthEnd = 20,
DepthStart = 10,
DurationHours = 1,
IdCategory = 5000,
IdParentCategory = null,
IdPlan = null,
IdUser = 1,
IdWellSectionType = 1,
LastUpdateDate = DateTimeOffset.Now,
NptHours = 1,
WellSectionTypeName = null,
UserName = null
},
new WellOperationDto()
{
Id = 4,
IdWell = idWell,
IdType = 1,
DateStart = DateTimeOffset.Now.AddDays(1000),
CategoryInfo = "1",
CategoryName = "1",
Comment = "1",
Day = 1,
DepthEnd = 20,
DepthStart = 10,
DurationHours = 1,
IdCategory = 5000,
IdParentCategory = null,
IdPlan = null,
IdUser = 1,
IdWellSectionType = 1,
LastUpdateDate = DateTimeOffset.Now,
NptHours = 1,
WellSectionTypeName = null,
UserName = null
} }
}; };
private IWellOperationClient wellOperationClient; private IWellOperationClient wellOperationClient;
public WellOperationControllerTest(WebAppFactoryFixture factory) public WellOperationControllerTest(WebAppFactoryFixture factory)
: base(factory) : base(factory)
{ {
wellOperationClient = factory.GetAuthorizedHttpClient<IWellOperationClient>(); wellOperationClient = factory.GetAuthorizedHttpClient<IWellOperationClient>();
var rep = factory.Get
} }
/// <summary> /// <summary>
/// Успешное добавление операции с предварительной очисткой /// Успешное добавление операций (с предварительной очисткой данных)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Fact] [Fact]
public async Task InsertRangeWithDeleteBefore_returns_success() public async Task InsertRange_returns_success()
{ {
////arrange //act
//dbContext.WellOperations.Add(wellOperation); var response = await wellOperationClient.InsertRangeAsync(idWell, 1, true, dtos);
//dbContext.SaveChanges();
//var request = new OperationStatRequest //assert
//{ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// DateStartUTC = schedule.DrillStart.DateTime, }
// DateEndUTC = schedule.DrillEnd.DateTime,
// DurationMinutesMin = 0,
// DurationMinutesMax = 5
//};
//var dtoExpected = new SlipsStatDto
//{
// DrillerName = $"{Data.Defaults.Drillers[0].Surname} {Data.Defaults.Drillers[0].Name} {Data.Defaults.Drillers[0].Patronymic}",
// WellCount = 1,
// SectionCaption = "Пилотный ствол",
// SlipsCount = 1,
// SlipsTimeInMinutes = (detectedOperation.DateEnd - detectedOperation.DateStart).TotalMinutes,
// SectionDepth = factWellOperation.DepthEnd - factWellOperation.DepthStart,
//};
////act
//var response = await slipsTimeClient.GetAll(request);
////assert
//Assert.NotNull(response.Content);
//Assert.Single(response.Content);
//var dtoActual = response.Content.First();
//MatchHelper.Match(dtoExpected, dtoActual);
}
/// <summary> /// <summary>
/// Успешное добавление операции без очистки /// Неуспешное добавление операций (с предварительной очисткой данных)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Fact] [Fact]
public async Task InsertRange_returns_success() { public async Task InsertRange_returns_error()
//arrange {
var dbset = dbContext.Set<WellOperation>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
operationRepository.Validate(dtos);
//act //act
var response = await wellOperationClient.InsertRange(idWell, dtos); var response = await wellOperationClient.InsertRangeAsync(idWell, 1, true, dtosWithError);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
/// <summary>
/// Успешное добавление операций (с предварительной очисткой данных)
/// </summary>
/// <returns></returns>
[Fact]
public async Task InsertRangeWithDeleteBefore_returns_success()
{
//act
var response = await wellOperationClient.InsertRangeAsync(idWell, 1, false, dtos);
//assert //assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
} }
/// <summary> /// <summary>
/// Неуспешное добавление операции с предварительной очисткой /// Неуспешное добавление операций (с предварительной очисткой данных)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Fact] [Fact]
public async Task InsertRangeWithDeleteBefore_returns_error() public async Task InsertRangeWithDeleteBefore_returns_error()
{ {
} //act
var response = await wellOperationClient.InsertRangeAsync(idWell, 1, false, dtosWithError);
/// <summary> //assert
/// Неуспешное добавление операции без очистки Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
/// </summary>
/// <returns></returns>
[Fact]
public async Task InsertRange_returns_error()
{
} }
} }

View File

@ -14,7 +14,28 @@ namespace AsbCloudWebApi.IntegrationTests.Data
Surname = "test" Surname = "test"
} }
}; };
public static WellOperation[] WellOperations = new WellOperation[]
{
new()
{
Id = 2,
IdWell = 1,
IdType = 1,
DateStart = DateTimeOffset.UtcNow.AddDays(-1),
CategoryInfo = "1",
Comment = "1",
DepthEnd = 20,
DepthStart = 10,
DurationHours = 1,
IdCategory = 5000,
IdPlan = null,
IdUser = 1,
IdWellSectionType = 1,
LastUpdateDate = DateTimeOffset.UtcNow
}
};
public static Deposit[] Deposits = new Deposit[] { public static Deposit[] Deposits = new Deposit[] {
new() new()
{ {

View File

@ -60,7 +60,8 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
dbContext.AddRange(Data.Defaults.RelationsCompanyWell); dbContext.AddRange(Data.Defaults.RelationsCompanyWell);
dbContext.AddRange(Data.Defaults.Telemetries); dbContext.AddRange(Data.Defaults.Telemetries);
dbContext.AddRange(Data.Defaults.Drillers); dbContext.AddRange(Data.Defaults.Drillers);
await dbContext.SaveChangesAsync(); dbContext.AddRange(Data.Defaults.WellOperations);
await dbContext.SaveChangesAsync();
} }
public new async Task DisposeAsync() public new async Task DisposeAsync()