Skip to content

Commit

Permalink
Move test to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Dec 3, 2024
1 parent 8d1b658 commit 0f6bd28
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,29 @@ namespace Snowflake.Data.Tests
{

[TestFixture]
[NonParallelizable]
public class FileUploadDownloadLargeFilesIT : SFBaseTest
{
private const string FileName = "large_file_to_test_dotnet_driver.json";
private static readonly string s_uniqueId = TestDataGenarator.NextAlphaNumeric(6);
private static readonly string s_localFolderName = Path.Combine(Path.GetTempPath(), s_uniqueId);
private static readonly string s_localFolderWithSpaceName = Path.Combine(Path.GetTempPath() + s_uniqueId, "folder with space");
private static readonly string s_remoteFolderName = $"files_to_test_put_get_{s_uniqueId}";
private static readonly string s_remoteFolderWithSpaceName = $"files_to_test_put_get_for_folder_with_space{s_uniqueId}";
private static readonly string s_downloadFolderName = Path.Combine(s_localFolderName, "download");
private static readonly string s_downloadFolderWithSpaceName = Path.Combine(s_localFolderName, "download with space");
private static readonly string s_fullFileName = Path.Combine(s_localFolderName, FileName);
private static readonly string s_fullFileWithSpaceName = Path.Combine(s_localFolderWithSpaceName, FileName);
private static readonly string s_fullDownloadedFileName = Path.Combine(s_downloadFolderName, FileName);
private static readonly string s_fullDownloadedFileWithSpaceName = Path.Combine(s_downloadFolderWithSpaceName, FileName);
private static readonly MD5 s_md5 = MD5.Create();

[OneTimeSetUp]
public static void GenerateLargeFileForTests()
{
CreateLocalDirectory(s_localFolderName);
GenerateLargeFile(s_fullFileName);
CreateLocalDirectory(s_localFolderWithSpaceName);
GenerateLargeFile(s_fullFileWithSpaceName);
}

[OneTimeTearDown]
public static void DeleteGeneratedLargeFile()
{
RemoveLocalFile(s_fullFileName);
RemoveDirectory(s_localFolderName);
RemoveLocalFile(s_fullFileWithSpaceName);
RemoveDirectory(s_localFolderWithSpaceName);
}

[Test]
Expand All @@ -65,68 +55,33 @@ public void TestThatUploadsAndDownloadsTheSameFile()
RemoveLocalFile(s_fullDownloadedFileName);
}

[Test]
public void TestThatUploadsAndDownloadsFilePathWithSpacesAndSingleQuotes()
{
// act
UploadFile(s_fullFileWithSpaceName, s_remoteFolderWithSpaceName, true);
DownloadFile(s_remoteFolderWithSpaceName, s_downloadFolderWithSpaceName, FileName, true);

// assert
Assert.AreEqual(
CalcualteMD5(s_fullFileWithSpaceName),
CalcualteMD5(s_fullDownloadedFileWithSpaceName));

// cleanup
RemoveFilesFromServer(s_remoteFolderWithSpaceName);
RemoveLocalFile(s_fullDownloadedFileWithSpaceName);
}

private static void GenerateLargeFile(string fullFileName)
{
File.Delete(fullFileName);
RandomJsonGenerator.GenerateRandomJsonFile(fullFileName, 128 * 1024);
}

private void UploadFile(string fullFileName, string remoteFolderName, bool encloseFilePathInSingleQuotes = false)
private void UploadFile(string fullFileName, string remoteFolderName)
{
using (var conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString + "FILE_TRANSFER_MEMORY_THRESHOLD=1048576;";
conn.Open();
var command = conn.CreateCommand();
if (encloseFilePathInSingleQuotes)
{
// Enclosing the file path in single quotes require forward slash
fullFileName = fullFileName.Replace("\\", "/");
command.CommandText = $"PUT 'file://{fullFileName}' @~/{remoteFolderName} AUTO_COMPRESS=FALSE";
}
else
{
command.CommandText = $"PUT file://{fullFileName} @~/{remoteFolderName} AUTO_COMPRESS=FALSE";
}
command.CommandText = $"PUT file://{fullFileName} @~/{remoteFolderName} AUTO_COMPRESS=FALSE";
command.ExecuteNonQuery();
}
}

private void DownloadFile(string remoteFolderName, string downloadFolderName, string fileName, bool encloseFilePathInSingleQuotes = false)
private void DownloadFile(string remoteFolderName, string downloadFolderName, string fileName)
{
var filePattern = $"{remoteFolderName}/{fileName}";
using (var conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString;
conn.Open();
var command = conn.CreateCommand();
if (encloseFilePathInSingleQuotes)
{
// Enclosing the file path in single quotes require forward slash
downloadFolderName = downloadFolderName.Replace("\\", "/");
command.CommandText = $"GET @~/{remoteFolderName} 'file://{downloadFolderName}' PATTERN='{filePattern}'";
}
else
{
command.CommandText = $"GET @~/{remoteFolderName} file://{downloadFolderName} PATTERN='{filePattern}'";
}
command.CommandText = $"GET @~/{remoteFolderName} file://{downloadFolderName} PATTERN='{filePattern}'";
command.ExecuteNonQuery();
}
}
Expand Down
49 changes: 43 additions & 6 deletions Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

Expand Down Expand Up @@ -560,6 +560,29 @@ public void TestPutGetGcsDownscopedCredential(
}
}

