Sybase ASE#

Integration with SAP Sybase Adaptive Server Enterprise (ASE).

Installation#

pip install pytest-databases

Install pymssql (built with FreeTDS TDS 5.0 support) if your tests connect from the host. The fixture checks readiness with a TCP probe and the isql client inside the container, so no driver is needed to start it.

Usage Example#

import pymssql
from pytest_databases.docker.sybase import SybaseService

pytest_plugins = ["pytest_databases.docker.sybase"]


def test_sybase_freetds_alias(sybase_service: SybaseService, sybase_freetds_alias: str) -> None:
    with pymssql.connect(
        server=sybase_freetds_alias,
        user=sybase_service.user,
        password=sybase_service.password,
        database=sybase_service.database,
        autocommit=True,
    ) as conn, conn.cursor() as cur:
        cur.execute("SELECT 1")
        res = cur.fetchone()[0]
        assert res == 1

FreeTDS 5.0 Client Configuration#

FreeTDS builds targeting TDS 7.x by default (for MS SQL Server) require tds version = 5.0 to talk to SAP ASE. sybase_freetds_alias writes a dedicated server block via write_freetds_conf() (preserving existing server sections in FREETDSCONF without mutating [global] tds version), sets FREETDSCONF for the session, and restores the prior environment variable at teardown so concurrent MS SQL Server clients remain unaffected.

Parallel Execution#

With the default database isolation, pytest-xdist workers share one container and each worker gets its own database: <sybase_database>_<worker>, or pytestdb_<worker> when sybase_database is master. The name must be a valid ASE identifier of at most 30 characters. Each database is created on its own <database>_dev device (48 MB) next to the master device, because the default image’s master device has too little free space for a copy of model. With server isolation every worker starts its own container and uses sybase_database.

exec_isql and SybaseService.exec_sql run isql --retserverror -b against 127.0.0.1:5000 inside the container and return a non-zero exit code for server errors of severity 11 or higher. Set SYBASE_ENABLE_MONITORING=1 (or override sybase_enable_monitoring to return True) to run configure_sybase_monitoring() once per container, enabling the ASE mon* MDA monitoring parameters via sp_configure and granting mon_role to non-sa logins. Custom fixtures calling provide_sybase_service() can also pass host_port, env, and one-shot init_sql scripts.

Available Fixtures#

  • sybase_image: Docker image tag for SAP Sybase ASE (default: datagrip/sybase:16.0, env SYBASE_IMAGE).

  • sybase_user: Login user for the Sybase ASE instance (default: sa, env SYBASE_USER).

  • sybase_password: Login password for the Sybase ASE instance (default: myPassword, env SYBASE_PASSWORD). The default image’s sa password is fixed to myPassword.

  • sybase_database: Database the service uses outside pytest-xdist and the base name of the worker databases (default: master, env SYBASE_DATABASE). Any value other than master is created on startup.

  • sybase_port: Optional pinned host TCP port (default: None, env SYBASE_PORT).

  • sybase_enable_monitoring: Enable SAP ASE mon* MDA monitoring tables on startup (default: False, env SYBASE_ENABLE_MONITORING).

  • sybase_startup_timeout: Timeout in seconds for container startup (default: 300, env SYBASE_STARTUP_TIMEOUT).

  • xdist_sybase_isolation_level: Worker isolation mode (default: database).

  • sybase_service: Provides a running Sybase ASE service container.

  • sybase_freetds_alias: Session-scoped server alias configured in FREETDSCONF with tds version = 5.0 (via SybaseService.write_freetds_conf).

Service API#

SAP Sybase ASE (Adaptive Server Enterprise) container service fixtures.

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

Bases: ServiceContainer

Service handle for a running SAP Sybase ASE container.

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

Connection configuration parameters compatible with Sybase drivers.

property dsn: str#

TDS connection string representation.

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

Execute a SQL statement inside the Sybase ASE container via isql.

Returns:

Tuple of (exit_code, output_bytes).

create_worker_database(db_name: str) → None[source]#

Create an isolated database inside the Sybase ASE container.

write_freetds_conf(target_dir: str | Path, section_name: str = 'PYTEST_DATABASES_SYBASE') → tuple[Path, str][source]#

Write a FreeTDS server alias section to <target_dir>/freetds.conf.

Merges readable sections from any pre-existing FREETDSCONF and the target freetds.conf without modifying [global], configuring host, port, tds version = 5.0, and client charset = UTF-8 strictly under [section_name].

Parameters:
  • target_dir – Directory where freetds.conf is created or updated.

  • section_name – Server section header name to configure.

Returns:

Tuple of (conf_path, section_name).

Raises:

ValueError – If section_name is empty, "global", or contains INI delimiters or control characters.

__init__(container: docker.models.containers.Container, host: str, port: int, user: str, password: str, database: str) → None#
pytest_databases.docker.sybase.configure_sybase_monitoring(container: docker.models.containers.Container, *, user: str = 'sa', password: str = 'myPassword', grant_to: str | None = None) → None[source]#

