From deca1bf35b6a69e46e0d53924e3a3a5ccc6eabe0 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 6 Feb 2024 11:44:29 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=D0=BC=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IDataSaubStatRepository.cs | 4 +-- .../PeriodicWorks/WorkDataSaubStat.cs | 2 +- .../Repository/DataSaubStatRepository.cs | 8 ++--- .../Repository/DataSaubStatRepositoryTest.cs | 36 ++++++++++--------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs index 6894fe41..efab95b7 100644 --- a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs +++ b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs @@ -11,12 +11,12 @@ namespace AsbCloudApp.Repositories public interface IDataSaubStatRepository { /// - /// Получение последних дат по телеметриям + /// Получение последних по дате окончания бурения записей в разрезе телеметрий /// /// ключи телеметрий /// /// - Task> GetLastDatesAsync(int[] idTelemetries, CancellationToken token); + Task> GetLastsAsync(int[] idTelemetries, CancellationToken token); /// /// Вставка записей статистики diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs index 93538b82..bc942353 100644 --- a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs @@ -43,7 +43,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks return; var dataSaubStatRepo = services.GetRequiredService(); - var stats = await dataSaubStatRepo.GetLastDatesAsync(idTelemetries, token); + var stats = await dataSaubStatRepo.GetLastsAsync(idTelemetries, token); for( var i =0; i < idTelemetries.Length; i++) { diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs index cadb4d69..59064abf 100644 --- a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs +++ b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs @@ -20,15 +20,11 @@ namespace AsbCloudInfrastructure.Repository db = dbContext; } - public async Task> GetLastDatesAsync(int[] idTelemetries, CancellationToken token) + public async Task> GetLastsAsync(int[] idTelemetries, CancellationToken token) { var stats = await db.Set() .Where(s => idTelemetries.Contains(s.IdTelemetry)) - .GroupBy(s => s.IdTelemetry, (key, group) => new DataSaubStatDto() - { - IdTelemetry = key, - DateEnd = group.Max(s => s.DateEnd) - }) + .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First().Adapt()) .ToArrayAsync(token); return stats; diff --git a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs index 08ff3584..b328e8b8 100644 --- a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs @@ -75,31 +75,30 @@ namespace AsbCloudWebApi.IntegrationTests.Repository public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) { - var dbSet = dbContext.Set(); - var categories = dbContext.Set(); - - categories.RemoveRange(categories); - dbSet.RemoveRange(dbSet); - dbContext.SaveChanges(); - - categories.Add(category); - - var entities = statDtos.Select(stat => stat.Adapt()); - dbSet.AddRange(entities); - dbContext.SaveChanges(); - dataSaubStatRepository = scope.ServiceProvider.GetRequiredService(); } [Fact] public async Task GetLastDatesAsync_returns_success() { + //prepare + dbContext.CleanupDbSet(); + + var dbSetSaubStat = dbContext.Set(); + var dbSetCategories = dbContext.Set(); + + var entities = statDtos.Select(stat => stat.Adapt()); + + dbSetCategories.Add(category); + dbSetSaubStat.AddRange(entities); + dbContext.SaveChanges(); + //act 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 actual = result.Any() ? result.First().DateEnd : DateTimeOffset.UnixEpoch; + var actual = result.First().DateEnd; //assert Assert.True((expected - actual).Ticks == 0.0); @@ -108,16 +107,19 @@ namespace AsbCloudWebApi.IntegrationTests.Repository [Fact] public async Task InsertRangeAsync_returns_success() { + //prepare + dbContext.CleanupDbSet(); + //act var dbSet = dbContext.Set(); dbSet.RemoveRange(dbSet); dbContext.SaveChanges(); var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); - + //assert Assert.Equal(statDtos.Length, result); - + var statDtosFromDb = dbSet.Select(stat => stat.Adapt()).ToArray(); var excludedProps = new[] {