Skip to content

Commit 06b45d4

Browse files
author
Niall Langley
committed
Added more tests and refactored some existing ones to remove linq queries on collections to make them more readable
1 parent 476d57a commit 06b45d4

File tree

2 files changed

+155
-20
lines changed

2 files changed

+155
-20
lines changed

DataPipelineTools.Tests/DataLake/DataLakeServiceTests/CheckPathAsyncTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CheckPathAsyncTests: DataLakeTestBase
1414
public CheckPathAsyncTests()
1515
{
1616
// Use the factory to inject the mock logger to get the mock client...
17-
var factory = new DataLakeServiceFactory(MockLogger.Object);
17+
var factory = new DataLakeServiceFactory(Logger);
1818
Sut = factory.CreateDataLakeService(MockFileSystemClient.Object);
1919
}
2020

DataPipelineTools.Tests/DataLake/DataLakeServiceTests/GetItemsAsyncTests.cs

Lines changed: 154 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class GetItemsAsyncTests : DataLakeTestBase
2121
public GetItemsAsyncTests()
2222
{
2323
// Use the factory to inject the mock logger to get the mock client...
24-
var factory = new DataLakeServiceFactory(MockLogger.Object);
24+
var factory = new DataLakeServiceFactory(Logger);
2525
Sut = factory.CreateDataLakeService(MockFileSystemClient.Object);
2626
}
2727

@@ -35,7 +35,7 @@ public void Setup()
3535
}
3636

