Test Fixtures and Conftest
Relevant source files
The following files were used as context for generating this wiki page:
The testing infrastructure for the meltano-state-backend-postgresql project provides a robust environment for verifying the PostgreSQL state backend through unit tests, integration tests, and compatibility checks. It utilizes pytest fixtures to manage database lifecycles, mock connections, and project environments.
The testing suite is designed to handle different execution layers: a unit testing layer that mocks psycopg connections, and an integration layer that leverages testcontainers to run a real PostgreSQL instance. This ensures that the backend behaves correctly both in isolation and when interacting with an actual database engine.
Core Fixtures and Lifecycle
The project defines several high-level fixtures in tests/conftest.py that provide the foundation for most test cases. These include environment setup, database containerization, and mocking utilities.
Global Test Configuration
The pytest_report_header function is used to include the current Meltano version in the test report, ensuring visibility of the core dependency version during execution.
Sources: tests/conftest.py:17-19
PostgreSQL Containerization
For integration testing, the suite uses a testcontainers fixture to spin up a temporary PostgreSQL instance.
The postgres_container fixture is scoped to the module level to optimize performance by reusing the same database instance across multiple tests within a module.
Sources: tests/conftest.py:22-26
Project and Mocking Fixtures
The following table summarizes the primary fixtures used across the test suite:
| Fixture Name | Scope | Description |
|---|---|---|
postgres_container | module | Provides a PostgresContainer running PostgreSQL 16. |
project | function | Creates a temporary Meltano project by copying the fixtures/project directory. |
mock_connection | function | Patches psycopg.connect and returns mocks for the connection and cursor. |
subject | function | Provides a PostgreSQLStateStoreManager instance pre-configured with mocked database components. |
Sources: tests/conftest.py:22-68
Unit Testing Fixtures
Unit tests rely on the mock_connection and subject fixtures to simulate database interactions without requiring a running PostgreSQL server.
The subject Fixture
The subject fixture initializes the PostgreSQLStateStoreManager with test credentials and injects a mocked connection. This allows for asserting that specific SQL queries are executed with the expected parameters during state operations like set, get, and delete.
Sources: tests/conftest.py:53-68, tests/test_state.py:18-28
Connection Mocking Flow
The mock_connection fixture handles the complex setup of nested context managers used by psycopg.
Sources: tests/conftest.py:38-50
Integration and Compatibility Fixtures
Integration tests use specialized fixtures to manage schemas and managers against the live testcontainers instance.
Schema Management
In tests/test_backwards_compat.py, a schema fixture generates unique schema names for each test run to ensure isolation. It automatically creates the schema before the test and drops it (using CASCADE) afterward.
Sources: tests/test_backwards_compat.py:38-46
Integration Manager Setup
The manager fixture in tests/test_integration.py handles the instantiation of the PostgreSQLStateStoreManager using connection URLs derived from the active container. It supports parameterized testing of various schema and table configurations.
Sources: tests/test_integration.py:46-70
Backwards Compatibility Utilities
To verify compatibility with legacy Meltano system databases, the suite includes fixtures for:
- systemdb_uri: A connection string formatted for Meltano's internal migration service.
- libpq_uri: A standard connection string for the new backend.
- engine: A SQLAlchemy engine used to run Meltano's internal migrations to create legacy table structures.
Sources: tests/test_backwards_compat.py:48-63
Project Fixtures
The project fixture ensures that tests operate on a clean, isolated Meltano environment. It copies the base configuration from fixtures/project/meltano.yml to a temporary directory.
Meltano Configuration Fixture
The fixtures/project/meltano.yml file defines the default state for the test project:
- requires_meltano:
>=3.7 - state_backend.uri:
postgresql://user:password@localhost:5432/meltano - sslmode:
prefer
Sources: fixtures/project/meltano.yml:1-11, tests/conftest.py:29-35
The testing infrastructure ensures that all backend functionalities—from connection parsing to complex state locking—are validated against both mocked interfaces and real database environments.