HackPoint commented on code in PR #44783: URL: https://github.com/apache/arrow/pull/44783#discussion_r1969424656
########## csharp/test/Apache.Arrow.Flight.Sql.Tests/FlightSqlClientTests.cs: ########## @@ -0,0 +1,855 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Apache.Arrow.Flight.Client; +using Apache.Arrow.Flight.Sql.Client; +using Apache.Arrow.Flight.TestWeb; +using Apache.Arrow.Types; +using Arrow.Flight.Protocol.Sql; +using Google.Protobuf; +using Grpc.Core.Utils; +using Xunit; + +namespace Apache.Arrow.Flight.Sql.Tests; + +public class FlightSqlClientTests : IDisposable +{ + readonly TestFlightSqlWebFactory _testWebFactory; + readonly FlightStore _flightStore; + private readonly FlightSqlClient _flightSqlClient; + private readonly FlightSqlTestUtils _testUtils; + + public FlightSqlClientTests() + { + _flightStore = new FlightStore(); + _testWebFactory = new TestFlightSqlWebFactory(_flightStore); + FlightClient flightClient = new(_testWebFactory.GetChannel()); + _flightSqlClient = new FlightSqlClient(flightClient); + + _testUtils = new FlightSqlTestUtils(_testWebFactory, _flightStore); + } + + #region Transactions + + [Fact] + public async Task CommitTransactionAsync() + { + // Arrange + string transactionId = "sample-transaction-id"; + var transaction = new Transaction(transactionId); + + // Act + var streamCall = _flightSqlClient.CommitAsync(transaction); + var result = await streamCall.ResponseStream.ToListAsync(); + + // Assert + Assert.NotNull(result); + Assert.Equal(transaction.TransactionId, result.FirstOrDefault()?.Body); + } + + [Fact] + public async Task BeginTransactionAsync() + { + // Arrange + string expectedTransactionId = "sample-transaction-id"; + + // Act + var transaction = await _flightSqlClient.BeginTransactionAsync(); + + // Assert + Assert.NotNull(transaction); + Assert.Equal(ByteString.CopyFromUtf8(expectedTransactionId), transaction.TransactionId); + } + + [Fact] + public async Task RollbackTransactionAsync() + { + // Arrange + string transactionId = "sample-transaction-id"; + var transaction = new Transaction(transactionId); + + // Act + var streamCall = _flightSqlClient.RollbackAsync(transaction); + var result = await streamCall.ResponseStream.ToListAsync(); + + // Assert + Assert.NotNull(transaction); + Assert.Equal(result.FirstOrDefault()?.Body, transaction.TransactionId); + } + + #endregion + + #region PreparedStatement + + [Fact] + public async Task PreparedAsync() + { + // Arrange + string query = "INSERT INTO users (id, name) VALUES (1, 'John Doe')"; + var transaction = new Transaction("sample-transaction-id"); + var flightDescriptor = FlightDescriptor.CreateCommandDescriptor("test"); + + // Create a sample schema for the dataset and parameters + var schema = new Schema.Builder() + .Field(f => f.Name("id").DataType(Int32Type.Default)) + .Field(f => f.Name("name").DataType(StringType.Default)) + .Build(); + + var recordBatch = new RecordBatch(schema, new Array[] + { + new Int32Array.Builder().Append(1).Build(), + new StringArray.Builder().Append("John Doe").Build() + }, 1); + + var flightHolder = new FlightHolder(flightDescriptor, schema, _testWebFactory.GetAddress()); + flightHolder.AddBatch(new RecordBatchWithMetadata(recordBatch)); + _flightStore.Flights.Add(flightDescriptor, flightHolder); + + var datasetSchemaBytes = SchemaExtensions.SerializeSchema(schema); + var parameterSchemaBytes = SchemaExtensions.SerializeSchema(schema); + + var preparedStatementResponse = new ActionCreatePreparedStatementResult + { + PreparedStatementHandle = ByteString.CopyFromUtf8("prepared-handle"), + DatasetSchema = ByteString.CopyFrom(datasetSchemaBytes), + ParameterSchema = ByteString.CopyFrom(parameterSchemaBytes) + }; + + // Act + var preparedStatement = await _flightSqlClient.PrepareAsync(query, transaction); + var deserializedDatasetSchema = SchemaExtensions.DeserializeSchema(preparedStatementResponse.DatasetSchema.ToByteArray()); + var deserializedParameterSchema = SchemaExtensions.DeserializeSchema(preparedStatementResponse.ParameterSchema.ToByteArray()); + + // Assert + Assert.NotNull(preparedStatement); + Assert.NotNull(deserializedDatasetSchema); + Assert.NotNull(deserializedParameterSchema); + CompareSchemas(schema, deserializedDatasetSchema); + CompareSchemas(schema, deserializedParameterSchema); + } + + #endregion + + [Fact] + public async Task ExecuteUpdateAsync() + { + // Arrange + string query = "UPDATE test_table SET column1 = 'value' WHERE column2 = 'condition'"; + var transaction = new Transaction("sample-transaction-id"); + var flightDescriptor = FlightDescriptor.CreateCommandDescriptor("test"); + + var schema = new Schema.Builder() + .Field(f => f.Name("id").DataType(Int32Type.Default)) + .Field(f => f.Name("name").DataType(StringType.Default)) + .Build(); + + var recordBatch = new RecordBatch(schema, new Array[] + { + new Int32Array.Builder().Append(1).Build(), + new StringArray.Builder().Append("John Doe").Build() + }, 1); + + Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
