From 629ff692547ccec07ed06563aeff42feb2b25339 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Mar 2024 15:19:53 +0200 Subject: [PATCH 1/6] Removed SqlConnection.ClearAllPools() to make the Task more efficient --- Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md | 7 +++++++ .../Frends.MicrosoftSQL.ExecuteQuery.Tests.csproj | 2 +- .../Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs | 4 ---- .../Frends.MicrosoftSQL.ExecuteQuery.csproj | 6 +++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md b/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md index 3665498..8d729ff 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md +++ b/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [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. +### Updated +- Newtonsoft.Json to version 13.0.3 +- System.Data.SqlClient to version 4.8.6 + ## [1.2.0] - 2023-11-30 ### Changed - [Breaking] QueryParameter.Value type to object so that binary data can be used. diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Frends.MicrosoftSQL.ExecuteQuery.Tests.csproj b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Frends.MicrosoftSQL.ExecuteQuery.Tests.csproj index 4f6cb8a..582aa8a 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Frends.MicrosoftSQL.ExecuteQuery.Tests.csproj +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Frends.MicrosoftSQL.ExecuteQuery.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs index 0e3803f..48fa25b 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs @@ -72,10 +72,6 @@ public static async Task ExecuteQuery([PropertyTab] Input input, [Proper return new Result(false, 0, eMsg, null); } - finally - { - SqlConnection.ClearAllPools(); - } } private static async Task ExecuteHandler(Input input, Options options, SqlCommand command, CancellationToken cancellationToken) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj index 2a2c5c5..d15eddc 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj @@ -2,7 +2,7 @@ net6.0 - 1.2.0 + 1.2.1 Frends Frends Frends @@ -22,7 +22,7 @@ - - + + \ No newline at end of file From edf75c151b49b6c2d110a6058373aa8659771173 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Mar 2024 15:37:38 +0200 Subject: [PATCH 2/6] CodeQL fixes and cleaned up tests --- .../AutoUnitTests.cs | 65 +++---------------- .../ExceptionUnitTests.cs | 41 +----------- .../ExecuteReaderUnitTests.cs | 54 +++------------ .../Lib/ExecuteQueryTestBase.cs | 39 +++++++++++ .../Lib/Helper.cs | 23 +++++++ .../ManualTesting.cs | 58 +---------------- .../NonQueryUnitTests.cs | 65 +++---------------- .../ScalarUnitTests.cs | 64 +++--------------- .../ExecuteQuery.cs | 2 +- 9 files changed, 100 insertions(+), 311 deletions(-) create mode 100644 Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs create mode 100644 Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs index 9e07445..ce62158 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs @@ -1,50 +1,13 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; +using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json.Linq; -using System.Data.SqlClient; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] -public class AutoUnitTests +public class AutoUnitTests : ExecuteQueryTestBase { - /* - docker-compose up -d - - How to use via terminal: - docker exec -it sql1 "bash" - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!" - SELECT * FROM TestTable - GO - */ - - private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; - private static readonly string _tableName = "TestTable"; - - [TestInitialize] - public void Init() - { - using var connection = new 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"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - - [TestCleanup] - public void CleanUp() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var createTable = connection.CreateCommand(); - createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - [TestMethod] public async Task TestExecuteQuery_Auto() { @@ -113,7 +76,7 @@ public async Task TestExecuteQuery_Auto() Assert.AreEqual(3, insert.RecordsAffected); Assert.IsNull(insert.ErrorMessage); Assert.AreEqual(3, (int)insert.Data["AffectedRows"]); - Assert.AreEqual(3, GetRowCount()); // Make sure rows inserted before moving on. + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // Make sure rows inserted before moving on. // Select all var select = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); @@ -127,7 +90,7 @@ public async Task TestExecuteQuery_Auto() Assert.AreEqual("Forst", (string)select.Data[1]["FirstName"]); Assert.AreEqual("Hiiri", (string)select.Data[2]["LastName"]); Assert.AreEqual("Mikki", (string)select.Data[2]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Select single var selectSingle = await MicrosoftSQL.ExecuteQuery(inputSelectSingle, options, default); @@ -137,14 +100,14 @@ public async Task TestExecuteQuery_Auto() Assert.AreEqual(typeof(JArray), selectSingle.Data.GetType()); Assert.AreEqual("Suku", (string)selectSingle.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)selectSingle.Data[0]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Update var update = await MicrosoftSQL.ExecuteQuery(inputUpdate, options, default); Assert.IsTrue(update.Success); Assert.AreEqual(1, update.RecordsAffected); Assert.IsNull(update.ErrorMessage); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check var checkUpdateResult = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); Assert.AreEqual("Suku", (string)checkUpdateResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkUpdateResult.Data[0]["FirstName"]); @@ -152,14 +115,14 @@ public async Task TestExecuteQuery_Auto() Assert.AreEqual("Forst", (string)checkUpdateResult.Data[1]["FirstName"]); Assert.AreEqual("Hiiri", (string)checkUpdateResult.Data[2]["LastName"]); Assert.AreEqual("Mikki", (string)checkUpdateResult.Data[2]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Delete var delete = await MicrosoftSQL.ExecuteQuery(inputDelete, options, default); Assert.IsTrue(delete.Success); Assert.AreEqual(1, delete.RecordsAffected); Assert.IsNull(delete.ErrorMessage); - Assert.AreEqual(2, GetRowCount()); // double check + Assert.AreEqual(2, Helper.GetRowCount(_connString, _tableName)); // double check var checkDeleteResult = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); Assert.AreEqual("Suku", (string)checkDeleteResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkDeleteResult.Data[0]["FirstName"]); @@ -169,16 +132,4 @@ public async Task TestExecuteQuery_Auto() CleanUp(); } } - - private static int GetRowCount() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var getRows = connection.CreateCommand(); - getRows.CommandText = $"SELECT COUNT(*) FROM {_tableName}"; - var count = (int)getRows.ExecuteScalar(); - connection.Close(); - connection.Dispose(); - return count; - } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs index 664abaf..5b63cd0 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs @@ -1,49 +1,12 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; +using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Data.SqlClient; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] -public class ExceptionUnitTests +public class ExceptionUnitTests : ExecuteQueryTestBase { - /* - docker-compose up - - How to use via terminal: - docker exec -it sql1 "bash" - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!" - SELECT * FROM TestTable - GO - */ - - private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; - private static readonly string _tableName = "TestTable"; - - [TestInitialize] - public void Init() - { - using var connection = new 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"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - - [TestCleanup] - public void CleanUp() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var createTable = connection.CreateCommand(); - createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - [TestMethod] public async Task TestExecuteQuery_Invalid_Creds_ThrowError() { diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs index db6b381..ed9938f 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs @@ -1,4 +1,5 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; +using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json.Linq; using System.Data.SqlClient; @@ -6,35 +7,8 @@ namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] -public class ExecuteReaderUnitTests +public class ExecuteReaderUnitTests : ExecuteQueryTestBase { - /* - docker-compose up - - How to use via terminal: - docker exec -it sql1 "bash" - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!" - SELECT * FROM TestTable - GO - */ - - private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; - private static readonly string _tableName = "TestTable"; - - [TestInitialize] - public void Init() - { - var command = $@"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"; - ExecuteQuery(command); - } - - [TestCleanup] - public void CleanUp() - { - var command = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; - ExecuteQuery(command); - } - [TestMethod] public async Task TestExecuteQuery_ExecuteReader() { @@ -102,7 +76,7 @@ public async Task TestExecuteQuery_ExecuteReader() Assert.IsTrue(insert.Success); Assert.AreEqual(3, insert.RecordsAffected); Assert.IsNull(insert.ErrorMessage); - Assert.AreEqual(3, GetRowCount()); // Make sure rows inserted before moving on. + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // Make sure rows inserted before moving on. // Select all var select = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); @@ -116,7 +90,7 @@ public async Task TestExecuteQuery_ExecuteReader() Assert.AreEqual("Forst", (string)select.Data[1]["FirstName"]); Assert.AreEqual("Hiiri", (string)select.Data[2]["LastName"]); Assert.AreEqual("Mikki", (string)select.Data[2]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Select single var selectSingle = await MicrosoftSQL.ExecuteQuery(inputSelectSingle, options, default); @@ -126,14 +100,14 @@ public async Task TestExecuteQuery_ExecuteReader() Assert.AreEqual(typeof(JArray), selectSingle.Data.GetType()); Assert.AreEqual("Suku", (string)selectSingle.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)selectSingle.Data[0]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Update var update = await MicrosoftSQL.ExecuteQuery(inputUpdate, options, default); Assert.IsTrue(update.Success); Assert.AreEqual(1, update.RecordsAffected); Assert.IsNull(update.ErrorMessage); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check var checkUpdateResult = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); Assert.AreEqual("Suku", (string)checkUpdateResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkUpdateResult.Data[0]["FirstName"]); @@ -141,14 +115,14 @@ public async Task TestExecuteQuery_ExecuteReader() Assert.AreEqual("Forst", (string)checkUpdateResult.Data[1]["FirstName"]); Assert.AreEqual("Hiiri", (string)checkUpdateResult.Data[2]["LastName"]); Assert.AreEqual("Mikki", (string)checkUpdateResult.Data[2]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Delete var delete = await MicrosoftSQL.ExecuteQuery(inputDelete, options, default); Assert.IsTrue(delete.Success); Assert.AreEqual(1, delete.RecordsAffected); Assert.IsNull(delete.ErrorMessage); - Assert.AreEqual(2, GetRowCount()); // double check + Assert.AreEqual(2, Helper.GetRowCount(_connString, _tableName)); // double check var checkDeleteResult = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); Assert.AreEqual("Suku", (string)checkDeleteResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkDeleteResult.Data[0]["FirstName"]); @@ -203,18 +177,6 @@ public async Task ExecuteQueryTestWithBinaryData() Assert.AreEqual(Convert.ToBase64String(binary), Convert.ToBase64String((byte[])result.Data[0]["Data"])); } - private static int GetRowCount() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var getRows = connection.CreateCommand(); - getRows.CommandText = $"SELECT COUNT(*) FROM {_tableName}"; - var count = (int)getRows.ExecuteScalar(); - connection.Close(); - connection.Dispose(); - return count; - } - private static void ExecuteQuery(string command) { using var connection = new SqlConnection(_connString); diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs new file mode 100644 index 0000000..85a7b40 --- /dev/null +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs @@ -0,0 +1,39 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; + +public class ExecuteQueryTestBase +{ + internal static readonly string _connString = Helper.CreateConnectionString(); + internal static readonly string _tableName = "TestTable"; + + [TestInitialize] + public void Init() + { + using var connection = new 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"; + createTable.ExecuteNonQuery(); + connection.Close(); + connection.Dispose(); + } + + [TestCleanup] + public void CleanUp() + { + using var connection = new SqlConnection(_connString); + connection.Open(); + var createTable = connection.CreateCommand(); + createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; + createTable.ExecuteNonQuery(); + connection.Close(); + connection.Dispose(); + } +} diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs new file mode 100644 index 0000000..d6a9d58 --- /dev/null +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs @@ -0,0 +1,23 @@ +using System.Data.SqlClient; + +namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; + +internal class Helper +{ + internal static string CreateConnectionString() + { + return "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; + } + + internal static int GetRowCount(string connString, string table) + { + using var connection = new SqlConnection(connString); + connection.Open(); + var getRows = connection.CreateCommand(); + getRows.CommandText = $"SELECT COUNT(*) FROM {table}"; + var count = (int)getRows.ExecuteScalar(); + connection.Close(); + connection.Dispose(); + return count; + } +} \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs index 0ca3eaa..c6d1dd7 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs @@ -1,51 +1,12 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; +using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Data.SqlClient; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] -public class ManualTesting +public class ManualTesting : ExecuteQueryTestBase { - /* - These tests requires code editing so they must be skipped in workflow. - - docker-compose up - - How to use via terminal: - docker exec -it sql1 "bash" - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!" - SELECT * FROM TestTable - GO - */ - - private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; - private static readonly string _tableName = "TestTable"; - - [TestInitialize] - public void Init() - { - using var connection = new 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"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - - [TestCleanup] - public void CleanUp() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var createTable = connection.CreateCommand(); - createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - // Add following line to ExecuteQuery.cs: 'throw new Exception();' under 'dataReader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);' (currently line 124) [Ignore("To run this test, comment this line after exception has been added to ExecuteQuery.cs.")] [TestMethod] @@ -71,7 +32,7 @@ public async Task TestExecuteQuery_RollbackInsert_ThrowErrorOnFailure_False() Assert.IsFalse(insert.Success); Assert.AreEqual(0, insert.RecordsAffected); Assert.IsTrue(insert.ErrorMessage.Contains("(If required) transaction rollback completed without exception.")); - Assert.AreEqual(0, GetRowCount()); + Assert.AreEqual(0, Helper.GetRowCount(_connString, _tableName)); } // Add following line to ExecuteQuery.cs: 'throw new Exception();' under 'dataReader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);' (currently line 124) @@ -98,17 +59,4 @@ public async Task TestExecuteQuery_RollbackInsert_ThrowErrorOnFailure_True() var insert = await Assert.ThrowsExceptionAsync(async () => await MicrosoftSQL.ExecuteQuery(inputInsert, options, default)); Assert.IsTrue(insert.Message.Contains("(If required) transaction rollback completed without exception.")); } - - // Simple select statement for result double checks. - private static int GetRowCount() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var getRows = connection.CreateCommand(); - getRows.CommandText = $"SELECT COUNT(*) FROM {_tableName}"; - var count = (int)getRows.ExecuteScalar(); - connection.Close(); - connection.Dispose(); - return count; - } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs index 17ed38f..7d9a9e9 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs @@ -1,49 +1,12 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; +using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Data.SqlClient; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] -public class NonQueryUnitTests +public class NonQueryUnitTests : ExecuteQueryTestBase { - /* - docker-compose up - - How to use via terminal: - docker exec -it sql1 "bash" - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!" - SELECT * FROM TestTable - GO - */ - - private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; - private static readonly string _tableName = "TestTable"; - - [TestInitialize] - public void Init() - { - using var connection = new 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"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - - [TestCleanup] - public void CleanUp() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var createTable = connection.CreateCommand(); - createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - [TestMethod] public async Task TestExecuteQuery_NonQuery() { @@ -119,7 +82,7 @@ public async Task TestExecuteQuery_NonQuery() Assert.AreEqual(3, insert.RecordsAffected); Assert.IsNull(insert.ErrorMessage); Assert.AreEqual(3, (int)insert.Data["AffectedRows"]); - Assert.AreEqual(3, GetRowCount()); // Make sure rows inserted before moving on. + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // Make sure rows inserted before moving on. // Select all var select = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); @@ -127,7 +90,7 @@ public async Task TestExecuteQuery_NonQuery() Assert.AreEqual(-1, select.RecordsAffected); Assert.IsNull(select.ErrorMessage); Assert.AreEqual(-1, (int)select.Data["AffectedRows"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Select single var selectSingle = await MicrosoftSQL.ExecuteQuery(inputSelectSingle, options, default); @@ -135,14 +98,14 @@ public async Task TestExecuteQuery_NonQuery() Assert.AreEqual(-1, selectSingle.RecordsAffected); Assert.IsNull(selectSingle.ErrorMessage); Assert.AreEqual(-1, (int)select.Data["AffectedRows"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Update var update = await MicrosoftSQL.ExecuteQuery(inputUpdate, options, default); Assert.IsTrue(update.Success); Assert.AreEqual(1, update.RecordsAffected); Assert.IsNull(update.ErrorMessage); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check var checkUpdateResult = await MicrosoftSQL.ExecuteQuery(inputSelectAfterExecution, options, default); Assert.AreEqual("Suku", (string)checkUpdateResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkUpdateResult.Data[0]["FirstName"]); @@ -150,14 +113,14 @@ public async Task TestExecuteQuery_NonQuery() Assert.AreEqual("Forst", (string)checkUpdateResult.Data[1]["FirstName"]); Assert.AreEqual("Hiiri", (string)checkUpdateResult.Data[2]["LastName"]); Assert.AreEqual("Mikki", (string)checkUpdateResult.Data[2]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Delete var delete = await MicrosoftSQL.ExecuteQuery(inputDelete, options, default); Assert.IsTrue(delete.Success); Assert.AreEqual(1, delete.RecordsAffected); Assert.IsNull(delete.ErrorMessage); - Assert.AreEqual(2, GetRowCount()); // double check + Assert.AreEqual(2, Helper.GetRowCount(_connString, _tableName)); // double check var checkDeleteResult = await MicrosoftSQL.ExecuteQuery(inputSelectAfterExecution, options, default); Assert.AreEqual("Suku", (string)checkDeleteResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkDeleteResult.Data[0]["FirstName"]); @@ -167,16 +130,4 @@ public async Task TestExecuteQuery_NonQuery() CleanUp(); } } - - private static int GetRowCount() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var getRows = connection.CreateCommand(); - getRows.CommandText = $"SELECT COUNT(*) FROM {_tableName}"; - var count = (int)getRows.ExecuteScalar(); - connection.Close(); - connection.Dispose(); - return count; - } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs index 9f3ef74..c0fade1 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs @@ -1,49 +1,13 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; +using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Data.SqlClient; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] -public class ScalarUnitTests +public class ScalarUnitTests : ExecuteQueryTestBase { - /* - docker-compose up - - How to use via terminal: - docker exec -it sql1 "bash" - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!" - SELECT * FROM TestTable - GO - */ - - private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; - private static readonly string _tableName = "TestTable"; - - [TestInitialize] - public void Init() - { - using var connection = new 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"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - - [TestCleanup] - public void CleanUp() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var createTable = connection.CreateCommand(); - createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END"; - createTable.ExecuteNonQuery(); - connection.Close(); - connection.Dispose(); - } - [TestMethod] public async Task TestExecuteQuery_Scalar() { @@ -119,7 +83,7 @@ public async Task TestExecuteQuery_Scalar() Assert.IsTrue(insert.Success); Assert.AreEqual(1, insert.RecordsAffected); Assert.IsNull(insert.ErrorMessage); - Assert.AreEqual(3, GetRowCount()); // Make sure rows inserted before moving on. + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // Make sure rows inserted before moving on. // Select all - Returns 1 because first value (row/column) is ID = 1 var select = await MicrosoftSQL.ExecuteQuery(inputSelect, options, default); @@ -127,7 +91,7 @@ public async Task TestExecuteQuery_Scalar() Assert.AreEqual(1, select.RecordsAffected); Assert.IsNull(select.ErrorMessage); Assert.AreEqual(1, (int)select.Data["Value"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Select single var selectSingle = await MicrosoftSQL.ExecuteQuery(inputSelectSingle, options, default); @@ -135,14 +99,14 @@ public async Task TestExecuteQuery_Scalar() Assert.AreEqual(1, selectSingle.RecordsAffected); Assert.IsNull(selectSingle.ErrorMessage); Assert.AreEqual("Suku", (string)selectSingle.Data["Value"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Update var update = await MicrosoftSQL.ExecuteQuery(inputUpdate, options, default); Assert.IsTrue(update.Success); Assert.AreEqual(1, update.RecordsAffected); Assert.IsNull(update.ErrorMessage); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check var checkUpdateResult = await MicrosoftSQL.ExecuteQuery(inputSelectAfterExecution, options, default); Assert.AreEqual("Suku", (string)checkUpdateResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkUpdateResult.Data[0]["FirstName"]); @@ -150,14 +114,14 @@ public async Task TestExecuteQuery_Scalar() Assert.AreEqual("Forst", (string)checkUpdateResult.Data[1]["FirstName"]); Assert.AreEqual("Hiiri", (string)checkUpdateResult.Data[2]["LastName"]); Assert.AreEqual("Mikki", (string)checkUpdateResult.Data[2]["FirstName"]); - Assert.AreEqual(3, GetRowCount()); // double check + Assert.AreEqual(3, Helper.GetRowCount(_connString, _tableName)); // double check // Delete var delete = await MicrosoftSQL.ExecuteQuery(inputDelete, options, default); Assert.IsTrue(delete.Success); Assert.AreEqual(1, delete.RecordsAffected); Assert.IsNull(delete.ErrorMessage); - Assert.AreEqual(2, GetRowCount()); // double check + Assert.AreEqual(2, Helper.GetRowCount(_connString, _tableName)); // double check var checkDeleteResult = await MicrosoftSQL.ExecuteQuery(inputSelectAfterExecution, options, default); Assert.AreEqual("Suku", (string)checkDeleteResult.Data[0]["LastName"]); Assert.AreEqual("Etu", (string)checkDeleteResult.Data[0]["FirstName"]); @@ -167,16 +131,4 @@ public async Task TestExecuteQuery_Scalar() CleanUp(); } } - - private static int GetRowCount() - { - using var connection = new SqlConnection(_connString); - connection.Open(); - var getRows = connection.CreateCommand(); - getRows.CommandText = $"SELECT COUNT(*) FROM {_tableName}"; - var count = (int)getRows.ExecuteScalar(); - connection.Close(); - connection.Dispose(); - return count; - } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs index 48fa25b..1d8f16d 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs @@ -79,7 +79,7 @@ private static async Task ExecuteHandler(Input input, Options options, S Result result; object dataObject; SqlDataReader dataReader = null; - var table = new DataTable(); + using var table = new DataTable(); try { From a169f0dd5f02382cdfd329a7e3d45fc38e555cd4 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Mar 2024 15:45:51 +0200 Subject: [PATCH 3/6] CodeQL fixes --- .../Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs index d6a9d58..132a7c3 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs @@ -6,7 +6,7 @@ internal class Helper { internal static string CreateConnectionString() { - return "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!"; + return "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!;Encrypt=true;TrustServerCertificate=True;"; } internal static int GetRowCount(string connString, string table) From 003cab31b5e44f42df760a9340df81e923606481 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Mar 2024 15:47:08 +0200 Subject: [PATCH 4/6] CodeQL fixes --- .../Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs index 132a7c3..ecf5955 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs @@ -6,7 +6,8 @@ internal class Helper { internal static string CreateConnectionString() { - return "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!;Encrypt=true;TrustServerCertificate=True;"; + var pw = "Salakala123!"; + return $"Server=127.0.0.1,1433;Database=Master;User Id=SA;Password={pw};Encrypt=true;TrustServerCertificate=True;"; } internal static int GetRowCount(string connString, string table) From d41be3751f9f2ba2916e9e8dde0940b58408eb0f Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Mar 2024 15:59:05 +0200 Subject: [PATCH 5/6] CodeQL fixes --- .../ExceptionUnitTests.cs | 62 +++++++------------ .../Lib/Helper.cs | 15 ++++- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs index 5b63cd0..78535c0 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs @@ -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(() => MicrosoftSQL.ExecuteQuery(input, options, default)); Assert.IsTrue(ex.Message.Contains("Login failed for user 'SA'.")); @@ -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); @@ -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(async () => await MicrosoftSQL.ExecuteQuery(input, options, default)); Assert.IsTrue(ex.Result.Message.Contains("System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Unit'.")); @@ -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); diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs index ecf5955..be550aa 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs @@ -4,10 +4,21 @@ namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; internal class Helper { + /// + /// Test credentials for docker server. + /// + 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) From d48892a5e6d37def8cd1095f195a9fe0c0590b71 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 8 Mar 2024 08:18:09 +0200 Subject: [PATCH 6/6] PR review changes --- .../Lib/ExecuteQueryTestBase.cs | 5 ----- .../ScalarUnitTests.cs | 1 - 2 files changed, 6 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs index 85a7b40..c763f57 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs @@ -1,10 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.Collections.Generic; using System.Data.SqlClient; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs index c0fade1..20f3be4 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs @@ -1,7 +1,6 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Data.SqlClient; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests;