Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RikuVirtanen committed Feb 8, 2024
1 parent 0a38248 commit ecd70a9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "Following Frends documentation guidelines")]
[assembly: SuppressMessage("StyleCop.CSharp.NamingRules", "SA1309:Field names should not begin with underscore", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1629:Documentation text should end with a period", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")]
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,29 @@ public class UnitTests
private static readonly string _tableName = "TestTable";
private static readonly string _destination = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../TestData/test.csv");

private Options _options;

[SetUp]
public void Init()
{
_options = new Options()
{
TimeoutSeconds = 30,
CsvOptions = new CsvOptions
{
FieldDelimiter = CsvFieldDelimiter.Semicolon,
LineBreak = CsvLineBreak.CRLF,
FileEncoding = FileEncoding.UTF8,
EnableBom = false,
IncludeHeadersInOutput = false,
SanitizeColumnHeaders = false,
AddQuotesToDates = false,
AddQuotesToStrings = false,
DateFormat = "yyyy-MM-dd",
DateTimeFormat = "yyyy-MM-ddTHH:mm:ss",
},
};

Helper.CreateTestTable(_connString, _tableName);

var parameters = new System.Data.SqlClient.SqlParameter[]
Expand Down Expand Up @@ -66,6 +86,34 @@ public void CleanUp()
File.Delete(_destination);
}

[Test]
public async Task ExecuteQueryToFile_SqlParameters()
{
var query = new Input
{
Query = $"Select Id, LastName, FirstName, REPLACE(Salary, '.', ',') AS 'Salary' from {_tableName} WHERE Id = @param",
QueryParameters = new Definitions.SqlParameter[]
{
new Definitions.SqlParameter
{
Name = "@param",
Value = 1,
SqlDataType = SqlDataTypes.Int,
},
},
ConnectionString = _connString,
OutputFilePath = _destination,
};

_options.CsvOptions.AddQuotesToStrings = true;
_options.CsvOptions.IncludeHeadersInOutput = true;

await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);
var output = File.ReadAllText(_destination);

Assert.AreEqual("Id;LastName;FirstName;Salary\r\n1;\"Meikalainen\";\"Matti\";\"1523,25\"\r\n", output);
}

[Test]
public async Task ExecuteQueryToFile_StringWithApostrophe()
{
Expand All @@ -77,25 +125,10 @@ public async Task ExecuteQueryToFile_StringWithApostrophe()
OutputFilePath = _destination,
};

var options = new Options
{
TimeoutSeconds = 30,
CsvOptions = new CsvOptions
{
FieldDelimiter = CsvFieldDelimiter.Semicolon,
LineBreak = CsvLineBreak.CRLF,
FileEncoding = FileEncoding.UTF8,
EnableBom = false,
IncludeHeadersInOutput = true,
SanitizeColumnHeaders = false,
AddQuotesToDates = false,
AddQuotesToStrings = true,
DateFormat = "yyyy-MM-dd",
DateTimeFormat = "yyyy-MM-ddTHH:mm:ss",
},
};
_options.CsvOptions.AddQuotesToStrings = true;
_options.CsvOptions.IncludeHeadersInOutput = true;

await MicrosoftSQL.ExecuteQueryToFile(query, options, default);
await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);
var output = File.ReadAllText(_destination);

Assert.AreEqual("Id;LastName;FirstName;Salary\r\n1;\"Meikalainen\";\"Matti\";\"1523,25\"\r\n", output);
Expand All @@ -112,25 +145,9 @@ public async Task ExecuteQueryToFile_StringWithoutApostrophe()
OutputFilePath = _destination,
};

var options = new Options
{
TimeoutSeconds = 30,
CsvOptions = new CsvOptions
{
FieldDelimiter = CsvFieldDelimiter.Semicolon,
LineBreak = CsvLineBreak.CRLF,
FileEncoding = FileEncoding.UTF8,
EnableBom = false,
IncludeHeadersInOutput = true,
SanitizeColumnHeaders = false,
AddQuotesToDates = false,
AddQuotesToStrings = false,
DateFormat = "yyyy-MM-dd",
DateTimeFormat = "yyyy-MM-ddTHH:mm:ss",
},
};
_options.CsvOptions.IncludeHeadersInOutput = true;

await MicrosoftSQL.ExecuteQueryToFile(query, options, default);
await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);

var output = File.ReadAllText(_destination);

Expand All @@ -148,25 +165,33 @@ public async Task ExecuteQueryToFile_WithImageDBType()
OutputFilePath = _destination,
};

var options = new Options
await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);

var output = File.ReadAllText(_destination);

