|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# Copyright (c) 2026, LlamaIndex |
| 3 | + |
| 4 | +"""PostgreSQL testcontainers connectivity tests. |
| 5 | +
|
| 6 | +These tests verify that the testcontainers PostgreSQL integration works correctly. |
| 7 | +They are marked with the 'docker' marker and require Docker to be running. |
| 8 | +
|
| 9 | +Run with: pytest -m docker |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from typing import Generator |
| 15 | + |
| 16 | +import pytest |
| 17 | +from testcontainers.postgres import PostgresContainer |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="module") |
| 21 | +def postgres_container() -> Generator[PostgresContainer, None, None]: |
| 22 | + """Module-scoped PostgreSQL container for connectivity tests. |
| 23 | +
|
| 24 | + Requires Docker to be running. |
| 25 | + """ |
| 26 | + from testcontainers.postgres import PostgresContainer |
| 27 | + |
| 28 | + with PostgresContainer("postgres:16", driver=None) as postgres: |
| 29 | + yield postgres |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.docker |
| 33 | +def test_postgres_container_starts(postgres_container: PostgresContainer) -> None: |
| 34 | + """Test that the PostgreSQL container starts successfully.""" |
| 35 | + # Container should be running if we got here |
| 36 | + assert postgres_container is not None |
| 37 | + connection_url = postgres_container.get_connection_url() |
| 38 | + assert "postgresql" in connection_url or "postgres" in connection_url |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.docker |
| 42 | +def test_postgres_basic_query(postgres_container: PostgresContainer) -> None: |
| 43 | + """Test basic SQL query execution against the PostgreSQL container.""" |
| 44 | + import psycopg |
| 45 | + |
| 46 | + connection_url = postgres_container.get_connection_url() |
| 47 | + |
| 48 | + with psycopg.connect(connection_url) as conn: |
| 49 | + with conn.cursor() as cur: |
| 50 | + cur.execute("SELECT 1 AS test_value") |
| 51 | + result = cur.fetchone() |
| 52 | + assert result is not None |
| 53 | + assert result[0] == 1 |
0 commit comments