DD.WellWorkover.Cloud/AsbCloudInfrastructure.Tests/CsvMockHelper.cs

47 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}
}