|
12 | 12 | from sqlalchemy.ext.asyncio import create_async_engine |
13 | 13 | from sqlalchemy.pool import AsyncAdaptedQueuePool, NullPool, QueuePool |
14 | 14 |
|
15 | | -from psqlpy_sqlalchemy.dialect import PSQLPyAsyncDialect, PsqlpyDialect |
| 15 | +from psqlpy_sqlalchemy.dialect import PSQLPyAsyncDialect, PsqlpyDialect, CompatibleNullPool |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class TestPoolclassCompatibility(unittest.TestCase): |
@@ -208,6 +208,82 @@ async def _test_explicit_nullpool(): |
208 | 208 | finally: |
209 | 209 | loop.close() |
210 | 210 |
|
| 211 | + def test_compatible_nullpool_with_pool_args_sync(self): |
| 212 | + """Test that CompatibleNullPool works with pool arguments in sync engines""" |
| 213 | + try: |
| 214 | + self.engine = create_engine( |
| 215 | + "postgresql+psqlpy://user:password@localhost/test", |
| 216 | + poolclass=CompatibleNullPool, |
| 217 | + pool_size=5, |
| 218 | + max_overflow=10, |
| 219 | + ) |
| 220 | + self.assertIsNotNone(self.engine.dialect) |
| 221 | + self.assertEqual(self.engine.dialect.driver, "psqlpy") |
| 222 | + self.assertEqual(self.engine.pool.__class__, CompatibleNullPool) |
| 223 | + except Exception as e: |
| 224 | + # Connection errors are acceptable, we're testing pool creation |
| 225 | + if "Invalid argument(s)" in str(e): |
| 226 | + self.fail(f"CompatibleNullPool should accept pool arguments: {e}") |
| 227 | + |
| 228 | + def test_compatible_nullpool_with_pool_args_async(self): |
| 229 | + """Test that CompatibleNullPool works with pool arguments in async engines""" |
| 230 | + |
| 231 | + async def _test_compatible_nullpool(): |
| 232 | + try: |
| 233 | + self.async_engine = create_async_engine( |
| 234 | + "postgresql+psqlpy://user:password@localhost/test", |
| 235 | + poolclass=CompatibleNullPool, |
| 236 | + pool_size=5, |
| 237 | + max_overflow=10, |
| 238 | + ) |
| 239 | + self.assertIsNotNone(self.async_engine) |
| 240 | + self.assertEqual( |
| 241 | + self.async_engine.sync_engine.pool.__class__, CompatibleNullPool |
| 242 | + ) |
| 243 | + return True |
| 244 | + except Exception as e: |
| 245 | + # Connection errors are acceptable, we're testing pool creation |
| 246 | + if "Invalid argument(s)" in str(e): |
| 247 | + self.fail(f"CompatibleNullPool should accept pool arguments: {e}") |
| 248 | + return True |
| 249 | + |
| 250 | + loop = asyncio.new_event_loop() |
| 251 | + asyncio.set_event_loop(loop) |
| 252 | + try: |
| 253 | + result = loop.run_until_complete(_test_compatible_nullpool()) |
| 254 | + self.assertTrue(result) |
| 255 | + finally: |
| 256 | + loop.close() |
| 257 | + |
| 258 | + def test_compatible_nullpool_ignores_pool_args(self): |
| 259 | + """Test that CompatibleNullPool ignores pool sizing arguments""" |
| 260 | + try: |
| 261 | + self.engine = create_engine( |
| 262 | + "postgresql+psqlpy://user:password@localhost/test", |
| 263 | + poolclass=CompatibleNullPool, |
| 264 | + pool_size=100, # Should be ignored |
| 265 | + max_overflow=200, # Should be ignored |
| 266 | + ) |
| 267 | + # If we get here, the arguments were successfully ignored |
| 268 | + self.assertIsNotNone(self.engine.dialect) |
| 269 | + self.assertEqual(self.engine.pool.__class__, CompatibleNullPool) |
| 270 | + except Exception as e: |
| 271 | + # Connection errors are acceptable |
| 272 | + if "Invalid argument(s)" in str(e): |
| 273 | + self.fail(f"CompatibleNullPool should ignore pool arguments: {e}") |
| 274 | + |
| 275 | + def test_regular_nullpool_still_fails_with_pool_args(self): |
| 276 | + """Test that regular NullPool still fails with pool arguments (regression test)""" |
| 277 | + with self.assertRaises(TypeError) as context: |
| 278 | + self.engine = create_engine( |
| 279 | + "postgresql+psqlpy://user:password@localhost/test", |
| 280 | + poolclass=NullPool, |
| 281 | + pool_size=5, |
| 282 | + max_overflow=10, |
| 283 | + ) |
| 284 | + |
| 285 | + self.assertIn("Invalid argument(s) 'pool_size','max_overflow'", str(context.exception)) |
| 286 | + |
211 | 287 |
|
212 | 288 | class TestFastAPIMiddlewareCompatibility(unittest.TestCase): |
213 | 289 | """Test cases for FastAPI middleware compatibility""" |
|
0 commit comments