xtr-orm-fixtures 7.2.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.
- xtr_orm_fixtures-7.2.0/LICENSE +23 -0
- xtr_orm_fixtures-7.2.0/PKG-INFO +389 -0
- xtr_orm_fixtures-7.2.0/README.md +367 -0
- xtr_orm_fixtures-7.2.0/pyproject.toml +181 -0
- xtr_orm_fixtures-7.2.0/pyproject.toml.orig +187 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/.agents/skills/xtr-orm-fixtures/SKILL.md +168 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/__init__.py +25 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/bundle/__init__.py +7 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/bundle/orm_fixtures_bundle.py +107 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/command/__init__.py +7 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/command/load_data_fixtures_command.py +176 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/dependent_fixture_interface.py +24 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/__init__.py +19 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/circular_reference_error.py +24 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/invalid_argument_error.py +29 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/missing_fixture_error.py +30 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/orm_fixtures_error.py +14 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/reference_exists_error.py +30 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/exception/reference_not_found_error.py +27 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/executor/__init__.py +9 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/executor/abstract_executor.py +86 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/executor/dry_run_orm_executor.py +40 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/executor/orm_executor.py +36 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/fixture.py +83 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/fixture_group_interface.py +25 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/fixture_interface.py +24 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/loader.py +183 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/ordered_fixture_interface.py +22 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/purger/__init__.py +17 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/purger/orm_purger.py +93 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/purger/orm_purger_factory.py +42 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/purger/purge_mode.py +20 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/purger/purger_factory_interface.py +42 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/purger/purger_interface.py +24 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/py.typed +0 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/recipe/__init__.py +14 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/recipe/manifest.toml +26 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/reference_repository.py +84 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/shared_fixture_interface.py +25 -0
- xtr_orm_fixtures-7.2.0/src/xtr_orm_fixtures/table_definitions.py +47 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 xterr
|
|
4
|
+
Copyright (c) 2006-2015 Doctrine Project
|
|
5
|
+
Copyright (c) 2011 Fabien Potencier, Doctrine Project
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xtr-orm-fixtures
|
|
3
|
+
Version: 7.2.0
|
|
4
|
+
Summary: Seed a database from fixture classes: ordered, grouped and sharing references, loaded in one transaction after an emptying in foreign-key order.
|
|
5
|
+
Keywords: orm,database,fixtures,seeding,sqlalchemy,asyncio
|
|
6
|
+
Author: Xterr
|
|
7
|
+
Author-email: Xterr <me@xterr.dev>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Topic :: Database
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0.52
|
|
17
|
+
Requires-Dist: xtr-dependency-injection>=7.0,<8
|
|
18
|
+
Requires-Dist: xtr-logging-contracts>=7.0,<8
|
|
19
|
+
Requires-Dist: xtr-orm>=7.0,<8
|
|
20
|
+
Requires-Python: >=3.14.4
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
<div align="center">
|
|
24
|
+
|
|
25
|
+
# xtr-orm-fixtures
|
|
26
|
+
|
|
27
|
+
**Seed a database from fixture classes: ordered, grouped and sharing references, loaded in one
|
|
28
|
+
transaction after an emptying in foreign-key order.**
|
|
29
|
+
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Why?
|
|
35
|
+
|
|
36
|
+
A development database is only useful with data in it: the users you log in as, the catalogue
|
|
37
|
+
the pages list, the orders a report counts. Writing that data by hand after every reset is slow,
|
|
38
|
+
and a SQL dump drifts from the models the day a column is added.
|
|
39
|
+
|
|
40
|
+
Fixtures are that data as code — classes written against the application's own models, loaded
|
|
41
|
+
in one command:
|
|
42
|
+
|
|
43
|
+
- 🧩 **A fixture is a class** with `async def load(self, session)`. It adds objects to the
|
|
44
|
+
session it is handed, and with a container its constructor takes any service it needs.
|
|
45
|
+
- 🔗 **References** hand objects from one fixture to the next by name: the users fixture adds
|
|
46
|
+
`"admin"`, the orders fixture places an order for it.
|
|
47
|
+
- 🧭 **Order without bookkeeping**: a fixture names the fixtures it depends on, or a number;
|
|
48
|
+
groups load a subset — `--group demo` — with everything it depends on.
|
|
49
|
+
- 🧹 **An emptying in foreign-key order** first — `DELETE`, or `TRUNCATE` where the database has
|
|
50
|
+
it — of exactly the tables the application maps, nothing else in the database.
|
|
51
|
+
- 🔒 **One transaction**: the emptying and every fixture commit together, or not at all. A dry run
|
|
52
|
+
does all of it and rolls it back.
|
|
53
|
+
- 🌱 **Development-only**: listed for `dev` and `test`, absent from a production install — whose
|
|
54
|
+
application modules may still import it.
|
|
55
|
+
|
|
56
|
+
## Install
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
uv add --dev xtr-orm-fixtures
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
It is a development dependency: production does not install it. It works on
|
|
63
|
+
[xtr-orm](../xtr-orm)'s connections; the database driver is xtr-orm's to bring, through its
|
|
64
|
+
`postgres`, `sqlite` or `mysql` extra. With [xtr-console](../xtr-console) installed beside it, the
|
|
65
|
+
bundle registers `orm:fixtures:load`.
|
|
66
|
+
|
|
67
|
+
## Quick start
|
|
68
|
+
|
|
69
|
+
A fixture, without a container:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
# app/fixtures/users.py
|
|
73
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
74
|
+
from xtr_orm_fixtures import Fixture
|
|
75
|
+
|
|
76
|
+
from app.models import User
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class UserFixtures(Fixture):
|
|
80
|
+
async def load(self, session: AsyncSession) -> None:
|
|
81
|
+
admin = User(email="admin@example.com")
|
|
82
|
+
session.add(admin)
|
|
83
|
+
self.add_reference("admin", admin)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Loaded by hand — the loader finds every fixture class the package's modules define, builds each
|
|
87
|
+
with no arguments and orders them; the executor empties the tables and loads them in one
|
|
88
|
+
transaction:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
92
|
+
from xtr_orm_fixtures import Loader
|
|
93
|
+
from xtr_orm_fixtures.executor import OrmExecutor
|
|
94
|
+
from xtr_orm_fixtures.purger import OrmPurger
|
|
95
|
+
|
|
96
|
+
from app.models import Base
|
|
97
|
+
|
|
98
|
+
engine = create_async_engine("sqlite+aiosqlite:///app.db")
|
|
99
|
+
loader = Loader()
|
|
100
|
+
loader.load_from_package("app.fixtures")
|
|
101
|
+
async with AsyncSession(engine) as session:
|
|
102
|
+
await OrmExecutor(session, OrmPurger(Base.metadata)).execute(loader.get_fixtures())
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
In an application, the bundle does all of that: `uv run app orm:fixtures:load`.
|
|
106
|
+
|
|
107
|
+
## Writing fixtures
|
|
108
|
+
|
|
109
|
+
Subclass `Fixture` and write `load`. Add objects to the session; the executor flushes after each
|
|
110
|
+
fixture, so the next one sees their keys, and commits once every fixture has loaded. Never commit
|
|
111
|
+
or roll back inside `load`.
|
|
112
|
+
|
|
113
|
+
**References.** `add_reference(name, obj)` keeps an object for the fixtures after it, refusing a
|
|
114
|
+
name taken (`ReferenceExistsError`); `set_reference` replaces it. `get_reference(name, User)`
|
|
115
|
+
returns it typed, or raises `ReferenceNotFoundError`; `has_reference(name, User)` asks. Names
|
|
116
|
+
are kept per class — a mapped object under its mapped class — so `"admin"` may name a `User`
|
|
117
|
+
and a `Group` both. The session is never cleared, so a reference is the live object it tracks.
|
|
118
|
+
|
|
119
|
+
**Dependencies.** A fixture that needs another's references says so:
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from collections.abc import Sequence
|
|
123
|
+
|
|
124
|
+
from xtr_orm_fixtures import DependentFixtureInterface, Fixture, FixtureInterface
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class OrderFixtures(Fixture, DependentFixtureInterface):
|
|
128
|
+
def get_dependencies(self) -> Sequence[type[FixtureInterface]]:
|
|
129
|
+
return (UserFixtures,)
|
|
130
|
+
|
|
131
|
+
async def load(self, session: AsyncSession) -> None:
|
|
132
|
+
session.add(Order(customer=self.get_reference("admin", User)))
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**Order by number.** `OrderedFixtureInterface.get_order()` returns a number; numbered fixtures
|
|
136
|
+
load first, lowest first. A fixture is ordered one way: numbered and dependent is refused, as are
|
|
137
|
+
empty dependencies and a fixture depending on itself (`InvalidArgumentError`). Fixtures nothing
|
|
138
|
+
orders load in the order they were found.
|
|
139
|
+
|
|
140
|
+
**Groups.** `FixtureGroupInterface.get_groups()` — a classmethod — names the groups a fixture is
|
|
141
|
+
in; every fixture is also in the group named after its class. `--group demo` loads the fixtures of
|
|
142
|
+
`demo` and every fixture they depend on, in the same order as a full run.
|
|
143
|
+
|
|
144
|
+
A dependency on a fixture that is not there raises `MissingFixtureError`; fixtures depending on
|
|
145
|
+
one another in a circle raise `CircularReferenceError`, naming them.
|
|
146
|
+
|
|
147
|
+
`FixtureInterface` alone — `async def load(self, session)` — is enough for a fixture that shares
|
|
148
|
+
no references; `Fixture` adds them.
|
|
149
|
+
|
|
150
|
+
## Data files
|
|
151
|
+
|
|
152
|
+
Rows kept in JSON or CSV are loaded inside `load` with what advanced-alchemy already has —
|
|
153
|
+
`open_fixture_async` reads `countries.json` (or `.csv`, gzipped or zipped), a repository adds them:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from pathlib import Path
|
|
157
|
+
|
|
158
|
+
from advanced_alchemy.repository import SQLAlchemyAsyncRepository
|
|
159
|
+
from advanced_alchemy.utils.fixtures import open_fixture_async
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class CountryRepository(SQLAlchemyAsyncRepository[Country]):
|
|
163
|
+
model_type = Country
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class CountryFixtures(Fixture):
|
|
167
|
+
async def load(self, session: AsyncSession) -> None:
|
|
168
|
+
rows = await open_fixture_async(Path(__file__).parent / "data", "countries")
|
|
169
|
+
await CountryRepository(session=session).add_many([Country(**row) for row in rows])
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`upsert_many` keeps rows that are there. Leave `auto_commit` off: the run commits.
|
|
173
|
+
|
|
174
|
+
## Purging
|
|
175
|
+
|
|
176
|
+
Unless a run appends, it empties the database first. `OrmPurger(metadata, excluded=(), mode=...)`
|
|
177
|
+
empties every table of the table definitions it is given, a referencing table before the one it
|
|
178
|
+
references, and nothing else the database holds:
|
|
179
|
+
|
|
180
|
+
| `PurgeMode` | Empties with |
|
|
181
|
+
|---|---|
|
|
182
|
+
| `DELETE` (default) | `DELETE` on each table, inside the run's transaction |
|
|
183
|
+
| `TRUNCATE` | PostgreSQL: one `TRUNCATE` of every table, without `CASCADE`. MySQL and MariaDB: `TRUNCATE` per table with foreign key checks off, turned back on whatever happens. Anything else, SQLite included: `DELETE` |
|
|
184
|
+
|
|
185
|
+
`excluded` leaves tables as they are, by name or schema-qualified name — reference data a
|
|
186
|
+
migration wrote, say. A purger never commits.
|
|
187
|
+
|
|
188
|
+
A purger is built by a `PurgerFactoryInterface` — `create_for_connection(connection, metadata, *,
|
|
189
|
+
excluded=(), purge_with_truncate=False)` — so an application can empty its database its own way
|
|
190
|
+
and pick it by name with `--purger`. `OrmPurgerFactory` builds `OrmPurger`, and is the one named
|
|
191
|
+
`default`.
|
|
192
|
+
|
|
193
|
+
## Use in an application
|
|
194
|
+
|
|
195
|
+
Everything adding this package to an application on
|
|
196
|
+
[xtr-dependency-injection](../xtr-dependency-injection) takes — and, read backwards, what removing
|
|
197
|
+
it undoes.
|
|
198
|
+
|
|
199
|
+
- **Install** — `uv run xtr-recipes recipes:add --dev xtr-orm-fixtures`, or
|
|
200
|
+
`uv add --dev xtr-orm-fixtures` then `uv run xtr-recipes recipes:sync`: fixtures seed
|
|
201
|
+
development and test databases, so the package is a development dependency. The database
|
|
202
|
+
driver is [xtr-orm](../xtr-orm)'s to bring, through its `postgres`, `sqlite` or `mysql` extra.
|
|
203
|
+
- **Recipe** — `uv run xtr-recipes recipes:sync` does the *Activate* step below, and only that: it
|
|
204
|
+
lists `OrmFixturesBundle` for `dev` and `test`, which is what this package's recipe declares.
|
|
205
|
+
There is nothing for it to configure, no variable to write and nothing to ignore.
|
|
206
|
+
- **Activate** — import the bundle at the top of `<app>/config/bundles.py`, like every other one,
|
|
207
|
+
and list it for `dev` and `test`:
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
# <app>/config/bundles.py
|
|
211
|
+
from xtr_orm_fixtures.bundle import OrmFixturesBundle
|
|
212
|
+
|
|
213
|
+
BUNDLES = {
|
|
214
|
+
OrmFixturesBundle: {"dev": True, "test": True},
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Production still starts without the development dependencies installed: there the kernel
|
|
219
|
+
stands in for the bundle, which is not active, and sets aside every application module that
|
|
220
|
+
needs this package or a library only development installs — see
|
|
221
|
+
[Development-only packages](../xtr-dependency-injection/README.md#development-only-packages).
|
|
222
|
+
To seed production as well — reference data, loaded with `--group` — install the package as a
|
|
223
|
+
runtime dependency instead (`uv add xtr-orm-fixtures`) and list the bundle `{"all": True}`.
|
|
224
|
+
- **Brings along** — the orm bundle, which it requires. The console bundle is read, not required:
|
|
225
|
+
`orm:fixtures:load` is registered when it is active.
|
|
226
|
+
- **Configure** — nothing: fixtures are found wherever the application keeps them, and what is
|
|
227
|
+
emptied is each connection's table definitions, as its migrations see them. A purger factory of
|
|
228
|
+
the application's joins under the name it gives, with
|
|
229
|
+
`@as_alias(PurgerFactoryInterface, qualifier="mine")`; in a module production imports too, keep
|
|
230
|
+
it to the environments the bundle runs in with `@when("dev", "test")`.
|
|
231
|
+
- **Environment** — nothing.
|
|
232
|
+
- **Ignore** — nothing: fixtures are code to commit.
|
|
233
|
+
- **Remove** — `uv run xtr-recipes recipes:remove xtr-orm-fixtures`: it runs
|
|
234
|
+
`uv remove --dev xtr-orm-fixtures`, and the sync after it drops the `BUNDLES` entry. Delete the
|
|
235
|
+
fixture classes, which no longer import.
|
|
236
|
+
- **Check** — `debug:bundles` lists `orm_fixtures` as active in `dev`;
|
|
237
|
+
`orm:fixtures:load --dry-run` loads every fixture and rolls it all back.
|
|
238
|
+
|
|
239
|
+
## Development-only installs
|
|
240
|
+
|
|
241
|
+
Fixtures, and the libraries only they use — a fake-data library, say — live anywhere in the
|
|
242
|
+
application, beside the code they seed for. A production install without development
|
|
243
|
+
dependencies (`uv sync --no-dev`) has neither this package nor those libraries, yet its kernel
|
|
244
|
+
imports every application module while it scans.
|
|
245
|
+
|
|
246
|
+
It still starts. The bundle list says the fixtures bundle does not run there, so the scan
|
|
247
|
+
[sets aside](../xtr-dependency-injection/README.md#development-only-packages) every module that
|
|
248
|
+
fails to import for a package that is not installed — the fixture modules, the helpers built on
|
|
249
|
+
the fake-data library — and registers nothing they define. `debug:bundles` lists them under
|
|
250
|
+
**Set aside**, with what each needs; one built only on a library no absent bundle accounts for is
|
|
251
|
+
*not expected*, and logged as a warning.
|
|
252
|
+
|
|
253
|
+
Two rules keep it so:
|
|
254
|
+
|
|
255
|
+
- **A production module never imports a fixture.** If it does, it is set aside too: nothing it
|
|
256
|
+
defines is registered, and a service it declares fails the production build with
|
|
257
|
+
`SetAsideModuleUsedError`. Keep fixtures, and what only they use, to themselves.
|
|
258
|
+
- **Nothing in a fixture module declares a service for production.** A purger factory under
|
|
259
|
+
`@as_alias` in a module that imports this package fails a production build with
|
|
260
|
+
`SetAsideModuleUsedError`, unless `@when("dev", "test")` keeps it to the environments the bundle
|
|
261
|
+
runs in.
|
|
262
|
+
|
|
263
|
+
Two installs are strict instead:
|
|
264
|
+
|
|
265
|
+
- **Every listed bundle installed and active** — fixtures installed as a runtime dependency and
|
|
266
|
+
the bundle listed `{"all": True}`, but the fake-data library only in the dev group. Nothing is
|
|
267
|
+
set aside where every listed bundle runs: the build fails with `ResourceImportError: cannot
|
|
268
|
+
import scanned module '<module>'`, caused by the `ModuleNotFoundError`. Install that library as
|
|
269
|
+
a runtime dependency too, or this package as a development one.
|
|
270
|
+
- **The bundle active, some other listed bundle not** — the fixtures bundle would register a
|
|
271
|
+
fixture whose module was set aside, so the build fails with `SetAsideModuleUsedError`:
|
|
272
|
+
|
|
273
|
+
```text
|
|
274
|
+
app.fixtures.users:UserFixtures is used (bundle 'orm_fixtures' would register it), but its
|
|
275
|
+
module 'app.fixtures.users' was set aside: it needs 'faker', not installed here. Install what
|
|
276
|
+
provides 'faker', or stop importing it from 'app.fixtures.users'.
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Kernel / bundle
|
|
280
|
+
|
|
281
|
+
`OrmFixturesBundle` has no configuration and scans nothing of its own. It requires the orm bundle
|
|
282
|
+
and registers:
|
|
283
|
+
|
|
284
|
+
- **Every fixture class the application defines** — a concrete class with `FixtureInterface`
|
|
285
|
+
among its bases, wherever the application's scan finds it. An abstract base is not one; a class
|
|
286
|
+
that merely has a `load` method is not one either. Each is built by the container once per run,
|
|
287
|
+
so its constructor takes services — the run's `AsyncSession` included, the same one `load` is
|
|
288
|
+
handed. It is tagged `FIXTURE_TAG` (`"orm_fixtures.fixture"`) and collected as a
|
|
289
|
+
`ServiceLocator[FixtureInterface]`, keyed by its qualified name.
|
|
290
|
+
- **`TableDefinitions`** — each connection's table definitions, read with xtr-orm's
|
|
291
|
+
`ConnectionConfig.table_definitions()`: the tables its migrations compare the database with,
|
|
292
|
+
so a run empties exactly those.
|
|
293
|
+
- **`OrmPurgerFactory`**, as the `PurgerFactoryInterface` named `DEFAULT_PURGER` (`"default"`).
|
|
294
|
+
Another joins under its own name:
|
|
295
|
+
|
|
296
|
+
```python
|
|
297
|
+
from xtr_dependency_injection import as_alias, when
|
|
298
|
+
from xtr_orm_fixtures.purger import OrmPurger, PurgerFactoryInterface
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
@when("dev", "test")
|
|
302
|
+
@as_alias(PurgerFactoryInterface, qualifier="keep_countries")
|
|
303
|
+
class KeepCountriesPurgerFactory(PurgerFactoryInterface):
|
|
304
|
+
def create_for_connection(
|
|
305
|
+
self, connection, metadata, *, excluded=(), purge_with_truncate=False
|
|
306
|
+
):
|
|
307
|
+
return OrmPurger(metadata, (*excluded, "countries"))
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
- **`orm:fixtures:load`**, when the console bundle is active.
|
|
311
|
+
|
|
312
|
+
## Console commands
|
|
313
|
+
|
|
314
|
+
`orm:fixtures:load` empties a connection's database, after asking, and loads every fixture:
|
|
315
|
+
|
|
316
|
+
| Option | Does |
|
|
317
|
+
|---|---|
|
|
318
|
+
| `--append` | Add to what the database holds; nothing is emptied or asked |
|
|
319
|
+
| `--dry-run` | Run everything, the emptying included, then roll it all back; nothing is asked |
|
|
320
|
+
| `--group NAME` | Only the fixtures of `NAME` and what they depend on; repeat for several |
|
|
321
|
+
| `--connection NAME` | The connection to load into; the default one when left out |
|
|
322
|
+
| `--purger NAME` | The purger factory to empty with; `default` when left out |
|
|
323
|
+
| `--purge-exclusions TABLE` | A table to leave as it is; repeat for several |
|
|
324
|
+
| `--purge-with-truncate` | Empty with `TRUNCATE` rather than `DELETE` |
|
|
325
|
+
|
|
326
|
+
It asks `Careful, database "<db>" will be purged. Do you want to continue?`; `-n` answers yes,
|
|
327
|
+
and a no exits successfully with nothing changed. Each step is written as it runs —
|
|
328
|
+
` > purging database`, ` > loading app.fixtures.users.UserFixtures` — and the run ends with
|
|
329
|
+
`Loaded 2 fixtures.` (`... and rolled them back.` for a dry run).
|
|
330
|
+
|
|
331
|
+
| Exit code | When |
|
|
332
|
+
|---|---|
|
|
333
|
+
| `SUCCESS` | Loaded, or declined |
|
|
334
|
+
| `FAILURE` | An unknown connection; no fixture to load (in the groups named); a refused fixture, a missing dependency or a circle; a connection without a session |
|
|
335
|
+
| `INVALID` | An unknown `--purger`, naming the ones there are; a connection without table definitions, when emptying |
|
|
336
|
+
|
|
337
|
+
A fixture that fails takes everything back with it and its error is raised as it is.
|
|
338
|
+
|
|
339
|
+
## Errors
|
|
340
|
+
|
|
341
|
+
Every one derives from `OrmFixturesError`, in `xtr_orm_fixtures.exception`:
|
|
342
|
+
|
|
343
|
+
| Error | Raised when |
|
|
344
|
+
|---|---|
|
|
345
|
+
| `InvalidArgumentError` | A fixture is numbered and dependent, depends on nothing or on itself; a fixture asks for a reference before a run gave it a repository; an executor without a purger is asked to empty; an unknown connection or one without definitions (also a `ValueError`) |
|
|
346
|
+
| `MissingFixtureError` | A fixture depends on one that was not added — `fixture`, `required_by` (also a `LookupError`) |
|
|
347
|
+
| `CircularReferenceError` | Fixtures depend on one another in a circle — `fixtures`, the circle's class names |
|
|
348
|
+
| `ReferenceNotFoundError` | `get_reference` of a name not kept for that class — `name`, `kind` (also a `LookupError`) |
|
|
349
|
+
| `ReferenceExistsError` | `add_reference` of a name taken for that class; use `set_reference` — `name`, `kind` |
|
|
350
|
+
|
|
351
|
+
## Known limitations
|
|
352
|
+
|
|
353
|
+
- **xtr-orm-messenger's messages tables cannot be told apart** in a connection's table
|
|
354
|
+
definitions. xtr-orm adds them there only once that connection's migrator is built, so a run
|
|
355
|
+
usually leaves them alone; where they are there — a migrator built earlier in the process, or
|
|
356
|
+
mapped by the application — a run empties them. Keep them with
|
|
357
|
+
`--purge-exclusions <messages table>`.
|
|
358
|
+
- **MySQL and MariaDB commit around every `TRUNCATE`**, so an emptying with
|
|
359
|
+
`--purge-with-truncate` cannot be rolled back there, by a failing fixture or a dry run.
|
|
360
|
+
- **PostgreSQL's `TRUNCATE` refuses a table that a table left out references**, rather than
|
|
361
|
+
emptying both: empty such a set with `DELETE`, or leave the referenced table out too.
|
|
362
|
+
- **The session is never cleared between fixtures**: a very large seed keeps every object it
|
|
363
|
+
added in memory until the run ends.
|
|
364
|
+
- **A run loads every fixture into one connection**, the one `--connection` names: a fixture
|
|
365
|
+
adding objects of another connection's tables fails the run, which then takes everything back.
|
|
366
|
+
Keep such fixtures in a group (`--connection reports --group reports`), and run the default
|
|
367
|
+
connection with the groups it owns.
|
|
368
|
+
- **PostgreSQL's `TRUNCATE` keeps its sequences**, as `DELETE` does; MySQL's restarts each
|
|
369
|
+
table's auto-increment counter.
|
|
370
|
+
- **`Loader.load_from_package` builds each fixture with no arguments**: a fixture whose
|
|
371
|
+
constructor takes services is built by the container, through the bundle.
|
|
372
|
+
- **A set-aside module's top-level code runs once** while the scan learns what it defines, with
|
|
373
|
+
stand-ins for what is missing — see the container's
|
|
374
|
+
[Known limitations](../xtr-dependency-injection/README.md#known-limitations).
|
|
375
|
+
|
|
376
|
+
## Development
|
|
377
|
+
|
|
378
|
+
Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
|
|
379
|
+
`packages/xtr-orm-fixtures`; run the commands below from there. The `python-xtr-orm-fixtures`
|
|
380
|
+
repository is a read-only copy, so send issues and pull requests to the monorepo.
|
|
381
|
+
|
|
382
|
+
```sh
|
|
383
|
+
uv sync
|
|
384
|
+
uv run ruff check && uv run ruff format --check && uv run basedpyright && uv run ty check && uv run pytest
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
## License
|
|
388
|
+
|
|
389
|
+
MIT — see [LICENSE](LICENSE).
|