Skip to content

Commit 5dd81fa

Browse files
committed
one test per block
1 parent 642816a commit 5dd81fa

File tree

8 files changed

+123
-123
lines changed

8 files changed

+123
-123
lines changed
Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
"""Test configuration example: Environment-based configuration."""
22

3-
import os
4-
from unittest.mock import patch
5-
6-
import pytest
7-
83

94
def test_extension_config() -> None:
105
from sqlspec.adapters.asyncpg import AsyncpgConfig
@@ -30,39 +25,3 @@ def test_extension_config() -> None:
3025
"enable_correlation_middleware": True,
3126
}
3227
}
33-
34-
35-
POSTGRES_PORT = 5433
36-
37-
38-
@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled")
39-
def test_environment_based_configuration() -> None:
40-
"""Test environment-based configuration pattern."""
41-
42-
# Mock environment variables
43-
env_vars = {
44-
"DB_HOST": "testhost",
45-
"DB_PORT": "5433",
46-
"DB_USER": "testuser",
47-
"DB_PASSWORD": "testpass",
48-
"DB_NAME": "testdb",
49-
}
50-
51-
with patch.dict(os.environ, env_vars, clear=False):
52-
from sqlspec.adapters.asyncpg import AsyncpgConfig
53-
54-
config = AsyncpgConfig(
55-
pool_config={
56-
"host": os.getenv("DB_HOST", "localhost"),
57-
"port": int(os.getenv("DB_PORT", "5432")),
58-
"user": os.getenv("DB_USER"),
59-
"password": os.getenv("DB_PASSWORD"),
60-
"database": os.getenv("DB_NAME"),
61-
}
62-
)
63-
64-
assert config.pool_config["host"] == "testhost"
65-
assert config.pool_config["port"] == POSTGRES_PORT
66-
assert config.pool_config["user"] == "testuser"
67-
assert config.pool_config["password"] == "testpass"
68-
assert config.pool_config["database"] == "testdb"
Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
"""Test configuration example: Best practice - Use connection pooling."""
1+
"""Test configuration example: Environment-based configuration."""
2+
3+
import os
4+
from unittest.mock import patch
25

36
import pytest
47

5-
MIN_POOL_SIZE = 10
6-
MAX_POOL_SIZE = 20
8+
POSTGRES_PORT = 5433
9+
10+
11+
@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled")
12+
def test_environment_based_configuration() -> None:
13+
"""Test environment-based configuration pattern."""
714

15+
# Mock environment variables
16+
env_vars = {
17+
"DB_HOST": "testhost",
18+
"DB_PORT": "5433",
19+
"DB_USER": "testuser",
20+
"DB_PASSWORD": "testpass",
21+
"DB_NAME": "testdb",
22+
}
823

9-
@pytest.mark.skipif(
10-
not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled"
11-
)
12-
def test_connection_pooling_best_practice() -> None:
13-
"""Test connection pooling best practice configuration."""
14-
from sqlspec.adapters.asyncpg import AsyncpgConfig
24+
with patch.dict(os.environ, env_vars, clear=False):
25+
from sqlspec.adapters.asyncpg import AsyncpgConfig
1526