3737
[Test]
38-
public void Given_ValidDirectoryPath_Should_ReturnContents()
38+
public void Given_ValidDirectoryPath_Should_ReturnContentsAsJson()
3939
{
4040
var itemsConfig = new DataLakeGetItemsConfig
4141
{
@@ -60,7 +60,7 @@ public void Given_DirectoryPathWithIncorrectCase_Should_ReturnContentsForCorrect
6060

6161
Assert.That(result.FileCount, Is.EqualTo(1));
6262
Assert.That(result.Files.Count, Is.EqualTo(1));
63-
Assert.That(result.Files.Count(x => x.FullPath == "raw/api/feb/delta_extract_3.json"), Is.EqualTo(1));
63+
Assert.That(result.Files, Has.All.Property(nameof(DataLakeItem.FullPath)).EqualTo("raw/api/feb/delta_extract_3.json"));
6464
}
6565

6666

@@ -101,6 +101,7 @@ public void Given_LimitNRecords_Should_ReturnNRecords(DataLakeGetItemsConfig ite
101101
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
102102

103103
Assert.That(result.FileCount, Is.EqualTo(expectedResultCount));
104+
Assert.That(result.Files, Has.Count.EqualTo(expectedResultCount));
104105
}
105106

106107

@@ -113,11 +114,13 @@ public void Given_LimitNRecords_Should_ReturnNRecords(DataLakeGetItemsConfig ite
113114
[TestCaseSource(nameof(OrderByColumn))]
114115
public void Given_OrderBy_Should_ReturnRecordsOrderedBySpecifiedColumnWithDirectionSpecifiedByOrderByDescendingFlag(DataLakeGetItemsConfig itemsConfig, int[] expectedContentLengths)
115116
{
117+
var expectedFileCount = expectedContentLengths.Length;
118+
116119
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
117120

118-
Assert.That(result.FileCount, Is.EqualTo(expectedContentLengths.Length));
119-
for (int i = 0; i < 5; i++)
120-
Assert.That(result.Files[i].ContentLength, Is.EqualTo(expectedContentLengths[i]));
121+
Assert.That(result.FileCount, Is.EqualTo(expectedFileCount));
122+
Assert.That(result.Files, Has.Count.EqualTo(expectedFileCount));
123+
Assert.That(result.Files.Select(item => item.ContentLength), Is.EquivalentTo(expectedContentLengths));
121124
}
122125

123126

@@ -130,24 +133,24 @@ public void Given_OrderBy_Should_ReturnRecordsOrderedBySpecifiedColumnWithDirect
130133
[TestCaseSource(nameof(LimitNRecordsAndOrderByColumn))]
131134
public void Given_LimitNRecordAndOrderBy_Should_ReturnTopNRecordsOrderedBySpecifiedColumnWithDirectionSpecifiedByOrderByDescendingFlag(DataLakeGetItemsConfig itemsConfig, int[] expectedContentLengths)
132135
{
133-
itemsConfig.Limit = expectedContentLengths.Length;
136+
var expectedFileCount = expectedContentLengths.Length;
137+
itemsConfig.Limit = expectedFileCount;
134138

135139
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
136140

137-
Assert.That(result.FileCount, Is.EqualTo(expectedContentLengths.Length));
138-
for (int i = 0; i < expectedContentLengths.Length; i++)
139-
Assert.That(result.Files[i].ContentLength, Is.EqualTo(expectedContentLengths[i]));
141+
Assert.That(result.FileCount, Is.EqualTo(expectedFileCount));
142+
Assert.That(result.Files, Has.Count.EqualTo(expectedFileCount));
143+
Assert.That(result.Files.Select(item => item.ContentLength), Is.EquivalentTo(expectedContentLengths));
140144
}
141145

142-
143-
144146
[Test]
145147
public void Given_RecursiveFlagIsTrue_Should_ReturnContentRecursively()
146148
{
147149
var itemsConfig = new DataLakeGetItemsConfig {Directory = "raw/database", Recursive = true };
148150
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
149151

150152
Assert.That(result.FileCount, Is.EqualTo(5));
153+
Assert.That(result.Files, Has.Count.EqualTo(5));
151154
}
152155

153156
[Test]
@@ -157,22 +160,154 @@ public void Given_RecursiveFlagIsFalse_Should_ReturnDirectoryContentsOnly()
157160
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
158161

159162
Assert.That(result.FileCount, Is.EqualTo(2));
160-
Assert.That(result.Files.All(x => x.Directory == itemsConfig.Directory), Is.True);
163+
Assert.That(result.Files, Has.Count.EqualTo(2));
164+
Assert.That(result.Files, Has.All.Property(nameof(DataLakeItem.Directory)).EqualTo(itemsConfig.Directory));
165+
}
166+
167+
[Test]
168+
public void Given_DirectoryPathWithIncorrectCaseAndIgnoreDirectoryCaseIsFalse_Should_Throw()
169+
{
170+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw/DATABASE", IgnoreDirectoryCase = false };
171+
Assert.CatchAsync(() => Sut.GetItemsAsync(DatalakeConfig, itemsConfig));
172+
}
173+
174+
[Test]
175+
public void Given_DirectoryPathWithIncorrectCaseAndIgnoreDirectoryCaseTrue_Should_ReturnContents()
176+
{
177+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw/DATABASE", IgnoreDirectoryCase = true };
178+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
179+
180+
Assert.That(result.FileCount, Is.GreaterThan(0));
181+
Assert.That(result.Files, Has.Count.GreaterThan(0));
182+
}
183+
184+
[Test]
185+
public void Given_DirectoryPathWithIncorrectCaseAndIgnoreDirectoryCaseIsTrue_WhenMatchesOneDirectoryPath_Should_ReturnCorrectedPath()
186+
{
187+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw/DATABASE", IgnoreDirectoryCase = true };
188+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
189+
190+
Assert.That(result.CorrectedFilePath, Is.EqualTo("raw/database"));
161191
}
162192

193+
[Test]
194+
public void Given_DirectoryPathWithIncorrectCaseAndIgnoreDirectoryCaseIsTrue_WhenMatcheMultipleDirectoryPaths_Should_Throw()
195+
{
196+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw/ApI", IgnoreDirectoryCase = true };
197+
Assert.CatchAsync(() => Sut.GetItemsAsync(DatalakeConfig, itemsConfig));
198+
}
163199

200+
201+
202+
private static object[][] Filters =
203+
{
204+
new object[] { "raw/api/jan", nameof(DataLakeItem.Name), "eq:delta_extract_4.json", 1},
205+
new object[] { "raw/api/jan", nameof(DataLakeItem.Name), "ne:delta_extract_4.json", 4},
206+
new object[] { "raw/api/jan", nameof(DataLakeItem.ContentLength), "gt:40", 1},
207+
new object[] { "raw/api/jan", nameof(DataLakeItem.ContentLength), "ge:40", 2},
208+
new object[] { "raw/api/jan", nameof(DataLakeItem.ContentLength), "lt:20", 1},
209+
new object[] { "raw/api/jan", nameof(DataLakeItem.ContentLength), "le:20", 2},
210+
new object[] { "raw/api", nameof(DataLakeItem.Directory), "like:*feb*", 1},
211+
new object[] { "raw/api", nameof(DataLakeItem.FullPath), "like:delta_extract_[3-5].json", 4},
212+
new object[] { "raw/api", nameof(DataLakeItem.Url), "like:.+raw\\/api.+", 8}, // 6 files + 2 directories
213+
new object[] { "raw/api", nameof(DataLakeItem.IsDirectory), "eq:true", 2},
214+
new object[] { "raw/api", nameof(DataLakeItem.IsDirectory), "eq:false", 6},
215+
new object[] { "raw/api", nameof(DataLakeItem.LastModified), "ge:2021-01-04T14:00:00", 2},
216+
};
217+
[TestCaseSource(nameof(Filters))]
218+
public void Given_Filter_Should_ReturnRecordsMatchingFilter(string directory, string filterProperty, string filterExpression, int expectedFileCount)
219+
{
220+
// Have to build the filter here rather than pass it in using TestCaseSource as the logger is not static
221+
var filter = FilterFactory<DataLakeItem>.Create(filterProperty, filterExpression, Logger);
222+
var itemsConfig = new DataLakeGetItemsConfig {Directory = directory, Filters = new[] {filter}};
223+
224+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
225+
226+
Assert.That(result.FileCount, Is.EqualTo(expectedFileCount));
227+
Assert.That(result.Files, Has.Count.EqualTo(expectedFileCount));
228+
}
229+
230+
private static object[][] InvalidFilters =
231+
{
232+
new object[] { "raw/api/jan", nameof(DataLakeItem.Name), "neq:delta_extract_4.json"}, // Invalid operator
233+
new object[] { "raw/api/jan", nameof(DataLakeItem.ContentLength), "eq:delta_extract_4.json"}, // Invalid value type
234+
new object[] { "raw/api/jan", "Some invalid property", "ne:delta_extract_4.json"},
235+
new object[] { "raw/api/jan", string.Empty, "ne:delta_extract_4.json"},
236+
new object[] { "raw/api/jan", null, "ne:delta_extract_4.json"},
237+
238+
new object[] { "raw/api/jan", nameof(DataLakeItem.Name), string.Empty},
239+
new object[] { "raw/api/jan", nameof(DataLakeItem.Name), null},
240+
241+
};
242+
[TestCaseSource(nameof(InvalidFilters))]
243+
public void Given_InvalidFilter_Should_Throw(string directory, string filterProperty, string filterExpression)
244+
{
245+
// Have to build the filter here rather than pass it in using TestCaseSource as the logger is not static
246+
var filter = FilterFactory<DataLakeItem>.Create(filterProperty, filterExpression, Logger);
247+
var itemsConfig = new DataLakeGetItemsConfig { Directory = directory, Filters = new[] { filter } };
248+
249+
Assert.CatchAsync(() => Sut.GetItemsAsync(DatalakeConfig, itemsConfig));
250+
}
251+
252+
[TestCaseSource(nameof(InvalidFilters))]
253+
public void Given_MultipleFilters_WhenSomeAreInvalid_Should_Throw(string directory, string filterProperty, string filterExpression)
254+
{
255+
var filters = new[]
256+
{
257+
FilterFactory<DataLakeItem>.Create(nameof(DataLakeItem.FullPath), "like:*jan*", Logger),
258+
FilterFactory<DataLakeItem>.Create(nameof(DataLakeItem.IsDirectory), "eq:5", Logger),
259+
};
260+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw", Filters = filters };
261+
262+
Assert.CatchAsync(() => Sut.GetItemsAsync(DatalakeConfig, itemsConfig));
263+
}
264+
265+
[Test]
266+
public void Given_MultipleValidFiltersOfDifferentTypes_Should_ReturnFilteredResults()
267+
{
268+
var filters = new[]
269+
{
270+
FilterFactory<DataLakeItem>.Create(nameof(DataLakeItem.FullPath), "like:*jan*", Logger),
271+
FilterFactory<DataLakeItem>.Create(nameof(DataLakeItem.IsDirectory), "eq:true", Logger),
272+
};
273+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw", Filters = filters};
274+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
275+
276+
Assert.That(result.FileCount, Is.EqualTo(3));
277+
Assert.That(result.Files, Has.Count.EqualTo(3));
278+
279+
Assert.That(result.Files, Has.All.Property(nameof(DataLakeItem.IsDirectory)).True);
280+
}
281+
282+
283+
[Test]
284+
public void Given_MultipleValidFiltersOfSameType_Should_ReturnFilteredResults()
285+
{
286+
var filters = new[]
287+
{
288+
FilterFactory<DataLakeItem>.Create(nameof(DataLakeItem.FullPath), "like:*jan*", Logger),
289+
FilterFactory<DataLakeItem>.Create(nameof(DataLakeItem.FullPath), "like:*delta", Logger),
290+
};
291+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw", Filters = filters };
292+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
293+
294+
Assert.That(result.FileCount, Is.EqualTo(6));
295+
Assert.That(result.Files, Has.Count.EqualTo(6));
296+
297+
Assert.That(result.Files, Has.All.Property(nameof(DataLakeItem.FullPath)).Contains("jan"));
298+
Assert.That(result.Files, Has.All.Property(nameof(DataLakeItem.FullPath)).Contains("delta"));
299+
}
164300

165301

166-
//Filters = new[] {FilterFactory<DataLakeItem>.Create("Name", "like:*1*", MockLogger.Object)}
167302
//[Test]
168-
//public void Given_ValidDirectoryPath_Should_ReturnDirectoryPath()
303+
//public void Given__Should_Return()
169304
//{
170-
// var testPath = "raw/database";
171-
// var resultPath = Sut.CheckPathAsync(testPath, true).Result;
305+
// var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw/database", Recursive = false };
306+
// var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
172307

173-
// Assert.That(resultPath, Is.EqualTo(testPath));
308+
// Assert.That(result.FileCount, Is.EqualTo(2));
309+
// Assert.That(result.Files.All(x => x.Directory == itemsConfig.Directory), Is.True);
174310
//}
175-
176311
}
177312

178313
public class GetItemsResponse

0 commit comments

Comments
 (0)