Enable SAP ASE Monitoring and Diagnostic Adapter (mon* MDA tables) via sp_configure.

Applies the dynamic and pipe MDA configuration parameters once per container under a cross-worker lock, and optionally grants mon_role to a non-sa login so master..mon* telemetry queries work out of the box.

Parameters:
  • container – Running Sybase ASE container handle.

  • user – Administrative login executing sp_configure (defaults to "sa").

  • password – Password for user.

  • grant_to – Optional non-sa login to grant mon_role.

Raises:
  • ValueError – If grant_to is not a valid Sybase identifier.

  • RuntimeError – If sp_configure or grant role mon_role fails.

pytest_databases.docker.sybase.create_worker_database(container: docker.models.containers.Container, db_name: str, *, user: str = 'sa', password: str = 'myPassword', size: str = '48M') → None[source]#

Create a database and its own device inside the Sybase ASE container if they do not already exist.

The master device of the default image has too little free space for a copy of model, so each database gets a <db_name>_dev device (DISK INIT) next to the master device file.

Parameters:
  • container – Running Sybase container.

  • db_name – Database name to create.

  • user – Login username.

  • password – User password.

  • size – Size of the device and the database, for example 48M.

Raises:

RuntimeError – If the device or database cannot be created.

pytest_databases.docker.sybase.exec_isql(container: docker.models.containers.Container, sql: str, *, database: str = 'master', user: str = 'sa', password: str = 'myPassword', port: int = 5000) → tuple[int, bytes][source]#

Execute a T-SQL batch inside a Sybase ASE container via the in-container isql.

The script sources /opt/sybase/SYBASE.sh when the image ships it, connects to 127.0.0.1:<port> so no interfaces entry is needed, and runs isql --retserverror -b (no column headers). isql exits 0 for some failures (for example when its localization files cannot be loaded), so a zero exit code is replaced by 1 when the output contains a server error of severity 11 or higher (Msg <n>, Level <11-29>) or a client library error.

Parameters:
  • container – Running Sybase ASE container.

  • sql – SQL statements to execute.

  • database – Target database name.

  • user – Login username.

  • password – User password.

  • port – Server port inside the container.

Returns:

Tuple of (exit_code, output_bytes).

pytest_databases.docker.sybase.provide_sybase_service(container_service: ContainerService, sybase_image: str, sybase_user: str, sybase_password: str, sybase_database: str, sybase_startup_timeout: int, xdist_sybase_isolation_level: XdistIsolationLevel, *, enable_monitoring: bool = False, host_port: int | None = None, env: Mapping[str, str] | None = None, init_sql: str | Path | Sequence[str | Path] | None = None) → Generator[SybaseService, None, None][source]#

Start and yield a ready SAP Sybase ASE service container.

Yields:

A configured SybaseService instance.

pytest_databases.docker.sybase.sybase_database() → str[source]#

Initial database name on the Sybase ASE container.

pytest_databases.docker.sybase.sybase_enable_monitoring() → bool[source]#

Whether Sybase ASE enables mon* MDA monitoring tables automatically during startup.

pytest_databases.docker.sybase.sybase_freetds_alias(sybase_service: SybaseService, tmp_path_factory: pytest.TempPathFactory) → Generator[str, None, None][source]#

Write a freetds.conf alias for sybase_service, export FREETDSCONF, and restore on teardown.

Yields:

The FreeTDS section alias configured in FREETDSCONF.

pytest_databases.docker.sybase.sybase_image() → str[source]#

Docker image tag for SAP Sybase ASE.

pytest_databases.docker.sybase.sybase_password() → str[source]#

Password for authenticating with the Sybase ASE container.

pytest_databases.docker.sybase.sybase_port() → int | None[source]#

Optional pinned host TCP port for Sybase ASE (SYBASE_PORT).

pytest_databases.docker.sybase.sybase_service(container_service: ContainerService, sybase_image: str, sybase_user: str, sybase_password: str, sybase_database: str, sybase_startup_timeout: int, sybase_enable_monitoring: bool, sybase_port: int | None, xdist_sybase_isolation_level: XdistIsolationLevel) → Generator[SybaseService, None, None][source]#

Provide a session-scoped Sybase ASE service container.

Yields:

A configured SybaseService instance.

pytest_databases.docker.sybase.sybase_startup_timeout() → int[source]#

Maximum seconds to wait for Sybase ASE container startup.

pytest_databases.docker.sybase.sybase_user() → str[source]#

Username for authenticating with the Sybase ASE container.

pytest_databases.docker.sybase.validate_sybase_database_name(db_name: str) → str[source]#

Validate that a Sybase database name conforms to ASE identifier limits.

Parameters:

db_name – Candidate Sybase database name.

Returns:

The validated database name.

Raises:

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

pytest_databases.docker.sybase.xdist_sybase_isolation_level() → Literal['database', 'server'][source]#

Provide the pytest-xdist isolation level for Sybase ASE.