2 using System.Collections.Generic;
5 using System.Threading.Tasks;
17 [Trait(
"Category",
"Integration")]
18 [Trait(
"Category",
"Async")]
22 private string? _tempDir;
23 private const string TestDirName =
"async_kifs_test_dir";
30 _tempDir = Path.Combine(Path.GetTempPath(), $
"async_kifs_test_{Guid.NewGuid():N}");
31 Directory.CreateDirectory(_tempDir);
34 try { _fileHandler.
DeleteDirectory(TestDirName,
true,
true); }
catch { }
36 await Task.CompletedTask;
42 if (_fileHandler !=
null)
44 try { _fileHandler.
DeleteDirectory(TestDirName,
true,
true); }
catch { }
48 if (_tempDir !=
null && Directory.Exists(_tempDir))
50 try { Directory.Delete(_tempDir,
true); }
catch { }
55 await Task.CompletedTask;
58 #region Directory Tests 84 Assert.NotNull(directories);
85 Assert.Contains(directories, d => d.KifsPath == TestDirName);
98 Assert.False(_fileHandler!.KifsDirectoryExists(
"non_existing_dir_" + Guid.NewGuid().ToString(
"N")));
118 Assert.NotNull(directories);
119 Assert.Single(directories);
120 Assert.Equal(TestDirName, directories[0].KifsPath);
121 Assert.NotEmpty(directories[0].CreatedBy);
126 #region Upload Tests (Async) 134 var testFile = Path.Combine(_tempDir!,
"test_upload.txt");
135 await File.WriteAllTextAsync(testFile,
"Test content for upload");
138 await _fileHandler.
UploadAsync(
new List<string> { testFile }, TestDirName);
141 var files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
142 Assert.Single(files);
143 Assert.Contains(files, f => f.FileName.EndsWith(
"test_upload.txt"));
152 var file1 = Path.Combine(_tempDir!,
"file1.txt");
153 var file2 = Path.Combine(_tempDir!,
"file2.txt");
154 var file3 = Path.Combine(_tempDir!,
"file3.txt");
156 await File.WriteAllTextAsync(file1,
"Content 1");
157 await File.WriteAllTextAsync(file2,
"Content 2");
158 await File.WriteAllTextAsync(file3,
"Content 3");
161 await _fileHandler.
UploadAsync(
new List<string> { file1, file2, file3 }, TestDirName);
164 var files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
165 Assert.Equal(3, files.Count);
174 var largeFile = Path.Combine(_tempDir!,
"large_file.bin");
175 var content =
new byte[5 * 1024 * 1024];
176 new Random().NextBytes(content);
177 await File.WriteAllBytesAsync(largeFile, content);
180 await _fileHandler.
UploadAsync(
new List<string> { largeFile }, TestDirName);
183 var files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
184 Assert.Single(files);
185 Assert.True(files[0].FileSize > 0);
193 var testFile = Path.Combine(_tempDir!,
"test_with_options.txt");
194 await File.WriteAllTextAsync(testFile,
"Test content with options");
198 DeleteIfExists =
false 201 await _fileHandler.
UploadAsync(
new List<string> { testFile }, TestDirName, options);
203 var files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
204 Assert.Single(files);
212 var testFile = Path.Combine(_tempDir!,
"test_callback.txt");
213 await File.WriteAllTextAsync(testFile,
"Hello, Callback!");
215 var callback =
new TestUploadListener();
219 Assert.True(callback.FullFileUploadCalled);
228 await _fileHandler.
UploadAsync(
new List<string> {
"/non/existent/file.txt" }, TestDirName));
234 var testFile = Path.Combine(_tempDir!,
"test.txt");
235 await File.WriteAllTextAsync(testFile,
"Test");
238 await _fileHandler!.
UploadAsync(
new List<string> { testFile },
"non_existent_dir_" + Guid.NewGuid().ToString(
"N")));
243 #region Download Tests (Async) 251 var uploadFile = Path.Combine(_tempDir!,
"upload_for_download.txt");
252 var testContent =
"Test content for download";
253 await File.WriteAllTextAsync(uploadFile, testContent);
254 await _fileHandler.
UploadAsync(
new List<string> { uploadFile }, TestDirName);
257 var downloadDir = Path.Combine(_tempDir!,
"downloads");
258 Directory.CreateDirectory(downloadDir);
261 new List<string> { $
"{TestDirName}/upload_for_download.txt" },
265 var downloadedFile = Path.Combine(downloadDir,
"upload_for_download.txt");
266 Assert.True(File.Exists(downloadedFile));
267 var downloadedContent = await File.ReadAllTextAsync(downloadedFile);
268 Assert.Equal(testContent, downloadedContent);
277 var file1 = Path.Combine(_tempDir!,
"download1.txt");
278 var file2 = Path.Combine(_tempDir!,
"download2.txt");
279 await File.WriteAllTextAsync(file1,
"Content 1");
280 await File.WriteAllTextAsync(file2,
"Content 2");
281 await _fileHandler.
UploadAsync(
new List<string> { file1, file2 }, TestDirName);
284 var downloadDir = Path.Combine(_tempDir!,
"downloads_multi");
285 Directory.CreateDirectory(downloadDir);
289 $
"{TestDirName}/download1.txt",
290 $
"{TestDirName}/download2.txt" 295 Assert.True(File.Exists(Path.Combine(downloadDir,
"download1.txt")));
296 Assert.True(File.Exists(Path.Combine(downloadDir,
"download2.txt")));
305 var csvFile1 = Path.Combine(_tempDir!,
"data1.csv");
306 var csvFile2 = Path.Combine(_tempDir!,
"data2.csv");
307 var txtFile = Path.Combine(_tempDir!,
"readme.txt");
309 await File.WriteAllTextAsync(csvFile1,
"csv1");
310 await File.WriteAllTextAsync(csvFile2,
"csv2");
311 await File.WriteAllTextAsync(txtFile,
"txt");
313 await _fileHandler.
UploadAsync(
new List<string> { csvFile1, csvFile2, txtFile }, TestDirName);
316 var downloadDir = Path.Combine(_tempDir!,
"downloads_glob");
317 Directory.CreateDirectory(downloadDir);
320 new List<string> { $
"{TestDirName}/*.csv" },
324 Assert.True(File.Exists(Path.Combine(downloadDir,
"data1.csv")));
325 Assert.True(File.Exists(Path.Combine(downloadDir,
"data2.csv")));
326 Assert.False(File.Exists(Path.Combine(downloadDir,
"readme.txt")));
334 var testFileName =
"test_overwrite.txt";
335 var uploadPath = Path.Combine(_tempDir!, testFileName);
336 await File.WriteAllTextAsync(uploadPath,
"Original content");
337 await _fileHandler.
UploadAsync(
new List<string> { uploadPath }, TestDirName);
339 var downloadDir = Path.Combine(_tempDir!,
"downloads_overwrite");
340 Directory.CreateDirectory(downloadDir);
341 var existingFile = Path.Combine(downloadDir, testFileName);
342 await File.WriteAllTextAsync(existingFile,
"Existing content");
344 var remoteFilePath = $
"{TestDirName}/{testFileName}";
348 await _fileHandler.
DownloadAsync(
new List<string> { remoteFilePath }, downloadDir, options,
null));
353 #region File Management Tests 360 var testFile = Path.Combine(_tempDir!,
"show_file_test.txt");
361 await File.WriteAllTextAsync(testFile,
"Test content");
362 await _fileHandler.
UploadAsync(
new List<string> { testFile }, TestDirName);
364 var files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
366 Assert.Single(files);
367 Assert.EndsWith(
"show_file_test.txt", files[0].FileName);
368 Assert.True(files[0].FileSize > 0);
376 var testFile = Path.Combine(_tempDir!,
"delete_test.txt");
377 await File.WriteAllTextAsync(testFile,
"Delete me");
378 await _fileHandler.
UploadAsync(
new List<string> { testFile }, TestDirName);
381 var filesBefore = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
382 Assert.Single(filesBefore);
385 _fileHandler.
DeleteFiles(
new List<string> { $
"{TestDirName}/delete_test.txt" });
388 var filesAfter = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
389 Assert.Empty(filesAfter);
397 var testFile = Path.Combine(_tempDir!,
"exists_test.txt");
398 await File.WriteAllTextAsync(testFile,
"I exist");
399 await _fileHandler.
UploadAsync(
new List<string> { testFile }, TestDirName);
401 Assert.True(_fileHandler.
KifsFileExists($
"{TestDirName}/exists_test.txt"));
407 Assert.False(_fileHandler!.KifsFileExists($
"{TestDirName}/nonexistent.txt"));
415 for (
int i = 1; i <= 3; i++)
417 var fileName = $
"test_delete_all_{i}.txt";
418 var filePath = Path.Combine(_tempDir!, fileName);
419 await File.WriteAllTextAsync(filePath, $
"Content {i}");
420 await _fileHandler.
UploadAsync(
new List<string> { filePath }, TestDirName);
423 var files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
424 Assert.Equal(3, files.Count);
428 files = _fileHandler.
ShowFiles(
new List<string> { TestDirName });
434 #region Helper Classes 438 public bool MultiPartUploadCompleteCalled {
get;
private set; }
439 public bool PartUploadCalled {
get;
private set; }
440 public bool FullFileUploadCalled {
get;
private set; }
441 public List<FileOperationResult> Results {
get; } =
new List<FileOperationResult>();
443 public void OnMultiPartUploadComplete(IList<FileOperationResult> results)
445 MultiPartUploadCompleteCalled =
true;
446 Results.AddRange(results);
451 PartUploadCalled =
true;
457 FullFileUploadCalled =
true;
void DeleteDirectory_ValidName_DeletesSuccessfully()
async Task DeleteFiles_RemovesFiles()
void KifsDirectoryExists_NonExistingDirectory_ReturnsFalse()
Async integration tests for KiFS (Kinetica File System) operations.
async Task DownloadAsync_OverwriteExistingFalse_ThrowsException()
bool KifsFileExists(string fileName)
Checks whether a KiFS file exists.
Interface for receiving callbacks during file upload operations.
Main class for handling file operations with Kinetica's KiFS (Kinetica File System).
void DeleteFilesInDirectory(string remoteDirName)
Deletes all files in a KiFS directory.
async Task UploadAsync_NonExistentDirectory_ThrowsException()
Test context that manages schema and cleanup for integration tests.
async Task KifsFileExists_ExistingFile_ReturnsTrue()
async Task UploadAsync_MultipleFiles_UploadsSuccessfully()
async Task UploadAsync_SingleFile_UploadsSuccessfully()
Options for uploading files to KiFS.
kinetica.Kinetica Kinetica
async Task ShowFiles_AfterUpload_ReturnsFileInfo()
void DeleteFiles(IList< string > fileNames)
Deletes files from KiFS, suppressing error if files don't exist.
void ShowDirectory_ValidName_ReturnsInfo()
void KifsDirectoryExists_ExistingDirectory_ReturnsTrue()
async Task UploadAsync_LargeFile_UploadsSuccessfully()
void CreateDirectory_ValidName_CreatesSuccessfully()
IList< KifsDirectoryInfo > ShowAllDirectories()
Returns statistics about all KiFS directories.
async Task UploadAsync(IList< string > fileNames, string remoteDirName, UploadOptions? uploadOptions=null, IFileUploadListener? callback=null)
Uploads files asynchronously to a KiFS directory.
async Task DisposeAsync()
bool KifsDirectoryExists(string dirName)
Checks whether a KiFS directory exists.
async Task UploadAsync_NonExistentFile_ThrowsException()
async Task DownloadAsync_WithGlobPattern_DownloadsMatchingFiles()
async Task InitializeAsync()
async Task UploadAsync_WithCallback_CallsCallback()
Options for downloading files from KiFS.
static UploadOptions Default
Returns the default upload options.
void DeleteDirectory(string remoteDirName)
Deletes a KiFS directory and all files under it, suppressing error if it doesn't exist.
void KifsFileExists_NonExistingFile_ReturnsFalse()
async Task DeleteFilesInDirectory_ValidDirectory_DeletesAllFiles()
async Task UploadAsync_WithOptions_UploadsSuccessfully()
async Task DownloadAsync_MultipleFiles_DownloadsSuccessfully()
void CreateDirectory(string remoteDirName)
Creates a KiFS directory, suppressing error if it already exists.
void CreateDirectory_ExistsNoError_NoException()
async Task DownloadAsync(IList< string > fileNames, string localDirName, DownloadOptions? downloadOptions=null, IFileDownloadListener? callback=null)
Downloads files asynchronously from KiFS to a local directory.
void ShowAllDirectories_ReturnsDirectories()
Failover to clusters in a random order (default)
IList< KifsFileInfo > ShowFiles(IList< string > remotePaths)
Returns statistics about the given KiFS files.
Contains the result of a file upload or download operation.
IList< KifsDirectoryInfo > ShowDirectory(string remoteDirName)
Returns statistics about a single KiFS directory.
async Task DownloadAsync_SingleFile_DownloadsSuccessfully()