Assert.AreEqual(BitConverter.ToString(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_destination), "Test_image.png"))), output.TrimEnd(new char[] { '\r', '\n' }));
}

[Test]
public async Task ExecuteQueryToFile_WithImageDBTypeWithWhereClause()
{
var query = new Input
{
TimeoutSeconds = 30,
CsvOptions = new CsvOptions
Query = $"SELECT Image from {_tableName} WHERE CONVERT(varbinary(max), Image) = @param",
QueryParameters = new Definitions.SqlParameter[]
{
FieldDelimiter = CsvFieldDelimiter.Semicolon,
LineBreak = CsvLineBreak.CRLF,
FileEncoding = FileEncoding.UTF8,
EnableBom = false,
IncludeHeadersInOutput = false,
SanitizeColumnHeaders = false,
AddQuotesToDates = false,
AddQuotesToStrings = false,
DateFormat = "yyyy-MM-dd",
DateTimeFormat = "yyyy-MM-ddTHH:mm:ss",
new Definitions.SqlParameter
{
Name = "@param",
Value = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_destination), "Test_image.png")),
SqlDataType = SqlDataTypes.VarBinary,
},
},
ConnectionString = _connString,
OutputFilePath = _destination,
};

await MicrosoftSQL.ExecuteQueryToFile(query, options, default);
await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);

var output = File.ReadAllText(_destination);

Expand All @@ -184,25 +209,33 @@ public async Task ExecuteQueryToFile_WithBinaryDBType()
OutputFilePath = _destination,
};

var options = new Options
await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);

var output = File.ReadAllText(_destination);

Assert.AreEqual(BitConverter.ToString(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_destination), "Test_text.txt"))), output.TrimEnd(new char[] { '\r', '\n' }));
}

[Test]
public async Task ExecuteQueryToFile_WithBinaryDBTypeWithWhereClause()
{
var query = new Input
{
TimeoutSeconds = 30,
CsvOptions = new CsvOptions
Query = $"SELECT TestText FROM {_tableName} WHERE TestText = @param",
QueryParameters = new Definitions.SqlParameter[]
{
FieldDelimiter = CsvFieldDelimiter.Semicolon,
LineBreak = CsvLineBreak.CRLF,
FileEncoding = FileEncoding.UTF8,
EnableBom = false,
IncludeHeadersInOutput = false,
SanitizeColumnHeaders = false,
AddQuotesToDates = false,
AddQuotesToStrings = false,
DateFormat = "yyyy-MM-dd",
DateTimeFormat = "yyyy-MM-ddTHH:mm:ss",
new Definitions.SqlParameter
{
Name = "@param",
Value = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_destination), "Test_text.txt")),
SqlDataType = SqlDataTypes.VarBinary,
},
},
ConnectionString = _connString,
OutputFilePath = _destination,
};

await MicrosoftSQL.ExecuteQueryToFile(query, options, default);
await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);

var output = File.ReadAllText(_destination);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,14 @@ public class CsvOptions
/// <returns>string</returns>
internal string GetFieldDelimiterAsString()
{
switch (FieldDelimiter)
return FieldDelimiter switch
{
case CsvFieldDelimiter.Comma:
return ",";
case CsvFieldDelimiter.Pipe:
return "|";
case CsvFieldDelimiter.Semicolon:
return ";";
case CsvFieldDelimiter.Custom:
return CustomFieldDelimiter;
default:
throw new Exception($"Unknown field delimeter: {FieldDelimiter}");
}
CsvFieldDelimiter.Comma => ",",
CsvFieldDelimiter.Pipe => "|",
CsvFieldDelimiter.Semicolon => ";",
CsvFieldDelimiter.Custom => CustomFieldDelimiter,
_ => throw new Exception($"Unknown field delimeter: {FieldDelimiter}"),
};
}

/// <summary>
Expand All @@ -132,16 +127,12 @@ internal string GetFieldDelimiterAsString()
/// <returns>string</returns>
internal string GetLineBreakAsString()
{
switch (LineBreak)
return LineBreak switch
{
case CsvLineBreak.CRLF:
return "\r\n";
case CsvLineBreak.CR:
return "\r";
case CsvLineBreak.LF:
return "\n";
default:
throw new Exception($"Unknown field delimeter: {FieldDelimiter}");
}
CsvLineBreak.CRLF => "\r\n",
CsvLineBreak.CR => "\r",
CsvLineBreak.LF => "\n",
_ => throw new Exception($"Unknown field delimeter: {FieldDelimiter}"),
};
}
}

0 comments on commit ecd70a9

Please sign in to comment.