Snowflake (Embucket)#

Integration with Embucket, a Snowflake-compatible SQL and Apache Iceberg engine (default image embucket/embucket:0.2.2, HTTP port 3000). The fixture is clientless: readiness, login, and SQL execution use Python’s standard library, so snowflake-connector-python is not required.

Installation#

pip install pytest-databases

Install snowflake-connector-python in your project if your application connects with the official Snowflake Python connector.

Usage Example#

Embucket always requires a login. SnowflakeService.query() logs in through /session/v1/login-request as snowflake_user / snowflake_password on first use, caches the session token, and runs SQL through /queries/v1/query-request with snowflake_database and snowflake_schema as the session’s current database and schema. SnowflakeService.connection_kwargs provides parameters for snowflake.connector.connect:

from pytest_databases.docker.snowflake import (
    SnowflakeService,
    login_snowflake_emulator,
    query_snowflake_emulator,
)

pytest_plugins = ["pytest_databases.docker.snowflake"]


def test_snowflake(snowflake_service: SnowflakeService) -> None:
    token = login_snowflake_emulator(snowflake_service)
    response = query_snowflake_emulator(snowflake_service, token, "SELECT 1")
    assert response["success"] is True
    assert response["data"]["rowset"] == [["1"]]
    assert snowflake_service.connection_kwargs["protocol"] == "http"

query() raises RuntimeError with the emulator’s message when a statement fails. By default, snowflake_wire_compat (SNOWFLAKE_WIRE_COMPAT, default True) runs a loopback REST v1 compatibility proxy in front of Embucket that decompresses Content-Encoding: gzip request payloads, substitutes positional ? bindings parameters in SQL text, and stringifies non-None rowset cells (such as [["1"]]) for snowflake-connector-python. Set SNOWFLAKE_WIRE_COMPAT=0 (or override snowflake_wire_compat to False and pass wire_compat=False to provide_snowflake_service) to target the raw Embucket port directly ([[1]]).

The container is started with snowflake_user and snowflake_password as its login (AUTH_DEMO_USER / AUTH_DEMO_PASSWORD) and an in-memory volume that holds snowflake_database and its public schema. Data does not survive a container restart.

Isolation#

With the default xdist_snowflake_isolation_level (database) all pytest-xdist workers share one container, and each worker gets its own schema public_<worker>, created before the service is yielded; unqualified table names in query() resolve to that schema. With server each worker starts its own container named snowflake_<worker> and uses public.

Available Fixtures#

  • snowflake_image: Container image (default embucket/embucket:0.2.2, env SNOWFLAKE_IMAGE).

  • snowflake_user, snowflake_password (default embucket / embucket), snowflake_account

  • snowflake_database, snowflake_schema, snowflake_warehouse, snowflake_role

  • snowflake_wire_compat: Enable the REST v1 compatibility loopback proxy (default True, env SNOWFLAKE_WIRE_COMPAT).

  • xdist_snowflake_isolation_level: "database" (worker-scoped schema) or "server"

  • snowflake_service: SnowflakeService

Service API#

Snowflake-compatible Embucket container service fixtures and stdlib HTTP helpers.

Embucket always requires authentication: clients log in through POST /session/v1/login-request and send the returned session token as Authorization: Snowflake Token="<token>" on /queries/v1/query-request.

class pytest_databases.docker.snowflake.SnowflakeService(container: docker.models.containers.Container, host: str, port: int, user: str, password: str, account: str, database: str, schema: str, warehouse: str, role: str)[source]#

Bases: ServiceContainer

Connection metadata and stdlib HTTP query helper for an Embucket Snowflake emulator.

query() logs in on first use with the service credentials, database, schema, and warehouse, caches the session token, and logs in again once when the emulator rejects the token.

user: str#
password: str#
account: str#
database: str#
schema: str#
warehouse: str#
role: str#
property endpoint: str#

The HTTP base endpoint URL for the Snowflake emulator.

property connection_kwargs: dict[str, str | int]#

Dictionary of connection parameters compatible with snowflake.connector.connect.

login() → str[source]#

Log in to the emulator, cache the session token, and return it.

The session’s current database and schema are database and schema.

query(sql: str) → dict[str, Any][source]#

Execute a SQL statement in this service’s database and schema without a host driver.

Returns:

The JSON response of a successful statement; response["data"]["rowset"] holds the rows.

__init__(container: docker.models.containers.Container, host: str, port: int, user: str, password: str, account: str, database: str, schema: str, warehouse: str, role: str) → None#
pytest_databases.docker.snowflake.check_snowflake_health(host: str, port: int, *, timeout: float = 2.0) → bool[source]#

