Правка автотестов по результатам ревью + отдельный хелпер для извлечения замоканных данных

This commit is contained in:
Olga Nemt 2024-10-17 15:42:01 +05:00
parent edb7d93a62
commit f0f8044e77
3 changed files with 75 additions and 64 deletions

View File

@ -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;
/// <summary>
/// Хелпер, который помогает получать мок-данные из csv файла
/// </summary>
public static class CsvMockHelper
{
/// <summary>
/// метод получения данных из файла
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">путь до файла</param>
/// <returns></returns>
/// <exception cref="ArgumentInvalidException"></exception>
public static IEnumerable<T> Get<T>(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<T>().ToArray();
return dataSaubMaps;
}
}
}
}

View File

@ -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<TelemetryDataSaubMap>(pathMockData);
var dtoMockData = mockData.Select(x => x.Adapt<TelemetryDataSaubDto>()).ToArray();
if (String.IsNullOrEmpty(resourceName))
Assert.Fail("No mock data for test");
dataSaubServiceMock
.Get(Arg.Any<int>(), Arg.Any<bool>(), Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(dtoMockData);
using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName)!;
//act
var actualData = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None);
var actualData = Enumerable.Empty<DataSaubStatDrillingQualityDto>();
var mockDataResult = CsvMockHelper.Get<DataSaubStatDrillingQualityDtoMap>(pathResult);
var expectedData = mockDataResult.Select(x => x.Adapt<DataSaubStatDrillingQualityDto>()).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<TelemetryDataSaubMap>().ToList();
var dtos = dataSaubMaps.Select(x => x.Adapt<TelemetryDataSaubDto>()).ToArray();
dataSaubServiceMock
.Get(Arg.Any<int>(), Arg.Any<bool>(), Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.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<DataSaubStatDrillingQualityDtoMap>().ToList();
var expectedData = dataSaubMaps.Select(x => x.Adapt<DataSaubStatDrillingQualityDto>()).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);
}
}
}

View File

@ -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

1 id_telemetry depth_start depth_end id_feed_regulator depth_drilling_quality
2 419 618 46 303.545989990234 251.279998779297 493.545989990234 1 0.0 50.00