SAP SQL Anywhere#

Integration with SAP SQL Anywhere.

Installation#

pip install pytest-databases

Install sqlanydb if your tests connect from the host. The fixture checks readiness with a TCP probe and the dbisqlc client inside the container, so no driver is needed to start it. sqlanydb also needs SAP’s native client libraries; the sqlanywhere_client_libs fixture copies them out of the running container.

Usage Example#

import sqlanydb
from pytest_databases.docker.sqlanywhere import SQLAnywhereService

pytest_plugins = ["pytest_databases.docker.sqlanywhere"]


def test_sqlanywhere_connection(sqlanywhere_service: SQLAnywhereService) -> None:
    conn = sqlanydb.connect(
        uid=sqlanywhere_service.user,
        pwd=sqlanywhere_service.password,
        eng=sqlanywhere_service.server_name,
        dbn=sqlanywhere_service.database,
        host=f"{sqlanywhere_service.host}:{sqlanywhere_service.port}",
    )
    with conn.cursor() as cur:
        cur.execute("SELECT 1")
        res = cur.fetchone()[0]
        assert res == 1

Parallel Execution#

Only server isolation is supported, and it is the default: every pytest-xdist worker starts its own container (sqlanywhere_<worker>). The default image serves one database file and cannot create a database per worker, so xdist_sqlanywhere_isolation_level returning database raises ValueError.

The default image always serves the demo database on the demo server as dba / sql. Overriding sqlanywhere_user, sqlanywhere_password, sqlanywhere_database or sqlanywhere_server only changes how the fixture connects, so use an image that matches the new values.

Available Fixtures#

  • sqlanywhere_image: Docker image tag for SAP SQL Anywhere (default: jaschweder/sybase:latest, env SQLANYWHERE_IMAGE).

  • sqlanywhere_user: Login user for the SQL Anywhere instance (default: dba, env SQLANYWHERE_USER).

  • sqlanywhere_password: Login password for the SQL Anywhere instance (default: sql, env SQLANYWHERE_PASSWORD).

  • sqlanywhere_database: Database to connect to (default: demo, env SQLANYWHERE_DATABASE).

  • sqlanywhere_server: Database server / engine name (default: demo, env SQLANYWHERE_SERVER).

  • sqlanywhere_startup_timeout: Timeout in seconds for container startup (default: 60, env SQLANYWHERE_STARTUP_TIMEOUT).

  • xdist_sqlanywhere_isolation_level: Worker isolation mode (default and only supported value: server).

  • sqlanywhere_service: Provides a running SAP SQL Anywhere service container.

  • sqlanywhere_client_libs: Copies the native client libraries (lib64 and res) out of the container, preloads them, and sets SQLANY16 and SQLANY_API_DLL for the rest of the session. The previous values are restored when the session ends.

Service API#

SAP SQL Anywhere container service fixtures.

class pytest_databases.docker.sqlanywhere.SQLAnywhereService(container: docker.models.containers.Container, host: str, port: int, user: str, password: str, database: str, server_name: str)[source]#

Bases: ServiceContainer

Service handle for a running SAP SQL Anywhere container.

user: str#
password: str#
database: str#
server_name: str#
property connection_config: dict[str, str | int]#

Connection configuration parameters compatible with sqlanydb.

property dsn: str#

ODBC/dbisql connection string representation.

exec_sql(sql: str, *, database: str | None = None) → tuple[int, bytes][source]#

Execute a SQL statement inside the SQL Anywhere container via dbisql.

__init__(container: docker.models.containers.Container, host: str, port: int, user: str, password: str, database: str, server_name: str) → None#
pytest_databases.docker.sqlanywhere.exec_dbisql(container: docker.models.containers.Container, sql: str, *, database: str = 'demo', server_name: str = 'demo', user: str = 'dba', password: str = 'sql', port: int = 2638) → tuple[int, bytes][source]#

Execute SQL statements inside a SQL Anywhere container using the in-container CLI.

Parameters:
  • container – Running SQL Anywhere container.

  • sql – SQL statements to execute.

  • database – Database name to connect to.

  • server_name – Database engine server name.

  • user – Login username.

  • password – User password.

  • port – Container internal TCP port.

Returns:

Tuple of (exit_code, output_bytes).

pytest_databases.docker.sqlanywhere.extract_sqlanywhere_client_libs(container: Container, target_dir: Path, *, container_base_path: str | None = None, preload: bool = True) → Path[source]#

