Автотесты для changeLog (окончание)
This commit is contained in:
parent
1e44e27b07
commit
c3c04f9c66
@ -1,5 +1,4 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Persistence.Models;
|
||||||
using Persistence.Models;
|
|
||||||
using Persistence.Models.Requests;
|
using Persistence.Models.Requests;
|
||||||
using Refit;
|
using Refit;
|
||||||
|
|
||||||
@ -10,7 +9,7 @@ namespace Persistence.Client.Clients;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IChangeLogClient
|
public interface IChangeLogClient
|
||||||
{
|
{
|
||||||
private const string BaseRoute = "/api/ChangeLog";
|
private const string BaseRoute = "/api/ChangeLog";
|
||||||
|
|
||||||
//[Get($"{BaseRoute}/current")]
|
//[Get($"{BaseRoute}/current")]
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -22,25 +21,15 @@ public interface IChangeLogClient
|
|||||||
[Post($"{BaseRoute}/replace")]
|
[Post($"{BaseRoute}/replace")]
|
||||||
Task<IApiResponse<int>> ClearAndInsertRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
|
Task<IApiResponse<int>> ClearAndInsertRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
|
||||||
|
|
||||||
///// <summary>
|
|
||||||
///// Получение данных на текущую дату (с пагинацией)
|
|
||||||
///// </summary>
|
|
||||||
///// <param name="idDiscriminator"></param>
|
|
||||||
///// <param name="request">параметры запроса</param>
|
|
||||||
///// <param name="token"></param>
|
|
||||||
///// <returns></returns>
|
|
||||||
//Task<IActionResult> GetCurrent(Guid idDiscriminator, SectionPartRequest request, CancellationToken token);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение данных на определенную дату (с пагинацией)
|
/// Получение актуальных данных на определенную дату (с пагинацией)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idDiscriminator"></param>
|
/// <param name="idDiscriminator"></param>
|
||||||
/// <param name="moment"></param>
|
/// <param name="moment"></param>
|
||||||
/// <param name="request">параметры запроса</param>
|
/// <param name="request">параметры запроса</param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Get($"{BaseRoute}/moment")]
|
[Get($"{BaseRoute}/moment")]
|
||||||
Task<IApiResponse<PaginationContainer<DataWithWellDepthAndSectionDto>>> GetByDate(Guid idDiscriminator, DateTimeOffset moment, [Query]SectionPartRequest request);
|
Task<IApiResponse<PaginationContainer<DataWithWellDepthAndSectionDto>>> GetByDate(Guid idDiscriminator, DateTimeOffset moment, [Query] SectionPartRequest request);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение исторических данных за определенный период времени
|
/// Получение исторических данных за определенный период времени
|
||||||
@ -48,7 +37,6 @@ public interface IChangeLogClient
|
|||||||
/// <param name="idDiscriminator"></param>
|
/// <param name="idDiscriminator"></param>
|
||||||
/// <param name="dateBegin"></param>
|
/// <param name="dateBegin"></param>
|
||||||
/// <param name="dateEnd"></param>
|
/// <param name="dateEnd"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Get($"{BaseRoute}/history")]
|
[Get($"{BaseRoute}/history")]
|
||||||
Task<IApiResponse<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd);
|
Task<IApiResponse<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd);
|
||||||
@ -67,7 +55,6 @@ public interface IChangeLogClient
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idDiscriminator"></param>
|
/// <param name="idDiscriminator"></param>
|
||||||
/// <param name="dtos"></param>
|
/// <param name="dtos"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Post($"{BaseRoute}/range")]
|
[Post($"{BaseRoute}/range")]
|
||||||
Task<IApiResponse<int>> AddRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
|
Task<IApiResponse<int>> AddRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
|
||||||
@ -86,7 +73,6 @@ public interface IChangeLogClient
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idDiscriminator"></param>
|
/// <param name="idDiscriminator"></param>
|
||||||
/// <param name="dtos"></param>
|
/// <param name="dtos"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Put($"{BaseRoute}/range")]
|
[Put($"{BaseRoute}/range")]
|
||||||
Task<IApiResponse<int>> UpdateRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
|
Task<IApiResponse<int>> UpdateRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
|
||||||
@ -95,7 +81,6 @@ public interface IChangeLogClient
|
|||||||
/// Удалить одну запись
|
/// Удалить одну запись
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Delete($"{BaseRoute}")]
|
[Delete($"{BaseRoute}")]
|
||||||
Task<IApiResponse<int>> Delete(Guid id);
|
Task<IApiResponse<int>> Delete(Guid id);
|
||||||
@ -104,7 +89,6 @@ public interface IChangeLogClient
|
|||||||
/// Удалить несколько записей
|
/// Удалить несколько записей
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ids"></param>
|
/// <param name="ids"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Delete($"{BaseRoute}/range")]
|
[Delete($"{BaseRoute}/range")]
|
||||||
Task<IApiResponse<int>> DeleteRange([Body] IEnumerable<Guid> ids);
|
Task<IApiResponse<int>> DeleteRange([Body] IEnumerable<Guid> ids);
|
||||||
@ -113,7 +97,6 @@ public interface IChangeLogClient
|
|||||||
/// Получение списка дат, в которые происходили изменения (день, месяц, год, без времени)
|
/// Получение списка дат, в которые происходили изменения (день, месяц, год, без времени)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idDiscriminator"></param>
|
/// <param name="idDiscriminator"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Get($"{BaseRoute}/datesRange")]
|
[Get($"{BaseRoute}/datesRange")]
|
||||||
Task<IApiResponse<DatesRangeDto?>> GetDatesRange(Guid idDiscriminator);
|
Task<IApiResponse<DatesRangeDto?>> GetDatesRange(Guid idDiscriminator);
|
||||||
|
@ -1,19 +1,13 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Mapster;
|
||||||
using Persistence.Client.Clients;
|
|
||||||
using Persistence.Client;
|
|
||||||
using Persistence.Models;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Xunit;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Net;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Mapster;
|
using Persistence.Client;
|
||||||
|
using Persistence.Client.Clients;
|
||||||
using Persistence.Database.Model;
|
using Persistence.Database.Model;
|
||||||
|
using Persistence.Models;
|
||||||
using Persistence.Models.Requests;
|
using Persistence.Models.Requests;
|
||||||
|
using System.Net;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
namespace Persistence.IntegrationTests.Controllers;
|
namespace Persistence.IntegrationTests.Controllers;
|
||||||
public class ChangeLogControllerTest : BaseIntegrationTest
|
public class ChangeLogControllerTest : BaseIntegrationTest
|
||||||
@ -58,7 +52,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
|
|||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
||||||
Assert.Equal(count, result.Content);
|
Assert.Equal(count, result.Content);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -127,7 +121,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
|
|||||||
var count = 2;
|
var count = 2;
|
||||||
var idDiscriminator = Guid.NewGuid();
|
var idDiscriminator = Guid.NewGuid();
|
||||||
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
||||||
var entities = dtos.Select(d => d.Adapt<ChangeLog>());
|
var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
|
||||||
dbContext.ChangeLog.AddRange(entities);
|
dbContext.ChangeLog.AddRange(entities);
|
||||||
dbContext.SaveChanges();
|
dbContext.SaveChanges();
|
||||||
|
|
||||||
@ -138,7 +132,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
|
|||||||
Id = c.Id,
|
Id = c.Id,
|
||||||
IdSection = c.IdSection,
|
IdSection = c.IdSection,
|
||||||
Value = c.Value
|
Value = c.Value
|
||||||
});
|
}).ToArray();
|
||||||
|
|
||||||
// act
|
// act
|
||||||
var result = await client.UpdateRange(idDiscriminator, dtos);
|
var result = await client.UpdateRange(idDiscriminator, dtos);
|
||||||
@ -189,7 +183,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
|
|||||||
public async Task GetDatesRange_returns_success()
|
public async Task GetDatesRange_returns_success()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
var changeLogItems = CreateChangeLogItems(3);
|
var changeLogItems = CreateChangeLogItems(3, (-15, 15));
|
||||||
var idDiscriminator = changeLogItems.Item1;
|
var idDiscriminator = changeLogItems.Item1;
|
||||||
var entities = changeLogItems.Item2;
|
var entities = changeLogItems.Item2;
|
||||||
|
|
||||||
@ -213,57 +207,82 @@ public class ChangeLogControllerTest : BaseIntegrationTest
|
|||||||
Assert.Equal(expectedMaxDate, actualMaxDate);
|
Assert.Equal(expectedMaxDate, actualMaxDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private (Guid, ChangeLog[]) CreateChangeLogItems(int count)
|
|
||||||
{
|
|
||||||
Guid idDiscriminator = Guid.NewGuid();
|
|
||||||
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
|
||||||
var entities = dtos.Select(d =>
|
|
||||||
{
|
|
||||||
var entity = d.Adapt<ChangeLog>();
|
|
||||||
entity.IdDiscriminator = idDiscriminator;
|
|
||||||
entity.Creation = DateTimeOffset.UtcNow.AddDays(generatorRandomDigits.Next(-15, 15));
|
|
||||||
|
|
||||||
return entity;
|
|
||||||
}).ToArray();
|
|
||||||
dbContext.ChangeLog.AddRange(entities);
|
|
||||||
dbContext.SaveChanges();
|
|
||||||
|
|
||||||
return (idDiscriminator, entities);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task GetByDate_returns_success()
|
public async Task GetByDate_returns_success()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
var changeLogItems = CreateChangeLogItems(3);
|
//создаем записи
|
||||||
|
var count = 5;
|
||||||
|
var changeLogItems = CreateChangeLogItems(count, (-15, 15));
|
||||||
var idDiscriminator = changeLogItems.Item1;
|
var idDiscriminator = changeLogItems.Item1;
|
||||||
var entities = changeLogItems.Item2;
|
var entities = changeLogItems.Item2;
|
||||||
var createdDates = entities.Select(e => e.Creation);
|
|
||||||
|
|
||||||
var dtos = entities.Select(e =>
|
//удаляем все созданные записи за исключением первой и второй
|
||||||
|
//даты 2-х оставшихся записей должны вернуться в методе GetByDate
|
||||||
|
var ids = entities.Select(e => e.Id);
|
||||||
|
var idsToDelete = ids.Skip(2);
|
||||||
|
|
||||||
|
var deletedCount = await client.DeleteRange(idsToDelete);
|
||||||
|
|
||||||
|
var request = new SectionPartRequest()
|
||||||
{
|
{
|
||||||
var dto = e.Adapt<DataWithWellDepthAndSectionDto>();
|
|
||||||
dto.DepthEnd = dto.DepthEnd + 10;
|
|
||||||
|
|
||||||
return dto;
|
|
||||||
}).ToArray();
|
|
||||||
|
|
||||||
var updatedItems = await client.UpdateRange(idDiscriminator, dtos);
|
|
||||||
|
|
||||||
var request = new SectionPartRequest() {
|
|
||||||
Skip = 0,
|
Skip = 0,
|
||||||
Take = 10,
|
Take = 10,
|
||||||
SortSettings = String.Empty,
|
SortSettings = String.Empty,
|
||||||
};
|
};
|
||||||
|
|
||||||
var moment = DateTimeOffset.UtcNow.AddDays(20);
|
var moment = DateTimeOffset.UtcNow.AddDays(16);
|
||||||
var result = await client.GetByDate(idDiscriminator, moment, request);
|
var result = await client.GetByDate(idDiscriminator, moment, request);
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
||||||
Assert.NotNull(result.Content);
|
Assert.NotNull(result.Content);
|
||||||
|
|
||||||
|
var restEntities = entities.Where(e => !idsToDelete.Contains(e.Id));
|
||||||
|
Assert.Equal(restEntities.Count(), result.Content.Count);
|
||||||
|
|
||||||
|
var actualIds = restEntities.Select(e => e.Id);
|
||||||
|
var expectedIds = result.Content.Items.Select(e => e.Id);
|
||||||
|
Assert.Equivalent(expectedIds, actualIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(5, -15, 15, -20, 20, 10)]
|
||||||
|
[InlineData(5, -15, -10, -16, -9, 5)]
|
||||||
|
public async Task GetChangeLogForDate_returns_success(
|
||||||
|
int insertedCount,
|
||||||
|
int leftCreationFromCurrentDate,
|
||||||
|
int rightCreationFromCurrentDate,
|
||||||
|
int leftPointFromCurrentDate,
|
||||||
|
int rightPointFromCurrentDate,
|
||||||
|
int changeLogCount)
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
//создаем записи
|
||||||
|
var count = insertedCount;
|
||||||
|
var daysRange = (leftCreationFromCurrentDate, rightCreationFromCurrentDate);
|
||||||
|
var changeLogItems = CreateChangeLogItems(count, daysRange);
|
||||||
|
var idDiscriminator = changeLogItems.Item1;
|
||||||
|
var entities = changeLogItems.Item2;
|
||||||
|
|
||||||
|
foreach (var entity in entities)
|
||||||
|
{
|
||||||
|
entity.DepthEnd = entity.DepthEnd + 10;
|
||||||
|
}
|
||||||
|
var dtos = entities.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>()).ToArray();
|
||||||
|
await client.UpdateRange(idDiscriminator, dtos);
|
||||||
|
|
||||||
|
//act
|
||||||
|
var dateBegin = DateTimeOffset.UtcNow.AddDays(leftPointFromCurrentDate);
|
||||||
|
var dateEnd = DateTimeOffset.UtcNow.AddDays(rightPointFromCurrentDate);
|
||||||
|
var result = await client.GetChangeLogForDate(idDiscriminator, dateBegin, dateEnd);
|
||||||
|
|
||||||
|
//assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
||||||
|
Assert.NotNull(result.Content);
|
||||||
|
Assert.Equal(changeLogCount, result.Content.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static IEnumerable<DataWithWellDepthAndSectionDto> Generate(int count, DateTimeOffset from)
|
private static IEnumerable<DataWithWellDepthAndSectionDto> Generate(int count, DateTimeOffset from)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
@ -280,4 +299,25 @@ public class ChangeLogControllerTest : BaseIntegrationTest
|
|||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private (Guid, ChangeLog[]) CreateChangeLogItems(int count, (int, int) daysRange)
|
||||||
|
{
|
||||||
|
var minDayCount = daysRange.Item1;
|
||||||
|
var maxDayCount = daysRange.Item2;
|
||||||
|
|
||||||
|
Guid idDiscriminator = Guid.NewGuid();
|
||||||
|
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
||||||
|
var entities = dtos.Select(d =>
|
||||||
|
{
|
||||||
|
var entity = d.Adapt<ChangeLog>();
|
||||||
|
entity.IdDiscriminator = idDiscriminator;
|
||||||
|
entity.Creation = DateTimeOffset.UtcNow.AddDays(generatorRandomDigits.Next(minDayCount, maxDayCount));
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
}).ToArray();
|
||||||
|
dbContext.ChangeLog.AddRange(entities);
|
||||||
|
dbContext.SaveChanges();
|
||||||
|
|
||||||
|
return (idDiscriminator, entities);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user