[Test]
public void TestPutFileWithSpaceAndSingleQuote()
{
var absolutePathPrefix = Path.Combine($"{Path.GetTempPath()}{Guid.NewGuid()} file path with space");
var files = new List<string> {
$"{absolutePathPrefix}_one.csv",
$"{absolutePathPrefix}_two.csv",
$"{absolutePathPrefix}_three.csv"
};
PrepareFileData(files);

// Set the PUT query variables
t_inputFilePath = $"{absolutePathPrefix}*";
t_internalStagePath = $"@{t_schemaName}.{t_stageName}";

using (var conn = new SnowflakeDbConnection(ConnectionString))
{
conn.Open();
PutFile(conn, "", ResultStatus.UPLOADED, true);
VerifyFilesAreUploaded(conn, files, t_internalStagePath);
}
}

private void PrepareTest(string sourceFileCompressionType, StageType stageType, string stagePath,
bool autoCompress, bool clientEncryption = true)
{
Expand Down Expand Up @@ -610,16 +633,30 @@ private static bool IsCompressedByTheDriver()
string PutFile(
SnowflakeDbConnection conn,
String additionalAttribute = "",
ResultStatus expectedStatus = ResultStatus.UPLOADED)
ResultStatus expectedStatus = ResultStatus.UPLOADED,
bool encloseInSingleQuotes = false)
{
string queryId;
using (var command = conn.CreateCommand())
{
// Prepare PUT query
string putQuery =
$"PUT file://{t_inputFilePath} {t_internalStagePath}" +
$" AUTO_COMPRESS={(t_autoCompress ? "TRUE" : "FALSE")}" +
$" {additionalAttribute}";
string putQuery;
if (encloseInSingleQuotes)
{
// Enclosing the file path in single quotes require forward slash
t_inputFilePath = t_inputFilePath.Replace("\\", "/");
putQuery =
$"PUT 'file://{t_inputFilePath}' {t_internalStagePath}" +
$" AUTO_COMPRESS={(t_autoCompress ? "TRUE" : "FALSE")}";
}
else
{
putQuery =
$"PUT file://{t_inputFilePath} {t_internalStagePath}" +
$" AUTO_COMPRESS={(t_autoCompress ? "TRUE" : "FALSE")}" +
$" {additionalAttribute}";
}

// Upload file
command.CommandText = putQuery;
var reader = command.ExecuteReader();
Expand Down

0 comments on commit 0f6bd28

Please sign in to comment.