lidavidm commented on a change in pull request #11507: URL: https://github.com/apache/arrow/pull/11507#discussion_r742320867
########## File path: cpp/src/arrow/flight/flight-sql/client_test.cc ########## @@ -0,0 +1,569 @@ +// 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. + +#include <arrow/flight/client.h> +#include <arrow/flight/flight-sql/FlightSql.pb.h> +#include <arrow/flight/flight-sql/api.h> +#include <arrow/testing/gtest_util.h> +#include <gmock/gmock.h> +#include <google/protobuf/any.pb.h> +#include <gtest/gtest.h> + +#include <utility> + +namespace pb = arrow::flight::protocol; +using ::testing::_; +using ::testing::Ref; + +namespace arrow { +namespace flight { +namespace sql { + +namespace internal { +class FlightClientImpl { + public: + ~FlightClientImpl() = default; + + MOCK_METHOD(Status, GetFlightInfo, + (const FlightCallOptions&, const FlightDescriptor&, + std::unique_ptr<FlightInfo>*)); + MOCK_METHOD(Status, DoGet, + (const FlightCallOptions& options, const Ticket& ticket, + std::unique_ptr<FlightStreamReader>* stream)); + MOCK_METHOD(Status, DoPut, + (const FlightCallOptions&, const FlightDescriptor&, + const std::shared_ptr<Schema>& schema, + std::unique_ptr<FlightStreamWriter>*, + std::unique_ptr<FlightMetadataReader>*)); + MOCK_METHOD(Status, DoAction, + (const FlightCallOptions& options, const Action& action, + std::unique_ptr<ResultStream>* results)); +}; + +Status FlightClientImpl_GetFlightInfo(FlightClientImpl* client, + const FlightCallOptions& options, + const FlightDescriptor& descriptor, + std::unique_ptr<FlightInfo>* info) { + return client->GetFlightInfo(options, descriptor, info); +} + +Status FlightClientImpl_DoPut(FlightClientImpl* client, const FlightCallOptions& options, + const FlightDescriptor& descriptor, + const std::shared_ptr<Schema>& schema, + std::unique_ptr<FlightStreamWriter>* stream, + std::unique_ptr<FlightMetadataReader>* reader) { + return client->DoPut(options, descriptor, schema, stream, reader); +} + +Status FlightClientImpl_DoGet(FlightClientImpl* client, const FlightCallOptions& options, + const Ticket& ticket, + std::unique_ptr<FlightStreamReader>* stream) { + return client->DoGet(options, ticket, stream); +} + +Status FlightClientImpl_DoAction(FlightClientImpl* client, + const FlightCallOptions& options, const Action& action, + std::unique_ptr<ResultStream>* results) { + return client->DoAction(options, action, results); +} + +} // namespace internal + +class FlightMetadataReaderMock : public FlightMetadataReader { + public: + std::shared_ptr<Buffer>* buffer; + + explicit FlightMetadataReaderMock(std::shared_ptr<Buffer>* buffer) { + this->buffer = buffer; + } + + Status ReadMetadata(std::shared_ptr<Buffer>* out) override { + *out = *buffer; + return Status::OK(); + } +}; + +class FlightStreamWriterMock : public FlightStreamWriter { + public: + FlightStreamWriterMock() = default; + + Status DoneWriting() override { return Status::OK(); } + + Status WriteMetadata(std::shared_ptr<Buffer> app_metadata) override { + return Status::OK(); + } + + Status Begin(const std::shared_ptr<Schema>& schema, + const ipc::IpcWriteOptions& options) override { + return Status::OK(); + } + + Status Begin(const std::shared_ptr<Schema>& schema) override { + return MetadataRecordBatchWriter::Begin(schema); + } + + ipc::WriteStats stats() const override { return ipc::WriteStats(); } + + Status WriteWithMetadata(const RecordBatch& batch, + std::shared_ptr<Buffer> app_metadata) override { + return Status::OK(); + } + + Status Close() override { return Status::OK(); } + + Status WriteRecordBatch(const RecordBatch& batch) override { return Status::OK(); } +}; + +FlightDescriptor getDescriptor(google::protobuf::Message& command) { + google::protobuf::Any any; + any.PackFrom(command); + + const std::string& string = any.SerializeAsString(); + return FlightDescriptor::Command(string); +} + +TEST(TestFlightSqlClient, TestGetCatalogs) { + auto client_mock = std::make_shared<internal::FlightClientImpl>(); + FlightSqlClient sql_client(client_mock); Review comment: Both libarrow_flight_sql.so and client_test.cc.o have symbols for FlightClientImpl for me. That said, they're marked as weak symbols, and presumably the one in the program takes precedence. (Confirmed with LD_DEBUG.) <details> ``` > nm -C src/arrow/flight/flight_sql/CMakeFiles/arrow-flight-sql-client-test.dir/client_test.cc.o | rg FlightClientImpl::DoPut 0000000000000000 W arrow::flight::sql::internal::FlightClientImpl::DoPut(arrow::flight::FlightCallOptions const&, arrow::flight::FlightDescriptor const&, std::shared_ptr<arrow::Schema> const&, std::unique_ptr<arrow::flight::FlightStreamWriter, std::default_delete<arrow::flight::FlightStreamWriter> >*, std::unique_ptr<arrow::flight::FlightMetadataReader, std::default_delete<arrow::flight::FlightMetadataReader> >*) > nm -C debug/libarrow_flight_sql.so | rg FlightClientImpl::DoPut 00000000000f1dc0 W arrow::flight::sql::internal::FlightClientImpl::DoPut(arrow::flight::FlightCallOptions const&, arrow::flight::FlightDescriptor const&, std::shared_ptr<arrow::Schema> const&, std::unique_ptr<arrow::flight::FlightStreamWriter, std::default_delete<arrow::flight::FlightStreamWriter> >*, std::unique_ptr<arrow::flight::FlightMetadataReader, std::default_delete<arrow::flight::FlightMetadataReader> >*) > nm -C debug/arrow-flight-sql-client-test | rg FlightClientImpl::DoPut 000000000055b320 W arrow::flight::sql::internal::FlightClientImpl::DoPut(arrow::flight::FlightCallOptions const&, arrow::flight::FlightDescriptor const&, std::shared_ptr<arrow::Schema> const&, std::unique_ptr<arrow::flight::FlightStreamWriter, std::default_delete<arrow::flight::FlightStreamWriter> >*, std::unique_ptr<arrow::flight::FlightMetadataReader, std::default_delete<arrow::flight::FlightMetadataReader> >*) ``` </details> This works and I'm not going to hold this up, however, it is rather magical and it's worth at least a comment explaining the intent of FlightClientImpl. -- 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]
