|
16 | 16 |
|
17 | 17 | logging.basicConfig(level=logging.INFO) |
18 | 18 |
|
| 19 | +# Control whether to use testcontainers |
| 20 | +USE_TESTCONTAINERS = os.getenv('USE_TESTCONTAINERS', 'true').lower() == 'true' |
| 21 | + |
| 22 | +# Disable Ryuk if not explicitly enabled (solves Docker connectivity issues) |
| 23 | +if 'TESTCONTAINERS_RYUK_DISABLED' not in os.environ: |
| 24 | + os.environ['TESTCONTAINERS_RYUK_DISABLED'] = 'true' |
| 25 | + |
| 26 | +# Import testcontainers conditionally |
| 27 | +if USE_TESTCONTAINERS: |
| 28 | + try: |
| 29 | + from testcontainers.postgres import PostgresContainer |
| 30 | + from testcontainers.redis import RedisContainer |
| 31 | + |
| 32 | + TESTCONTAINERS_AVAILABLE = True |
| 33 | + except ImportError: |
| 34 | + TESTCONTAINERS_AVAILABLE = False |
| 35 | + logging.warning('Testcontainers not available. Falling back to manual configuration.') |
| 36 | +else: |
| 37 | + TESTCONTAINERS_AVAILABLE = False |
| 38 | + |
19 | 39 |
|
20 | 40 | # Shared configuration fixtures |
21 | 41 | @pytest.fixture(scope='session') |
@@ -91,6 +111,75 @@ def test_config(): |
91 | 111 | } |
92 | 112 |
|
93 | 113 |
|
| 114 | +# Testcontainers fixtures |
| 115 | +@pytest.fixture(scope='session') |
| 116 | +def postgres_container(): |
| 117 | + """PostgreSQL container for integration tests""" |
| 118 | + if not TESTCONTAINERS_AVAILABLE: |
| 119 | + pytest.skip('Testcontainers not available') |
| 120 | + |
| 121 | + container = PostgresContainer(image='postgres:13', username='test_user', password='test_pass', dbname='test_db') |
| 122 | + container.start() |
| 123 | + |
| 124 | + yield container |
| 125 | + |
| 126 | + container.stop() |
| 127 | + |
| 128 | + |
| 129 | +@pytest.fixture(scope='session') |
| 130 | +def redis_container(): |
| 131 | + """Redis container for integration tests""" |
| 132 | + if not TESTCONTAINERS_AVAILABLE: |
| 133 | + pytest.skip('Testcontainers not available') |
| 134 | + |
| 135 | + container = RedisContainer(image='redis:7-alpine') |
| 136 | + container.start() |
| 137 | + |
| 138 | + yield container |
| 139 | + |
| 140 | + container.stop() |
| 141 | + |
| 142 | + |
| 143 | +@pytest.fixture(scope='session') |
| 144 | +def postgresql_test_config(request): |
| 145 | + """PostgreSQL configuration from testcontainer or environment""" |
| 146 | + if TESTCONTAINERS_AVAILABLE and USE_TESTCONTAINERS: |
| 147 | + # Get the postgres_container fixture |
| 148 | + postgres_container = request.getfixturevalue('postgres_container') |
| 149 | + return { |
| 150 | + 'host': postgres_container.get_container_host_ip(), |
| 151 | + 'port': postgres_container.get_exposed_port(5432), |
| 152 | + 'database': 'test_db', |
| 153 | + 'user': 'test_user', |
| 154 | + 'password': 'test_pass', |
| 155 | + 'max_connections': 10, |
| 156 | + 'batch_size': 10000, |
| 157 | + } |
| 158 | + else: |
| 159 | + # Fall back to manual config from environment |
| 160 | + return request.getfixturevalue('postgresql_config') |
| 161 | + |
| 162 | + |
| 163 | +@pytest.fixture(scope='session') |
| 164 | +def redis_test_config(request): |
| 165 | + """Redis configuration from testcontainer or environment""" |
| 166 | + if TESTCONTAINERS_AVAILABLE and USE_TESTCONTAINERS: |
| 167 | + # Get the redis_container fixture |
| 168 | + redis_container = request.getfixturevalue('redis_container') |
| 169 | + return { |
| 170 | + 'host': redis_container.get_container_host_ip(), |
| 171 | + 'port': redis_container.get_exposed_port(6379), |
| 172 | + 'db': 0, |
| 173 | + 'password': None, # Default Redis container has no password |
| 174 | + 'max_connections': 10, |
| 175 | + 'batch_size': 100, |
| 176 | + 'pipeline_size': 500, |
| 177 | + } |
| 178 | + else: |
| 179 | + # Fall back to manual config from environment |
| 180 | + return request.getfixturevalue('redis_config') |
| 181 | + |
| 182 | + |
94 | 183 | @pytest.fixture(scope='session') |
95 | 184 | def delta_test_env(): |
96 | 185 | """Create Delta Lake test environment for the session""" |
|
0 commit comments