Skip to content

Commit

Permalink
feat: FIR-27628 implemented fake transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed Oct 26, 2023
1 parent f39741e commit f0cb582
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
3 changes: 2 additions & 1 deletion FireboltDotNetSdk.Tests/Unit/FireboltCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ public void SetTransaction()
command.Transaction = null;
Assert.That(command.Transaction, Is.EqualTo(null));
Mock<DbTransaction> transaction = new();
Assert.Throws<NotSupportedException>(() => command.Transaction = transaction.Object);
command.Transaction = transaction.Object;
Assert.That(command.Transaction, Is.SameAs(transaction.Object));
}

[Test]
Expand Down
17 changes: 16 additions & 1 deletion FireboltDotNetSdk.Tests/Unit/FireboltConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System.Data.Common;
using System.Net;
using FireboltDotNetSdk.Client;
using FireboltDotNetSdk.Exception;
Expand Down Expand Up @@ -329,13 +330,27 @@ public void SetDifferentDatabaseAsync()
That(cs.ClosedWithConnectionString, Is.EqualTo(connectionString2));
}

[Test]
public void FakeTransaction()
{
const string connectionString = "clientid=testuser;clientsecret=testpwd;account=accountname;engine=diesel";
var cs = new FireboltConnection(connectionString);
DbTransaction transaction = cs.BeginTransaction();
NotNull(transaction);
False(transaction.SupportsSavepoints);
That(transaction.Connection, Is.SameAs(cs));
That(transaction.IsolationLevel, Is.EqualTo(IsolationLevel.Unspecified));
transaction.Commit();
Throws<NotImplementedException>(() => transaction.Rollback());
}

[Test]
public void NotImplementedMethods()
{
const string connectionString = "clientid=testuser;clientsecret=testpwd;account=accountname;engine=diesel";
var cs = new FireboltConnection(connectionString);
Throws<NotImplementedException>(() => cs.BeginTransaction());
Throws<NotSupportedException>(() => cs.EnlistTransaction(null));
Throws<NotSupportedException>(() => cs.EnlistTransaction(new Mock<System.Transactions.Transaction>().Object));
Throws<NotImplementedException>(() => cs.GetSchema());
Throws<NotImplementedException>(() => cs.GetSchema("collection"));
Throws<NotImplementedException>(() => cs.GetSchema("collection", new string[0]));
Expand Down
16 changes: 2 additions & 14 deletions FireboltNETSDK/Client/FireboltCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,11 @@ protected override DbConnection? DbConnection
set => _connection = value == null ? null : (FireboltConnection)value;
}


/// <summary>
/// Gets or sets the transaction within which the command executes. Always returns <b>null</b>.
/// Gets or sets the transaction within which the command executes. The transation is ignored.
/// </summary>
/// <returns><b>null</b></returns>
/// <exception cref="NotSupportedException">The value set is not <b>null</b>.</exception>
protected override DbTransaction? DbTransaction
{
get => null;
set
{
if (value != null)
{
throw new NotSupportedException($"{nameof(DbTransaction)} is read only.'");
}
}
}
protected override DbTransaction? DbTransaction { get; set; }

/// <summary>
/// Gets the <see cref="FireboltParameterCollection"/>.
Expand Down
4 changes: 2 additions & 2 deletions FireboltNETSDK/Client/FireboltConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public override Task ChangeDatabaseAsync(string databaseName, CancellationToken
}

/// <summary>
/// Not supported. Transactions are not supported by the Firebolt server.
/// Not supported. Transactions are not supported by the Firebolt server but we ignore attempts to use them.
/// </summary>
/// <exception cref="NotSupportedException">Always throws <see cref="NotSupportedException"/>.</exception>
public override void EnlistTransaction(Transaction? transaction)
Expand Down Expand Up @@ -392,7 +392,7 @@ public override void Open()

protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
{
throw new NotImplementedException();
return new FireboltTransaction(this);
}

private string EditConnectionString(string orig, string name, string value)
Expand Down
55 changes: 55 additions & 0 deletions FireboltNETSDK/FireboltTransaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#region License Apache 2.0
/* Copyright 2022
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion

using System.Data.Common;
using System.Runtime.CompilerServices;
using IsolationLevel = System.Data.IsolationLevel;


[assembly: InternalsVisibleTo("FireboltDotNetSdk.Tests")]
namespace FireboltDotNetSdk.Client
{
/// <summary>
/// Represents a connection to a Firebolt database. This class cannot be inherited.
/// </summary>
internal sealed class FireboltTransaction : DbTransaction
{
private DbConnection _dbConnection;
internal FireboltTransaction(DbConnection connection) : base()
{
_dbConnection = connection;
}

public override IsolationLevel IsolationLevel { get => IsolationLevel.Unspecified; }

protected override DbConnection? DbConnection { get => _dbConnection; }

/// <summary>
/// Empty NOP implementation - does nothing.
/// </summary>
public override void Commit() { }

/// <summary>
/// Throws NotImplementedException to indicate that rollback is not supported.
/// </summary>
public override void Rollback()
{
throw new NotImplementedException();
}

}
}

0 comments on commit f0cb582

Please sign in to comment.