fastapi-toolsets 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_toolsets-0.1.0/LICENSE +21 -0
- fastapi_toolsets-0.1.0/PKG-INFO +89 -0
- fastapi_toolsets-0.1.0/README.md +41 -0
- fastapi_toolsets-0.1.0/pyproject.toml +82 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/__init__.py +24 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/cli/__init__.py +5 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/cli/app.py +97 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/cli/commands/__init__.py +1 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/cli/commands/fixtures.py +225 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/crud.py +378 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/db.py +175 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/exceptions/__init__.py +19 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/exceptions/exceptions.py +166 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/exceptions/handler.py +169 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/fixtures/__init__.py +17 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/fixtures/fixtures.py +321 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/fixtures/pytest_plugin.py +204 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/py.typed +0 -0
- fastapi_toolsets-0.1.0/src/fastapi_toolsets/schemas.py +116 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 d3vyce
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-toolsets
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reusable tools for FastAPI: async CRUD, fixtures, CLI, and standardized responses for SQLAlchemy + PostgreSQL
|
|
5
|
+
Keywords: fastapi,sqlalchemy,postgresql
|
|
6
|
+
Author: d3vyce
|
|
7
|
+
Author-email: d3vyce <contact@d3vyce.fr>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Framework :: FastAPI
|
|
13
|
+
Classifier: Framework :: Pydantic
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Information Technology
|
|
16
|
+
Classifier: Intended Audience :: System Administrators
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
25
|
+
Classifier: Topic :: Software Development
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Dist: fastapi>=0.100.0
|
|
28
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0
|
|
29
|
+
Requires-Dist: asyncpg>=0.29.0
|
|
30
|
+
Requires-Dist: pydantic>=2.0
|
|
31
|
+
Requires-Dist: typer>=0.9.0
|
|
32
|
+
Requires-Dist: httpx>=0.25.0
|
|
33
|
+
Requires-Dist: fastapi-toolsets[test] ; extra == 'dev'
|
|
34
|
+
Requires-Dist: ruff>=0.1.0 ; extra == 'dev'
|
|
35
|
+
Requires-Dist: ty>=0.0.1a0 ; extra == 'dev'
|
|
36
|
+
Requires-Dist: pytest>=8.0.0 ; extra == 'test'
|
|
37
|
+
Requires-Dist: pytest-anyio>=0.0.0 ; extra == 'test'
|
|
38
|
+
Requires-Dist: coverage>=7.0.0 ; extra == 'test'
|
|
39
|
+
Requires-Dist: pytest-cov>=4.0.0 ; extra == 'test'
|
|
40
|
+
Requires-Python: >=3.11
|
|
41
|
+
Project-URL: Homepage, https://github.com/d3vyce/fastapi-toolsets
|
|
42
|
+
Project-URL: Documentation, https://fastapi-toolsets.d3vyce.fr/
|
|
43
|
+
Project-URL: Repository, https://github.com/d3vyce/fastapi-toolsets
|
|
44
|
+
Project-URL: Issues, https://github.com/d3vyce/fastapi-toolsets/issues
|
|
45
|
+
Provides-Extra: dev
|
|
46
|
+
Provides-Extra: test
|
|
47
|
+
Description-Content-Type: text/markdown
|
|
48
|
+
|
|
49
|
+
# FastAPI Toolsets
|
|
50
|
+
|
|
51
|
+
FastAPI Toolsets provides production-ready utilities for FastAPI applications built with async SQLAlchemy and PostgreSQL. It includes generic CRUD operations, a fixture system with dependency resolution, a Django-like CLI, standardized API responses, and structured exception handling with automatic OpenAPI documentation.
|
|
52
|
+
|
|
53
|
+
[](https://github.com/d3vyce/fastapi-toolsets/actions/workflows/ci.yml)
|
|
54
|
+
[](https://codecov.io/gh/d3vyce/fastapi-toolsets)
|
|
55
|
+
[](https://github.com/astral-sh/ty)
|
|
56
|
+
[](https://github.com/astral-sh/uv)
|
|
57
|
+
[](https://github.com/astral-sh/ruff)
|
|
58
|
+
[](https://www.python.org/downloads/)
|
|
59
|
+
[](https://opensource.org/licenses/MIT)
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
**Documentation**: [https://fastapi-toolsets.d3vyce.fr](https://fastapi-toolsets.d3vyce.fr)
|
|
64
|
+
|
|
65
|
+
**Source Code**: [https://github.com/d3vyce/fastapi-toolsets](https://github.com/d3vyce/fastapi-toolsets)
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
uv add fastapi-toolsets
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Features
|
|
76
|
+
|
|
77
|
+
- **CRUD**: Generic async CRUD operations with `CrudFactory`
|
|
78
|
+
- **Fixtures**: Fixture system with dependency management, context support and pytest integration
|
|
79
|
+
- **CLI**: Django-like command-line interface for fixtures and custom commands
|
|
80
|
+
- **Standardized API Responses**: Consistent response format across your API
|
|
81
|
+
- **Exception Handling**: Structured error responses with automatic OpenAPI documentation
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
86
|
+
|
|
87
|
+
## Contributing
|
|
88
|
+
|
|
89
|
+
Contributions are welcome! Please feel free to submit issues and pull requests.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# FastAPI Toolsets
|
|
2
|
+
|
|
3
|
+
FastAPI Toolsets provides production-ready utilities for FastAPI applications built with async SQLAlchemy and PostgreSQL. It includes generic CRUD operations, a fixture system with dependency resolution, a Django-like CLI, standardized API responses, and structured exception handling with automatic OpenAPI documentation.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/d3vyce/fastapi-toolsets/actions/workflows/ci.yml)
|
|
6
|
+
[](https://codecov.io/gh/d3vyce/fastapi-toolsets)
|
|
7
|
+
[](https://github.com/astral-sh/ty)
|
|
8
|
+
[](https://github.com/astral-sh/uv)
|
|
9
|
+
[](https://github.com/astral-sh/ruff)
|
|
10
|
+
[](https://www.python.org/downloads/)
|
|
11
|
+
[](https://opensource.org/licenses/MIT)
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
**Documentation**: [https://fastapi-toolsets.d3vyce.fr](https://fastapi-toolsets.d3vyce.fr)
|
|
16
|
+
|
|
17
|
+
**Source Code**: [https://github.com/d3vyce/fastapi-toolsets](https://github.com/d3vyce/fastapi-toolsets)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
uv add fastapi-toolsets
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- **CRUD**: Generic async CRUD operations with `CrudFactory`
|
|
30
|
+
- **Fixtures**: Fixture system with dependency management, context support and pytest integration
|
|
31
|
+
- **CLI**: Django-like command-line interface for fixtures and custom commands
|
|
32
|
+
- **Standardized API Responses**: Consistent response format across your API
|
|
33
|
+
- **Exception Handling**: Structured error responses with automatic OpenAPI documentation
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
38
|
+
|
|
39
|
+
## Contributing
|
|
40
|
+
|
|
41
|
+
Contributions are welcome! Please feel free to submit issues and pull requests.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fastapi-toolsets"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Reusable tools for FastAPI: async CRUD, fixtures, CLI, and standardized responses for SQLAlchemy + PostgreSQL"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "d3vyce", email = "contact@d3vyce.fr" }
|
|
11
|
+
]
|
|
12
|
+
keywords = ["fastapi", "sqlalchemy", "postgresql"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Framework :: AsyncIO",
|
|
16
|
+
"Framework :: FastAPI",
|
|
17
|
+
"Framework :: Pydantic",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Intended Audience :: Information Technology",
|
|
20
|
+
"Intended Audience :: System Administrators",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
28
|
+
"Topic :: Software Development :: Libraries",
|
|
29
|
+
"Topic :: Software Development",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
dependencies = [
|
|
33
|
+
"fastapi>=0.100.0",
|
|
34
|
+
"sqlalchemy[asyncio]>=2.0",
|
|
35
|
+
"asyncpg>=0.29.0",
|
|
36
|
+
"pydantic>=2.0",
|
|
37
|
+
"typer>=0.9.0",
|
|
38
|
+
"httpx>=0.25.0",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/d3vyce/fastapi-toolsets"
|
|
43
|
+
Documentation = "https://fastapi-toolsets.d3vyce.fr/"
|
|
44
|
+
Repository = "https://github.com/d3vyce/fastapi-toolsets"
|
|
45
|
+
Issues = "https://github.com/d3vyce/fastapi-toolsets/issues"
|
|
46
|
+
|
|
47
|
+
[project.optional-dependencies]
|
|
48
|
+
test = [
|
|
49
|
+
"pytest>=8.0.0",
|
|
50
|
+
"pytest-anyio>=0.0.0",
|
|
51
|
+
"coverage>=7.0.0",
|
|
52
|
+
"pytest-cov>=4.0.0",
|
|
53
|
+
]
|
|
54
|
+
dev = [
|
|
55
|
+
"fastapi-toolsets[test]",
|
|
56
|
+
"ruff>=0.1.0",
|
|
57
|
+
"ty>=0.0.1a0",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[project.scripts]
|
|
61
|
+
fastapi-toolsets = "fastapi_toolsets.cli:app"
|
|
62
|
+
|
|
63
|
+
[build-system]
|
|
64
|
+
requires = ["uv_build>=0.9.26,<0.10.0"]
|
|
65
|
+
build-backend = "uv_build"
|
|
66
|
+
|
|
67
|
+
[tool.pytest.ini_options]
|
|
68
|
+
testpaths = ["tests"]
|
|
69
|
+
filterwarnings = [
|
|
70
|
+
"ignore::DeprecationWarning",
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
[tool.coverage.run]
|
|
74
|
+
source = ["src/fastapi_toolsets"]
|
|
75
|
+
branch = true
|
|
76
|
+
|
|
77
|
+
[tool.coverage.report]
|
|
78
|
+
exclude_lines = [
|
|
79
|
+
"pragma: no cover",
|
|
80
|
+
"if TYPE_CHECKING:",
|
|
81
|
+
"raise NotImplementedError",
|
|
82
|
+
]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""FastAPI utilities package.
|
|
2
|
+
|
|
3
|
+
Provides CRUD operations, fixtures, CLI, and standardized API responses
|
|
4
|
+
for FastAPI with async SQLAlchemy and PostgreSQL.
|
|
5
|
+
|
|
6
|
+
Example usage:
|
|
7
|
+
from fastapi import FastAPI, Depends
|
|
8
|
+
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
|
9
|
+
from fastapi_toolsets.crud import CrudFactory
|
|
10
|
+
from fastapi_toolsets.db import create_db_dependency
|
|
11
|
+
from fastapi_toolsets.schemas import Response
|
|
12
|
+
|
|
13
|
+
app = FastAPI()
|
|
14
|
+
init_exceptions_handlers(app)
|
|
15
|
+
|
|
16
|
+
UserCrud = CrudFactory(User)
|
|
17
|
+
|
|
18
|
+
@app.get("/users/{user_id}", response_model=Response[dict])
|
|
19
|
+
async def get_user(user_id: int, session = Depends(get_db)):
|
|
20
|
+
user = await UserCrud.get(session, [User.id == user_id])
|
|
21
|
+
return Response(data={"user": user.username}, message="Success")
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Main CLI application."""
|
|
2
|
+
|
|
3
|
+
import importlib.util
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Annotated
|
|
7
|
+
|
|
8
|
+
import typer
|
|
9
|
+
|
|
10
|
+
from .commands import fixtures
|
|
11
|
+
|
|
12
|
+
app = typer.Typer(
|
|
13
|
+
name="fastapi-utils",
|
|
14
|
+
help="CLI utilities for FastAPI projects.",
|
|
15
|
+
no_args_is_help=True,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# Register built-in commands
|
|
19
|
+
app.add_typer(fixtures.app, name="fixtures")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def register_command(command: typer.Typer, name: str) -> None:
|
|
23
|
+
"""Register a custom command group.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
command: Typer app for the command group
|
|
27
|
+
name: Name for the command group
|
|
28
|
+
|
|
29
|
+
Example:
|
|
30
|
+
# In your project's cli.py:
|
|
31
|
+
import typer
|
|
32
|
+
from fastapi_toolsets.cli import app, register_command
|
|
33
|
+
|
|
34
|
+
my_commands = typer.Typer()
|
|
35
|
+
|
|
36
|
+
@my_commands.command()
|
|
37
|
+
def seed():
|
|
38
|
+
'''Seed the database.'''
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
register_command(my_commands, "db")
|
|
42
|
+
# Now available as: fastapi-utils db seed
|
|
43
|
+
"""
|
|
44
|
+
app.add_typer(command, name=name)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@app.callback()
|
|
48
|
+
def main(
|
|
49
|
+
ctx: typer.Context,
|
|
50
|
+
config: Annotated[
|
|
51
|
+
Path | None,
|
|
52
|
+
typer.Option(
|
|
53
|
+
"--config",
|
|
54
|
+
"-c",
|
|
55
|
+
help="Path to project config file (Python module with fixtures registry).",
|
|
56
|
+
envvar="FASTAPI_TOOLSETS_CONFIG",
|
|
57
|
+
),
|
|
58
|
+
] = None,
|
|
59
|
+
) -> None:
|
|
60
|
+
"""FastAPI utilities CLI."""
|
|
61
|
+
ctx.ensure_object(dict)
|
|
62
|
+
|
|
63
|
+
if config:
|
|
64
|
+
ctx.obj["config_path"] = config
|
|
65
|
+
# Load the config module
|
|
66
|
+
config_module = _load_module_from_path(config)
|
|
67
|
+
ctx.obj["config_module"] = config_module
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _load_module_from_path(path: Path) -> object:
|
|
71
|
+
"""Load a Python module from a file path.
|
|
72
|
+
|
|
73
|
+
Handles both absolute and relative imports by adding the config's
|
|
74
|
+
parent directory to sys.path temporarily.
|
|
75
|
+
"""
|
|
76
|
+
path = path.resolve()
|
|
77
|
+
|
|
78
|
+
# Add the parent directory to sys.path to support relative imports
|
|
79
|
+
parent_dir = str(
|
|
80
|
+
path.parent.parent
|
|
81
|
+
) # Go up two levels (e.g., from app/cli_config.py to project root)
|
|
82
|
+
if parent_dir not in sys.path:
|
|
83
|
+
sys.path.insert(0, parent_dir)
|
|
84
|
+
|
|
85
|
+
# Also add immediate parent for direct module imports
|
|
86
|
+
immediate_parent = str(path.parent)
|
|
87
|
+
if immediate_parent not in sys.path:
|
|
88
|
+
sys.path.insert(0, immediate_parent)
|
|
89
|
+
|
|
90
|
+
spec = importlib.util.spec_from_file_location("config", path)
|
|
91
|
+
if spec is None or spec.loader is None:
|
|
92
|
+
raise typer.BadParameter(f"Cannot load module from {path}")
|
|
93
|
+
|
|
94
|
+
module = importlib.util.module_from_spec(spec)
|
|
95
|
+
sys.modules["config"] = module
|
|
96
|
+
spec.loader.exec_module(module)
|
|
97
|
+
return module
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Built-in CLI commands."""
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"""Fixture management commands."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from typing import Annotated
|
|
5
|
+
|
|
6
|
+
import typer
|
|
7
|
+
|
|
8
|
+
from ...fixtures import Context, FixtureRegistry, LoadStrategy, load_fixtures_by_context
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(
|
|
11
|
+
name="fixtures",
|
|
12
|
+
help="Manage database fixtures.",
|
|
13
|
+
no_args_is_help=True,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _get_registry(ctx: typer.Context) -> FixtureRegistry:
|
|
18
|
+
"""Get fixture registry from context."""
|
|
19
|
+
config = ctx.obj.get("config_module") if ctx.obj else None
|
|
20
|
+
if config is None:
|
|
21
|
+
raise typer.BadParameter(
|
|
22
|
+
"No config provided. Use --config to specify a config file with a 'fixtures' registry."
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
registry = getattr(config, "fixtures", None)
|
|
26
|
+
if registry is None:
|
|
27
|
+
raise typer.BadParameter(
|
|
28
|
+
"Config module must have a 'fixtures' attribute (FixtureRegistry instance)."
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if not isinstance(registry, FixtureRegistry):
|
|
32
|
+
raise typer.BadParameter(
|
|
33
|
+
f"'fixtures' must be a FixtureRegistry instance, got {type(registry).__name__}"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
return registry
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _get_db_context(ctx: typer.Context):
|
|
40
|
+
"""Get database context manager from config."""
|
|
41
|
+
config = ctx.obj.get("config_module") if ctx.obj else None
|
|
42
|
+
if config is None:
|
|
43
|
+
raise typer.BadParameter("No config provided.")
|
|
44
|
+
|
|
45
|
+
get_db_context = getattr(config, "get_db_context", None)
|
|
46
|
+
if get_db_context is None:
|
|
47
|
+
raise typer.BadParameter("Config module must have a 'get_db_context' function.")
|
|
48
|
+
|
|
49
|
+
return get_db_context
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@app.command("list")
|
|
53
|
+
def list_fixtures(
|
|
54
|
+
ctx: typer.Context,
|
|
55
|
+
context: Annotated[
|
|
56
|
+
str | None,
|
|
57
|
+
typer.Option(
|
|
58
|
+
"--context",
|
|
59
|
+
"-c",
|
|
60
|
+
help="Filter by context (base, production, development, testing).",
|
|
61
|
+
),
|
|
62
|
+
] = None,
|
|
63
|
+
) -> None:
|
|
64
|
+
"""List all registered fixtures."""
|
|
65
|
+
registry = _get_registry(ctx)
|
|
66
|
+
|
|
67
|
+
if context:
|
|
68
|
+
fixtures = registry.get_by_context(context)
|
|
69
|
+
else:
|
|
70
|
+
fixtures = registry.get_all()
|
|
71
|
+
|
|
72
|
+
if not fixtures:
|
|
73
|
+
typer.echo("No fixtures found.")
|
|
74
|
+
return
|
|
75
|
+
|
|
76
|
+
typer.echo(f"\n{'Name':<30} {'Contexts':<30} {'Dependencies'}")
|
|
77
|
+
typer.echo("-" * 80)
|
|
78
|
+
|
|
79
|
+
for fixture in fixtures:
|
|
80
|
+
contexts = ", ".join(fixture.contexts)
|
|
81
|
+
deps = ", ".join(fixture.depends_on) if fixture.depends_on else "-"
|
|
82
|
+
typer.echo(f"{fixture.name:<30} {contexts:<30} {deps}")
|
|
83
|
+
|
|
84
|
+
typer.echo(f"\nTotal: {len(fixtures)} fixture(s)")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@app.command("graph")
|
|
88
|
+
def show_graph(
|
|
89
|
+
ctx: typer.Context,
|
|
90
|
+
fixture_name: Annotated[
|
|
91
|
+
str | None,
|
|
92
|
+
typer.Argument(help="Show dependencies for a specific fixture."),
|
|
93
|
+
] = None,
|
|
94
|
+
) -> None:
|
|
95
|
+
"""Show fixture dependency graph."""
|
|
96
|
+
registry = _get_registry(ctx)
|
|
97
|
+
|
|
98
|
+
if fixture_name:
|
|
99
|
+
try:
|
|
100
|
+
order = registry.resolve_dependencies(fixture_name)
|
|
101
|
+
typer.echo(f"\nDependency chain for '{fixture_name}':\n")
|
|
102
|
+
for i, name in enumerate(order):
|
|
103
|
+
indent = " " * i
|
|
104
|
+
arrow = "└─> " if i > 0 else ""
|
|
105
|
+
typer.echo(f"{indent}{arrow}{name}")
|
|
106
|
+
except KeyError:
|
|
107
|
+
typer.echo(f"Fixture '{fixture_name}' not found.", err=True)
|
|
108
|
+
raise typer.Exit(1)
|
|
109
|
+
else:
|
|
110
|
+
# Show full graph
|
|
111
|
+
fixtures = registry.get_all()
|
|
112
|
+
|
|
113
|
+
typer.echo("\nFixture Dependency Graph:\n")
|
|
114
|
+
for fixture in fixtures:
|
|
115
|
+
deps = (
|
|
116
|
+
f" -> [{', '.join(fixture.depends_on)}]" if fixture.depends_on else ""
|
|
117
|
+
)
|
|
118
|
+
typer.echo(f" {fixture.name}{deps}")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@app.command("load")
|
|
122
|
+
def load(
|
|
123
|
+
ctx: typer.Context,
|
|
124
|
+
contexts: Annotated[
|
|
125
|
+
list[str] | None,
|
|
126
|
+
typer.Argument(
|
|
127
|
+
help="Contexts to load (base, production, development, testing)."
|
|
128
|
+
),
|
|
129
|
+
] = None,
|
|
130
|
+
strategy: Annotated[
|
|
131
|
+
str,
|
|
132
|
+
typer.Option(
|
|
133
|
+
"--strategy", "-s", help="Load strategy: merge, insert, skip_existing."
|
|
134
|
+
),
|
|
135
|
+
] = "merge",
|
|
136
|
+
dry_run: Annotated[
|
|
137
|
+
bool,
|
|
138
|
+
typer.Option(
|
|
139
|
+
"--dry-run", "-n", help="Show what would be loaded without loading."
|
|
140
|
+
),
|
|
141
|
+
] = False,
|
|
142
|
+
) -> None:
|
|
143
|
+
"""Load fixtures into the database."""
|
|
144
|
+
registry = _get_registry(ctx)
|
|
145
|
+
get_db_context = _get_db_context(ctx)
|
|
146
|
+
|
|
147
|
+
# Parse contexts
|
|
148
|
+
if contexts:
|
|
149
|
+
context_list = contexts
|
|
150
|
+
else:
|
|
151
|
+
context_list = [Context.BASE]
|
|
152
|
+
|
|
153
|
+
# Parse strategy
|
|
154
|
+
try:
|
|
155
|
+
load_strategy = LoadStrategy(strategy)
|
|
156
|
+
except ValueError:
|
|
157
|
+
typer.echo(
|
|
158
|
+
f"Invalid strategy: {strategy}. Use: merge, insert, skip_existing", err=True
|
|
159
|
+
)
|
|
160
|
+
raise typer.Exit(1)
|
|
161
|
+
|
|
162
|
+
# Resolve what will be loaded
|
|
163
|
+
ordered = registry.resolve_context_dependencies(*context_list)
|
|
164
|
+
|
|
165
|
+
if not ordered:
|
|
166
|
+
typer.echo("No fixtures to load for the specified context(s).")
|
|
167
|
+
return
|
|
168
|
+
|
|
169
|
+
typer.echo(f"\nFixtures to load ({load_strategy.value} strategy):")
|
|
170
|
+
for name in ordered:
|
|
171
|
+
fixture = registry.get(name)
|
|
172
|
+
instances = list(fixture.func())
|
|
173
|
+
model_name = type(instances[0]).__name__ if instances else "?"
|
|
174
|
+
typer.echo(f" - {name}: {len(instances)} {model_name}(s)")
|
|
175
|
+
|
|
176
|
+
if dry_run:
|
|
177
|
+
typer.echo("\n[Dry run - no changes made]")
|
|
178
|
+
return
|
|
179
|
+
|
|
180
|
+
typer.echo("\nLoading...")
|
|
181
|
+
|
|
182
|
+
async def do_load():
|
|
183
|
+
async with get_db_context() as session:
|
|
184
|
+
result = await load_fixtures_by_context(
|
|
185
|
+
session, registry, *context_list, strategy=load_strategy
|
|
186
|
+
)
|
|
187
|
+
return result
|
|
188
|
+
|
|
189
|
+
result = asyncio.run(do_load())
|
|
190
|
+
|
|
191
|
+
total = sum(len(items) for items in result.values())
|
|
192
|
+
typer.echo(f"\nLoaded {total} record(s) successfully.")
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@app.command("show")
|
|
196
|
+
def show_fixture(
|
|
197
|
+
ctx: typer.Context,
|
|
198
|
+
name: Annotated[str, typer.Argument(help="Fixture name to show.")],
|
|
199
|
+
) -> None:
|
|
200
|
+
"""Show details of a specific fixture."""
|
|
201
|
+
registry = _get_registry(ctx)
|
|
202
|
+
|
|
203
|
+
try:
|
|
204
|
+
fixture = registry.get(name)
|
|
205
|
+
except KeyError:
|
|
206
|
+
typer.echo(f"Fixture '{name}' not found.", err=True)
|
|
207
|
+
raise typer.Exit(1)
|
|
208
|
+
|
|
209
|
+
typer.echo(f"\nFixture: {fixture.name}")
|
|
210
|
+
typer.echo(f"Contexts: {', '.join(fixture.contexts)}")
|
|
211
|
+
typer.echo(
|
|
212
|
+
f"Dependencies: {', '.join(fixture.depends_on) if fixture.depends_on else 'None'}"
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# Show instances
|
|
216
|
+
instances = list(fixture.func())
|
|
217
|
+
if instances:
|
|
218
|
+
model_name = type(instances[0]).__name__
|
|
219
|
+
typer.echo(f"\nInstances ({len(instances)} {model_name}):")
|
|
220
|
+
for instance in instances[:10]: # Limit to 10
|
|
221
|
+
typer.echo(f" - {instance!r}")
|
|
222
|
+
if len(instances) > 10:
|
|
223
|
+
typer.echo(f" ... and {len(instances) - 10} more")
|
|
224
|
+
else:
|
|
225
|
+
typer.echo("\nNo instances (empty fixture)")
|