Skip to content

Commit cd4f7ea

Browse files
author
HackPoint
committed
test(Transaction): PreparedAsync: null, empty cases
1 parent 37e4369 commit cd4f7ea

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

csharp/src/Apache.Arrow.Flight.Sql/Transaction.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Apache.Arrow.Flight.Sql;
1717

18-
using Google.Protobuf; // Ensure you have the Protobuf dependency
18+
using Google.Protobuf;
1919

2020
public readonly struct Transaction
2121
{
@@ -37,12 +37,21 @@ public Transaction(string transactionId)
3737
_transactionId = ByteString.CopyFromUtf8(transactionId);
3838
}
3939

40-
public bool IsValid() => _transactionId.Length > 0;
40+
public bool IsValid() => TransactionId.Length > 0;
4141

42-
public override bool Equals(object? obj) => obj is Transaction other && _transactionId.Equals(other._transactionId);
42+
public override bool Equals(object? obj)
43+
{
44+
if (obj is not Transaction other)
45+
return false;
46+
47+
// Safe compare even if _transactionId is null (from default(Transaction))
48+
return (_transactionId ?? TransactionIdDefaultValue)
49+
.Equals(other._transactionId);
50+
}
4351

44-
public override int GetHashCode() => _transactionId.GetHashCode();
52+
public override int GetHashCode() => (_transactionId ?? TransactionIdDefaultValue).GetHashCode();
53+
4554

4655
public static bool operator ==(Transaction left, Transaction right) => left.Equals(right);
4756
public static bool operator !=(Transaction left, Transaction right) => !left.Equals(right);
48-
}
57+
}

csharp/test/Apache.Arrow.Flight.Sql.Tests/FlightSqlClientTests.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,17 @@ public async Task RollbackTransactionAsync()
9696

9797
#region PreparedStatement
9898

99-
[Fact]
100-
public async Task PreparedAsync()
99+
[Theory]
100+
[InlineData("sample-transaction-id", true)]
101+
[InlineData(null, false)]
102+
public async Task PreparedAsync(string transactionId, bool expectTransaction)
101103
{
102104
// Arrange
103105
string query = "INSERT INTO users (id, name) VALUES (1, 'John Doe')";
104-
var transaction = new Transaction("sample-transaction-id");
106+
var transaction = string.IsNullOrEmpty(transactionId)
107+
? Transaction.NoTransaction
108+
: new Transaction(transactionId);
109+
105110
var flightDescriptor = FlightDescriptor.CreateCommandDescriptor("test");
106111

107112
// Create a sample schema for the dataset and parameters
@@ -131,7 +136,15 @@ public async Task PreparedAsync()
131136
};
132137

133138
// Act
134-
var preparedStatement = await _flightSqlClient.PrepareAsync(query, transaction);
139+
PreparedStatement preparedStatement;
140+
if (expectTransaction)
141+
{
142+
preparedStatement = await _flightSqlClient.PrepareAsync(query, transaction);
143+
}
144+
else
145+
{
146+
preparedStatement = await _flightSqlClient.PrepareAsync(query);
147+
}
135148
var deserializedDatasetSchema =
136149
SchemaExtensions.DeserializeSchema(preparedStatementResponse.DatasetSchema.ToByteArray());
137150
var deserializedParameterSchema =

0 commit comments

Comments
 (0)