Skip to content

Commit

Permalink
CodeQL fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Riku Virtanen committed Mar 1, 2024
1 parent 003cab3 commit d41be37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@ namespace Frends.MicrosoftSQL.ExecuteQuery.Tests;
[TestClass]
public class ExceptionUnitTests : ExecuteQueryTestBase
{
[TestMethod]
public async Task TestExecuteQuery_Invalid_Creds_ThrowError()
private Input input = new();
private Options options = new();

[TestInitialize]
public void SetUp()
{
var input = new Input()
input = new Input()
{
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord",
ConnectionString = _connString,
};

var options = new Options()
options = new Options()
{
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
CommandTimeoutSeconds = 2,
ThrowErrorOnFailure = true
};
}

[TestMethod]
public async Task TestExecuteQuery_Invalid_Creds_ThrowError()
{
input.ConnectionString = Helper.GetInvalidConnectionString();

var ex = await Assert.ThrowsExceptionAsync<Exception>(() => MicrosoftSQL.ExecuteQuery(input, options, default));
Assert.IsTrue(ex.Message.Contains("Login failed for user 'SA'."));
Expand All @@ -29,17 +38,8 @@ public async Task TestExecuteQuery_Invalid_Creds_ThrowError()
[TestMethod]
public async Task TestExecuteQuery_Invalid_Creds_ReturnErrorMessage()
{
var input = new Input()
{
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord",
};

var options = new Options()
{
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
CommandTimeoutSeconds = 2,
ThrowErrorOnFailure = false
};
options.ThrowErrorOnFailure = false;
input.ConnectionString = Helper.GetInvalidConnectionString();

var result = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsFalse(result.Success);
Expand All @@ -50,19 +50,8 @@ public async Task TestExecuteQuery_Invalid_Creds_ReturnErrorMessage()
[TestMethod]
public void TestExecuteQuery_ExceptionIsThrownWhenQueryFails()
{
var input = new Input()
{
Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)",
ExecuteType = ExecuteTypes.NonQuery,
ConnectionString = _connString,
};

var options = new Options()
{
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
CommandTimeoutSeconds = 2,
ThrowErrorOnFailure = true
};
input.Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)";
input.ExecuteType = ExecuteTypes.NonQuery;

var ex = Assert.ThrowsExceptionAsync<Exception>(async () => await MicrosoftSQL.ExecuteQuery(input, options, default));
Assert.IsTrue(ex.Result.Message.Contains("System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Unit'."));
Expand All @@ -71,19 +60,10 @@ public void TestExecuteQuery_ExceptionIsThrownWhenQueryFails()
[TestMethod]
public async Task TestExecuteQuery_ErrorMessageWhenQueryFails()
{
var input = new Input()
{
Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)",
ExecuteType = ExecuteTypes.NonQuery,
ConnectionString = _connString,
};
input.Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)";
input.ExecuteType = ExecuteTypes.NonQuery;

var options = new Options()
{
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
CommandTimeoutSeconds = 2,
ThrowErrorOnFailure = false
};
options.ThrowErrorOnFailure = false;

var result = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsFalse(result.Success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib;

internal class Helper
{
/// <summary>
/// Test credentials for docker server.
/// </summary>
private static readonly string _dockerAddress = "127.0.0.1,1433";
private static readonly string _dockerUsername = "SA";
private static readonly string _dockerPassword = "Salakala123!";

internal static string CreateConnectionString()
{
var pw = "Salakala123!";
return $"Server=127.0.0.1,1433;Database=Master;User Id=SA;Password={pw};Encrypt=true;TrustServerCertificate=True;";
return $"Server={_dockerAddress};Database=Master;User Id={_dockerUsername};Password={_dockerPassword};Encrypt=true;TrustServerCertificate=True;";
}

internal static string GetInvalidConnectionString()
{
return $"Server=127.0.0.1,1433;Database=Master;User Id={_dockerUsername};Password={Guid.NewGuid()};Encrypt=true;TrustServerCertificate=True;";
}

internal static int GetRowCount(string connString, string table)
Expand Down

0 comments on commit d41be37

Please sign in to comment.