|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.IO; |
4 | | -using System.Linq; |
5 | | -using System.Reflection; |
6 | | -using System.Threading; |
7 | | -using System.Threading.Tasks; |
8 | | -using Azure; |
9 | | -using Azure.Core.Extensions; |
10 | | -using Azure.Storage.Files.DataLake; |
11 | | -using Azure.Storage.Files.DataLake.Models; |
12 | | -using Microsoft.Extensions.Logging; |
13 | | -using Moq; |
14 | | -using NUnit.Framework; |
| 1 | +using NUnit.Framework; |
15 | 2 | using SqlCollaborative.Azure.DataPipelineTools.DataLake; |
16 | | -using SqlCollaborative.Azure.DataPipelineTools.DataLake.Model; |
17 | 3 |
|
18 | 4 | namespace DataPipelineTools.Tests.DataLake |
19 | 5 | { |
20 | 6 | [TestFixture] |
21 | | - public class DataLakeServiceTests: TestBase |
| 7 | + public class DataLakeServiceTests: DataLakeTestBase |
22 | 8 | { |
23 | | - private const string AccountUri = "mydatalake"; |
24 | | - private const string ContainerName = "mycontainer"; |
25 | 9 |
|
26 | | - private readonly IEnumerable<PathItem> TestData; |
27 | | - |
28 | | - private readonly Mock<DataLakeFileSystemClient> mockFileSystemClient; |
29 | | - private readonly Mock<ILogger<DataLakeServiceFactory>> mockLogger; |
30 | | - |
31 | | - private readonly DataLakeService Sut; |
| 10 | + protected readonly DataLakeService Sut; |
32 | 11 |
|
33 | 12 | public DataLakeServiceTests() |
34 | 13 | { |
35 | | - // Get test data to mock the file system |
36 | | - TestData = GetTestData(); |
37 | | - |
38 | | - // Mock the logger to test where it is called |
39 | | - mockLogger = new Mock<ILogger<DataLakeServiceFactory>>(); |
40 | | - |
41 | | - // Mock the file system client |
42 | | - mockFileSystemClient = BuildMockDataLakeFileSystemClient(); |
43 | | - |
44 | 14 | // Use the factory to inject the mock logger to get the mock client... |
45 | | - var factory = new DataLakeServiceFactory(mockLogger.Object); |
46 | | - Sut = factory.CreateDataLakeService(mockFileSystemClient.Object); |
47 | | - } |
48 | | - |
49 | | - private Mock<DataLakeFileSystemClient> BuildMockDataLakeFileSystemClient() |
50 | | - { |
51 | | - var mockFileSystemClient = new Mock<DataLakeFileSystemClient>(); |
52 | | - mockFileSystemClient.SetupGet(x => x.Name).Returns(ContainerName); |
53 | | - mockFileSystemClient.SetupGet(x => x.AccountName).Returns(AccountUri); |
54 | | - |
55 | | - mockFileSystemClient |
56 | | - .Setup(x => x.GetDirectoryClient(It.IsAny<String>())) |
57 | | - .Returns<string>(BuildMockDataLakeDirectoryClient<DataLakeDirectoryClient>); |
58 | | - mockFileSystemClient |
59 | | - .Setup(x => x.GetFileClient(It.IsAny<String>())) |
60 | | - .Returns<string>(BuildMockDataLakeDirectoryClient<DataLakeFileClient>); |
61 | | - |
62 | | - mockFileSystemClient |
63 | | - .Setup(x => x.GetPaths(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) |
64 | | - .Returns((string path, bool recursive, bool userPrinciaplName, CancellationToken token) => |
65 | | - { |
66 | | - var items = TestData |
67 | | - // Include all files starting with the test path |
68 | | - .Where(x => x.Name.StartsWith(path ?? string.Empty) && path != null) |
69 | | - // Still include them if the recursive flag is set, otherwise check if the relative path after the search path contains |
70 | | - // directory separator to exclude sub dirs |
71 | | - .Where(x => recursive || !x.Name.Substring(path.Length).Contains('/')) |
72 | | - .ToList() |
73 | | - .AsReadOnly(); |
74 | | - |
75 | | - var page = Page<PathItem>.FromValues(items, null, Mock.Of<Response>()); |
76 | | - return Pageable<PathItem>.FromPages(new[] { page }); |
77 | | - }); |
78 | | - |
79 | | - return mockFileSystemClient; |
| 15 | + var factory = new DataLakeServiceFactory(MockLogger.Object); |
| 16 | + Sut = factory.CreateDataLakeService(MockFileSystemClient.Object); |
80 | 17 | } |
81 | 18 |
|
82 | | - private T BuildMockDataLakeDirectoryClient<T>(string directoryName) where T: DataLakePathClient |
83 | | - { |
84 | | - var mockDirectoryClient = new Mock<T>(); |
85 | | - mockDirectoryClient.SetupGet(x => x.FileSystemName).Returns(ContainerName); |
86 | | - mockDirectoryClient.SetupGet(x => x.AccountName).Returns(AccountUri); |
87 | | - mockDirectoryClient.SetupGet(x => x.Name).Returns(directoryName); |
88 | | - |
89 | | - var directoryNameExists = TestData.Any(i => i.Name == directoryName); |
90 | | - mockDirectoryClient |
91 | | - .Setup(x => x.ExistsAsync(It.IsAny<CancellationToken>())) |
92 | | - .ReturnsAsync(() => Response.FromValue(directoryNameExists, new Mock<Response>().Object)); |
93 | | - |
94 | | - mockDirectoryClient |
95 | | - .Setup(x => x.Exists(It.IsAny<CancellationToken>())) |
96 | | - .Returns(() => Response.FromValue(directoryNameExists, new Mock<Response>().Object)); |
97 | | - |
98 | | - return mockDirectoryClient.Object; |
99 | | - } |
100 | | - |
101 | | - private IEnumerable<PathItem> GetTestData() |
102 | | - { |
103 | | - return GetTestData(",", properties => |
104 | | - { |
105 | | - return DataLakeModelFactory.PathItem( |
106 | | - properties[nameof(PathItem.Name)], |
107 | | - Convert.ToBoolean(properties[nameof(PathItem.IsDirectory)]), |
108 | | - Convert.ToDateTime(properties[nameof(PathItem.LastModified)]), |
109 | | - ETag.All, |
110 | | - Convert.ToInt32(properties[nameof(PathItem.ContentLength)]), |
111 | | - null, |
112 | | - null, |
113 | | - null |
114 | | - ); |
115 | | - }).ToArray(); |
116 | | - } |
117 | | - |
118 | | - |
119 | 19 | [SetUp] |
120 | 20 | public void Setup() |
121 | 21 | { |
|
0 commit comments