From cfcea276df680aca1f8e03aa49bfd50b6be5675d Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Dec 2023 07:53:50 +0200 Subject: [PATCH] Linting fixes and added Tests for code coverage --- .../AutoUnitTests.cs | 2 +- .../ExceptionUnitTests.cs | 45 ++++++++++++++++++- .../ExecuteReaderUnitTests.cs | 2 +- .../ManualTesting.cs | 2 +- .../NonQueryUnitTests.cs | 2 +- .../ScalarUnitTests.cs | 2 +- .../Definitions/Enums.cs | 10 ++--- 7 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs index 1a26eb5..9e07445 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs @@ -20,7 +20,7 @@ docker exec -it sql1 "bash" 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() { diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs index 040de24..664abaf 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs @@ -19,7 +19,7 @@ docker exec -it sql1 "bash" 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() { @@ -83,4 +83,47 @@ public async Task TestExecuteQuery_Invalid_Creds_ReturnErrorMessage() Assert.IsTrue(result.ErrorMessage.Contains("Login failed for user 'SA'.")); Assert.AreEqual(0, result.RecordsAffected); } + + [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 + }; + + 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'.")); + } + + [TestMethod] + public async Task TestExecuteQuery_ErrorMessageWhenQueryFails() + { + 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 = false + }; + + 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'.")); + } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs index bcf5b24..db6b381 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExecuteReaderUnitTests.cs @@ -20,7 +20,7 @@ docker exec -it sql1 "bash" 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() { diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs index 950a8a5..0ca3eaa 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ManualTesting.cs @@ -21,7 +21,7 @@ docker exec -it sql1 "bash" 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() { diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs index 7be4b94..c06bc78 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/NonQueryUnitTests.cs @@ -20,7 +20,7 @@ docker exec -it sql1 "bash" 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() { diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs index ed9df56..9f3ef74 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs @@ -19,7 +19,7 @@ docker exec -it sql1 "bash" 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() { diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Definitions/Enums.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Definitions/Enums.cs index e82aa67..9566f01 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Definitions/Enums.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Definitions/Enums.cs @@ -3,7 +3,7 @@ /// /// SQL transaction isolation levels. /// -public enum SqlTransactionIsolationLevel +public enum SqlTransactionIsolationLevel { /// /// A different isolation level than the one specified is being used, but the level cannot be determined. @@ -28,8 +28,8 @@ public enum SqlTransactionIsolationLevel /// /// A dirty read is possible, meaning that no shared locks are issued and no exclusive locks are honored. /// - ReadUncommitted = 256, - + ReadUncommitted = 256, + /// /// Locks are placed on all data that is used in a query, preventing other users from updating the data. /// Prevents non-repeatable reads but phantom rows are still possible. @@ -40,12 +40,12 @@ public enum SqlTransactionIsolationLevel /// A range lock is placed on the System.Data.DataSet, preventing other users from updating or inserting rows into the dataset until the transaction is complete. /// Serializable = 1048576, - + /// /// Reduces blocking by storing a version of data that one application can read while another is modifying the same data. /// Indicates that from one transaction you cannot see changes made in other transactions, even if you requery. /// - Snapshot = 16777216 + Snapshot = 16777216, } ///