16-
config = AsyncpgConfig(
17-
pool_config={"dsn": "postgresql://localhost/db", "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE}
18-
)
27+
config = AsyncpgConfig(
28+
pool_config={
29+
"host": os.getenv("DB_HOST", "localhost"),
30+
"port": int(os.getenv("DB_PORT", "5432")),
31+
"user": os.getenv("DB_USER"),
32+
"password": os.getenv("DB_PASSWORD"),
33+
"database": os.getenv("DB_NAME"),
34+
}
35+
)
1936

20-
assert config.pool_config["min_size"] == MIN_POOL_SIZE
21-
assert config.pool_config["max_size"] == MAX_POOL_SIZE
22-
assert config.supports_connection_pooling is True
37+
assert config.pool_config["host"] == "testhost"
38+
assert config.pool_config["port"] == POSTGRES_PORT
39+
assert config.pool_config["user"] == "testuser"
40+
assert config.pool_config["password"] == "testpass"
41+
assert config.pool_config["database"] == "testdb"
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
"""Test configuration example: Best practice - Enable caching."""
1+
"""Test configuration example: Best practice - Use connection pooling."""
22

3+
import pytest
34

4-
def test_enable_caching_best_practice() -> None:
5-
"""Test caching best practice configuration."""
6-
from sqlspec.core.statement import StatementConfig
5+
MIN_POOL_SIZE = 10
6+
MAX_POOL_SIZE = 20
77

8-
statement_config = StatementConfig(dialect="postgres", enable_caching=True)
98

10-
assert statement_config.enable_caching is True
11-
assert statement_config.dialect == "postgres"
9+
@pytest.mark.skipif(
10+
not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled"
11+
)
12+
def test_connection_pooling_best_practice() -> None:
13+
"""Test connection pooling best practice configuration."""
14+
from sqlspec.adapters.asyncpg import AsyncpgConfig
15+
16+
config = AsyncpgConfig(
17+
pool_config={"dsn": "postgresql://localhost/db", "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE}
18+
)
19+
20+
assert config.pool_config["min_size"] == MIN_POOL_SIZE
21+
assert config.pool_config["max_size"] == MAX_POOL_SIZE
22+
assert config.supports_connection_pooling is True
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1-
"""Test configuration example: Best practice - Tune pool sizes."""
1+
"""Test configuration example: Best practice - Enable caching."""
22

3-
MIN_POOL_SIZE_CPU = 5
4-
MAX_POOL_SIZE_CPU = 10
5-
MIN_IO_BOUND_POOL_SIZE = 20
6-
MAX_IO_BOUND_POOL_SIZE = 50
7-
8-
9-
def test_tune_pool_sizes_best_practice() -> None:
10-
"""Test pool sizing best practices for different workloads."""
11-
12-
# CPU-bound workload - smaller pool
13-
cpu_bound_pool_config = {"min_size": MIN_POOL_SIZE_CPU, "max_size": MAX_POOL_SIZE_CPU}
14-
assert cpu_bound_pool_config["min_size"] == MIN_POOL_SIZE_CPU
15-
assert cpu_bound_pool_config["max_size"] == MAX_POOL_SIZE_CPU
16-
17-
# I/O-bound workload - larger pool
18-
io_bound_pool_config = {"min_size": MIN_IO_BOUND_POOL_SIZE, "max_size": MAX_IO_BOUND_POOL_SIZE}
19-
assert io_bound_pool_config["min_size"] == MIN_IO_BOUND_POOL_SIZE
20-
assert io_bound_pool_config["max_size"] == MAX_IO_BOUND_POOL_SIZE
21-
22-
23-
def test_disable_security_checks_best_practice() -> None:
24-
"""Test disabling security checks when necessary."""
253

4+
def test_enable_caching_best_practice() -> None:
5+
"""Test caching best practice configuration."""
266
from sqlspec.core.statement import StatementConfig
277

28-
# Example: Disabling security checks for trusted internal queries
29-
statement_config = StatementConfig(
30-
dialect="postgres",
31-
enable_validation=False, # Skip security checks
32-
)
33-
assert statement_config.enable_validation is False
8+
statement_config = StatementConfig(dialect="postgres", enable_caching=True)
9+
10+
assert statement_config.enable_caching is True
11+
assert statement_config.dialect == "postgres"
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
"""Test configuration example: Best practice - Clean up resources."""
1+
"""Test configuration example: Best practice - Tune pool sizes."""
22

3-
import pytest
3+
MIN_POOL_SIZE_CPU = 5
4+
MAX_POOL_SIZE_CPU = 10
5+
MIN_IO_BOUND_POOL_SIZE = 20
6+
MAX_IO_BOUND_POOL_SIZE = 50
47

58

6-
@pytest.mark.asyncio
7-
async def test_cleanup_resources_best_practice() -> None:
8-
"""Test resource cleanup best practice."""
9-
import tempfile
9+
def test_tune_pool_sizes_best_practice() -> None:
10+
"""Test pool sizing best practices for different workloads."""
1011

11-
from sqlspec import SQLSpec
12-
from sqlspec.adapters.aiosqlite import AiosqliteConfig
12+
# CPU-bound workload - smaller pool
13+
cpu_bound_pool_config = {"min_size": MIN_POOL_SIZE_CPU, "max_size": MAX_POOL_SIZE_CPU}
14+
assert cpu_bound_pool_config["min_size"] == MIN_POOL_SIZE_CPU
15+
assert cpu_bound_pool_config["max_size"] == MAX_POOL_SIZE_CPU
1316

14-
with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp:
15-
spec = SQLSpec()
16-
db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name}))
17-
18-
# Use the connection
19-
async with spec.provide_session(db) as session:
20-
await session.execute("CREATE TABLE test (id INTEGER)")
21-
22-
# Clean up resources - important for async adapters
23-
await spec.close_all_pools()
24-
25-
# Verify pools are closed
26-
assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool")
17+
# I/O-bound workload - larger pool
18+
io_bound_pool_config = {"min_size": MIN_IO_BOUND_POOL_SIZE, "max_size": MAX_IO_BOUND_POOL_SIZE}
19+
assert io_bound_pool_config["min_size"] == MIN_IO_BOUND_POOL_SIZE
20+
assert io_bound_pool_config["max_size"] == MAX_IO_BOUND_POOL_SIZE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Test configuration example: Best practice - Tune pool sizes."""
2+
3+
def test_disable_security_checks_best_practice() -> None:
4+
"""Test disabling security checks when necessary."""
5+
6+
from sqlspec.core.statement import StatementConfig
7+
8+
# Example: Disabling security checks for trusted internal queries
9+
statement_config = StatementConfig(
10+
dialect="postgres",
11+
enable_validation=False, # Skip security checks
12+
)
13+
assert statement_config.enable_validation is False
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test configuration example: Best practice - Clean up resources."""
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_cleanup_resources_best_practice() -> None:
8+
"""Test resource cleanup best practice."""
9+
import tempfile
10+
11+
from sqlspec import SQLSpec
12+
from sqlspec.adapters.aiosqlite import AiosqliteConfig
13+
14+
with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp:
15+
spec = SQLSpec()
16+
db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name}))
17+
18+
# Use the connection
19+
async with spec.provide_session(db) as session:
20+
await session.execute("CREATE TABLE test (id INTEGER)")
21+
22+
# Clean up resources - important for async adapters
23+
await spec.close_all_pools()
24+
25+
# Verify pools are closed
26+
assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool")