Extract native SAP SQL Anywhere client libraries from a container.

Copies the lib64 shared libraries and res message resources from the container. With preload the dbtasks/dblib dependencies and the dbcapi library are loaded into the current process with ctypes and RTLD_GLOBAL, so a later load of dbcapi by name (for example by sqlanydb) resolves to the extracted copy. The process environment is not modified; use sqlanywhere_client_environment() for the SQLANY16 and SQLANY_API_DLL values.

Parameters:
  • container – Running SAP SQL Anywhere container.

  • target_dir – Local destination directory to write extracted libraries into.

  • container_base_path – Optional base directory in the container (auto-detected if None).

  • preload – Whether to load the extracted libraries into the current process.

Returns:

The target directory containing extracted client libraries.

pytest_databases.docker.sqlanywhere.provide_sqlanywhere_service(container_service: ContainerService, sqlanywhere_image: str, sqlanywhere_user: str, sqlanywhere_password: str, sqlanywhere_database: str, sqlanywhere_server: str, sqlanywhere_startup_timeout: int, xdist_sqlanywhere_isolation_level: XdistIsolationLevel) → Generator[SQLAnywhereService, None, None][source]#

Start and yield a ready SAP SQL Anywhere service container.

Yields:

A configured SQLAnywhereService instance.

pytest_databases.docker.sqlanywhere.sqlanywhere_client_environment(target_dir: Path) → dict[str, str][source]#

Return the environment variables that point SQL Anywhere clients at extracted libraries.

Parameters:

target_dir – Directory previously populated by extract_sqlanywhere_client_libs().

Returns:

SQLANY16 (the extraction directory) and, when a dbcapi library was extracted, SQLANY_API_DLL.

pytest_databases.docker.sqlanywhere.sqlanywhere_client_libs(sqlanywhere_service: SQLAnywhereService, tmp_path_factory: pytest.TempPathFactory) → Generator[Path, None, None][source]#

Extract SAP SQL Anywhere native client libraries from the container.

Copies native client libraries and message resources from the container, preloads them, and sets SQLANY16 and SQLANY_API_DLL for driver packages such as sqlanydb. The previous values of both variables are restored when the session ends.

Parameters:
  • sqlanywhere_service – Running SQL Anywhere container service.

  • tmp_path_factory – Pytest session temporary directory factory.

Yields:

Path to directory containing the extracted client libraries and resources.

pytest_databases.docker.sqlanywhere.sqlanywhere_database() → str[source]#

Database name on the SQL Anywhere container.

pytest_databases.docker.sqlanywhere.sqlanywhere_image() → str[source]#

Docker image tag for SAP SQL Anywhere.

pytest_databases.docker.sqlanywhere.sqlanywhere_password() → str[source]#

Password for authenticating with the SQL Anywhere container.

pytest_databases.docker.sqlanywhere.sqlanywhere_server() → str[source]#

Server/engine name on the SQL Anywhere container.

pytest_databases.docker.sqlanywhere.sqlanywhere_service(container_service: ContainerService, sqlanywhere_image: str, sqlanywhere_user: str, sqlanywhere_password: str, sqlanywhere_database: str, sqlanywhere_server: str, sqlanywhere_startup_timeout: int, xdist_sqlanywhere_isolation_level: XdistIsolationLevel) → Generator[SQLAnywhereService, None, None][source]#

Provide a session-scoped SQL Anywhere service container.

Yields:

A configured SQLAnywhereService instance.

pytest_databases.docker.sqlanywhere.sqlanywhere_startup_timeout() → int[source]#

Maximum seconds to wait for SQL Anywhere container startup.

pytest_databases.docker.sqlanywhere.sqlanywhere_user() → str[source]#

Username for authenticating with the SQL Anywhere container.

pytest_databases.docker.sqlanywhere.validate_sqlanywhere_database_name(db_name: str) → str[source]#

Validate that a SQL Anywhere database name conforms to identifier rules.

Parameters:

db_name – Candidate database name.

Returns:

The validated database name.

Raises:

ValueError – If db_name exceeds length limits or has invalid characters.

pytest_databases.docker.sqlanywhere.xdist_sqlanywhere_isolation_level() → Literal['database', 'server'][source]#

Provide the pytest-xdist isolation level for SQL Anywhere.