Skip to content

Commit

Permalink
Change the library from System.Data.SqlClient to Microsoft.Data.SqlCl…
Browse files Browse the repository at this point in the history
…ient.

Add environment variables for secrets.
  • Loading branch information
MichalFrends1 committed Jun 3, 2024
1 parent 939ed11 commit df2978a
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 27 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ExecuteQuery_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ on:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_main.yml@main
uses: FrendsPlatform/FrendsTasks/.github/workflows/build_main.yml@main
with:
workdir: Frends.MicrosoftSQL.ExecuteQuery
prebuild_command: docker-compose -f ./Frends.MicrosoftSQL.ExecuteQuery.Tests/docker-compose.yml up -d
env_var_name_1: FRENDS_MICROSOFTSQL_CONNSTRING
env_var_name_2: FRENDS_MICROSOFTSQL_USERID
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
env_var_value_1: ${{ secrets.FRENDS_MICROSOFTSQL_CONNSTRING }}
env_var_value_2: ${{ secrets.FRENDS_MICROSOFTSQL_USERID }}
7 changes: 5 additions & 2 deletions .github/workflows/ExecuteQuery_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_test.yml@main
with:
workdir: Frends.MicrosoftSQL.ExecuteQuery
prebuild_command: docker-compose -f ./Frends.MicrosoftSQL.ExecuteQuery.Tests/docker-compose.yml up -d
env_var_name_1: FRENDS_MICROSOFTSQL_CONNSTRING
env_var_name_2: FRENDS_MICROSOFTSQL_USERID
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
env_var_value_1: ${{ secrets.FRENDS_MICROSOFTSQL_CONNSTRING }}
env_var_value_2: ${{ secrets.FRENDS_MICROSOFTSQL_USERID }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# Custom gitignores
**/.env.local

# User-specific files
*.suo
*.user
Expand Down
5 changes: 5 additions & 0 deletions Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.2.2] - 2024-06-03
### Changed
- Changed the library from System.Data.SqlClient to Microsoft.Data.SqlClient
- Added environment variables for secrets

## [1.2.1] - 2024-03-01
### Changed
- Removed finally block from the Task so that the SQLConnection pool is not touched after every call to the ExecuteQuery method.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FRENDS_MICROSOFTSQL_CONNSTRING=""
FRENDS_MICROSOFTSQL_USERID=""
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class ExceptionUnitTests : ExecuteQueryTestBase
{
private Input input = new();
private Options options = new();
internal static readonly string _userName = Helper.GetUserName();

[TestInitialize]
public void SetUp()
Expand All @@ -32,18 +33,17 @@ 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'."));
Assert.IsTrue(ex.Message.Contains($"Login failed for user '{_userName}'."));
}

[TestMethod]
public async Task TestExecuteQuery_Invalid_Creds_ReturnErrorMessage()
{
options.ThrowErrorOnFailure = false;
input.ConnectionString = Helper.GetInvalidConnectionString();

var result = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsFalse(result.Success);
Assert.IsTrue(result.ErrorMessage.Contains("Login failed for user 'SA'."));
Assert.IsTrue(result.ErrorMessage.Contains($"Login failed for user '{_userName}'."));
Assert.AreEqual(0, result.RecordsAffected);
}

Expand All @@ -54,7 +54,7 @@ public void TestExecuteQuery_ExceptionIsThrownWhenQueryFails()
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'."));
Assert.IsTrue(ex.Result.Message.Contains("Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Unit'."));
}

[TestMethod]
Expand All @@ -67,6 +67,6 @@ public async Task TestExecuteQuery_ErrorMessageWhenQueryFails()

var result = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsFalse(result.Success);
Assert.IsTrue(result.ErrorMessage.Contains("System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Unit'."));
Assert.IsTrue(result.ErrorMessage.Contains("Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Unit'."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;

namespace Frends.MicrosoftSQL.ExecuteQuery.Tests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.1.3" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using dotenv.net;

namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib;

public class ExecuteQueryTestBase
{
internal static readonly string _connString = Helper.CreateConnectionString();
internal static readonly string _connString = Environment.GetEnvironmentVariable(

Check warning on line 9 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Possible null reference assignment.
"FRENDS_MICROSOFTSQL_CONNSTRING");
internal static readonly string _tableName = "TestTable";

[TestInitialize]
public void Init()
{
using var connection = new SqlConnection(_connString);
var root = Directory.GetCurrentDirectory();
string projDir = Directory.GetParent(root).Parent.Parent.FullName;

Check warning on line 17 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Dereference of a possibly null reference.

Check warning on line 17 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Dereference of a possibly null reference.

Check warning on line 17 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Dereference of a possibly null reference.

Check warning on line 17 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Dereference of a possibly null reference.

Check warning on line 17 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Dereference of a possibly null reference.
DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] { $"{projDir}/.env.local" }));
using var connection = new Microsoft.Data.SqlClient.SqlConnection(_connString);
connection.Open();
var createTable = connection.CreateCommand();
createTable.CommandText = $@"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN CREATE TABLE {_tableName} ( Id int, LastName varchar(255), FirstName varchar(255) ); END";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using System.Text.RegularExpressions;

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!";

private static readonly string _connectionString = Environment.GetEnvironmentVariable(

Check warning on line 9 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Possible null reference assignment.

Check warning on line 9 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Possible null reference assignment.
"FRENDS_MICROSOFTSQL_CONNSTRING");
private static readonly string _userId = Environment.GetEnvironmentVariable(

Check warning on line 11 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Possible null reference assignment.

Check warning on line 11 in Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-latest

Possible null reference assignment.
"FRENDS_MICROSOFTSQL_USERID");

internal static string CreateConnectionString()
{
return $"Server={_dockerAddress};Database=Master;User Id={_dockerUsername};Password={_dockerPassword};Encrypt=true;TrustServerCertificate=True;";
return _connectionString;
}

internal static string GetUserName()
{
return _userId;
}

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

internal static int GetRowCount(string connString, string table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using IsolationLevel = System.Data.IsolationLevel;
using Microsoft.Data.SqlClient;

namespace Frends.MicrosoftSQL.ExecuteQuery;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand All @@ -22,7 +22,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup>
</Project>

0 comments on commit df2978a

Please sign in to comment.