Return whether the Embucket /health endpoint answers HTTP 200.

Parameters:
  • host – Mapped container host address.

  • port – Mapped container HTTP port.

  • timeout – Socket timeout in seconds.

Returns:

True if the emulator responds with HTTP 200, otherwise False.

pytest_databases.docker.snowflake.login_snowflake_emulator(host: str, port: int, *, user: str, password: str, account: str, database: str | None = None, schema: str | None = None, warehouse: str | None = None, timeout: float = 5.0) → str[source]#

Log in to an Embucket Snowflake emulator and return the session token.

Parameters:
  • host – Mapped container host address.

  • port – Mapped container HTTP port.

  • user – Login name.

  • password – Password.

  • account – Account name.

  • database – Current database of the new session.

  • schema – Current schema of the new session.

  • warehouse – Warehouse of the new session.

  • timeout – HTTP request timeout in seconds.

Returns:

The session token to send as Authorization: Snowflake Token="<token>".

Raises:

RuntimeError – If the emulator rejects the credentials.

pytest_databases.docker.snowflake.provide_snowflake_service(container_service: ContainerService, snowflake_image: str, snowflake_user: str, snowflake_password: str, snowflake_account: str, snowflake_database: str, snowflake_schema: str, snowflake_warehouse: str, snowflake_role: str, xdist_snowflake_isolation_level: XdistIsolationLevel, *, wire_compat: bool = True) → Generator[SnowflakeService, None, None][source]#

Start and yield a ready Embucket Snowflake-compatible service container.

The container seeds an in-memory volume with snowflake_database and its public schema, and its login user is snowflake_user / snowflake_password. A schema other than public is created before the service is yielded. When wire_compat is True, the container is fronted by an ephemeral stdlib loopback proxy that translates gzip request bodies, ? SQL bindings, and stringified rowset cells.

Yields:

A configured SnowflakeService instance.

pytest_databases.docker.snowflake.query_snowflake_emulator(host: str, port: int, sql: str, *, token: str, timeout: float = 30.0) → dict[str, Any][source]#

Execute a SQL statement against the Embucket Snowflake REST endpoint /queries/v1/query-request.

Parameters:
  • host – Mapped container host address.

  • port – Mapped container HTTP port.

  • sql – SQL statement to execute.

  • token – Session token from login_snowflake_emulator.

  • timeout – HTTP request timeout in seconds.

Returns:

The JSON response payload; response["data"]["rowset"] holds the rows.

Raises:
  • RuntimeError – If the emulator reports that the statement failed.

  • urllib.error.HTTPError – If the emulator rejects the request, for example with 401 for an expired token.

pytest_databases.docker.snowflake.snowflake_account() → str[source]#

Provide the Snowflake test account identifier.

pytest_databases.docker.snowflake.snowflake_database() → str[source]#

Provide the Snowflake database name seeded in the container.

pytest_databases.docker.snowflake.snowflake_image() → str[source]#

Provide the Embucket Snowflake emulator container image.

pytest_databases.docker.snowflake.snowflake_password() → str[source]#

Provide the Snowflake login password; the container is started with it as its login password.

pytest_databases.docker.snowflake.snowflake_role() → str[source]#

Provide the default Snowflake role name.

pytest_databases.docker.snowflake.snowflake_schema(xdist_snowflake_isolation_level: Literal['database', 'server']) → str[source]#

Provide the Snowflake schema; public_<worker> under xdist with database isolation.

pytest_databases.docker.snowflake.snowflake_service(container_service: ContainerService, snowflake_image: str, snowflake_user: str, snowflake_password: str, snowflake_account: str, snowflake_database: str, snowflake_schema: str, snowflake_warehouse: str, snowflake_role: str, xdist_snowflake_isolation_level: XdistIsolationLevel, snowflake_wire_compat: bool) → Generator[SnowflakeService, None, None][source]#

Start and yield a ready Embucket Snowflake-compatible service container.

Yields:

A configured SnowflakeService instance.

pytest_databases.docker.snowflake.snowflake_user() → str[source]#

Provide the Snowflake login name; the container is started with it as its login user.

pytest_databases.docker.snowflake.snowflake_warehouse() → str[source]#

Provide the default Snowflake warehouse name.

pytest_databases.docker.snowflake.snowflake_wire_compat() → bool[source]#

Return whether snowflake_service enables the Snowflake REST v1 wire-compatibility proxy.

pytest_databases.docker.snowflake.xdist_snowflake_isolation_level() → Literal['database', 'server'][source]#

Provide the pytest-xdist isolation level for the Snowflake emulator.