BigQuery#
Integration with Google BigQuery using the BigQuery Emulator.
Installation#
The BigQuery emulator fixture validates readiness using Python stdlib HTTP requests against the emulator REST API, so no Google Cloud Python SDK dependency is required to start the service:
pip install pytest-databases
Usage Example#
from google.api_core.client_options import ClientOptions
from google.auth.credentials import AnonymousCredentials
from google.cloud import bigquery
from pytest_databases.docker.bigquery import BigQueryService
pytest_plugins = ["pytest_databases.docker.bigquery"]
def test_bigquery(bigquery_service: BigQueryService) -> None:
assert bigquery_service.is_healthy() is True
client = bigquery.Client(
project=bigquery_service.project,
client_options=ClientOptions(api_endpoint=bigquery_service.endpoint),
credentials=AnonymousCredentials(),
)
job = client.query(query="SELECT 1 as one")
resp = list(job.result())
assert resp[0].one == 1
Available Fixtures#
bigquery_image: The Docker image to use for the BigQuery emulator (defaultghcr.io/goccy/bigquery-emulator:0.8.1viaDEFAULT_BIGQUERY_IMAGE, envBIGQUERY_IMAGE; note that the image tag omits a leadingv).bigquery_service: A fixture that provides aBigQueryServiceinstance (withis_healthy(*, timeout=5.0) -> bool).bigquery_client: Deprecated fixture that provides agoogle.cloud.bigquery.Client.xdist_bigquery_isolation_level:"server"(default) or"database".
By default ("server"), single-process sessions reuse a non-transient shared container while pytest-xdist gives each worker its own isolated container to avoid goccy/bigquery-emulator SQLite catalog DDL races. With opt-in "database" isolation, workers share one emulator and each worker receives test-dataset_<worker> in BigQueryService.dataset. Read bigquery_service.dataset instead of hard-coding it.
Service API#
Google BigQuery emulator container service fixtures.
- class pytest_databases.docker.bigquery.BigQueryService(container: docker.models.containers.Container, host: str, port: int, project: str, dataset: str, credentials: Any = None)[source]#
Bases:
ServiceContainerConnection metadata for a Google BigQuery emulator container service.
datasetis the dataset reserved for the current test process. Underpytest-xdistwithdatabaseisolation, workers share one emulator and each gets its own dataset (test-dataset_<worker>); otherwise it istest-dataset.- __init__(container: docker.models.containers.Container, host: str, port: int, project: str, dataset: str, credentials: Any = None) None[source]#
Initialize the service metadata.
- Parameters:
container – Running emulator container.
host – Host name the emulator is reachable on.
port – Published emulator HTTP port.
project – Emulator project ID.
dataset – Dataset reserved for the current test process.
credentials – Deprecated and ignored; credentials are built on access by
credentials.
- pytest_databases.docker.bigquery.bigquery_client(bigquery_service: BigQueryService) Generator[Any, None, None][source]#
Provide a deprecated
google.cloud.bigquery.Clientconnected to the emulator.- Yields:
A configured
bigquery.Clientinstance.
- pytest_databases.docker.bigquery.bigquery_image() str[source]#
Return the BigQuery emulator container image tag, honoring
BIGQUERY_IMAGE.
- pytest_databases.docker.bigquery.bigquery_platform(request: FixtureRequest) str | None[source]#
Return the container platform for the BigQuery emulator, which publishes x86_64 images only.
- pytest_databases.docker.bigquery.bigquery_service(docker_service: DockerService, xdist_bigquery_isolation_level: XdistIsolationLevel, bigquery_image: str, bigquery_platform: str | None) Generator[BigQueryService, None, None][source]#
Provide a session-scoped BigQuery emulator service.
- Yields:
A running
BigQueryServiceinstance.
- pytest_databases.docker.bigquery.platform() str[source]#
Return the legacy
linux/x86_64platform string.Deprecated since version 0.20.0: Use
bigquery_platforminstead.platformwill be removed in v1.0.0.
- pytest_databases.docker.bigquery.provide_bigquery_client(bigquery_service: BigQueryService) Generator[Any, None, None][source]#
Emit a deprecation warning and yield a
google.cloud.bigquery.Clientinstance.- Yields:
A configured
bigquery.Clientconnected tobigquery_service.
- pytest_databases.docker.bigquery.provide_bigquery_service(docker_service: DockerService, isolation_level: XdistIsolationLevel, image: str, platform_arch: str | None) Generator[BigQueryService, None, None][source]#
Start and yield a BigQuery emulator service container.
- Parameters:
docker_service – Docker service manager.
isolation_level – xdist isolation mode.
image – BigQuery emulator image name.
platform_arch – Target container platform architecture.
- Yields:
A running
BigQueryServiceinstance.