Типичная иерархия файла будет такой:
Код: Выделить всё
sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv
Код: Выделить всё
GetFiles
Код: Выделить всё
using Azure.Storage.Files.DataLake;
public class DataLakeService
{
//this object is pointing to the directory "sample-container/process_Id_12/"
private DataLakeDirectoryClient downloadClient;
private readonly string taskid = "task_id_34";
public DataLakeService(DataLakeDirectoryClient dataLakeDirectoryClient)
{
this.downloadClient = dataLakeDirectoryClient;
}
public IAsyncEnumerable GetFiles(DateTime processingDate)
{
DataLakeDirectoryClient dir = downloadClient.GetSubDirectoryClient(taskid)
.GetSubDirectoryClient($"year{processingDate.Year}")
.GetSubDirectoryClient($"month{processingDate.Month}");
if(!dir.Exists()){
return AsyncEnumerable.Empty();
}
var pathItems = dir.GetPathsAsync(true);
await foreach(var pathItem in pathItems)
{
string absoluteFullPathToFile = pathItem.Name; //e.g., sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv
string fileName = System.IO.Path.GetFileName(absoluteFullPathToFile);
if(!IsRegexPatternMatch(fileName))
{
continue;
}
var continer = downloadClient.GetParentFileSystemClient();
string pathToDirectory = System.IO.Path.GetDirectoryName(absoluteFullPathToFile);
var directoryClient = container.GetDirectoryClient(pathToDirectory);
var fileClient = directoryClient.GetFileClient(fileName);
yield return await fileClient.OpenReadAsync();
}
}
}
Я застрял на настройке защищенного метода базы класс DataLakeDirectoryClient.
Класс DataLakeDirectoryClient является производным от DataLakePathClient
Этот конкретный защищенный метод базового класса вызывается через метод расширения базового класса (определение метода расширения GetParentFileSystemClient)
Я попробовал, как показано ниже:
Код: Выделить всё
using Nunit.Framework;
using Moq;
public class DataLakeService
{
public async Task GetFiles_WhenSingleFileInStorage_ThenReturnAStream()
{
//Arrange
var mockDirectoryClient = new Mock();
mockDirectoryClient
.Setup(dc => dc.GetSubDirectoryClient(It.IsAny()))
.Returns(mockDirectoryClient.Object);
mockDirectoryClient
.Setup(dc => dc.Exists(default(CancelationToken)))
.Returns(Response.FromValue(true, Mock.Of()));
mockDirectoryClient
.Setup(dc => dc.GetPathsAsync(true, false, default))
.Returns(() => {
var pathItem1 = DataLakeModelFactory
.PathItem("sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv",
false, new DateTime(2024, 7, 10), ETag.All, 256, "", "", "");
var pathItem2 = DataLakeModelFactory
.PathItem("sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.txt",
false, new DateTime(2024, 7, 10), ETag.All, 256, "", "", "");
var page1 = Page
.FromValues(values: new[] { pathItem1, pathItem2 },
continuationToken: null, response: Mock.Of());
return AsyncPageable.FromPages(new[] { page1 });
});
var mockFileSystemClient = new Mock();
mockFileSystemClient.Setup(fsc => fsc.GetDirectoryClient(It.IsAny()))
.Returns(mockDirectoryClient.Object);
var mockFileClient = new Mock();
Stream stream = BinaryData.FromString("This is a line.\n This is the second line\n").ToStream();
mockFileClient.Setup(fc => fc.OpenReadAsync(0, default, default, default))
.Returns(Task.FromResult(stream));
mockDirectoryClient
.Setup(dc => dc.GetFileClient(It.IsAny()))
.Returns(mockFileClient.Object);
// DataLakeDirectoryClient's GetParentFileSystemClient method get invoked in the SUT
// To set up that method, I have tried both below
mockDirectoryClient.Protected()
.Setup("GetParentDataLakeFileSystemClientCore")
.Returns(mockFileSystemClient.Object);
//Error for above is "No protected method DataLakeDirectoryClient.GetParentDataLakeFileSystemClientCore found whose signature is compatible with the provided arguments ()."
mockDirectoryClient
.Setup(dc => dc.GetParentFileSystemClient())
.Returns(mockFileSystemClient.Object);
// Error: "Unsupported expression : dc => dc.GetParentFileSystemClient()
// Extension meethods(here: SpecializedDataLakeExtensions.GetParentFileSystemClient) May not be used in setup / verification expression "
//Act
//Assert
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... re-storage