diff --git a/pyproject.toml b/pyproject.toml index 3f49ba9a..13759f81 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -90,6 +90,9 @@ namespaces = false # to disable scanning PEP 420 namespaces (true by default) # === Linting & Formatting === +[tool.black] +line-length = 120 + # --- ruff --- [tool.ruff] @@ -146,6 +149,7 @@ lint.per-file-ignores."tests/**" = [ "D401", "S101", "S105", + "S110", "S311", "S603", ] @@ -188,6 +192,7 @@ addopts = [ "-v", "-s", "-W error", + # Note: parallel execution is opt-in via -n/--numprocesses (pytest-xdist) ] markers = [ "mongo: test the MongoDB core", @@ -199,6 +204,8 @@ markers = [ "maxage: test the max_age functionality", "asyncio: marks tests as async", "smoke: fast smoke tests with no external service dependencies", + "flaky: tests that are known to be flaky and should be retried", + "seriallocal: local core tests that should run serially", ] [tool.coverage.report] @@ -209,10 +216,17 @@ exclude_lines = [ "raise NotImplementedError", # Don't complain if tests don't hit defensive assertion code: "if TYPE_CHECKING:", # Is only true when running mypy, not tests ] +# Parallel test execution configuration +# Use: pytest -n auto (for automatic worker detection) +# Or: pytest -n 4 (for specific number of workers) +# Memory tests are safe to run in parallel by default +# Pickle tests require isolation (handled by conftest.py fixture) + # --- coverage --- [tool.coverage.run] branch = true +parallel = true # dynamic_context = "test_function" omit = [ "tests/*", diff --git a/scripts/test-local.sh b/scripts/test-local.sh index e7efc571..88a0e689 100755 --- a/scripts/test-local.sh +++ b/scripts/test-local.sh @@ -25,7 +25,9 @@ COVERAGE_REPORT="term" KEEP_RUNNING=false SELECTED_CORES="" INCLUDE_LOCAL_CORES=false -TEST_FILES="" +TEST_FILES=() +PARALLEL=false +PARALLEL_WORKERS="auto" # Function to print colored messages print_message() { @@ -57,6 +59,8 @@ OPTIONS: -k, --keep-running Keep containers running after tests -h, --html-coverage Generate HTML coverage report -f, --files Specify test files to run (can be used multiple times) + -p, --parallel Run tests in parallel using pytest-xdist + -w, --workers Number of parallel workers (default: auto) --help Show this help message EXAMPLES: @@ -66,6 +70,8 @@ EXAMPLES: $0 external -k # Run external backends, keep containers $0 mongo memory -v # Run MongoDB and memory tests verbosely $0 all -f tests/test_main.py -f tests/test_redis_core_coverage.py # Run specific test files + $0 memory pickle -p # Run local tests in parallel + $0 all -p -w 4 # Run all tests with 4 parallel workers ENVIRONMENT: You can also set cores via CACHIER_TEST_CORES environment variable: @@ -96,13 +102,27 @@ while [[ $# -gt 0 ]]; do usage exit 1 fi - TEST_FILES="$TEST_FILES $1" + TEST_FILES+=("$1") shift ;; --help) usage exit 0 ;; + -p|--parallel) + PARALLEL=true + shift + ;; + -w|--workers) + shift + if [[ $# -eq 0 ]] || [[ "$1" == -* ]]; then + print_message $RED "Error: -w/--workers requires a number argument" + usage + exit 1 + fi + PARALLEL_WORKERS="$1" + shift + ;; -*) print_message $RED "Unknown option: $1" usage @@ -234,6 +254,17 @@ check_dependencies() { } fi + # Check for pytest-xdist if parallel testing is requested + if [ "$PARALLEL" = true ]; then + if ! python -c "import xdist" 2>/dev/null; then + print_message $YELLOW "Installing pytest-xdist for parallel testing..." + pip install pytest-xdist || { + print_message $RED "Failed to install pytest-xdist" + exit 1 + } + fi + fi + # Check MongoDB dependencies if testing MongoDB if echo "$SELECTED_CORES" | grep -qw "mongo"; then if ! python -c "import pymongo" 2>/dev/null; then @@ -423,14 +454,20 @@ main() { # Check and install dependencies check_dependencies - # Check if we need Docker + # Check if we need Docker, and if we should run serial pickle tests needs_docker=false + run_serial_local_tests=false for core in $SELECTED_CORES; do case $core in mongo|redis|sql) needs_docker=true ;; esac + case $core in + pickle|all) + run_serial_local_tests=true + ;; + esac done if [ "$needs_docker" = true ]; then @@ -497,26 +534,46 @@ main() { sql) test_sql ;; esac done + if [ -n "$pytest_markers" ]; then + pytest_markers="($pytest_markers) and not seriallocal" + else + pytest_markers="not seriallocal" + fi # Run pytest # Build pytest command - PYTEST_CMD="pytest" + PYTEST_ARGS=(pytest) + # and the specific pytest command for running serial pickle tests + SERIAL_PYTEST_ARGS=(pytest -m seriallocal) + # Only add -n0 if pytest-xdist is available; otherwise, plain pytest is already serial + if python - << 'EOF' >/dev/null 2>&1 +import xdist # noqa: F401 +EOF + then + SERIAL_PYTEST_ARGS+=(-n0) + fi # Add test files if specified - if [ -n "$TEST_FILES" ]; then - PYTEST_CMD="$PYTEST_CMD $TEST_FILES" - print_message $BLUE "Test files specified: $TEST_FILES" + if [ ${#TEST_FILES[@]} -gt 0 ]; then + PYTEST_ARGS+=("${TEST_FILES[@]}") + print_message $BLUE "Test files specified: ${TEST_FILES[*]}" + # and turn off serial local tests, so we run only selected files + run_serial_local_tests=false fi # Add markers if needed (only if no specific test files were given) - if [ -z "$TEST_FILES" ]; then + if [ ${#TEST_FILES[@]} -eq 0 ]; then # Check if we selected all cores - if so, run all tests without marker filtering all_cores="memory mongo pickle redis s3 sql" selected_sorted=$(echo "$SELECTED_CORES" | tr ' ' '\n' | sort | tr '\n' ' ' | xargs) all_sorted=$(echo "$all_cores" | tr ' ' '\n' | sort | tr '\n' ' ' | xargs) if [ "$selected_sorted" != "$all_sorted" ]; then - PYTEST_CMD="$PYTEST_CMD -m \"$pytest_markers\"" + PYTEST_ARGS+=(-m "$pytest_markers") + else + print_message $BLUE "Running all tests without markers since all cores are selected" + PYTEST_ARGS+=(-m "not seriallocal") + run_serial_local_tests=true fi else # When test files are specified, still apply markers if not running all cores @@ -525,21 +582,47 @@ main() { all_sorted=$(echo "$all_cores" | tr ' ' '\n' | sort | tr '\n' ' ' | xargs) if [ "$selected_sorted" != "$all_sorted" ]; then - PYTEST_CMD="$PYTEST_CMD -m \"$pytest_markers\"" + PYTEST_ARGS+=(-m "$pytest_markers") fi fi # Add verbose flag if needed if [ "$VERBOSE" = true ]; then - PYTEST_CMD="$PYTEST_CMD -v" + PYTEST_ARGS+=(-v) + SERIAL_PYTEST_ARGS+=(-v) + fi + + # Add parallel testing options if requested + if [ "$PARALLEL" = true ]; then + PYTEST_ARGS+=(-n "$PARALLEL_WORKERS") + + # Show parallel testing info + if [ "$PARALLEL_WORKERS" = "auto" ]; then + print_message $BLUE "Running tests in parallel with automatic worker detection" + else + print_message $BLUE "Running tests in parallel with $PARALLEL_WORKERS workers" + fi + + # Special note for pickle tests + if echo "$SELECTED_CORES" | grep -qw "pickle"; then + print_message $YELLOW "Note: Pickle tests will use isolated cache directories for parallel safety" + fi fi # Add coverage options - PYTEST_CMD="$PYTEST_CMD --cov=cachier --cov-report=$COVERAGE_REPORT" + PYTEST_ARGS+=(--cov=cachier --cov-report="$COVERAGE_REPORT") + SERIAL_PYTEST_ARGS+=(--cov=cachier --cov-report="$COVERAGE_REPORT" --cov-append) # Print and run the command - print_message $BLUE "Running: $PYTEST_CMD" - eval $PYTEST_CMD + print_message $BLUE "Running: $(printf '%q ' "${PYTEST_ARGS[@]}")" + "${PYTEST_ARGS[@]}" + + if [ "$run_serial_local_tests" = true ]; then + print_message $BLUE "Running serial local tests (pickle, memory) with: $(printf '%q ' "${SERIAL_PYTEST_ARGS[@]}")" + "${SERIAL_PYTEST_ARGS[@]}" + else + print_message $BLUE "Skipping serial local tests (pickle, memory) since not requested" + fi TEST_EXIT_CODE=$? diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..5fbbb7ad --- /dev/null +++ b/tests/README.md @@ -0,0 +1,485 @@ +# Cachier Test Suite Documentation + +This document provides comprehensive guidelines for writing and running tests for the Cachier package. + +## Table of Contents + +1. [Test Suite Overview](#test-suite-overview) +2. [Test Structure](#test-structure) +3. [Running Tests](#running-tests) +4. [Writing Tests](#writing-tests) +5. [Test Isolation](#test-isolation) +6. [Backend-Specific Testing](#backend-specific-testing) +7. [Parallel Testing](#parallel-testing) +8. [CI/CD Integration](#cicd-integration) +9. [Troubleshooting](#troubleshooting) + +## Test Suite Overview + +The Cachier test suite is designed to comprehensively test all caching backends while maintaining proper isolation between tests. The suite uses pytest with custom markers for backend-specific tests. + +### Supported Backends + +- **Memory**: In-memory caching (no external dependencies) +- **Pickle**: File-based caching using pickle (default backend) +- **MongoDB**: Database caching using MongoDB +- **Redis**: In-memory data store caching +- **SQL**: SQL database caching via SQLAlchemy (PostgreSQL, SQLite, MySQL) + +### Test Categories + +1. **Core Functionality**: Basic caching operations (get, set, clear) +2. **Stale Handling**: Testing `stale_after` parameter +3. **Concurrency**: Thread-safety and multi-process tests +4. **Error Handling**: Exception scenarios and recovery +5. **Performance**: Speed and efficiency tests +6. **Integration**: Cross-backend compatibility + +## Test Structure + +``` +tests/ +├── conftest.py # Shared fixtures and configuration +├── requirements.txt # Base test dependencies (includes pytest-rerunfailures) +├── requirements_mongodb.txt # MongoDB-specific test dependencies +├── requirements_redis.txt # Redis-specific test dependencies +├── requirements_postgres.txt # PostgreSQL/SQL-specific test dependencies +│ +├── test_*.py # Backend-agnostic test modules +├── mongo_tests/ # MongoDB-specific tests +│ └── test_mongo_core.py +├── sql_tests/ # SQL-specific tests +│ └── test_sql_core.py +├── test_redis_core.py # Redis backend tests +├── test_memory_core.py # Memory backend tests +├── test_pickle_core.py # Pickle backend tests +├── test_general.py # Cross-backend tests +└── ... +``` + +### Test Markers + +Tests are marked with backend-specific markers: + +```python +@pytest.mark.mongo # MongoDB tests +@pytest.mark.redis # Redis tests +@pytest.mark.sql # SQL tests +@pytest.mark.memory # Memory backend tests +@pytest.mark.pickle # Pickle backend tests +@pytest.mark.maxage # Tests involving stale_after functionality +@pytest.mark.flaky # Flaky tests that should be retried (see Flaky Tests section) +``` + +## Running Tests + +### Quick Start + +```bash +# Run all tests +pytest + +# Run tests for specific backend +pytest -m mongo +pytest -m redis +pytest -m sql + +# Run tests for multiple backends +pytest -m "mongo or redis" + +# Exclude specific backends +pytest -m "not mongo" + +# Run with verbose output +pytest -v +``` + +### Using the Test Script + +The recommended way to run tests with proper backend setup: + +```bash +# Test single backend +./scripts/test-local.sh mongo + +# Test multiple backends +./scripts/test-local.sh mongo redis sql + +# Test all backends +./scripts/test-local.sh all + +# Run tests in parallel +./scripts/test-local.sh all -p + +# Keep containers running for debugging +./scripts/test-local.sh mongo redis -k +``` + +### Parallel Testing + +Tests can be run in parallel using pytest-xdist: + +```bash +# Run with automatic worker detection +./scripts/test-local.sh all -p + +# Specify number of workers +./scripts/test-local.sh all -p -w 4 + +# Or directly with pytest +pytest -n auto +pytest -n 4 +``` + +## Writing Tests + +### Basic Test Structure + +```python +import pytest +from cachier import cachier + + +def test_basic_caching(): + """Test basic caching functionality.""" + + # Define a cached function local to this test + @cachier() + def expensive_computation(x): + return x**2 + + # First call - should compute + result1 = expensive_computation(5) + assert result1 == 25 + + # Second call - should return from cache + result2 = expensive_computation(5) + assert result2 == 25 + + # Clear cache for cleanup + expensive_computation.clear_cache() +``` + +### Backend-Specific Tests + +```python +@pytest.mark.mongo +def test_mongo_specific_feature(): + """Test MongoDB-specific functionality.""" + from tests.test_mongo_core import _test_mongetter + + @cachier(mongetter=_test_mongetter) + def mongo_cached_func(x): + return x * 2 + + # Test implementation + assert mongo_cached_func(5) == 10 +``` + +## Test Isolation + +### Critical Rule: Function Isolation + +**Never share cachier-decorated functions between test functions.** Each test must have its own decorated function to ensure proper isolation. + +#### Why This Matters + +Cachier identifies cached functions by their full module path and function name. When tests share decorated functions: + +- Cache entries can conflict between tests +- Parallel test execution may fail unpredictably +- Test results become non-deterministic + +#### Good Practice + +```python +def test_feature_one(): + @cachier() + def compute_one(x): # Unique to this test + return x * 2 + + assert compute_one(5) == 10 + + +def test_feature_two(): + @cachier() + def compute_two(x): # Different function for different test + return x * 2 + + assert compute_two(5) == 10 +``` + +#### Bad Practice + +```python +# DON'T DO THIS! +@cachier() +def shared_compute(x): # Shared between tests + return x * 2 + + +def test_feature_one(): + assert shared_compute(5) == 10 # May conflict with test_feature_two + + +def test_feature_two(): + assert shared_compute(5) == 10 # May conflict with test_feature_one +``` + +### Isolation Mechanisms + +1. **Pickle Backend**: Uses `isolated_cache_directory` fixture that creates unique directories per pytest-xdist worker +2. **External Backends**: Rely on function namespacing (module + function name) +3. **Clear Cache**: Always clear cache at test end for cleanup + +### Best Practices for Isolation + +1. Define cached functions inside test functions +2. Use unique, descriptive function names +3. Clear cache after each test +4. Avoid module-level cached functions in tests +5. Use fixtures for common setup/teardown + +## Backend-Specific Testing + +### MongoDB Tests + +```python +@pytest.mark.mongo +def test_mongo_feature(): + """Test with MongoDB backend.""" + + @cachier(mongetter=_test_mongetter, wait_for_calc_timeout=2) + def mongo_func(x): + return x + + # MongoDB-specific assertions + assert mongo_func.get_cache_mongetter() is not None +``` + +### Redis Tests + +```python +@pytest.mark.redis +def test_redis_feature(): + """Test with Redis backend.""" + + @cachier(backend="redis", redis_client=_test_redis_client) + def redis_func(x): + return x + + # Redis-specific testing + assert redis_func(5) == 5 +``` + +### SQL Tests + +```python +@pytest.mark.sql +def test_sql_feature(): + """Test with SQL backend.""" + + @cachier(backend="sql", sql_engine=test_engine) + def sql_func(x): + return x + + # SQL-specific testing + assert sql_func(5) == 5 +``` + +### Memory Tests + +```python +@pytest.mark.memory +def test_memory_feature(): + """Test with memory backend.""" + + @cachier(backend="memory") + def memory_func(x): + return x + + # Memory-specific testing + assert memory_func(5) == 5 +``` + +## Parallel Testing + +### How It Works + +1. pytest-xdist creates multiple worker processes +2. Each worker gets a subset of tests +3. Cachier's function identification ensures natural isolation +4. Pickle backend uses worker-specific cache directories + +### Running Parallel Tests + +```bash +# Automatic worker detection +./scripts/test-local.sh all -p + +# Specify workers +./scripts/test-local.sh all -p -w 4 + +# Direct pytest command +pytest -n auto +``` + +### Parallel Testing Considerations + +1. **Resource Usage**: More workers = more CPU/memory usage +2. **External Services**: Ensure Docker has sufficient resources +3. **Test Output**: May be interleaved; use `-v` for clarity +4. **Debugging**: Harder with parallel execution; use `-n 1` for debugging + +## CI/CD Integration + +### GitHub Actions + +The CI pipeline runs a matrix job per backend. Each backend uses the commands below: + +```bash +# Local backends (memory, pickle, and other non-external tests) +pytest -m "not mongo and not sql and not redis and not s3" + +# MongoDB backend +pytest -m mongo + +# PostgreSQL/SQL backend +pytest -m sql + +# Redis backend +pytest -m redis + +# S3 backend +pytest -m s3 +``` + +Note: local tests do not use `pytest-xdist` (`-n`) in CI. External backends +(MongoDB, PostgreSQL, Redis, S3) each run in their own isolated matrix job with +the corresponding Docker service started beforehand. + +### Environment Variables + +- `CACHIER_TEST_VS_DOCKERIZED_MONGO`: Use real MongoDB in CI +- `CACHIER_TEST_REDIS_HOST`: Redis connection details +- `SQLALCHEMY_DATABASE_URL`: SQL database connection + +## Troubleshooting + +### Common Issues + +1. **Import Errors**: Install backend-specific requirements + + ```bash + pip install -r tests/requirements_redis.txt + ``` + +2. **Docker Not Running**: Start Docker Desktop or daemon + + ```bash + docker ps # Check if Docker is running + ``` + +3. **Port Conflicts**: Stop conflicting services + + ```bash + docker stop cachier-test-mongo cachier-test-redis cachier-test-postgres + ``` + +4. **Flaky Tests**: Usually due to timing issues + + - Increase timeouts + - Add proper waits + - Check for race conditions + +5. **Cache Conflicts**: Ensure function isolation + + - Don't share decorated functions + - Clear cache after tests + - Use unique function names + +### Handling Flaky Tests + +Some tests, particularly in the pickle core module, may occasionally fail due to race conditions in multi-threaded scenarios. To handle these, we use the `pytest-rerunfailures` plugin. + +#### Marking Flaky Tests + +```python +@pytest.mark.flaky(reruns=5, reruns_delay=0.1) +def test_that_may_fail_intermittently(): + """This test will retry up to 5 times with 0.1s delay between attempts.""" + # Test implementation +``` + +#### Current Flaky Tests + +- `test_bad_cache_file`: Tests handling of corrupted cache files with concurrent access +- `test_delete_cache_file`: Tests handling of missing cache files during concurrent operations + +These tests involve race conditions between threads that are difficult to reproduce consistently, so they're configured to retry multiple times before being marked as failed. + +### Debugging Tips + +1. **Run Single Test**: + + ```bash + pytest -k test_name -v + ``` + +2. **Disable Parallel**: + + ```bash + pytest -n 1 + ``` + +3. **Check Logs**: + + ```bash + docker logs cachier-test-mongo + ``` + +4. **Interactive Debugging**: + + ```python + import pdb + + pdb.set_trace() + ``` + +### Performance Considerations + +1. **Test Speed**: Memory/pickle tests are fastest +2. **External Backends**: Add overhead for Docker/network +3. **Parallel Execution**: Speeds up test suite significantly +4. **Cache Size**: Large caches slow down tests + +## Best Practices Summary + +1. **Always** define cached functions inside test functions +2. **Never** share cached functions between tests +3. **Clear** cache after each test +4. **Use** appropriate markers for backend-specific tests +5. **Run** full test suite before submitting PRs +6. **Test** with parallel execution to catch race conditions +7. **Document** any special test requirements +8. **Follow** existing test patterns in the codebase + +## Adding New Tests + +When adding new tests: + +1. Follow existing naming conventions +2. Add appropriate backend markers +3. Ensure function isolation +4. Include docstrings explaining test purpose +5. Test both success and failure cases +6. Consider edge cases and error conditions +7. Run with all backends if applicable +8. Update this documentation if needed + +## Questions or Issues? + +- Check existing tests for examples +- Review the main README.rst +- Open an issue on GitHub +- Contact maintainers listed in README.rst diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..4450efa6 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,196 @@ +"""Pytest configuration and shared fixtures for cachier tests.""" + +import logging +import os +import re +from urllib.parse import parse_qs, unquote, urlencode, urlparse, urlunparse + +import pytest + +logger = logging.getLogger(__name__) + + +def _worker_schema_name(worker_id: str) -> str | None: + """Return a safe SQL schema name for an xdist worker ID.""" + match = re.fullmatch(r"gw(\d+)", worker_id) + if match is None: + return None + return f"test_worker_{match.group(1)}" + + +@pytest.fixture(autouse=True) +def inject_worker_schema_for_sql_tests(monkeypatch, request): + """Automatically inject worker-specific schema into SQL connection string. + + This fixture enables parallel SQL test execution by giving each pytest-xdist worker its own PostgreSQL schema, + preventing table creation conflicts. + + """ + # Only apply to SQL tests + if "sql" not in request.node.keywords: + yield + return + + worker_id = os.environ.get("PYTEST_XDIST_WORKER", "master") + + if worker_id == "master": + # Not running in parallel, no schema isolation needed + yield + return + + # Get the original SQL connection string + original_url = os.environ.get("SQLALCHEMY_DATABASE_URL", "sqlite:///:memory:") + + if "postgresql" in original_url: + # Create worker-specific schema name + schema_name = _worker_schema_name(worker_id) + if schema_name is None: + logger.warning("Unexpected worker ID for SQL schema isolation: %s", worker_id) + yield + return + + # Parse the URL + parsed = urlparse(original_url) + + # Get existing query parameters + query_params = parse_qs(parsed.query) + + # Add or update the options parameter to set search_path + if "options" in query_params: + # Append to existing options + current_options = unquote(query_params["options"][0]) + new_options = f"{current_options} -csearch_path={schema_name}" + else: + # Create new options + new_options = f"-csearch_path={schema_name}" + + query_params["options"] = [new_options] + + # Rebuild the URL with updated query parameters + new_query = urlencode(query_params, doseq=True) + new_url = urlunparse( + ( + parsed.scheme, + parsed.netloc, + parsed.path, + parsed.params, + new_query, + parsed.fragment, + ) + ) + + # Override both the environment variable and the module constant + monkeypatch.setenv("SQLALCHEMY_DATABASE_URL", new_url) + + # Also patch the SQL_CONN_STR constant used in tests + import tests.sql_tests.test_sql_core + + monkeypatch.setattr(tests.sql_tests.test_sql_core, "SQL_CONN_STR", new_url) + + # Ensure schema creation by creating it before tests run + try: + from sqlalchemy import create_engine, text + + # Use original URL to create schema (without search_path) + engine = create_engine(original_url) + with engine.connect() as conn: + conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {schema_name}")) + conn.commit() + engine.dispose() + except Exception as e: + # If we can't create the schema, the test will fail anyway + logger.debug(f"Failed to create schema {schema_name}: {e}") + + yield + + +@pytest.fixture +def worker_id(request): + """Get the pytest-xdist worker ID.""" + return os.environ.get("PYTEST_XDIST_WORKER", "master") + + +@pytest.fixture(autouse=True) +def isolated_cache_directory(tmp_path, monkeypatch, request, worker_id): + """Ensure each test gets an isolated cache directory. + + This is especially important for pickle and maxage tests when running in parallel. Each pytest-xdist worker gets its + own cache directory to avoid conflicts. + + Only applies when running in parallel mode (pytest-xdist), to avoid breaking tests that use module-level path + constants computed from the default cache directory at import time. + + """ + if worker_id != "master" and ("pickle" in request.node.keywords or "maxage" in request.node.keywords): + cache_dir = tmp_path / f"cachier_cache_{worker_id}" + + cache_dir.mkdir(exist_ok=True, parents=True) + + # Monkeypatch the global cache directory for this test + import cachier.config + + monkeypatch.setattr(cachier.config._global_params, "cache_dir", str(cache_dir)) + + +def pytest_collection_modifyitems(items): + """Mark local backends as serial-local for the split test runner flow.""" + for item in items: + if "memory" in item.keywords or "pickle" in item.keywords: + item.add_marker(pytest.mark.seriallocal) + + +@pytest.fixture(scope="session", autouse=True) +def cleanup_test_schemas(request): + """Clean up test schemas after all tests complete. + + This fixture ensures that worker-specific PostgreSQL schemas created during parallel test execution are properly + cleaned up. + + """ + yield # Let all tests run first + + # Cleanup after all tests + worker_id = os.environ.get("PYTEST_XDIST_WORKER", "master") + + if worker_id != "master": + # Clean up the worker-specific schema + original_url = os.environ.get("SQLALCHEMY_DATABASE_URL", "") + + if "postgresql" in original_url: + schema_name = _worker_schema_name(worker_id) + if schema_name is None: + logger.warning("Unexpected worker ID for SQL schema cleanup: %s", worker_id) + return + + try: + from sqlalchemy import create_engine, text + + # Parse URL to remove any schema options for cleanup + parsed = urlparse(original_url) + query_params = parse_qs(parsed.query) + + # Remove options parameter if it exists + query_params.pop("options", None) + + # Rebuild clean URL + clean_query = urlencode(query_params, doseq=True) if query_params else "" + clean_url = urlunparse( + ( + parsed.scheme, + parsed.netloc, + parsed.path, + parsed.params, + clean_query, + parsed.fragment, + ) + ) + + engine = create_engine(clean_url) + with engine.connect() as conn: + # Drop the schema and all its contents + conn.execute(text(f"DROP SCHEMA IF EXISTS {schema_name} CASCADE")) + conn.commit() + engine.dispose() + except Exception as e: + # If cleanup fails, it's not critical + logger.debug(f"Failed to cleanup schema {schema_name}: {e}") diff --git a/tests/requirements.txt b/tests/requirements.txt index 78297278..f4e666d0 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,9 +1,11 @@ # todo: add some version range or pinning latest versions # tests and coverages pytest +pytest-asyncio +pytest-xdist # for parallel test execution +pytest-rerunfailures # for retrying flaky tests coverage pytest-cov -pytest-asyncio birch # to be able to run `python setup.py checkdocs` collective.checkdocs