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, envSYBASE_IMAGE).sybase_user: Login user for the Sybase ASE instance (default:sa, envSYBASE_USER).sybase_password: Login password for the Sybase ASE instance (default:myPassword, envSYBASE_PASSWORD). The default image’ssapassword is fixed tomyPassword.sybase_database: Database the service uses outsidepytest-xdistand the base name of the worker databases (default:master, envSYBASE_DATABASE). Any value other thanmasteris created on startup.sybase_port: Optional pinned host TCP port (default:None, envSYBASE_PORT).sybase_enable_monitoring: Enable SAP ASEmon*MDA monitoring tables on startup (default:False, envSYBASE_ENABLE_MONITORING).sybase_startup_timeout: Timeout in seconds for container startup (default:300, envSYBASE_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 inFREETDSCONFwithtds version = 5.0(viaSybaseService.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:
ServiceContainerService handle for a running SAP Sybase ASE container.
- property connection_config: dict[str, str | int | bool]#
Connection configuration parameters compatible with Sybase drivers.
- 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
FREETDSCONFand the targetfreetds.confwithout modifying[global], configuringhost,port,tds version = 5.0, andclient charset = UTF-8strictly under[section_name].- Parameters:
target_dir – Directory where
freetds.confis created or updated.section_name – Server section header name to configure.
- Returns:
Tuple of
(conf_path, section_name).- Raises:
ValueError – If
section_nameis empty,"global", or contains INI delimiters or control characters.
- 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) viasp_configure.Applies the dynamic and pipe MDA configuration parameters once per container under a cross-worker lock, and optionally grants
mon_roleto a non-salogin somaster..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-
salogin to grantmon_role.
- Raises:
ValueError – If
grant_tois not a valid Sybase identifier.RuntimeError – If
sp_configureorgrant role mon_rolefails.
- 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
masterdevice of the default image has too little free space for a copy ofmodel, so each database gets a<db_name>_devdevice (DISK INIT) next to themasterdevice 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.shwhen the image ships it, connects to127.0.0.1:<port>so nointerfacesentry is needed, and runsisql --retserverror -b(no column headers).isqlexits0for some failures (for example when its localization files cannot be loaded), so a zero exit code is replaced by1when 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.confalias forsybase_service, exportFREETDSCONF, and restore on teardown.- Yields:
The FreeTDS section alias configured in
FREETDSCONF.
- 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_nameexceeds length limits or has invalid characters.