Правки по результатам ревью

This commit is contained in:
Olga Nemt 2024-02-06 11:44:29 +05:00
parent a7d7440a07
commit deca1bf35b
4 changed files with 24 additions and 26 deletions

View File

@ -11,12 +11,12 @@ namespace AsbCloudApp.Repositories
public interface IDataSaubStatRepository public interface IDataSaubStatRepository
{ {
/// <summary> /// <summary>
/// Получение последних дат по телеметриям /// Получение последних по дате окончания бурения записей в разрезе телеметрий
/// </summary> /// </summary>
/// <param name="idTelemetries">ключи телеметрий</param> /// <param name="idTelemetries">ключи телеметрий</param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<DataSaubStatDto>> GetLastDatesAsync(int[] idTelemetries, CancellationToken token); Task<IEnumerable<DataSaubStatDto>> GetLastsAsync(int[] idTelemetries, CancellationToken token);
/// <summary> /// <summary>
/// Вставка записей статистики /// Вставка записей статистики

View File

@ -43,7 +43,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
return; return;
var dataSaubStatRepo = services.GetRequiredService<IDataSaubStatRepository>(); var dataSaubStatRepo = services.GetRequiredService<IDataSaubStatRepository>();
var stats = await dataSaubStatRepo.GetLastDatesAsync(idTelemetries, token); var stats = await dataSaubStatRepo.GetLastsAsync(idTelemetries, token);
for( var i =0; i < idTelemetries.Length; i++) for( var i =0; i < idTelemetries.Length; i++)
{ {

View File

@ -20,15 +20,11 @@ namespace AsbCloudInfrastructure.Repository
db = dbContext; db = dbContext;
} }
public async Task<IEnumerable<DataSaubStatDto>> GetLastDatesAsync(int[] idTelemetries, CancellationToken token) public async Task<IEnumerable<DataSaubStatDto>> GetLastsAsync(int[] idTelemetries, CancellationToken token)
{ {
var stats = await db.Set<DataSaubStat>() var stats = await db.Set<DataSaubStat>()
.Where(s => idTelemetries.Contains(s.IdTelemetry)) .Where(s => idTelemetries.Contains(s.IdTelemetry))
.GroupBy(s => s.IdTelemetry, (key, group) => new DataSaubStatDto() .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First().Adapt<DataSaubStatDto>())
{
IdTelemetry = key,
DateEnd = group.Max(s => s.DateEnd)
})
.ToArrayAsync(token); .ToArrayAsync(token);
return stats; return stats;

View File

@ -75,31 +75,30 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory)
{ {
var dbSet = dbContext.Set<DataSaubStat>();
var categories = dbContext.Set<WellOperationCategory>();
categories.RemoveRange(categories);
dbSet.RemoveRange(dbSet);
dbContext.SaveChanges();
categories.Add(category);
var entities = statDtos.Select(stat => stat.Adapt<DataSaubStat>());
dbSet.AddRange(entities);
dbContext.SaveChanges();
dataSaubStatRepository = scope.ServiceProvider.GetRequiredService<IDataSaubStatRepository>(); dataSaubStatRepository = scope.ServiceProvider.GetRequiredService<IDataSaubStatRepository>();
} }
[Fact] [Fact]
public async Task GetLastDatesAsync_returns_success() public async Task GetLastDatesAsync_returns_success()
{ {
//prepare
dbContext.CleanupDbSet<DataSaubStat>();
var dbSetSaubStat = dbContext.Set<DataSaubStat>();
var dbSetCategories = dbContext.Set<WellOperationCategory>();
var entities = statDtos.Select(stat => stat.Adapt<DataSaubStat>());
dbSetCategories.Add(category);
dbSetSaubStat.AddRange(entities);
dbContext.SaveChanges();
//act //act
var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray(); var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray();
var result = await dataSaubStatRepository.GetLastDatesAsync(telemetryIds, CancellationToken.None); var result = await dataSaubStatRepository.GetLastsAsync(telemetryIds, CancellationToken.None);
var expected = statDtos.Max(stat => stat.DateEnd); var expected = statDtos.Max(stat => stat.DateEnd);
var actual = result.Any() ? result.First().DateEnd : DateTimeOffset.UnixEpoch; var actual = result.First().DateEnd;
//assert //assert
Assert.True((expected - actual).Ticks == 0.0); Assert.True((expected - actual).Ticks == 0.0);
@ -108,16 +107,19 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
[Fact] [Fact]
public async Task InsertRangeAsync_returns_success() public async Task InsertRangeAsync_returns_success()
{ {
//prepare
dbContext.CleanupDbSet<DataSaubStat>();
//act //act
var dbSet = dbContext.Set<DataSaubStat>(); var dbSet = dbContext.Set<DataSaubStat>();
dbSet.RemoveRange(dbSet); dbSet.RemoveRange(dbSet);
dbContext.SaveChanges(); dbContext.SaveChanges();
var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None);
//assert //assert
Assert.Equal(statDtos.Length, result); Assert.Equal(statDtos.Length, result);
var statDtosFromDb = dbSet.Select(stat => stat.Adapt<DataSaubStatDto>()).ToArray(); var statDtosFromDb = dbSet.Select(stat => stat.Adapt<DataSaubStatDto>()).ToArray();
var excludedProps = new[] { var excludedProps = new[] {