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 (default ghcr.io/goccy/bigquery-emulator:0.8.1 via DEFAULT_BIGQUERY_IMAGE, env BIGQUERY_IMAGE; note that the image tag omits a leading v).

  • bigquery_service: A fixture that provides a BigQueryService instance (with is_healthy(*, timeout=5.0) -> bool).

  • bigquery_client: Deprecated fixture that provides a google.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: ServiceContainer

Connection metadata for a Google BigQuery emulator container service.

dataset is the dataset reserved for the current test process. Under pytest-xdist with database isolation, workers share one emulator and each gets its own dataset (test-dataset_<worker>); otherwise it is test-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.

project: str#
dataset: str#
property endpoint: str#

The HTTP API endpoint URL for the BigQuery emulator.

property credentials: Any#

Deprecated AnonymousCredentials instance for the BigQuery emulator.

property client_options: Any#

Deprecated ClientOptions configured with the emulator API endpoint.

is_healthy(*, timeout: float = 5.0) → bool[source]#

Return True when SELECT 1 completes successfully against the emulator.

pytest_databases.docker.bigquery.bigquery_client(bigquery_service: BigQueryService) → Generator[Any, None, None][source]#

Provide a deprecated google.cloud.bigquery.Client connected to the emulator.

Yields:

A configured bigquery.Client instance.

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 BigQueryService instance.

pytest_databases.docker.bigquery.platform() → str[source]#

Return the legacy linux/x86_64 platform string.

Deprecated since version 0.20.0: Use bigquery_platform instead. platform will 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.Client instance.

Yields:

A configured bigquery.Client connected to bigquery_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 BigQueryService instance.

pytest_databases.docker.bigquery.query_bigquery_emulator(host: str, port: int, project: str, sql: str, *, timeout: float = 2.0) → dict[str, Any][source]#

Execute a SQL query against the BigQuery emulator REST API bypassing ambient proxies.

pytest_databases.docker.bigquery.xdist_bigquery_isolation_level() → Literal['database', 'server'][source]#

Return the default pytest-xdist isolation level for BigQuery (server).