diff --git a/google/cloud/bigquery_storage_v1/services/big_query_read/transports/README.rst b/google/cloud/bigquery_storage_v1/services/big_query_read/transports/README.rst new file mode 100644 index 00000000..687d374a --- /dev/null +++ b/google/cloud/bigquery_storage_v1/services/big_query_read/transports/README.rst @@ -0,0 +1,9 @@ + +transport inheritance structure +_______________________________ + +`BigQueryReadTransport` is the ABC for all transports. +- public child `BigQueryReadGrpcTransport` for sync gRPC transport (defined in `grpc.py`). +- public child `BigQueryReadGrpcAsyncIOTransport` for async gRPC transport (defined in `grpc_asyncio.py`). +- private child `_BaseBigQueryReadRestTransport` for base REST transport with inner classes `_BaseMETHOD` (defined in `rest_base.py`). +- public child `BigQueryReadRestTransport` for sync REST transport with inner classes `METHOD` derived from the parent's corresponding `_BaseMETHOD` classes (defined in `rest.py`). diff --git a/google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc_asyncio.py b/google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc_asyncio.py index fd55ee75..897b3cae 100644 --- a/google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc_asyncio.py +++ b/google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc_asyncio.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -228,6 +229,9 @@ def __init__( ) # Wrap messages. This must be done after self._grpc_channel exists + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) self._prep_wrapped_messages(client_info) @property @@ -364,7 +368,7 @@ def split_read_stream( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_read_session: gapic_v1.method_async.wrap_method( + self.create_read_session: self._wrap_method( self.create_read_session, default_retry=retries.AsyncRetry( initial=0.1, @@ -379,7 +383,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.read_rows: gapic_v1.method_async.wrap_method( + self.read_rows: self._wrap_method( self.read_rows, default_retry=retries.AsyncRetry( initial=0.1, @@ -393,7 +397,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=86400.0, client_info=client_info, ), - self.split_read_stream: gapic_v1.method_async.wrap_method( + self.split_read_stream: self._wrap_method( self.split_read_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -410,8 +414,17 @@ def _prep_wrapped_messages(self, client_info): ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): return self.grpc_channel.close() + @property + def kind(self) -> str: + return "grpc_asyncio" + __all__ = ("BigQueryReadGrpcAsyncIOTransport",) diff --git a/google/cloud/bigquery_storage_v1/services/big_query_write/transports/README.rst b/google/cloud/bigquery_storage_v1/services/big_query_write/transports/README.rst new file mode 100644 index 00000000..d2db1fed --- /dev/null +++ b/google/cloud/bigquery_storage_v1/services/big_query_write/transports/README.rst @@ -0,0 +1,9 @@ + +transport inheritance structure +_______________________________ + +`BigQueryWriteTransport` is the ABC for all transports. +- public child `BigQueryWriteGrpcTransport` for sync gRPC transport (defined in `grpc.py`). +- public child `BigQueryWriteGrpcAsyncIOTransport` for async gRPC transport (defined in `grpc_asyncio.py`). +- private child `_BaseBigQueryWriteRestTransport` for base REST transport with inner classes `_BaseMETHOD` (defined in `rest_base.py`). +- public child `BigQueryWriteRestTransport` for sync REST transport with inner classes `METHOD` derived from the parent's corresponding `_BaseMETHOD` classes (defined in `rest.py`). diff --git a/google/cloud/bigquery_storage_v1/services/big_query_write/transports/grpc_asyncio.py b/google/cloud/bigquery_storage_v1/services/big_query_write/transports/grpc_asyncio.py index 45aa39f3..daf45972 100644 --- a/google/cloud/bigquery_storage_v1/services/big_query_write/transports/grpc_asyncio.py +++ b/google/cloud/bigquery_storage_v1/services/big_query_write/transports/grpc_asyncio.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -232,6 +233,9 @@ def __init__( ) # Wrap messages. This must be done after self._grpc_channel exists + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) self._prep_wrapped_messages(client_info) @property @@ -463,7 +467,7 @@ def flush_rows( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_write_stream: gapic_v1.method_async.wrap_method( + self.create_write_stream: self._wrap_method( self.create_write_stream, default_retry=retries.AsyncRetry( initial=10.0, @@ -479,7 +483,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=1200.0, client_info=client_info, ), - self.append_rows: gapic_v1.method_async.wrap_method( + self.append_rows: self._wrap_method( self.append_rows, default_retry=retries.AsyncRetry( initial=0.1, @@ -493,7 +497,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=86400.0, client_info=client_info, ), - self.get_write_stream: gapic_v1.method_async.wrap_method( + self.get_write_stream: self._wrap_method( self.get_write_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -509,7 +513,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.finalize_write_stream: gapic_v1.method_async.wrap_method( + self.finalize_write_stream: self._wrap_method( self.finalize_write_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -525,7 +529,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.batch_commit_write_streams: gapic_v1.method_async.wrap_method( + self.batch_commit_write_streams: self._wrap_method( self.batch_commit_write_streams, default_retry=retries.AsyncRetry( initial=0.1, @@ -541,7 +545,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.flush_rows: gapic_v1.method_async.wrap_method( + self.flush_rows: self._wrap_method( self.flush_rows, default_retry=retries.AsyncRetry( initial=0.1, @@ -559,8 +563,17 @@ def _prep_wrapped_messages(self, client_info): ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): return self.grpc_channel.close() + @property + def kind(self) -> str: + return "grpc_asyncio" + __all__ = ("BigQueryWriteGrpcAsyncIOTransport",) diff --git a/google/cloud/bigquery_storage_v1/types/storage.py b/google/cloud/bigquery_storage_v1/types/storage.py index 77c44f92..1d2d02ff 100644 --- a/google/cloud/bigquery_storage_v1/types/storage.py +++ b/google/cloud/bigquery_storage_v1/types/storage.py @@ -389,6 +389,10 @@ class AppendRowsRequest(proto.Message): size. Requests larger than this return an error, typically ``INVALID_ARGUMENT``. + This message has `oneof`_ fields (mutually exclusive fields). + For each oneof, at most one member field can be set at the same time. + Setting any member of the oneof automatically clears all other + members. .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields @@ -436,6 +440,11 @@ class AppendRowsRequest(proto.Message): proto_rows (google.cloud.bigquery_storage_v1.types.AppendRowsRequest.ProtoData): Rows in proto format. + This field is a member of `oneof`_ ``rows``. + arrow_rows (google.cloud.bigquery_storage_v1.types.AppendRowsRequest.ArrowData): + Rows in arrow format. This is an experimental + feature only selected for allowlisted customers. + This field is a member of `oneof`_ ``rows``. trace_id (str): Id set by client to annotate its identity. @@ -496,6 +505,31 @@ class MissingValueInterpretation(proto.Enum): NULL_VALUE = 1 DEFAULT_VALUE = 2 + class ArrowData(proto.Message): + r"""Arrow schema and data. + Arrow format is an experimental feature only selected for + allowlisted customers. + + Attributes: + writer_schema (google.cloud.bigquery_storage_v1.types.ArrowSchema): + Optional. Arrow Schema used to serialize the + data. + rows (google.cloud.bigquery_storage_v1.types.ArrowRecordBatch): + Required. Serialized row data in Arrow + format. + """ + + writer_schema: arrow.ArrowSchema = proto.Field( + proto.MESSAGE, + number=1, + message=arrow.ArrowSchema, + ) + rows: arrow.ArrowRecordBatch = proto.Field( + proto.MESSAGE, + number=2, + message=arrow.ArrowRecordBatch, + ) + class ProtoData(proto.Message): r"""ProtoData contains the data rows and schema when constructing append requests. @@ -544,6 +578,12 @@ class ProtoData(proto.Message): oneof="rows", message=ProtoData, ) + arrow_rows: ArrowData = proto.Field( + proto.MESSAGE, + number=5, + oneof="rows", + message=ArrowData, + ) trace_id: str = proto.Field( proto.STRING, number=6, diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/README.rst b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/README.rst new file mode 100644 index 00000000..ff0f177a --- /dev/null +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/README.rst @@ -0,0 +1,9 @@ + +transport inheritance structure +_______________________________ + +`MetastorePartitionServiceTransport` is the ABC for all transports. +- public child `MetastorePartitionServiceGrpcTransport` for sync gRPC transport (defined in `grpc.py`). +- public child `MetastorePartitionServiceGrpcAsyncIOTransport` for async gRPC transport (defined in `grpc_asyncio.py`). +- private child `_BaseMetastorePartitionServiceRestTransport` for base REST transport with inner classes `_BaseMETHOD` (defined in `rest_base.py`). +- public child `MetastorePartitionServiceRestTransport` for sync REST transport with inner classes `METHOD` derived from the parent's corresponding `_BaseMETHOD` classes (defined in `rest.py`). diff --git a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py index 3ed1e0c2..10e3b144 100644 --- a/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py +++ b/google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/transports/grpc_asyncio.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -230,6 +231,9 @@ def __init__( ) # Wrap messages. This must be done after self._grpc_channel exists + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) self._prep_wrapped_messages(client_info) @property @@ -408,7 +412,7 @@ def stream_metastore_partitions( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.batch_create_metastore_partitions: gapic_v1.method_async.wrap_method( + self.batch_create_metastore_partitions: self._wrap_method( self.batch_create_metastore_partitions, default_retry=retries.AsyncRetry( initial=0.1, @@ -422,7 +426,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=240.0, client_info=client_info, ), - self.batch_delete_metastore_partitions: gapic_v1.method_async.wrap_method( + self.batch_delete_metastore_partitions: self._wrap_method( self.batch_delete_metastore_partitions, default_retry=retries.AsyncRetry( initial=0.1, @@ -436,7 +440,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=240.0, client_info=client_info, ), - self.batch_update_metastore_partitions: gapic_v1.method_async.wrap_method( + self.batch_update_metastore_partitions: self._wrap_method( self.batch_update_metastore_partitions, default_retry=retries.AsyncRetry( initial=0.1, @@ -450,7 +454,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=240.0, client_info=client_info, ), - self.list_metastore_partitions: gapic_v1.method_async.wrap_method( + self.list_metastore_partitions: self._wrap_method( self.list_metastore_partitions, default_retry=retries.AsyncRetry( initial=0.1, @@ -464,15 +468,24 @@ def _prep_wrapped_messages(self, client_info): default_timeout=240.0, client_info=client_info, ), - self.stream_metastore_partitions: gapic_v1.method_async.wrap_method( + self.stream_metastore_partitions: self._wrap_method( self.stream_metastore_partitions, default_timeout=240.0, client_info=client_info, ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): return self.grpc_channel.close() + @property + def kind(self) -> str: + return "grpc_asyncio" + __all__ = ("MetastorePartitionServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/README.rst b/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/README.rst new file mode 100644 index 00000000..687d374a --- /dev/null +++ b/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/README.rst @@ -0,0 +1,9 @@ + +transport inheritance structure +_______________________________ + +`BigQueryReadTransport` is the ABC for all transports. +- public child `BigQueryReadGrpcTransport` for sync gRPC transport (defined in `grpc.py`). +- public child `BigQueryReadGrpcAsyncIOTransport` for async gRPC transport (defined in `grpc_asyncio.py`). +- private child `_BaseBigQueryReadRestTransport` for base REST transport with inner classes `_BaseMETHOD` (defined in `rest_base.py`). +- public child `BigQueryReadRestTransport` for sync REST transport with inner classes `METHOD` derived from the parent's corresponding `_BaseMETHOD` classes (defined in `rest.py`). diff --git a/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/grpc_asyncio.py b/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/grpc_asyncio.py index e8b8a924..88091294 100644 --- a/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/grpc_asyncio.py +++ b/google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/grpc_asyncio.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -231,6 +232,9 @@ def __init__( ) # Wrap messages. This must be done after self._grpc_channel exists + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) self._prep_wrapped_messages(client_info) @property @@ -367,7 +371,7 @@ def split_read_stream( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_read_session: gapic_v1.method_async.wrap_method( + self.create_read_session: self._wrap_method( self.create_read_session, default_retry=retries.AsyncRetry( initial=0.1, @@ -382,7 +386,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.read_rows: gapic_v1.method_async.wrap_method( + self.read_rows: self._wrap_method( self.read_rows, default_retry=retries.AsyncRetry( initial=0.1, @@ -396,7 +400,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=86400.0, client_info=client_info, ), - self.split_read_stream: gapic_v1.method_async.wrap_method( + self.split_read_stream: self._wrap_method( self.split_read_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -413,8 +417,17 @@ def _prep_wrapped_messages(self, client_info): ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): return self.grpc_channel.close() + @property + def kind(self) -> str: + return "grpc_asyncio" + __all__ = ("BigQueryReadGrpcAsyncIOTransport",) diff --git a/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/README.rst b/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/README.rst new file mode 100644 index 00000000..d2db1fed --- /dev/null +++ b/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/README.rst @@ -0,0 +1,9 @@ + +transport inheritance structure +_______________________________ + +`BigQueryWriteTransport` is the ABC for all transports. +- public child `BigQueryWriteGrpcTransport` for sync gRPC transport (defined in `grpc.py`). +- public child `BigQueryWriteGrpcAsyncIOTransport` for async gRPC transport (defined in `grpc_asyncio.py`). +- private child `_BaseBigQueryWriteRestTransport` for base REST transport with inner classes `_BaseMETHOD` (defined in `rest_base.py`). +- public child `BigQueryWriteRestTransport` for sync REST transport with inner classes `METHOD` derived from the parent's corresponding `_BaseMETHOD` classes (defined in `rest.py`). diff --git a/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/grpc_asyncio.py b/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/grpc_asyncio.py index 3abe9037..337877b2 100644 --- a/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/grpc_asyncio.py +++ b/google/cloud/bigquery_storage_v1beta2/services/big_query_write/transports/grpc_asyncio.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -233,6 +234,9 @@ def __init__( ) # Wrap messages. This must be done after self._grpc_channel exists + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) self._prep_wrapped_messages(client_info) @property @@ -446,7 +450,7 @@ def flush_rows( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_write_stream: gapic_v1.method_async.wrap_method( + self.create_write_stream: self._wrap_method( self.create_write_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -462,7 +466,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.append_rows: gapic_v1.method_async.wrap_method( + self.append_rows: self._wrap_method( self.append_rows, default_retry=retries.AsyncRetry( initial=0.1, @@ -477,7 +481,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=86400.0, client_info=client_info, ), - self.get_write_stream: gapic_v1.method_async.wrap_method( + self.get_write_stream: self._wrap_method( self.get_write_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -492,7 +496,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.finalize_write_stream: gapic_v1.method_async.wrap_method( + self.finalize_write_stream: self._wrap_method( self.finalize_write_stream, default_retry=retries.AsyncRetry( initial=0.1, @@ -507,7 +511,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.batch_commit_write_streams: gapic_v1.method_async.wrap_method( + self.batch_commit_write_streams: self._wrap_method( self.batch_commit_write_streams, default_retry=retries.AsyncRetry( initial=0.1, @@ -522,7 +526,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), - self.flush_rows: gapic_v1.method_async.wrap_method( + self.flush_rows: self._wrap_method( self.flush_rows, default_retry=retries.AsyncRetry( initial=0.1, @@ -539,8 +543,17 @@ def _prep_wrapped_messages(self, client_info): ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): return self.grpc_channel.close() + @property + def kind(self) -> str: + return "grpc_asyncio" + __all__ = ("BigQueryWriteGrpcAsyncIOTransport",) diff --git a/scripts/fixup_bigquery_storage_v1_keywords.py b/scripts/fixup_bigquery_storage_v1_keywords.py index 75731cb8..e6c2750a 100644 --- a/scripts/fixup_bigquery_storage_v1_keywords.py +++ b/scripts/fixup_bigquery_storage_v1_keywords.py @@ -39,7 +39,7 @@ def partition( class bigquery_storageCallTransformer(cst.CSTTransformer): CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { - 'append_rows': ('write_stream', 'offset', 'proto_rows', 'trace_id', 'missing_value_interpretations', 'default_missing_value_interpretation', ), + 'append_rows': ('write_stream', 'offset', 'proto_rows', 'arrow_rows', 'trace_id', 'missing_value_interpretations', 'default_missing_value_interpretation', ), 'batch_commit_write_streams': ('parent', 'write_streams', ), 'create_read_session': ('parent', 'read_session', 'max_stream_count', 'preferred_min_stream_count', ), 'create_write_stream': ('parent', 'write_stream', ), diff --git a/tests/unit/gapic/bigquery_storage_v1/test_big_query_read.py b/tests/unit/gapic/bigquery_storage_v1/test_big_query_read.py index 3b481d5c..0847f6d0 100644 --- a/tests/unit/gapic/bigquery_storage_v1/test_big_query_read.py +++ b/tests/unit/gapic/bigquery_storage_v1/test_big_query_read.py @@ -24,8 +24,22 @@ import math +from google.api_core import api_core_version +import grpc +from grpc.experimental import aio +from proto.marshal.rules import wrappers +from proto.marshal.rules.dates import DurationRule, TimestampRule +import pytest + +try: + from google.auth.aio import credentials as ga_credentials_async + + HAS_GOOGLE_AUTH_AIO = True +except ImportError: # pragma: NO COVER + HAS_GOOGLE_AUTH_AIO = False + from google.api_core import gapic_v1, grpc_helpers, grpc_helpers_async, path_template -from google.api_core import api_core_version, client_options +from google.api_core import client_options from google.api_core import exceptions as core_exceptions from google.api_core import retry as retries import google.auth @@ -33,11 +47,6 @@ from google.auth.exceptions import MutualTLSChannelError from google.oauth2 import service_account from google.protobuf import timestamp_pb2 # type: ignore -import grpc -from grpc.experimental import aio -from proto.marshal.rules import wrappers -from proto.marshal.rules.dates import DurationRule, TimestampRule -import pytest from google.cloud.bigquery_storage_v1.services.big_query_read import ( BigQueryReadAsyncClient, @@ -47,10 +56,24 @@ from google.cloud.bigquery_storage_v1.types import arrow, avro, storage, stream +async def mock_async_gen(data, chunk_size=1): + for i in range(0, len(data)): # pragma: NO COVER + chunk = data[i : i + chunk_size] + yield chunk.encode("utf-8") + + def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. +# See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. +def async_anonymous_credentials(): + if HAS_GOOGLE_AUTH_AIO: + return ga_credentials_async.AnonymousCredentials() + return ga_credentials.AnonymousCredentials() + + # If default endpoint is localhost, then default mtls endpoint will be the same. # This method modifies the default endpoint so the client can produce a different # mtls endpoint for endpoint testing purposes. @@ -1124,27 +1147,6 @@ def test_create_read_session(request_type, transport: str = "grpc"): assert response.trace_id == "trace_id_value" -def test_create_read_session_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_read_session), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.create_read_session() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateReadSessionRequest() - - def test_create_read_session_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1214,37 +1216,6 @@ def test_create_read_session_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_create_read_session_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_read_session), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - stream.ReadSession( - name="name_value", - data_format=stream.DataFormat.AVRO, - table="table_value", - estimated_total_bytes_scanned=3076, - estimated_total_physical_file_size=3608, - estimated_row_count=2047, - trace_id="trace_id_value", - ) - ) - response = await client.create_read_session() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateReadSessionRequest() - - @pytest.mark.asyncio async def test_create_read_session_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1253,7 +1224,7 @@ async def test_create_read_session_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1292,7 +1263,7 @@ async def test_create_read_session_async( transport: str = "grpc_asyncio", request_type=storage.CreateReadSessionRequest ): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1374,7 +1345,7 @@ def test_create_read_session_field_headers(): @pytest.mark.asyncio async def test_create_read_session_field_headers_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1456,7 +1427,7 @@ def test_create_read_session_flattened_error(): @pytest.mark.asyncio async def test_create_read_session_flattened_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1493,7 +1464,7 @@ async def test_create_read_session_flattened_async(): @pytest.mark.asyncio async def test_create_read_session_flattened_error_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -1541,25 +1512,6 @@ def test_read_rows(request_type, transport: str = "grpc"): assert isinstance(message, storage.ReadRowsResponse) -def test_read_rows_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.read_rows), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.read_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.ReadRowsRequest() - - def test_read_rows_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1623,35 +1575,13 @@ def test_read_rows_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_read_rows_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.read_rows), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = mock.Mock(aio.UnaryStreamCall, autospec=True) - call.return_value.read = mock.AsyncMock( - side_effect=[storage.ReadRowsResponse()] - ) - response = await client.read_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.ReadRowsRequest() - - @pytest.mark.asyncio async def test_read_rows_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1690,7 +1620,7 @@ async def test_read_rows_async( transport: str = "grpc_asyncio", request_type=storage.ReadRowsRequest ): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1755,7 +1685,7 @@ def test_read_rows_field_headers(): @pytest.mark.asyncio async def test_read_rows_field_headers_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1831,7 +1761,7 @@ def test_read_rows_flattened_error(): @pytest.mark.asyncio async def test_read_rows_flattened_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1862,7 +1792,7 @@ async def test_read_rows_flattened_async(): @pytest.mark.asyncio async def test_read_rows_flattened_error_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -1910,27 +1840,6 @@ def test_split_read_stream(request_type, transport: str = "grpc"): assert isinstance(response, storage.SplitReadStreamResponse) -def test_split_read_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.split_read_stream), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.split_read_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.SplitReadStreamRequest() - - def test_split_read_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1998,29 +1907,6 @@ def test_split_read_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_split_read_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.split_read_stream), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.SplitReadStreamResponse() - ) - response = await client.split_read_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.SplitReadStreamRequest() - - @pytest.mark.asyncio async def test_split_read_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2029,7 +1915,7 @@ async def test_split_read_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2068,7 +1954,7 @@ async def test_split_read_stream_async( transport: str = "grpc_asyncio", request_type=storage.SplitReadStreamRequest ): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2135,7 +2021,7 @@ def test_split_read_stream_field_headers(): @pytest.mark.asyncio async def test_split_read_stream_field_headers_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2257,17 +2143,187 @@ def test_transport_adc(transport_class): adc.assert_called_once() -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - ], -) -def test_transport_kind(transport_name): - transport = BigQueryReadClient.get_transport_class(transport_name)( +def test_transport_kind_grpc(): + transport = BigQueryReadClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_read_session_empty_call_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_read_session), "__call__" + ) as call: + call.return_value = stream.ReadSession() + client.create_read_session(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateReadSessionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_read_rows_empty_call_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.read_rows), "__call__") as call: + call.return_value = iter([storage.ReadRowsResponse()]) + client.read_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.ReadRowsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_split_read_stream_empty_call_grpc(): + client = BigQueryReadClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", ) - assert transport.kind == transport_name + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.split_read_stream), "__call__" + ) as call: + call.return_value = storage.SplitReadStreamResponse() + client.split_read_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.SplitReadStreamRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = BigQueryReadAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_read_session_empty_call_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_read_session), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + stream.ReadSession( + name="name_value", + data_format=stream.DataFormat.AVRO, + table="table_value", + estimated_total_bytes_scanned=3076, + estimated_total_physical_file_size=3608, + estimated_row_count=2047, + trace_id="trace_id_value", + ) + ) + await client.create_read_session(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateReadSessionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_read_rows_empty_call_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.read_rows), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = mock.Mock(aio.UnaryStreamCall, autospec=True) + call.return_value.read = mock.AsyncMock( + side_effect=[storage.ReadRowsResponse()] + ) + await client.read_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.ReadRowsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_split_read_stream_empty_call_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.split_read_stream), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.SplitReadStreamResponse() + ) + await client.split_read_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.SplitReadStreamRequest() + + assert args[0] == request_msg def test_transport_grpc_default(): @@ -2859,37 +2915,31 @@ def test_client_with_default_client_info(): prep.assert_called_once_with(client_info) +def test_transport_close_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + @pytest.mark.asyncio -async def test_transport_close_async(): +async def test_transport_close_grpc_asyncio(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", + credentials=async_anonymous_credentials(), transport="grpc_asyncio" ) with mock.patch.object( - type(getattr(client.transport, "grpc_channel")), "close" + type(getattr(client.transport, "_grpc_channel")), "close" ) as close: async with client: close.assert_not_called() close.assert_called_once() -def test_transport_close(): - transports = { - "grpc": "_grpc_channel", - } - - for transport, close_name in transports.items(): - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - with mock.patch.object( - type(getattr(client.transport, close_name)), "close" - ) as close: - with client: - close.assert_not_called() - close.assert_called_once() - - def test_client_ctx(): transports = [ "grpc", diff --git a/tests/unit/gapic/bigquery_storage_v1/test_big_query_write.py b/tests/unit/gapic/bigquery_storage_v1/test_big_query_write.py index cb2372c3..6919f64f 100644 --- a/tests/unit/gapic/bigquery_storage_v1/test_big_query_write.py +++ b/tests/unit/gapic/bigquery_storage_v1/test_big_query_write.py @@ -24,8 +24,22 @@ import math +from google.api_core import api_core_version +import grpc +from grpc.experimental import aio +from proto.marshal.rules import wrappers +from proto.marshal.rules.dates import DurationRule, TimestampRule +import pytest + +try: + from google.auth.aio import credentials as ga_credentials_async + + HAS_GOOGLE_AUTH_AIO = True +except ImportError: # pragma: NO COVER + HAS_GOOGLE_AUTH_AIO = False + from google.api_core import gapic_v1, grpc_helpers, grpc_helpers_async, path_template -from google.api_core import api_core_version, client_options +from google.api_core import client_options from google.api_core import exceptions as core_exceptions from google.api_core import retry as retries import google.auth @@ -36,24 +50,39 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.protobuf import wrappers_pb2 # type: ignore from google.rpc import status_pb2 # type: ignore -import grpc -from grpc.experimental import aio -from proto.marshal.rules import wrappers -from proto.marshal.rules.dates import DurationRule, TimestampRule -import pytest from google.cloud.bigquery_storage_v1.services.big_query_write import ( BigQueryWriteAsyncClient, BigQueryWriteClient, transports, ) -from google.cloud.bigquery_storage_v1.types import protobuf, storage, stream, table +from google.cloud.bigquery_storage_v1.types import ( + arrow, + protobuf, + storage, + stream, + table, +) + + +async def mock_async_gen(data, chunk_size=1): + for i in range(0, len(data)): # pragma: NO COVER + chunk = data[i : i + chunk_size] + yield chunk.encode("utf-8") def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. +# See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. +def async_anonymous_credentials(): + if HAS_GOOGLE_AUTH_AIO: + return ga_credentials_async.AnonymousCredentials() + return ga_credentials.AnonymousCredentials() + + # If default endpoint is localhost, then default mtls endpoint will be the same. # This method modifies the default endpoint so the client can produce a different # mtls endpoint for endpoint testing purposes. @@ -1141,27 +1170,6 @@ def test_create_write_stream(request_type, transport: str = "grpc"): assert response.location == "location_value" -def test_create_write_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_write_stream), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.create_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateWriteStreamRequest() - - def test_create_write_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1231,34 +1239,6 @@ def test_create_write_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_create_write_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_write_stream), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - stream.WriteStream( - name="name_value", - type_=stream.WriteStream.Type.COMMITTED, - write_mode=stream.WriteStream.WriteMode.INSERT, - location="location_value", - ) - ) - response = await client.create_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateWriteStreamRequest() - - @pytest.mark.asyncio async def test_create_write_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1267,7 +1247,7 @@ async def test_create_write_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1306,7 +1286,7 @@ async def test_create_write_stream_async( transport: str = "grpc_asyncio", request_type=storage.CreateWriteStreamRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1382,7 +1362,7 @@ def test_create_write_stream_field_headers(): @pytest.mark.asyncio async def test_create_write_stream_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1459,7 +1439,7 @@ def test_create_write_stream_flattened_error(): @pytest.mark.asyncio async def test_create_write_stream_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1492,7 +1472,7 @@ async def test_create_write_stream_flattened_async(): @pytest.mark.asyncio async def test_create_write_stream_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -1582,7 +1562,7 @@ async def test_append_rows_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1621,7 +1601,7 @@ async def test_append_rows_async( transport: str = "grpc_asyncio", request_type=storage.AppendRowsRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1696,25 +1676,6 @@ def test_get_write_stream(request_type, transport: str = "grpc"): assert response.location == "location_value" -def test_get_write_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.get_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.GetWriteStreamRequest() - - def test_get_write_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1780,32 +1741,6 @@ def test_get_write_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_get_write_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - stream.WriteStream( - name="name_value", - type_=stream.WriteStream.Type.COMMITTED, - write_mode=stream.WriteStream.WriteMode.INSERT, - location="location_value", - ) - ) - response = await client.get_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.GetWriteStreamRequest() - - @pytest.mark.asyncio async def test_get_write_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1814,7 +1749,7 @@ async def test_get_write_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1853,7 +1788,7 @@ async def test_get_write_stream_async( transport: str = "grpc_asyncio", request_type=storage.GetWriteStreamRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1925,7 +1860,7 @@ def test_get_write_stream_field_headers(): @pytest.mark.asyncio async def test_get_write_stream_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1993,7 +1928,7 @@ def test_get_write_stream_flattened_error(): @pytest.mark.asyncio async def test_get_write_stream_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2020,7 +1955,7 @@ async def test_get_write_stream_flattened_async(): @pytest.mark.asyncio async def test_get_write_stream_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2070,27 +2005,6 @@ def test_finalize_write_stream(request_type, transport: str = "grpc"): assert response.row_count == 992 -def test_finalize_write_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.finalize_write_stream), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.finalize_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FinalizeWriteStreamRequest() - - def test_finalize_write_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2161,31 +2075,6 @@ def test_finalize_write_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_finalize_write_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.finalize_write_stream), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.FinalizeWriteStreamResponse( - row_count=992, - ) - ) - response = await client.finalize_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FinalizeWriteStreamRequest() - - @pytest.mark.asyncio async def test_finalize_write_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2194,7 +2083,7 @@ async def test_finalize_write_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2233,7 +2122,7 @@ async def test_finalize_write_stream_async( transport: str = "grpc_asyncio", request_type=storage.FinalizeWriteStreamRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2303,7 +2192,7 @@ def test_finalize_write_stream_field_headers(): @pytest.mark.asyncio async def test_finalize_write_stream_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2377,7 +2266,7 @@ def test_finalize_write_stream_flattened_error(): @pytest.mark.asyncio async def test_finalize_write_stream_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2408,7 +2297,7 @@ async def test_finalize_write_stream_flattened_async(): @pytest.mark.asyncio async def test_finalize_write_stream_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2455,27 +2344,6 @@ def test_batch_commit_write_streams(request_type, transport: str = "grpc"): assert isinstance(response, storage.BatchCommitWriteStreamsResponse) -def test_batch_commit_write_streams_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_commit_write_streams), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_commit_write_streams() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.BatchCommitWriteStreamsRequest() - - def test_batch_commit_write_streams_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2546,29 +2414,6 @@ def test_batch_commit_write_streams_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_batch_commit_write_streams_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_commit_write_streams), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.BatchCommitWriteStreamsResponse() - ) - response = await client.batch_commit_write_streams() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.BatchCommitWriteStreamsRequest() - - @pytest.mark.asyncio async def test_batch_commit_write_streams_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2577,7 +2422,7 @@ async def test_batch_commit_write_streams_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2616,7 +2461,7 @@ async def test_batch_commit_write_streams_async( transport: str = "grpc_asyncio", request_type=storage.BatchCommitWriteStreamsRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2683,7 +2528,7 @@ def test_batch_commit_write_streams_field_headers(): @pytest.mark.asyncio async def test_batch_commit_write_streams_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2757,7 +2602,7 @@ def test_batch_commit_write_streams_flattened_error(): @pytest.mark.asyncio async def test_batch_commit_write_streams_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2788,7 +2633,7 @@ async def test_batch_commit_write_streams_flattened_async(): @pytest.mark.asyncio async def test_batch_commit_write_streams_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2836,25 +2681,6 @@ def test_flush_rows(request_type, transport: str = "grpc"): assert response.offset == 647 -def test_flush_rows_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.flush_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FlushRowsRequest() - - def test_flush_rows_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2918,36 +2744,13 @@ def test_flush_rows_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_flush_rows_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.FlushRowsResponse( - offset=647, - ) - ) - response = await client.flush_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FlushRowsRequest() - - @pytest.mark.asyncio async def test_flush_rows_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2986,7 +2789,7 @@ async def test_flush_rows_async( transport: str = "grpc_asyncio", request_type=storage.FlushRowsRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -3052,7 +2855,7 @@ def test_flush_rows_field_headers(): @pytest.mark.asyncio async def test_flush_rows_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -3122,7 +2925,7 @@ def test_flush_rows_flattened_error(): @pytest.mark.asyncio async def test_flush_rows_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -3151,7 +2954,7 @@ async def test_flush_rows_flattened_async(): @pytest.mark.asyncio async def test_flush_rows_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -3254,17 +3057,288 @@ def test_transport_adc(transport_class): adc.assert_called_once() -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - ], -) -def test_transport_kind(transport_name): - transport = BigQueryWriteClient.get_transport_class(transport_name)( +def test_transport_kind_grpc(): + transport = BigQueryWriteClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_write_stream_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_write_stream), "__call__" + ) as call: + call.return_value = stream.WriteStream() + client.create_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_write_stream_empty_call_grpc(): + client = BigQueryWriteClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: + call.return_value = stream.WriteStream() + client.get_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.GetWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_finalize_write_stream_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.finalize_write_stream), "__call__" + ) as call: + call.return_value = storage.FinalizeWriteStreamResponse() + client.finalize_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FinalizeWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_batch_commit_write_streams_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_commit_write_streams), "__call__" + ) as call: + call.return_value = storage.BatchCommitWriteStreamsResponse() + client.batch_commit_write_streams(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.BatchCommitWriteStreamsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_flush_rows_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: + call.return_value = storage.FlushRowsResponse() + client.flush_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FlushRowsRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = BigQueryWriteAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_write_stream_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_write_stream), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + stream.WriteStream( + name="name_value", + type_=stream.WriteStream.Type.COMMITTED, + write_mode=stream.WriteStream.WriteMode.INSERT, + location="location_value", + ) + ) + await client.create_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_write_stream_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + stream.WriteStream( + name="name_value", + type_=stream.WriteStream.Type.COMMITTED, + write_mode=stream.WriteStream.WriteMode.INSERT, + location="location_value", + ) + ) + await client.get_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.GetWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_finalize_write_stream_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", ) - assert transport.kind == transport_name + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.finalize_write_stream), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.FinalizeWriteStreamResponse( + row_count=992, + ) + ) + await client.finalize_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FinalizeWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_batch_commit_write_streams_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_commit_write_streams), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.BatchCommitWriteStreamsResponse() + ) + await client.batch_commit_write_streams(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.BatchCommitWriteStreamsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_flush_rows_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.FlushRowsResponse( + offset=647, + ) + ) + await client.flush_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FlushRowsRequest() + + assert args[0] == request_msg def test_transport_grpc_default(): @@ -3850,37 +3924,31 @@ def test_client_with_default_client_info(): prep.assert_called_once_with(client_info) +def test_transport_close_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + @pytest.mark.asyncio -async def test_transport_close_async(): +async def test_transport_close_grpc_asyncio(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", + credentials=async_anonymous_credentials(), transport="grpc_asyncio" ) with mock.patch.object( - type(getattr(client.transport, "grpc_channel")), "close" + type(getattr(client.transport, "_grpc_channel")), "close" ) as close: async with client: close.assert_not_called() close.assert_called_once() -def test_transport_close(): - transports = { - "grpc": "_grpc_channel", - } - - for transport, close_name in transports.items(): - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - with mock.patch.object( - type(getattr(client.transport, close_name)), "close" - ) as close: - with client: - close.assert_not_called() - close.assert_called_once() - - def test_client_ctx(): transports = [ "grpc", diff --git a/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py b/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py index 1dd722d3..69b7e0fe 100644 --- a/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py +++ b/tests/unit/gapic/bigquery_storage_v1alpha/test_metastore_partition_service.py @@ -24,8 +24,22 @@ import math +from google.api_core import api_core_version +import grpc +from grpc.experimental import aio +from proto.marshal.rules import wrappers +from proto.marshal.rules.dates import DurationRule, TimestampRule +import pytest + +try: + from google.auth.aio import credentials as ga_credentials_async + + HAS_GOOGLE_AUTH_AIO = True +except ImportError: # pragma: NO COVER + HAS_GOOGLE_AUTH_AIO = False + from google.api_core import gapic_v1, grpc_helpers, grpc_helpers_async, path_template -from google.api_core import api_core_version, client_options +from google.api_core import client_options from google.api_core import exceptions as core_exceptions from google.api_core import retry as retries import google.auth @@ -34,11 +48,6 @@ from google.oauth2 import service_account from google.protobuf import field_mask_pb2 # type: ignore from google.protobuf import timestamp_pb2 # type: ignore -import grpc -from grpc.experimental import aio -from proto.marshal.rules import wrappers -from proto.marshal.rules.dates import DurationRule, TimestampRule -import pytest from google.cloud.bigquery_storage_v1alpha.services.metastore_partition_service import ( MetastorePartitionServiceAsyncClient, @@ -48,10 +57,24 @@ from google.cloud.bigquery_storage_v1alpha.types import metastore_partition, partition +async def mock_async_gen(data, chunk_size=1): + for i in range(0, len(data)): # pragma: NO COVER + chunk = data[i : i + chunk_size] + yield chunk.encode("utf-8") + + def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. +# See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. +def async_anonymous_credentials(): + if HAS_GOOGLE_AUTH_AIO: + return ga_credentials_async.AnonymousCredentials() + return ga_credentials.AnonymousCredentials() + + # If default endpoint is localhost, then default mtls endpoint will be the same. # This method modifies the default endpoint so the client can produce a different # mtls endpoint for endpoint testing purposes. @@ -1192,27 +1215,6 @@ def test_batch_create_metastore_partitions(request_type, transport: str = "grpc" ) -def test_batch_create_metastore_partitions_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_create_metastore_partitions), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_create_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.BatchCreateMetastorePartitionsRequest() - - def test_batch_create_metastore_partitions_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1283,29 +1285,6 @@ def test_batch_create_metastore_partitions_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_batch_create_metastore_partitions_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_create_metastore_partitions), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - metastore_partition.BatchCreateMetastorePartitionsResponse() - ) - response = await client.batch_create_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.BatchCreateMetastorePartitionsRequest() - - @pytest.mark.asyncio async def test_batch_create_metastore_partitions_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1314,7 +1293,7 @@ async def test_batch_create_metastore_partitions_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1354,7 +1333,7 @@ async def test_batch_create_metastore_partitions_async( request_type=metastore_partition.BatchCreateMetastorePartitionsRequest, ): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1423,7 +1402,7 @@ def test_batch_create_metastore_partitions_field_headers(): @pytest.mark.asyncio async def test_batch_create_metastore_partitions_field_headers_async(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1489,27 +1468,6 @@ def test_batch_delete_metastore_partitions(request_type, transport: str = "grpc" assert response is None -def test_batch_delete_metastore_partitions_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_delete_metastore_partitions), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_delete_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.BatchDeleteMetastorePartitionsRequest() - - def test_batch_delete_metastore_partitions_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1580,27 +1538,6 @@ def test_batch_delete_metastore_partitions_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_batch_delete_metastore_partitions_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_delete_metastore_partitions), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) - response = await client.batch_delete_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.BatchDeleteMetastorePartitionsRequest() - - @pytest.mark.asyncio async def test_batch_delete_metastore_partitions_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1609,7 +1546,7 @@ async def test_batch_delete_metastore_partitions_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1649,7 +1586,7 @@ async def test_batch_delete_metastore_partitions_async( request_type=metastore_partition.BatchDeleteMetastorePartitionsRequest, ): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1714,7 +1651,7 @@ def test_batch_delete_metastore_partitions_field_headers(): @pytest.mark.asyncio async def test_batch_delete_metastore_partitions_field_headers_async(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1780,27 +1717,6 @@ def test_batch_update_metastore_partitions(request_type, transport: str = "grpc" ) -def test_batch_update_metastore_partitions_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_update_metastore_partitions), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_update_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.BatchUpdateMetastorePartitionsRequest() - - def test_batch_update_metastore_partitions_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1871,29 +1787,6 @@ def test_batch_update_metastore_partitions_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_batch_update_metastore_partitions_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_update_metastore_partitions), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - metastore_partition.BatchUpdateMetastorePartitionsResponse() - ) - response = await client.batch_update_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.BatchUpdateMetastorePartitionsRequest() - - @pytest.mark.asyncio async def test_batch_update_metastore_partitions_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1902,7 +1795,7 @@ async def test_batch_update_metastore_partitions_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1942,7 +1835,7 @@ async def test_batch_update_metastore_partitions_async( request_type=metastore_partition.BatchUpdateMetastorePartitionsRequest, ): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2011,7 +1904,7 @@ def test_batch_update_metastore_partitions_field_headers(): @pytest.mark.asyncio async def test_batch_update_metastore_partitions_field_headers_async(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2077,27 +1970,6 @@ def test_list_metastore_partitions(request_type, transport: str = "grpc"): assert isinstance(response, metastore_partition.ListMetastorePartitionsResponse) -def test_list_metastore_partitions_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.list_metastore_partitions), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.list_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.ListMetastorePartitionsRequest() - - def test_list_metastore_partitions_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2170,29 +2042,6 @@ def test_list_metastore_partitions_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_list_metastore_partitions_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.list_metastore_partitions), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - metastore_partition.ListMetastorePartitionsResponse() - ) - response = await client.list_metastore_partitions() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == metastore_partition.ListMetastorePartitionsRequest() - - @pytest.mark.asyncio async def test_list_metastore_partitions_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2201,7 +2050,7 @@ async def test_list_metastore_partitions_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2241,7 +2090,7 @@ async def test_list_metastore_partitions_async( request_type=metastore_partition.ListMetastorePartitionsRequest, ): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2308,7 +2157,7 @@ def test_list_metastore_partitions_field_headers(): @pytest.mark.asyncio async def test_list_metastore_partitions_field_headers_async(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2382,7 +2231,7 @@ def test_list_metastore_partitions_flattened_error(): @pytest.mark.asyncio async def test_list_metastore_partitions_flattened_async(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2413,7 +2262,7 @@ async def test_list_metastore_partitions_flattened_async(): @pytest.mark.asyncio async def test_list_metastore_partitions_flattened_error_async(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2513,7 +2362,7 @@ async def test_stream_metastore_partitions_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2553,7 +2402,7 @@ async def test_stream_metastore_partitions_async( request_type=metastore_partition.StreamMetastorePartitionsRequest, ): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2679,17 +2528,230 @@ def test_transport_adc(transport_class): adc.assert_called_once() -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - ], -) -def test_transport_kind(transport_name): - transport = MetastorePartitionServiceClient.get_transport_class(transport_name)( +def test_transport_kind_grpc(): + transport = MetastorePartitionServiceClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_batch_create_metastore_partitions_empty_call_grpc(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + call.return_value = metastore_partition.BatchCreateMetastorePartitionsResponse() + client.batch_create_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.BatchCreateMetastorePartitionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_batch_delete_metastore_partitions_empty_call_grpc(): + client = MetastorePartitionServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + call.return_value = None + client.batch_delete_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.BatchDeleteMetastorePartitionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_batch_update_metastore_partitions_empty_call_grpc(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + call.return_value = metastore_partition.BatchUpdateMetastorePartitionsResponse() + client.batch_update_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.BatchUpdateMetastorePartitionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_metastore_partitions_empty_call_grpc(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + call.return_value = metastore_partition.ListMetastorePartitionsResponse() + client.list_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.ListMetastorePartitionsRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = MetastorePartitionServiceAsyncClient.get_transport_class( + "grpc_asyncio" + )(credentials=async_anonymous_credentials()) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = MetastorePartitionServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_batch_create_metastore_partitions_empty_call_grpc_asyncio(): + client = MetastorePartitionServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", ) - assert transport.kind == transport_name + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_create_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchCreateMetastorePartitionsResponse() + ) + await client.batch_create_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.BatchCreateMetastorePartitionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_batch_delete_metastore_partitions_empty_call_grpc_asyncio(): + client = MetastorePartitionServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_delete_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.batch_delete_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.BatchDeleteMetastorePartitionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_batch_update_metastore_partitions_empty_call_grpc_asyncio(): + client = MetastorePartitionServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_update_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.BatchUpdateMetastorePartitionsResponse() + ) + await client.batch_update_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.BatchUpdateMetastorePartitionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_metastore_partitions_empty_call_grpc_asyncio(): + client = MetastorePartitionServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_metastore_partitions), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + metastore_partition.ListMetastorePartitionsResponse() + ) + await client.list_metastore_partitions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = metastore_partition.ListMetastorePartitionsRequest() + + assert args[0] == request_msg def test_transport_grpc_default(): @@ -3276,37 +3338,31 @@ def test_client_with_default_client_info(): prep.assert_called_once_with(client_info) +def test_transport_close_grpc(): + client = MetastorePartitionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + @pytest.mark.asyncio -async def test_transport_close_async(): +async def test_transport_close_grpc_asyncio(): client = MetastorePartitionServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", + credentials=async_anonymous_credentials(), transport="grpc_asyncio" ) with mock.patch.object( - type(getattr(client.transport, "grpc_channel")), "close" + type(getattr(client.transport, "_grpc_channel")), "close" ) as close: async with client: close.assert_not_called() close.assert_called_once() -def test_transport_close(): - transports = { - "grpc": "_grpc_channel", - } - - for transport, close_name in transports.items(): - client = MetastorePartitionServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - with mock.patch.object( - type(getattr(client.transport, close_name)), "close" - ) as close: - with client: - close.assert_not_called() - close.assert_called_once() - - def test_client_ctx(): transports = [ "grpc", diff --git a/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_read.py b/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_read.py index abc361a8..a985fb67 100644 --- a/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_read.py +++ b/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_read.py @@ -24,8 +24,22 @@ import math +from google.api_core import api_core_version +import grpc +from grpc.experimental import aio +from proto.marshal.rules import wrappers +from proto.marshal.rules.dates import DurationRule, TimestampRule +import pytest + +try: + from google.auth.aio import credentials as ga_credentials_async + + HAS_GOOGLE_AUTH_AIO = True +except ImportError: # pragma: NO COVER + HAS_GOOGLE_AUTH_AIO = False + from google.api_core import gapic_v1, grpc_helpers, grpc_helpers_async, path_template -from google.api_core import api_core_version, client_options +from google.api_core import client_options from google.api_core import exceptions as core_exceptions from google.api_core import retry as retries import google.auth @@ -33,11 +47,6 @@ from google.auth.exceptions import MutualTLSChannelError from google.oauth2 import service_account from google.protobuf import timestamp_pb2 # type: ignore -import grpc -from grpc.experimental import aio -from proto.marshal.rules import wrappers -from proto.marshal.rules.dates import DurationRule, TimestampRule -import pytest from google.cloud.bigquery_storage_v1beta2.services.big_query_read import ( BigQueryReadAsyncClient, @@ -47,10 +56,24 @@ from google.cloud.bigquery_storage_v1beta2.types import arrow, avro, storage, stream +async def mock_async_gen(data, chunk_size=1): + for i in range(0, len(data)): # pragma: NO COVER + chunk = data[i : i + chunk_size] + yield chunk.encode("utf-8") + + def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. +# See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. +def async_anonymous_credentials(): + if HAS_GOOGLE_AUTH_AIO: + return ga_credentials_async.AnonymousCredentials() + return ga_credentials.AnonymousCredentials() + + # If default endpoint is localhost, then default mtls endpoint will be the same. # This method modifies the default endpoint so the client can produce a different # mtls endpoint for endpoint testing purposes. @@ -1116,27 +1139,6 @@ def test_create_read_session(request_type, transport: str = "grpc"): assert response.table == "table_value" -def test_create_read_session_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_read_session), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.create_read_session() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateReadSessionRequest() - - def test_create_read_session_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1206,33 +1208,6 @@ def test_create_read_session_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_create_read_session_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_read_session), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - stream.ReadSession( - name="name_value", - data_format=stream.DataFormat.AVRO, - table="table_value", - ) - ) - response = await client.create_read_session() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateReadSessionRequest() - - @pytest.mark.asyncio async def test_create_read_session_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1241,7 +1216,7 @@ async def test_create_read_session_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1280,7 +1255,7 @@ async def test_create_read_session_async( transport: str = "grpc_asyncio", request_type=storage.CreateReadSessionRequest ): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1354,7 +1329,7 @@ def test_create_read_session_field_headers(): @pytest.mark.asyncio async def test_create_read_session_field_headers_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1436,7 +1411,7 @@ def test_create_read_session_flattened_error(): @pytest.mark.asyncio async def test_create_read_session_flattened_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1473,7 +1448,7 @@ async def test_create_read_session_flattened_async(): @pytest.mark.asyncio async def test_create_read_session_flattened_error_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -1521,25 +1496,6 @@ def test_read_rows(request_type, transport: str = "grpc"): assert isinstance(message, storage.ReadRowsResponse) -def test_read_rows_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.read_rows), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.read_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.ReadRowsRequest() - - def test_read_rows_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1603,35 +1559,13 @@ def test_read_rows_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_read_rows_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.read_rows), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = mock.Mock(aio.UnaryStreamCall, autospec=True) - call.return_value.read = mock.AsyncMock( - side_effect=[storage.ReadRowsResponse()] - ) - response = await client.read_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.ReadRowsRequest() - - @pytest.mark.asyncio async def test_read_rows_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1670,7 +1604,7 @@ async def test_read_rows_async( transport: str = "grpc_asyncio", request_type=storage.ReadRowsRequest ): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1735,7 +1669,7 @@ def test_read_rows_field_headers(): @pytest.mark.asyncio async def test_read_rows_field_headers_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1811,7 +1745,7 @@ def test_read_rows_flattened_error(): @pytest.mark.asyncio async def test_read_rows_flattened_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1842,7 +1776,7 @@ async def test_read_rows_flattened_async(): @pytest.mark.asyncio async def test_read_rows_flattened_error_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -1890,27 +1824,6 @@ def test_split_read_stream(request_type, transport: str = "grpc"): assert isinstance(response, storage.SplitReadStreamResponse) -def test_split_read_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.split_read_stream), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.split_read_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.SplitReadStreamRequest() - - def test_split_read_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1978,29 +1891,6 @@ def test_split_read_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_split_read_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.split_read_stream), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.SplitReadStreamResponse() - ) - response = await client.split_read_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.SplitReadStreamRequest() - - @pytest.mark.asyncio async def test_split_read_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2009,7 +1899,7 @@ async def test_split_read_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2048,7 +1938,7 @@ async def test_split_read_stream_async( transport: str = "grpc_asyncio", request_type=storage.SplitReadStreamRequest ): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2115,7 +2005,7 @@ def test_split_read_stream_field_headers(): @pytest.mark.asyncio async def test_split_read_stream_field_headers_async(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2237,17 +2127,183 @@ def test_transport_adc(transport_class): adc.assert_called_once() -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - ], -) -def test_transport_kind(transport_name): - transport = BigQueryReadClient.get_transport_class(transport_name)( +def test_transport_kind_grpc(): + transport = BigQueryReadClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_read_session_empty_call_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_read_session), "__call__" + ) as call: + call.return_value = stream.ReadSession() + client.create_read_session(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateReadSessionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_read_rows_empty_call_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.read_rows), "__call__") as call: + call.return_value = iter([storage.ReadRowsResponse()]) + client.read_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.ReadRowsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_split_read_stream_empty_call_grpc(): + client = BigQueryReadClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.split_read_stream), "__call__" + ) as call: + call.return_value = storage.SplitReadStreamResponse() + client.split_read_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.SplitReadStreamRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = BigQueryReadAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() ) - assert transport.kind == transport_name + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_read_session_empty_call_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_read_session), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + stream.ReadSession( + name="name_value", + data_format=stream.DataFormat.AVRO, + table="table_value", + ) + ) + await client.create_read_session(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateReadSessionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_read_rows_empty_call_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.read_rows), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = mock.Mock(aio.UnaryStreamCall, autospec=True) + call.return_value.read = mock.AsyncMock( + side_effect=[storage.ReadRowsResponse()] + ) + await client.read_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.ReadRowsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_split_read_stream_empty_call_grpc_asyncio(): + client = BigQueryReadAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.split_read_stream), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.SplitReadStreamResponse() + ) + await client.split_read_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.SplitReadStreamRequest() + + assert args[0] == request_msg def test_transport_grpc_default(): @@ -2839,37 +2895,31 @@ def test_client_with_default_client_info(): prep.assert_called_once_with(client_info) +def test_transport_close_grpc(): + client = BigQueryReadClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + @pytest.mark.asyncio -async def test_transport_close_async(): +async def test_transport_close_grpc_asyncio(): client = BigQueryReadAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", + credentials=async_anonymous_credentials(), transport="grpc_asyncio" ) with mock.patch.object( - type(getattr(client.transport, "grpc_channel")), "close" + type(getattr(client.transport, "_grpc_channel")), "close" ) as close: async with client: close.assert_not_called() close.assert_called_once() -def test_transport_close(): - transports = { - "grpc": "_grpc_channel", - } - - for transport, close_name in transports.items(): - client = BigQueryReadClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - with mock.patch.object( - type(getattr(client.transport, close_name)), "close" - ) as close: - with client: - close.assert_not_called() - close.assert_called_once() - - def test_client_ctx(): transports = [ "grpc", diff --git a/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_write.py b/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_write.py index 88a5a663..ddf63d35 100644 --- a/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_write.py +++ b/tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_write.py @@ -24,8 +24,22 @@ import math +from google.api_core import api_core_version +import grpc +from grpc.experimental import aio +from proto.marshal.rules import wrappers +from proto.marshal.rules.dates import DurationRule, TimestampRule +import pytest + +try: + from google.auth.aio import credentials as ga_credentials_async + + HAS_GOOGLE_AUTH_AIO = True +except ImportError: # pragma: NO COVER + HAS_GOOGLE_AUTH_AIO = False + from google.api_core import gapic_v1, grpc_helpers, grpc_helpers_async, path_template -from google.api_core import api_core_version, client_options +from google.api_core import client_options from google.api_core import exceptions as core_exceptions from google.api_core import retry as retries import google.auth @@ -36,11 +50,6 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.protobuf import wrappers_pb2 # type: ignore from google.rpc import status_pb2 # type: ignore -import grpc -from grpc.experimental import aio -from proto.marshal.rules import wrappers -from proto.marshal.rules.dates import DurationRule, TimestampRule -import pytest from google.cloud.bigquery_storage_v1beta2.services.big_query_write import ( BigQueryWriteAsyncClient, @@ -50,10 +59,24 @@ from google.cloud.bigquery_storage_v1beta2.types import protobuf, storage, stream, table +async def mock_async_gen(data, chunk_size=1): + for i in range(0, len(data)): # pragma: NO COVER + chunk = data[i : i + chunk_size] + yield chunk.encode("utf-8") + + def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. +# See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. +def async_anonymous_credentials(): + if HAS_GOOGLE_AUTH_AIO: + return ga_credentials_async.AnonymousCredentials() + return ga_credentials.AnonymousCredentials() + + # If default endpoint is localhost, then default mtls endpoint will be the same. # This method modifies the default endpoint so the client can produce a different # mtls endpoint for endpoint testing purposes. @@ -1137,27 +1160,6 @@ def test_create_write_stream(request_type, transport: str = "grpc"): assert response.type_ == stream.WriteStream.Type.COMMITTED -def test_create_write_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_write_stream), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.create_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateWriteStreamRequest() - - def test_create_write_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1227,32 +1229,6 @@ def test_create_write_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_create_write_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.create_write_stream), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - stream.WriteStream( - name="name_value", - type_=stream.WriteStream.Type.COMMITTED, - ) - ) - response = await client.create_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.CreateWriteStreamRequest() - - @pytest.mark.asyncio async def test_create_write_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1261,7 +1237,7 @@ async def test_create_write_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1300,7 +1276,7 @@ async def test_create_write_stream_async( transport: str = "grpc_asyncio", request_type=storage.CreateWriteStreamRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1372,7 +1348,7 @@ def test_create_write_stream_field_headers(): @pytest.mark.asyncio async def test_create_write_stream_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1449,7 +1425,7 @@ def test_create_write_stream_flattened_error(): @pytest.mark.asyncio async def test_create_write_stream_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1482,7 +1458,7 @@ async def test_create_write_stream_flattened_async(): @pytest.mark.asyncio async def test_create_write_stream_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -1572,7 +1548,7 @@ async def test_append_rows_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1611,7 +1587,7 @@ async def test_append_rows_async( transport: str = "grpc_asyncio", request_type=storage.AppendRowsRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1682,25 +1658,6 @@ def test_get_write_stream(request_type, transport: str = "grpc"): assert response.type_ == stream.WriteStream.Type.COMMITTED -def test_get_write_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.get_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.GetWriteStreamRequest() - - def test_get_write_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -1766,30 +1723,6 @@ def test_get_write_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_get_write_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - stream.WriteStream( - name="name_value", - type_=stream.WriteStream.Type.COMMITTED, - ) - ) - response = await client.get_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.GetWriteStreamRequest() - - @pytest.mark.asyncio async def test_get_write_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -1798,7 +1731,7 @@ async def test_get_write_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1837,7 +1770,7 @@ async def test_get_write_stream_async( transport: str = "grpc_asyncio", request_type=storage.GetWriteStreamRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -1905,7 +1838,7 @@ def test_get_write_stream_field_headers(): @pytest.mark.asyncio async def test_get_write_stream_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -1973,7 +1906,7 @@ def test_get_write_stream_flattened_error(): @pytest.mark.asyncio async def test_get_write_stream_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2000,7 +1933,7 @@ async def test_get_write_stream_flattened_async(): @pytest.mark.asyncio async def test_get_write_stream_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2050,27 +1983,6 @@ def test_finalize_write_stream(request_type, transport: str = "grpc"): assert response.row_count == 992 -def test_finalize_write_stream_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.finalize_write_stream), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.finalize_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FinalizeWriteStreamRequest() - - def test_finalize_write_stream_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2141,31 +2053,6 @@ def test_finalize_write_stream_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_finalize_write_stream_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.finalize_write_stream), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.FinalizeWriteStreamResponse( - row_count=992, - ) - ) - response = await client.finalize_write_stream() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FinalizeWriteStreamRequest() - - @pytest.mark.asyncio async def test_finalize_write_stream_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2174,7 +2061,7 @@ async def test_finalize_write_stream_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2213,7 +2100,7 @@ async def test_finalize_write_stream_async( transport: str = "grpc_asyncio", request_type=storage.FinalizeWriteStreamRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2283,7 +2170,7 @@ def test_finalize_write_stream_field_headers(): @pytest.mark.asyncio async def test_finalize_write_stream_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2357,7 +2244,7 @@ def test_finalize_write_stream_flattened_error(): @pytest.mark.asyncio async def test_finalize_write_stream_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2388,7 +2275,7 @@ async def test_finalize_write_stream_flattened_async(): @pytest.mark.asyncio async def test_finalize_write_stream_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2435,27 +2322,6 @@ def test_batch_commit_write_streams(request_type, transport: str = "grpc"): assert isinstance(response, storage.BatchCommitWriteStreamsResponse) -def test_batch_commit_write_streams_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_commit_write_streams), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_commit_write_streams() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.BatchCommitWriteStreamsRequest() - - def test_batch_commit_write_streams_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2526,29 +2392,6 @@ def test_batch_commit_write_streams_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_batch_commit_write_streams_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_commit_write_streams), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.BatchCommitWriteStreamsResponse() - ) - response = await client.batch_commit_write_streams() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.BatchCommitWriteStreamsRequest() - - @pytest.mark.asyncio async def test_batch_commit_write_streams_async_use_cached_wrapped_rpc( transport: str = "grpc_asyncio", @@ -2557,7 +2400,7 @@ async def test_batch_commit_write_streams_async_use_cached_wrapped_rpc( # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2596,7 +2439,7 @@ async def test_batch_commit_write_streams_async( transport: str = "grpc_asyncio", request_type=storage.BatchCommitWriteStreamsRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2663,7 +2506,7 @@ def test_batch_commit_write_streams_field_headers(): @pytest.mark.asyncio async def test_batch_commit_write_streams_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2737,7 +2580,7 @@ def test_batch_commit_write_streams_flattened_error(): @pytest.mark.asyncio async def test_batch_commit_write_streams_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -2768,7 +2611,7 @@ async def test_batch_commit_write_streams_flattened_async(): @pytest.mark.asyncio async def test_batch_commit_write_streams_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -2816,25 +2659,6 @@ def test_flush_rows(request_type, transport: str = "grpc"): assert response.offset == 647 -def test_flush_rows_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.flush_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FlushRowsRequest() - - def test_flush_rows_non_empty_request_with_auto_populated_field(): # This test is a coverage failsafe to make sure that UUID4 fields are # automatically populated, according to AIP-4235, with non-empty requests. @@ -2898,36 +2722,13 @@ def test_flush_rows_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -@pytest.mark.asyncio -async def test_flush_rows_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - storage.FlushRowsResponse( - offset=647, - ) - ) - response = await client.flush_rows() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == storage.FlushRowsRequest() - - @pytest.mark.asyncio async def test_flush_rows_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -2966,7 +2767,7 @@ async def test_flush_rows_async( transport: str = "grpc_asyncio", request_type=storage.FlushRowsRequest ): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), transport=transport, ) @@ -3032,7 +2833,7 @@ def test_flush_rows_field_headers(): @pytest.mark.asyncio async def test_flush_rows_field_headers_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -3102,7 +2903,7 @@ def test_flush_rows_flattened_error(): @pytest.mark.asyncio async def test_flush_rows_flattened_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -3131,7 +2932,7 @@ async def test_flush_rows_flattened_async(): @pytest.mark.asyncio async def test_flush_rows_flattened_error_async(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened @@ -3234,17 +3035,284 @@ def test_transport_adc(transport_class): adc.assert_called_once() -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - ], -) -def test_transport_kind(transport_name): - transport = BigQueryWriteClient.get_transport_class(transport_name)( +def test_transport_kind_grpc(): + transport = BigQueryWriteClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_write_stream_empty_call_grpc(): + client = BigQueryWriteClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_write_stream), "__call__" + ) as call: + call.return_value = stream.WriteStream() + client.create_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_write_stream_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: + call.return_value = stream.WriteStream() + client.get_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.GetWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_finalize_write_stream_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.finalize_write_stream), "__call__" + ) as call: + call.return_value = storage.FinalizeWriteStreamResponse() + client.finalize_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FinalizeWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_batch_commit_write_streams_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_commit_write_streams), "__call__" + ) as call: + call.return_value = storage.BatchCommitWriteStreamsResponse() + client.batch_commit_write_streams(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.BatchCommitWriteStreamsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_flush_rows_empty_call_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: + call.return_value = storage.FlushRowsResponse() + client.flush_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FlushRowsRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = BigQueryWriteAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_write_stream_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_write_stream), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + stream.WriteStream( + name="name_value", + type_=stream.WriteStream.Type.COMMITTED, + ) + ) + await client.create_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.CreateWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_write_stream_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", ) - assert transport.kind == transport_name + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_write_stream), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + stream.WriteStream( + name="name_value", + type_=stream.WriteStream.Type.COMMITTED, + ) + ) + await client.get_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.GetWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_finalize_write_stream_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.finalize_write_stream), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.FinalizeWriteStreamResponse( + row_count=992, + ) + ) + await client.finalize_write_stream(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FinalizeWriteStreamRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_batch_commit_write_streams_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.batch_commit_write_streams), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.BatchCommitWriteStreamsResponse() + ) + await client.batch_commit_write_streams(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.BatchCommitWriteStreamsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_flush_rows_empty_call_grpc_asyncio(): + client = BigQueryWriteAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.flush_rows), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + storage.FlushRowsResponse( + offset=647, + ) + ) + await client.flush_rows(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = storage.FlushRowsRequest() + + assert args[0] == request_msg def test_transport_grpc_default(): @@ -3830,37 +3898,31 @@ def test_client_with_default_client_info(): prep.assert_called_once_with(client_info) +def test_transport_close_grpc(): + client = BigQueryWriteClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + @pytest.mark.asyncio -async def test_transport_close_async(): +async def test_transport_close_grpc_asyncio(): client = BigQueryWriteAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", + credentials=async_anonymous_credentials(), transport="grpc_asyncio" ) with mock.patch.object( - type(getattr(client.transport, "grpc_channel")), "close" + type(getattr(client.transport, "_grpc_channel")), "close" ) as close: async with client: close.assert_not_called() close.assert_called_once() -def test_transport_close(): - transports = { - "grpc": "_grpc_channel", - } - - for transport, close_name in transports.items(): - client = BigQueryWriteClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - with mock.patch.object( - type(getattr(client.transport, close_name)), "close" - ) as close: - with client: - close.assert_not_called() - close.assert_called_once() - - def test_client_ctx(): transports = [ "grpc",