2024-12-03 11:35:58 +05:00
|
|
|
|
using Mapster;
|
2024-12-02 18:14:45 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-12-03 11:35:58 +05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Persistence.Client;
|
|
|
|
|
using Persistence.Client.Clients;
|
2024-12-02 18:14:45 +05:00
|
|
|
|
using Persistence.Database.Model;
|
2024-12-03 11:35:58 +05:00
|
|
|
|
using Persistence.Models;
|
2024-12-02 18:14:45 +05:00
|
|
|
|
using Persistence.Models.Requests;
|
2024-12-03 11:35:58 +05:00
|
|
|
|
using System.Net;
|
|
|
|
|
using Xunit;
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
namespace Persistence.IntegrationTests.Controllers;
|
|
|
|
|
public class ChangeLogControllerTest : BaseIntegrationTest
|
|
|
|
|
{
|
|
|
|
|
private readonly IChangeLogClient client;
|
|
|
|
|
private static Random generatorRandomDigits = new Random();
|
|
|
|
|
|
|
|
|
|
public ChangeLogControllerTest(WebAppFactoryFixture factory) : base(factory)
|
|
|
|
|
{
|
|
|
|
|
var persistenceClientFactory = scope.ServiceProvider
|
|
|
|
|
.GetRequiredService<PersistenceClientFactory>();
|
|
|
|
|
|
|
|
|
|
client = persistenceClientFactory.GetClient<IChangeLogClient>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2024-12-05 18:13:52 +05:00
|
|
|
|
public async Task ClearAndInsertRange_InEmptyDb()
|
2024-12-02 18:14:45 +05:00
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var idDiscriminator = Guid.NewGuid();
|
|
|
|
|
var dtos = Generate(2, DateTimeOffset.UtcNow);
|
|
|
|
|
|
|
|
|
|
// act
|
2024-12-05 11:01:00 +05:00
|
|
|
|
var result = await client.ClearAndAddRange(idDiscriminator, dtos);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.Equal(2, result.Content);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 18:13:52 +05:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ClearAndInsertRange_InNotEmptyDb()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var insertedCount = 10;
|
|
|
|
|
var createdResult = CreateChangeLogItems(insertedCount, (-15, 15));
|
|
|
|
|
var idDiscriminator = createdResult.Item1;
|
|
|
|
|
var dtos = createdResult.Item2.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>());
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var result = await client.ClearAndAddRange(idDiscriminator, dtos);
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.Equal(insertedCount*2, result.Content);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 18:14:45 +05:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Add_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var count = 1;
|
|
|
|
|
var idDiscriminator = Guid.NewGuid();
|
|
|
|
|
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
|
|
|
|
var dto = dtos.FirstOrDefault()!;
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var result = await client.Add(idDiscriminator, dto);
|
|
|
|
|
|
|
|
|
|
// assert
|
2024-12-05 11:01:00 +05:00
|
|
|
|
Assert.Equal(HttpStatusCode.Created, result.StatusCode);
|
2024-12-03 11:35:58 +05:00
|
|
|
|
Assert.Equal(count, result.Content);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task AddRange_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var count = 3;
|
|
|
|
|
var idDiscriminator = Guid.NewGuid();
|
|
|
|
|
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var result = await client.AddRange(idDiscriminator, dtos);
|
|
|
|
|
|
|
|
|
|
// assert
|
2024-12-05 11:01:00 +05:00
|
|
|
|
Assert.Equal(HttpStatusCode.Created, result.StatusCode);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
Assert.Equal(count, result.Content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Update_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var idDiscriminator = Guid.NewGuid();
|
|
|
|
|
var dtos = Generate(1, DateTimeOffset.UtcNow);
|
|
|
|
|
var dto = dtos.FirstOrDefault()!;
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var result = await client.Add(idDiscriminator, dto);
|
2024-12-05 11:01:00 +05:00
|
|
|
|
Assert.Equal(HttpStatusCode.Created, result.StatusCode);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var entity = dbContext.ChangeLog
|
|
|
|
|
.Where(x => x.IdDiscriminator == idDiscriminator)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
dto = entity.Adapt<DataWithWellDepthAndSectionDto>();
|
2024-12-02 18:14:45 +05:00
|
|
|
|
dto.DepthEnd = dto.DepthEnd + 10;
|
|
|
|
|
|
|
|
|
|
// act
|
2024-12-03 17:05:46 +05:00
|
|
|
|
result = await client.Update(idDiscriminator, dto);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.Equal(2, result.Content);
|
|
|
|
|
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var dateBegin = DateTimeOffset.UtcNow.AddDays(-1);
|
|
|
|
|
var dateEnd = DateTimeOffset.UtcNow.AddDays(1);
|
|
|
|
|
|
2024-12-03 17:36:41 +05:00
|
|
|
|
var changeLogResult = await client.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd);
|
2024-12-03 17:05:46 +05:00
|
|
|
|
Assert.Equal(HttpStatusCode.OK, changeLogResult.StatusCode);
|
|
|
|
|
Assert.NotNull(changeLogResult.Content);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var changeLogDtos = changeLogResult.Content;
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var obsoleteDto = changeLogDtos
|
|
|
|
|
.Where(e => e.Obsolete.HasValue)
|
|
|
|
|
.FirstOrDefault();
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var activeDto = changeLogDtos
|
|
|
|
|
.Where(e => !e.Obsolete.HasValue)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (obsoleteDto == null || activeDto == null)
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.Equal(activeDto.Id, obsoleteDto.IdNext);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdateRange_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var count = 2;
|
|
|
|
|
var idDiscriminator = Guid.NewGuid();
|
|
|
|
|
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
2024-12-03 11:35:58 +05:00
|
|
|
|
var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
|
2024-12-02 18:14:45 +05:00
|
|
|
|
dbContext.ChangeLog.AddRange(entities);
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
dtos = entities.Select(c => new DataWithWellDepthAndSectionDto()
|
|
|
|
|
{
|
|
|
|
|
DepthEnd = c.DepthEnd + 10,
|
|
|
|
|
DepthStart = c.DepthStart + 10,
|
|
|
|
|
Id = c.Id,
|
|
|
|
|
IdSection = c.IdSection,
|
|
|
|
|
Value = c.Value
|
2024-12-03 11:35:58 +05:00
|
|
|
|
}).ToArray();
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var result = await client.UpdateRange(idDiscriminator, dtos);
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.Equal(count * 2, result.Content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Delete_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var dtos = Generate(1, DateTimeOffset.UtcNow);
|
|
|
|
|
var dto = dtos.FirstOrDefault()!;
|
|
|
|
|
var entity = dto.Adapt<ChangeLog>();
|
|
|
|
|
dbContext.ChangeLog.Add(entity);
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var result = await client.Delete(entity.Id);
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.Equal(1, result.Content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeleteRange_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
var count = 10;
|
|
|
|
|
var dtos = Generate(count, DateTimeOffset.UtcNow);
|
|
|
|
|
var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
|
|
|
|
|
dbContext.ChangeLog.AddRange(entities);
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var ids = entities.Select(e => e.Id);
|
|
|
|
|
var result = await client.DeleteRange(ids);
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.Equal(count, result.Content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetDatesRange_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
2024-12-03 11:35:58 +05:00
|
|
|
|
var changeLogItems = CreateChangeLogItems(3, (-15, 15));
|
2024-12-02 18:14:45 +05:00
|
|
|
|
var idDiscriminator = changeLogItems.Item1;
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var entities = changeLogItems.Item2.OrderBy(e => e.Creation);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var result = await client.GetDatesRange(idDiscriminator);
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.NotNull(result.Content);
|
|
|
|
|
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var minDate = entities.First().Creation;
|
|
|
|
|
var maxDate = entities.Last().Creation;
|
|
|
|
|
|
2024-12-02 18:14:45 +05:00
|
|
|
|
var expectedMinDate = minDate.ToUniversalTime().ToString();
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var actualMinDate = result.Content.From.ToUniversalTime().ToString();
|
2024-12-02 18:14:45 +05:00
|
|
|
|
Assert.Equal(expectedMinDate, actualMinDate);
|
|
|
|
|
|
|
|
|
|
var expectedMaxDate = maxDate.ToUniversalTime().ToString();
|
2024-12-03 17:05:46 +05:00
|
|
|
|
var actualMaxDate = result.Content.To.ToUniversalTime().ToString();
|
2024-12-02 18:14:45 +05:00
|
|
|
|
Assert.Equal(expectedMaxDate, actualMaxDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetByDate_returns_success()
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
2024-12-03 11:35:58 +05:00
|
|
|
|
//создаем записи
|
|
|
|
|
var count = 5;
|
|
|
|
|
var changeLogItems = CreateChangeLogItems(count, (-15, 15));
|
2024-12-02 18:14:45 +05:00
|
|
|
|
var idDiscriminator = changeLogItems.Item1;
|
|
|
|
|
var entities = changeLogItems.Item2;
|
|
|
|
|
|
2024-12-03 11:35:58 +05:00
|
|
|
|
//удаляем все созданные записи за исключением первой и второй
|
|
|
|
|
//даты 2-х оставшихся записей должны вернуться в методе GetByDate
|
|
|
|
|
var ids = entities.Select(e => e.Id);
|
|
|
|
|
var idsToDelete = ids.Skip(2);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
2024-12-03 11:35:58 +05:00
|
|
|
|
var deletedCount = await client.DeleteRange(idsToDelete);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
2024-12-05 11:01:00 +05:00
|
|
|
|
var filterRequest = new SectionPartRequest()
|
|
|
|
|
{
|
|
|
|
|
DepthStart = 0,
|
|
|
|
|
DepthEnd = 1000,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var paginationRequest = new PaginationRequest()
|
2024-12-03 11:35:58 +05:00
|
|
|
|
{
|
2024-12-02 18:14:45 +05:00
|
|
|
|
Skip = 0,
|
|
|
|
|
Take = 10,
|
|
|
|
|
SortSettings = String.Empty,
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-03 11:35:58 +05:00
|
|
|
|
var moment = DateTimeOffset.UtcNow.AddDays(16);
|
2024-12-05 11:01:00 +05:00
|
|
|
|
var result = await client.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest);
|
2024-12-02 18:14:45 +05:00
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.NotNull(result.Content);
|
|
|
|
|
|
2024-12-03 11:35:58 +05:00
|
|
|
|
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)]
|
2024-12-03 17:36:41 +05:00
|
|
|
|
public async Task GetChangeLogForInterval_returns_success(
|
2024-12-03 11:35:58 +05:00
|
|
|
|
int insertedCount,
|
2024-12-05 11:01:00 +05:00
|
|
|
|
int daysBeforeNowChangeLog,
|
|
|
|
|
int daysAfterNowChangeLog,
|
|
|
|
|
int daysBeforeNowFilter,
|
|
|
|
|
int daysAfterNowFilter,
|
2024-12-03 11:35:58 +05:00
|
|
|
|
int changeLogCount)
|
|
|
|
|
{
|
|
|
|
|
// arrange
|
|
|
|
|
//создаем записи
|
|
|
|
|
var count = insertedCount;
|
2024-12-05 11:01:00 +05:00
|
|
|
|
var daysRange = (daysBeforeNowChangeLog, daysAfterNowChangeLog);
|
2024-12-03 11:35:58 +05:00
|
|
|
|
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
|
2024-12-05 11:01:00 +05:00
|
|
|
|
var dateBegin = DateTimeOffset.UtcNow.AddDays(daysBeforeNowFilter);
|
|
|
|
|
var dateEnd = DateTimeOffset.UtcNow.AddDays(daysAfterNowFilter);
|
2024-12-03 17:36:41 +05:00
|
|
|
|
var result = await client.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd);
|
2024-12-03 11:35:58 +05:00
|
|
|
|
|
|
|
|
|
//assert
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
|
|
|
Assert.NotNull(result.Content);
|
|
|
|
|
Assert.Equal(changeLogCount, result.Content.Count());
|
2024-12-02 18:14:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 11:35:58 +05:00
|
|
|
|
|
2024-12-02 18:14:45 +05:00
|
|
|
|
private static IEnumerable<DataWithWellDepthAndSectionDto> Generate(int count, DateTimeOffset from)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
yield return new DataWithWellDepthAndSectionDto()
|
|
|
|
|
{
|
|
|
|
|
Value = new Dictionary<string, object>()
|
|
|
|
|
{
|
|
|
|
|
{ "Key", 1 }
|
|
|
|
|
},
|
|
|
|
|
DepthStart = generatorRandomDigits.Next(1, 5),
|
|
|
|
|
DepthEnd = generatorRandomDigits.Next(5, 15),
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
IdSection = Guid.NewGuid()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2024-12-03 11:35:58 +05:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-12-02 18:14:45 +05:00
|
|
|
|
}
|