forked from ddrilling/AsbCloudServer
47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|