Skip to content

Commit aabfaee

Browse files
author
Niall Langley
committed
added more unit tests for GetItemsAsync
1 parent e2ebaba commit aabfaee

File tree

2 files changed

+114
-8
lines changed

2 files changed

+114
-8
lines changed

DataPipelineTools.Tests/DataLake/DataLakeServiceTests/GetItemsAsyncTests.cs

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Newtonsoft.Json;
66
using Newtonsoft.Json.Linq;
77
using NUnit.Framework;
8+
using SqlCollaborative.Azure.DataPipelineTools.Common;
89
using SqlCollaborative.Azure.DataPipelineTools.DataLake;
910
using SqlCollaborative.Azure.DataPipelineTools.DataLake.Model;
1011

@@ -46,21 +47,123 @@ public void Given_ValidDirectoryPath_Should_ReturnContents()
4647
Assert.That(result.Count, Is.EqualTo(2));
4748
}
4849

50+
private static DataLakeGetItemsConfig[] DirectoryPathWithIncorrectCase =
51+
{
52+
new DataLakeGetItemsConfig {Directory = "RAW/api/feb"},
53+
new DataLakeGetItemsConfig {Directory = "raw/API/feb"},
54+
new DataLakeGetItemsConfig {Directory = "raw/api/FEB"}
55+
};
56+
[TestCaseSource(nameof(DirectoryPathWithIncorrectCase))]
57+
public void Given_DirectoryPathWithIncorrectCase_Should_ReturnContentsForCorrectedPath(DataLakeGetItemsConfig itemsConfig)
58+
{
59+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
60+
61+
Assert.That(result.FileCount, Is.EqualTo(1));
62+
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));
64+
}
65+
66+
4967
[Test]
50-
public void Given_DirectoryPathWithIncorrectCase_Should_ReturnContentsForCorrectedPath()
68+
public void Given_InvalidDirectoryPath_Should_ThrowException()
5169
{
5270
var itemsConfig = new DataLakeGetItemsConfig
5371
{
54-
Directory = "raw/aPi/feb"
72+
Directory = "some/invalid/path"
5573
};
5674

75+
Assert.CatchAsync(() => Sut.GetItemsAsync(DatalakeConfig, itemsConfig));
76+
}
77+
78+
[Test]
79+
public void Given_DirectoryPathWithIncorrectCase_When_MatchesMultipleDirectories_Should_ThrowException()
80+
{
81+
var itemsConfig = new DataLakeGetItemsConfig
82+
{
83+
Directory = "RAW/api/JAN"
84+
};
85+
86+
Assert.CatchAsync(() => Sut.GetItemsAsync(DatalakeConfig, itemsConfig));
87+
}
88+
89+
90+
91+
private static object[] LimitNRecords =
92+
{
93+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", Limit = 1}, 1},
94+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", Limit = 3}, 3},
95+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", Limit = 5}, 5},
96+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", Limit = 10}, 5} // There's 5 files for that folder in the test csv
97+
};
98+
[TestCaseSource(nameof(LimitNRecords))]
99+
public void Given_LimitNRecords_Should_ReturnNRecords(DataLakeGetItemsConfig itemsConfig, int expectedResultCount)
100+
{
57101
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
58102

59-
Assert.That(result.fileCount, Is.EqualTo(1));
60-
Assert.That(result.files.Count, Is.EqualTo(1));
61-
Assert.That(result.files.Count(x => x.FullPath == "raw/api/feb/delta_extract_3.json"), Is.EqualTo(1));
103+
Assert.That(result.FileCount, Is.EqualTo(expectedResultCount));
62104
}
63105

