Skip to content

Commit 6edf1fe

Browse files
euri10cofin
authored andcommitted
docs: usage cli tests
1 parent 6044ff6 commit 6edf1fe

File tree

3 files changed

+68
-49
lines changed

3 files changed

+68
-49
lines changed

docs/examples/usage/usage_cli_1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from sqlspec.adapters.asyncpg import AsyncpgConfig
2+
3+
4+
def test_single_and_multiple_configs() -> None:
5+
# start-example
6+
# Single config
7+
db_config = AsyncpgConfig(
8+
pool_config={"dsn": "postgresql://user:pass@localhost/mydb"},
9+
migration_config={"script_location": "migrations", "enabled": True},
10+
)
11+
12+
# Multiple configs
13+
configs = [
14+
AsyncpgConfig(
15+
bind_key="postgres",
16+
pool_config={"dsn": "postgresql://..."},
17+
migration_config={"script_location": "migrations/postgres"},
18+
)
19+
# ... more configs
20+
]
21+
22+
# Callable function
23+
def get_configs() -> list[AsyncpgConfig]:
24+
return [db_config]
25+
26+
# end-example
27+
assert isinstance(db_config, AsyncpgConfig)
28+
assert isinstance(configs, list)
29+
assert callable(get_configs)
30+
assert get_configs()[0] is db_config

docs/examples/usage/usage_cli_2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from sqlspec.adapters.asyncmy import AsyncmyConfig
2+
from sqlspec.adapters.asyncpg import AsyncpgConfig
3+
4+
5+
def test_multi_config() -> None:
6+
# start-example
7+
configs = [
8+
AsyncpgConfig(
9+
bind_key="postgres",
10+
pool_config={"dsn": "postgresql://..."},
11+
migration_config={"script_location": "migrations/postgres", "enabled": True}
12+
),
13+
AsyncmyConfig(
14+
bind_key="mysql",
15+
pool_config={"host": "localhost", "database": "mydb"},
16+
migration_config={"script_location": "migrations/mysql", "enabled": True}
17+
),
18+
AsyncpgConfig(
19+
bind_key="analytics",
20+
pool_config={"dsn": "postgresql://analytics/..."},
21+
migration_config={"script_location": "migrations/analytics", "enabled": True}
22+
),
23+
]
24+
# end-example
25+
assert isinstance(configs, list)
26+
assert all(hasattr(cfg, "bind_key") for cfg in configs)

docs/usage/cli.rst

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -218,32 +218,12 @@ The ``--config`` option accepts a dotted path to either:
218218

219219
Example configuration file (``myapp/config.py``):
220220

221-
.. code-block:: python
222-
223-
from sqlspec.adapters.asyncpg import AsyncpgConfig
224-
225-
# Single config
226-
db_config = AsyncpgConfig(
227-
pool_config={"dsn": "postgresql://user:pass@localhost/mydb"},
228-
migration_config={
229-
"script_location": "migrations",
230-
"enabled": True
231-
}
232-
)
233-
234-
# Multiple configs
235-
configs = [
236-
AsyncpgConfig(
237-
bind_key="postgres",
238-
pool_config={"dsn": "postgresql://..."},
239-
migration_config={"script_location": "migrations/postgres"}
240-
),
241-
# ... more configs
242-
]
243-
244-
# Callable function
245-
def get_configs():
246-
return [db_config]
221+
.. literalinclude:: /examples/usage/usage_cli_1.py
222+
:language: python
223+
:dedent: 0
224+
:start-after: # start-example
225+
:end-before: # end-example
226+
:caption: `configuration loading`
247227

248228
Global Options
249229
--------------
@@ -769,29 +749,12 @@ them collectively or selectively.
769749
Scenario: Multiple Databases
770750
-----------------------------
771751

772-
.. code-block:: python
773-
774-
# config.py
775-
from sqlspec.adapters.asyncpg import AsyncpgConfig
776-
from sqlspec.adapters.asyncmy import AsyncmyConfig
777-
778-
configs = [
779-
AsyncpgConfig(
780-
bind_key="postgres",
781-
pool_config={"dsn": "postgresql://..."},
782-
migration_config={"script_location": "migrations/postgres", "enabled": True}
783-
),
784-
AsyncmyConfig(
785-
bind_key="mysql",
786-
pool_config={"host": "localhost", "database": "mydb"},
787-
migration_config={"script_location": "migrations/mysql", "enabled": True}
788-
),
789-
AsyncpgConfig(
790-
bind_key="analytics",
791-
pool_config={"dsn": "postgresql://analytics/..."},
792-
migration_config={"script_location": "migrations/analytics", "enabled": True}
793-
),
794-
]
752+
.. literalinclude:: /examples/usage/usage_cli_2.py
753+
:language: python
754+
:dedent: 0
755+
:start-after: # start-example
756+
:end-before: # end-example
757+
:caption: `multiple databases`
795758

796759
Upgrade All Enabled Configs
797760
----------------------------

0 commit comments

Comments
 (0)