From f0f8044e77a9426ff1ebfddd36c350d08c9d3a5e Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 17 Oct 2024 15:42:01 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20=D0=B0?= =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2=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=20+=20=D0=BE?= =?UTF-8?q?=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D1=85=D0=B5?= =?UTF-8?q?=D0=BB=D0=BF=D0=B5=D1=80=20=D0=B4=D0=BB=D1=8F=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=BA=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudInfrastructure.Tests/CsvMockHelper.cs | 46 ++++++++++ .../DataSaubStatDtillingQualityServiceTest.cs | 89 ++++++------------- .../Services/DataSaubStat/test1_result.csv | 4 +- 3 files changed, 75 insertions(+), 64 deletions(-) create mode 100644 AsbCloudInfrastructure.Tests/CsvMockHelper.cs diff --git a/AsbCloudInfrastructure.Tests/CsvMockHelper.cs b/AsbCloudInfrastructure.Tests/CsvMockHelper.cs new file mode 100644 index 00000000..2c9f2228 --- /dev/null +++ b/AsbCloudInfrastructure.Tests/CsvMockHelper.cs @@ -0,0 +1,46 @@ +using AsbCloudApp.Exceptions; +using CsvHelper; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace AsbCloudInfrastructure.Tests; + +/// +/// Хелпер, который помогает получать мок-данные из csv файла +/// +public static class CsvMockHelper +{ + /// + /// метод получения данных из файла + /// + /// + /// путь до файла + /// + /// + public static IEnumerable Get(string path) + { + var resourceName = Assembly.GetExecutingAssembly() + .GetManifestResourceNames() + .Where(r => r.Contains(path)) + .FirstOrDefault(); + + if (String.IsNullOrEmpty(resourceName)) + throw new ArgumentInvalidException(nameof(path), "Файл с mock-данными не найден"); + + using var stream = Assembly.GetExecutingAssembly() + .GetManifestResourceStream(resourceName)!; + + using (var reader = new StreamReader(stream)) + { + using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) + { + var dataSaubMaps = csv.GetRecords().ToArray(); + return dataSaubMaps; + } + } + } +} diff --git a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs index 0ad9700c..0a3d4192 100644 --- a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs @@ -39,79 +39,44 @@ public class DataSaubStatDtillingQualityServiceTest [Theory] [InlineData("DataSaubStat.test3.csv", "DataSaubStat.test3_result.csv")] [InlineData("DataSaubStat.test2.csv", "DataSaubStat.test2_result.csv")] - //[InlineData("DataSaubStat.test1.csv", "DataSaubStat.test1_result.csv")] + [InlineData("DataSaubStat.test1.csv", "DataSaubStat.test1_result.csv")] public async Task Create_DataSaubStatDrillingQuality_From_CSVFile_ShouldReturn_Success( string pathMockData, string pathResult ) { - var resourceName = Assembly.GetExecutingAssembly() - .GetManifestResourceNames() - .Where(r => r.Contains(pathMockData)) - .FirstOrDefault(); + var mockData = CsvMockHelper.Get(pathMockData); + var dtoMockData = mockData.Select(x => x.Adapt()).ToArray(); - if (String.IsNullOrEmpty(resourceName)) - Assert.Fail("No mock data for test"); + dataSaubServiceMock + .Get(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(dtoMockData); - using var stream = Assembly.GetExecutingAssembly() - .GetManifestResourceStream(resourceName)!; + //act + var actualData = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None); - var actualData = Enumerable.Empty(); + var mockDataResult = CsvMockHelper.Get(pathResult); + var expectedData = mockDataResult.Select(x => x.Adapt()).ToArray(); - using (var reader = new StreamReader(stream)) - using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) + //assert + var actual = actualData + .OrderBy(d => d.IdFeedRegulator) + .ToArray(); + + var expected = expectedData + .OrderBy(d => d.IdFeedRegulator) + .ToArray(); + + Assert.Equal(expected.Count(), actual.Count()); + + for (var i = 0; i < actual.Count(); i++) { - var dataSaubMaps = csv.GetRecords().ToList(); - - var dtos = dataSaubMaps.Select(x => x.Adapt()).ToArray(); - - dataSaubServiceMock - .Get(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) - .Returns(dtos); - - //act - actualData = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None); - } - - resourceName = Assembly.GetExecutingAssembly() - .GetManifestResourceNames() - .Where(r => r.Contains(pathResult)) - .FirstOrDefault(); - - if (String.IsNullOrEmpty(resourceName)) - Assert.Fail("No mock data for test"); - - using var stream2 = Assembly.GetExecutingAssembly() - .GetManifestResourceStream(resourceName)!; - - using (var reader = new StreamReader(stream2)) - using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) - { - var dataSaubMaps = csv.GetRecords().ToList(); - var expectedData = dataSaubMaps.Select(x => x.Adapt()).ToArray(); - - //assert - var actual = actualData - .OrderBy(d => d.IdFeedRegulator) - .ToArray(); - - var expected = expectedData - .OrderBy(d => d.IdFeedRegulator) - .ToArray(); - - Assert.Equal(expected.Count(), actual.Count()); - - for (var i = 0; i < actual.Count(); i++) - { - var expectedItem = expected[i]; - var actualItem = actual[i]; - - Assert.True(Math.Abs(expectedItem.DepthStart - actualItem.DepthStart) < 0.000001); - Assert.True(Math.Abs(expectedItem.DepthEnd - actualItem.DepthEnd) < 0.000001); - Assert.True(Math.Abs(expectedItem.DepthDrillingQuality - actualItem.DepthDrillingQuality) < 0.000001); - } - + var expectedItem = expected[i]; + var actualItem = actual[i]; + Assert.True(Math.Abs(expectedItem.DepthStart - actualItem.DepthStart) < 0.000001); + Assert.True(Math.Abs(expectedItem.DepthEnd - actualItem.DepthEnd) < 0.000001); + Assert.True(Math.Abs(expectedItem.DepthDrillingQuality - actualItem.DepthDrillingQuality) < 0.000001); } } } diff --git a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test1_result.csv b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test1_result.csv index 64c49983..fe77a356 100644 --- a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test1_result.csv +++ b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test1_result.csv @@ -1,2 +1,2 @@ -"id_telemetry","depth_start","depth_end","depth_drilling_quality" -419,46,251.279998779297,0.0 +"id_telemetry","depth_start","depth_end","id_feed_regulator","depth_drilling_quality" +618,303.545989990234,493.545989990234,1,50.00