forked from ddrilling/AsbCloudServer
Правка автотестов по результатам ревью + отдельный хелпер для извлечения замоканных данных
This commit is contained in:
parent
edb7d93a62
commit
f0f8044e77
46
AsbCloudInfrastructure.Tests/CsvMockHelper.cs
Normal file
46
AsbCloudInfrastructure.Tests/CsvMockHelper.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -39,79 +39,44 @@ public class DataSaubStatDtillingQualityServiceTest
|
|||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("DataSaubStat.test3.csv", "DataSaubStat.test3_result.csv")]
|
[InlineData("DataSaubStat.test3.csv", "DataSaubStat.test3_result.csv")]
|
||||||
[InlineData("DataSaubStat.test2.csv", "DataSaubStat.test2_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(
|
public async Task Create_DataSaubStatDrillingQuality_From_CSVFile_ShouldReturn_Success(
|
||||||
string pathMockData,
|
string pathMockData,
|
||||||
string pathResult
|
string pathResult
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var resourceName = Assembly.GetExecutingAssembly()
|
var mockData = CsvMockHelper.Get<TelemetryDataSaubMap>(pathMockData);
|
||||||
.GetManifestResourceNames()
|
var dtoMockData = mockData.Select(x => x.Adapt<TelemetryDataSaubDto>()).ToArray();
|
||||||
.Where(r => r.Contains(pathMockData))
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (String.IsNullOrEmpty(resourceName))
|
dataSaubServiceMock
|
||||||
Assert.Fail("No mock data for test");
|
.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()
|
//act
|
||||||
.GetManifestResourceStream(resourceName)!;
|
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))
|
//assert
|
||||||
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
|
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 expectedItem = expected[i];
|
||||||
|
var actualItem = actual[i];
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
"id_telemetry","depth_start","depth_end","depth_drilling_quality"
|
"id_telemetry","depth_start","depth_end","id_feed_regulator","depth_drilling_quality"
|
||||||
419,46,251.279998779297,0.0
|
618,303.545989990234,493.545989990234,1,50.00
|
||||||
|
|
Loading…
Reference in New Issue
Block a user