docs/usage/configuration.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,18 @@ Litestar Plugin Configuration
292292
.. literalinclude:: /examples/usage/test_configuration_23.py
293293
:language: python
294294
:caption: `litestar plugin configuration`
295-
:lines: 10-31
295+
:lines: 2-26
296296
:dedent: 2
297297

298298
Environment-Based Configuration
299299
-------------------------------
300300

301301
Use environment variables for configuration:
302302

303-
.. literalinclude:: /examples/usage/test_configuration_23.py
303+
.. literalinclude:: /examples/usage/test_configuration_24.py
304304
:language: python
305305
:caption: `environnment-based configuration`
306-
:lines: 49-59
306+
:lines: 25-41
307307
:dedent: 4
308308

309309
Configuration Best Practices
@@ -313,7 +313,7 @@ Configuration Best Practices
313313

314314
Always use pooling in production:
315315

316-
.. literalinclude:: /examples/usage/test_configuration_24.py
316+
.. literalinclude:: /examples/usage/test_configuration_25.py
317317
:language: python
318318
:caption: `connection pooling`
319319
:lines: 11-13
@@ -323,7 +323,7 @@ Always use pooling in production:
323323

324324
Enable caching to avoid recompiling SQL statements:
325325

326-
.. literalinclude:: /examples/usage/test_configuration_25.py
326+
.. literalinclude:: /examples/usage/test_configuration_26.py
327327
:language: python
328328
:caption: `enable caching`
329329
:lines: 6-8
@@ -333,28 +333,28 @@ Enable caching to avoid recompiling SQL statements:
333333

334334
Size pools based on your workload:
335335

336-
.. literalinclude:: /examples/usage/test_configuration_26.py
336+
.. literalinclude:: /examples/usage/test_configuration_27.py
337337
:language: python
338338
:caption: `tune pool sizes`
339-
:lines: 6-14
339+
:lines: 3-20
340340
:dedent: 2
341341

342342
**4. Disable Validation in Production**
343343

344344
For trusted, performance-critical queries:
345345

346-
.. literalinclude:: /examples/usage/test_configuration_26.py
346+
.. literalinclude:: /examples/usage/test_configuration_28.py
347347
:language: python
348348
:caption: `no validation`
349-
:lines: 19-25
349+
:lines: 6-12
350350
:dedent: 2
351351

352352

353353
**5. Clean Up Resources**
354354

355355
Always close pools on shutdown:
356356

357-
.. literalinclude:: /examples/usage/test_configuration_27.py
357+
.. literalinclude:: /examples/usage/test_configuration_29.py
358358
:language: python
359359
:caption: `cleanup resources`
360360
:lines: 10-27

0 commit comments

Comments
 (0)