Skip to content

Commit

Permalink
Merge pull request #53 from FrendsPlatform/BatchOperation---add-manag…
Browse files Browse the repository at this point in the history
…ed-identity-authentication

BatchOperation - add managed identity authentication
  • Loading branch information
jefim authored Aug 8, 2024
2 parents b2e97c7 + 7953038 commit 8ff33ae
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 19 deletions.
6 changes: 5 additions & 1 deletion Frends.MicrosoftSQL.BatchOperation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.0.0] - 2024-08-05
### Changed
- [Breaking] The task now uses Microsoft.Data.SqlClient instead of System.Data.SqlClient.

## [1.0.0] - 2023-02-03
### Added
- Initial implementation
- Initial implementation
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task TestBatchOperation_Invalid_Creds_ThrowError()
{
var input = new Input()
{
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord",
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord;TrustServerCertificate=True",
};

var options = new Options()
Expand All @@ -35,7 +35,7 @@ public async Task TestBatchOperation_Invalid_Creds_ReturnErrorMessage()
{
var input = new Input()
{
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord",
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord;TrustServerCertificate=True",
};

var options = new Options()
Expand All @@ -50,4 +50,50 @@ public async Task TestBatchOperation_Invalid_Creds_ReturnErrorMessage()
Assert.IsTrue(result.ErrorMessage.Contains("Login failed for user 'SA'."));
Assert.AreEqual(0, result.RecordsAffected);
}

[TestMethod]
public async Task TestBatchOperation_ExecuteHandler_Exception_TransactionIsNull()
{
// Arrange
var input = new Input
{
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!;TrustServerCertificate=True",
};

var options = new Options
{
ThrowErrorOnFailure = true,
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.None,
CommandTimeoutSeconds = 60,
};

// Act
var ex = await Assert.ThrowsExceptionAsync<Exception>(() => MicrosoftSQL.BatchOperation(input, options, CancellationToken.None));
Console.WriteLine("Actual exception message: " + ex.Message);

// Assert
Assert.IsNotNull(ex.InnerException);
Assert.IsTrue(ex.InnerException.Message.Contains("ExecuteHandler exception: 'Options.SqlTransactionIsolationLevel = None', so there was no transaction rollback."));
}

[TestMethod]
public async Task TestBatchOperation_ExecuteHandler_Exception_TransactionIsNotNull()
{
var input = new Input
{
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!;TrustServerCertificate=True",
};

var options = new Options
{
ThrowErrorOnFailure = true,
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
CommandTimeoutSeconds = 60,
};

var ex = await Assert.ThrowsExceptionAsync<Exception>(() => MicrosoftSQL.BatchOperation(input, options, CancellationToken.None));

Assert.IsNotNull(ex.InnerException);
Assert.IsTrue(ex.InnerException.Message.Contains("ExecuteHandler exception: (If required) transaction rollback completed without exception."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<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,6 +1,6 @@
using Frends.MicrosoftSQL.BatchOperation.Definitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;

namespace Frends.MicrosoftSQL.BatchOperation.Tests;

Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Frends.MicrosoftSQL.BatchOperation.Definitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;

namespace Frends.MicrosoftSQL.BatchOperation.Tests;

Expand All @@ -17,9 +17,9 @@ docker exec -it sql1 "bash"
GO
*/

private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!";
private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!;TrustServerCertificate=True";
private static readonly string _tableName = "TestTable";

[TestInitialize]
public void Init()
{
Expand All @@ -46,7 +46,7 @@ public void CleanUp()

[TestMethod]
public async Task TestBatchOperation()
{
{
var transactionLevels = new List<SqlTransactionIsolationLevel>() {
SqlTransactionIsolationLevel.Unspecified,
SqlTransactionIsolationLevel.Serializable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
using IsolationLevel = System.Data.IsolationLevel;
using Microsoft.Data.SqlClient;

namespace Frends.MicrosoftSQL.BatchOperation;

/// <summary>
/// MicrosoftSQL Task.
/// MicrosoftSQL BatchOperation Task.
/// </summary>
public class MicrosoftSQL
{
Expand Down Expand Up @@ -78,7 +78,7 @@ private static async Task<Result> ExecuteHandler(Input input, Options options, S
commandType: CommandType.Text,
transaction: transaction)
.ConfigureAwait(false);

if (transaction != null)
await transaction.CommitAsync(cancellationToken);

Expand Down Expand Up @@ -137,4 +137,4 @@ private static void OnPluginUnloadingRequested(AssemblyLoadContext obj)
{
obj.Unloading -= OnPluginUnloadingRequested;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// SQL transaction isolation levels.
/// </summary>
public enum SqlTransactionIsolationLevel
public enum SqlTransactionIsolationLevel
{
/// <summary>
/// A different isolation level than the one specified is being used, but the level cannot be determined.
Expand All @@ -29,7 +29,7 @@ public enum SqlTransactionIsolationLevel
/// A dirty read is possible, meaning that no shared locks are issued and no exclusive locks are honored.
/// </summary>
ReadUncommitted = 256,

/// <summary>
/// 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.
Expand All @@ -40,7 +40,7 @@ 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.
/// </summary>
Serializable = 1048576,

/// <summary>
/// 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.
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.0.0</Version>
<Version>2.0.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand All @@ -22,9 +22,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup>
</Project>

0 comments on commit 8ff33ae

Please sign in to comment.