|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +from testcontainers.core.container import DockerContainer |
| 14 | +from google.cloud.bigquery import Client as BigQueryClient |
| 15 | +from google.api_core.client_options import ClientOptions |
| 16 | +from google.auth.credentials import AnonymousCredentials |
| 17 | + |
| 18 | + |
| 19 | +class BigQueryContainer(DockerContainer): |
| 20 | + """ |
| 21 | + Docker container to emulate BigQuery, based on https://github.com/testcontainers/testcontainers-java/blob/main/modules/gcloud/src/main/java/org/testcontainers/containers/BigQueryEmulatorContainer.java. |
| 22 | + Uses ghcr.io/goccy/bigquery-emulator image by default. |
| 23 | + Example: |
| 24 | +
|
| 25 | + The example will spin up a Google Cloud BigQuery emulator that you can use for integration |
| 26 | + tests. |
| 27 | +
|
| 28 | + .. doctest:: |
| 29 | +
|
| 30 | + >>> from testcontainers.google import BigQueryContainer |
| 31 | + >>> from testcontainers.core.waiting_utils import wait_for_logs |
| 32 | + >>> from google.cloud.bigquery import QueryJobConfig |
| 33 | +
|
| 34 | + >>> with BigQueryContainer(platform="linux/x86_64") as bigquery: |
| 35 | + ... wait_for_logs(bigquery, "gRPC server listening", timeout=60) |
| 36 | + ... client = bigquery.get_client() |
| 37 | + ... result = client.query("SELECT 1", job_config=QueryJobConfig()).result() |
| 38 | + ... print(result.total_rows) |
| 39 | + """ |
| 40 | + def __init__(self, image: str = "ghcr.io/goccy/bigquery-emulator:latest", project: str = "test-project", |
| 41 | + http_port: int = 9050, grpc_port: int = 9060, **kwargs) -> None: |
| 42 | + super(BigQueryContainer, self).__init__(image=image, **kwargs) |
| 43 | + self.project = project |
| 44 | + self.http_port = http_port |
| 45 | + self.grpc_port = grpc_port |
| 46 | + self.with_exposed_ports(http_port, grpc_port) |
| 47 | + command = [ |
| 48 | + "--project", project, |
| 49 | + "--port", str(http_port), |
| 50 | + "--grpc-port", str(grpc_port), |
| 51 | + ] |
| 52 | + self.with_command(' '.join(command)) |
| 53 | + |
| 54 | + def get_emulator_http_endpoint(self) -> str: |
| 55 | + return f"http://{self.get_container_host_ip()}:{self.get_exposed_port(self.http_port)}" |
| 56 | + |
| 57 | + def get_client(self) -> BigQueryClient: |
| 58 | + client_options = ClientOptions(api_endpoint=self.get_emulator_http_endpoint()) |
| 59 | + return BigQueryClient( |
| 60 | + project=self.project, |
| 61 | + client_options=client_options, |
| 62 | + credentials=AnonymousCredentials(), |
| 63 | + ) |
| 64 | + |
0 commit comments