106+
107+
private static object[] OrderByColumn =
108+
{
109+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", OrderByColumn = "ContentLength" }, new []{10, 20, 30, 40, 50}}, // Default is ascending order
110+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", OrderByColumn = "ContentLength", OrderByDescending = false }, new []{10, 20, 30, 40, 50}},
111+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", OrderByColumn = "ContentLength", OrderByDescending = true }, new []{50, 40, 30, 20, 10}}
112+
};
113+
[TestCaseSource(nameof(OrderByColumn))]
114+
public void Given_OrderBy_Should_ReturnRecordsOrderedBySpecifiedColumnWithDirectionSpecifiedByOrderByDescendingFlag(DataLakeGetItemsConfig itemsConfig, int[] expectedContentLengths)
115+
{
116+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
117+
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+
}
122+
123+
124+
private static object[] LimitNRecordsAndOrderByColumn =
125+
{
126+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", OrderByColumn = "ContentLength" }, new []{10, 20, 30}}, // Default is ascending order
127+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", OrderByColumn = "ContentLength", OrderByDescending = false }, new []{10, 20, 30, 40}},
128+
new object[] {new DataLakeGetItemsConfig {Directory = "raw/api/jan", OrderByColumn = "ContentLength", OrderByDescending = true }, new []{50, 40}}
129+
};
130+
[TestCaseSource(nameof(LimitNRecordsAndOrderByColumn))]
131+
public void Given_LimitNRecordAndOrderBy_Should_ReturnTopNRecordsOrderedBySpecifiedColumnWithDirectionSpecifiedByOrderByDescendingFlag(DataLakeGetItemsConfig itemsConfig, int[] expectedContentLengths)
132+
{
133+
itemsConfig.Limit = expectedContentLengths.Length;
134+
135+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
136+
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]));
140+
}
141+
142+
143+
144+
[Test]
145+
public void Given_RecursiveFlagIsTrue_Should_ReturnContentRecursively()
146+
{
147+
var itemsConfig = new DataLakeGetItemsConfig {Directory = "raw/database", Recursive = true };
148+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
149+
150+
Assert.That(result.FileCount, Is.EqualTo(5));
151+
}
152+
153+
[Test]
154+
public void Given_RecursiveFlagIsFalse_Should_ReturnDirectoryContentsOnly()
155+
{
156+
var itemsConfig = new DataLakeGetItemsConfig { Directory = "raw/database", Recursive = false };
157+
var result = Sut.GetItemsAsync(DatalakeConfig, itemsConfig).Result.ToObject<GetItemsResponse>();
158+
159+
Assert.That(result.FileCount, Is.EqualTo(2));
160+
Assert.That(result.Files.All(x => x.Directory == itemsConfig.Directory), Is.True);
161+
}
162+
163+
164+
165+
166+
//Filters = new[] {FilterFactory<DataLakeItem>.Create("Name", "like:*1*", MockLogger.Object)}
64167
//[Test]
65168
//public void Given_ValidDirectoryPath_Should_ReturnDirectoryPath()
66169
//{
@@ -74,8 +177,8 @@ public void Given_DirectoryPathWithIncorrectCase_Should_ReturnContentsForCorrect
74177

75178
public class GetItemsResponse
76179
{
77-
public int fileCount { get; set; }
78-
public string correctedFilePath { get; set; }
79-
public List<DataLakeItem> files { get; set; }
180+
public int FileCount { get; set; }
181+
public string CorrectedFilePath { get; set; }
182+
public List<DataLakeItem> Files { get; set; }
80183
}
81184
}

DataPipelineTools.Tests/DataLake/DataLakeServiceTests/GetItemsAsyncTests_Data_PathItem.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ raw/database/feb/extract_2.csv,FALSE,20,03/01/2021 13:00
44
raw/database/feb/EXTRACT_2.csv,FALSE,25,03/01/2021 13:00
55
raw/api/jan/delta_extract_1.json,FALSE,10,01/01/2021 14:00
66
raw/api/jan/delta_extract_2.json,FALSE,20,02/01/2021 14:00
7+
raw/api/jan/delta_extract_3.json,FALSE,30,03/01/2021 14:00
8+
raw/api/jan/delta_extract_4.json,FALSE,40,04/01/2021 14:00
9+
raw/api/jan/delta_extract_5.json,FALSE,50,05/01/2021 14:00
710
raw/api/feb/delta_extract_3.json,FALSE,30,03/01/2021 14:00
811
raw/API/jan/delta_extract_1.json,FALSE,10,01/01/2021 15:00

0 commit comments

Comments
 (0)