Автотесты для changeLog (окончание)

This commit is contained in:
Оля Бизюкова 2024-12-03 11:35:58 +05:00
parent 1e44e27b07
commit c3c04f9c66
2 changed files with 92 additions and 69 deletions

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
using Persistence.Models;
using Persistence.Models.Requests;
using Refit;
@ -10,7 +9,7 @@ namespace Persistence.Client.Clients;
/// </summary>
public interface IChangeLogClient
{
private const string BaseRoute = "/api/ChangeLog";
private const string BaseRoute = "/api/ChangeLog";
//[Get($"{BaseRoute}/current")]
/// <summary>
@ -22,25 +21,15 @@ public interface IChangeLogClient
[Post($"{BaseRoute}/replace")]
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>
/// <param name="idDiscriminator"></param>
/// <param name="moment"></param>
/// <param name="request">параметры запроса</param>
/// <param name="token"></param>
/// <returns></returns>
[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>
/// Получение исторических данных за определенный период времени
@ -48,7 +37,6 @@ public interface IChangeLogClient
/// <param name="idDiscriminator"></param>
/// <param name="dateBegin"></param>
/// <param name="dateEnd"></param>
/// <param name="token"></param>
/// <returns></returns>
[Get($"{BaseRoute}/history")]
Task<IApiResponse<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd);
@ -67,7 +55,6 @@ public interface IChangeLogClient
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[Post($"{BaseRoute}/range")]
Task<IApiResponse<int>> AddRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
@ -86,7 +73,6 @@ public interface IChangeLogClient
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[Put($"{BaseRoute}/range")]
Task<IApiResponse<int>> UpdateRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos);
@ -95,7 +81,6 @@ public interface IChangeLogClient
/// Удалить одну запись
/// </summary>
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns></returns>
[Delete($"{BaseRoute}")]
Task<IApiResponse<int>> Delete(Guid id);
@ -104,7 +89,6 @@ public interface IChangeLogClient
/// Удалить несколько записей
/// </summary>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
[Delete($"{BaseRoute}/range")]
Task<IApiResponse<int>> DeleteRange([Body] IEnumerable<Guid> ids);
@ -113,7 +97,6 @@ public interface IChangeLogClient
/// Получение списка дат, в которые происходили изменения (день, месяц, год, без времени)
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="token"></param>
/// <returns></returns>
[Get($"{BaseRoute}/datesRange")]
Task<IApiResponse<DatesRangeDto?>> GetDatesRange(Guid idDiscriminator);

View File

@ -1,19 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
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 Mapster;
using Microsoft.EntityFrameworkCore;
using System.Net;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Client;
using Persistence.Client.Clients;
using Persistence.Database.Model;
using Persistence.Models;
using Persistence.Models.Requests;
using System.Net;
using Xunit;
namespace Persistence.IntegrationTests.Controllers;
public class ChangeLogControllerTest : BaseIntegrationTest
@ -58,7 +52,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
// assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal(count, result.Content);
Assert.Equal(count, result.Content);
}
[Fact]
@ -127,7 +121,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
var count = 2;
var idDiscriminator = Guid.NewGuid();
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.SaveChanges();
@ -138,7 +132,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
Id = c.Id,
IdSection = c.IdSection,
Value = c.Value
});
}).ToArray();
// act
var result = await client.UpdateRange(idDiscriminator, dtos);
@ -189,7 +183,7 @@ public class ChangeLogControllerTest : BaseIntegrationTest
public async Task GetDatesRange_returns_success()
{
// arrange
var changeLogItems = CreateChangeLogItems(3);
var changeLogItems = CreateChangeLogItems(3, (-15, 15));
var idDiscriminator = changeLogItems.Item1;
var entities = changeLogItems.Item2;
@ -213,57 +207,82 @@ public class ChangeLogControllerTest : BaseIntegrationTest
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]
public async Task GetByDate_returns_success()
{
// arrange
var changeLogItems = CreateChangeLogItems(3);
//создаем записи
var count = 5;
var changeLogItems = CreateChangeLogItems(count, (-15, 15));
var idDiscriminator = changeLogItems.Item1;
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,
Take = 10,
SortSettings = String.Empty,
};
var moment = DateTimeOffset.UtcNow.AddDays(20);
var moment = DateTimeOffset.UtcNow.AddDays(16);
var result = await client.GetByDate(idDiscriminator, moment, request);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
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)
{
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);
}
}