pytest-orm-boundaries
💡 Even if you control your imports, boundaries can still leak through the ORM.
pytest-orm-boundaries is a pytest plugin that reports ORM queries crossing your DDD aggregate boundaries.
Currently works with Django ORM, SQLAlchemy is in roadmap.
In domain-driven design, an aggregate is a consistency boundary: code in one
aggregate should not reach into the internals of another. Django's __ relation
lookups make it easy to cross those boundaries silently:
# Purchase and Client belong to different aggregates — this query couples them.
Purchase.objects.get(client__name="John")
pytest-orm-boundaries watches the ORM activity exercised by your test suite
and reports access that crosses a configured boundary - through __ lookups,
select_related, prefetch_related, subqueries, or hand-written .raw() SQL.
Install
Install the plugin with Django support:
pip install "pytest-orm-boundaries[django]"
pytest discovers the plugin automatically.
Configure
Declare your aggregates and their Django models in boundaries.toml at the project
root (or point at the file with --boundaries-config / the
boundaries_config ini option):
[aggregates.client]
models = ["bookshop.Client"]
[aggregates.book]
models = ["bookshop.Book"]
[aggregates.purchase]
models = ["bookshop.Purchase", "bookshop.PurchaseLine"]
Models are written as app_label.Model. Models not listed in any aggregate are
not checked. Without a config file the plugin emits a warning and runs no checks.
What it catches
The plugin flags executed ORM access that spans more than one configured group.
In the DDD example below, each crossing couples the purchase and client
aggregates:
-
__relation lookups:Purchase.objects.get(client__name="John")
-
select_related:Purchase.objects.select_related("client")
-
Subqueries - a table reached through a subquery still counts:
berlin_clients = Client.objects.filter(city="Berlin").values("id") Purchase.objects.filter(client_id__in=berlin_clients)
-
Hand-written
.raw()SQL:Purchase.objects.raw( "SELECT p.id FROM bookshop_purchase p " "JOIN bookshop_client c ON p.client_id = c.id" )
-
Bare
cursor.execute()- the same join reached through a raw cursor. -
prefetch_related:Purchase.objects.prefetch_related("client")
Queries that don't actually join across the boundary are not flagged - for example a foreign-key lookup by id, which Django resolves without a join:
Purchase.objects.filter(client_id=42) # reads one table
Purchase.objects.filter(client__pk=42) # Django trims the join
The report
At the end of the run, the plugin prints one grouped entry per offending place:
====================== orm-boundaries: boundary crossings ======================
1 place(s) in your code crossed aggregate boundaries, affecting 1 test(s):
bookshop/reports.py:13
crossed aggregates: client ↔ purchase
models: bookshop.Client, bookshop.Purchase
1 test(s) affected:
test_purchases.py::test_list_purchases_with_client
orm-boundaries: FAILED - 1 boundary crossing(s), run exits non-zero.
Each entry names the aggregates the query crossed and the models it joined.
Places are ordered by how many tests they affect. Pass -v to see every affected test
(otherwise the list is capped at 5 per place).
Allow and ignore
CQRS read models may cross boundaries intentionally, also existing application code may contain crossings you want to fix over time. [allow] and [ignore] let you tell the plugin which is which:
[allow]- the crossing is intentional. Use it for code that is meant to span aggregates, such as CQRS read models or cross-aggregate reports. An allowed crossing is suppressed and never reported.[ignore]- the crossing is known debt you plan to fix. It is suppressed for now, and the plugin reminds you when an entry is no longer needed.
[allow]
files = [
"app/reports/sales_summary.py",
]
[ignore]
files = [
"app/billing.py",
"app/legacy/*",
]
Each entry is a glob (fnmatch),
resolved relative to pytest's root directory and matched against either:
- the file that issues the query, or
- the test file.
An [ignore] whose file runs through the whole suite without ever crossing a
boundary is stale — it is clean now, so the plugin lists it for removal:
======================= orm-boundaries: stale ignores ========================
These [ignore] entries no longer suppress any boundary crossing - their files are clean now.
Remove them from boundaries.toml:
- app/billing.py
[allow] entries are never reported this way. If a file sits in both sections,
the allow wins and its [ignore] entry shows up as stale to remove.
A note on Django internals
Catching prefetch_related relies on Django internals that come with no stability
promise, so new Django releases may require compatibility updates.
Known gaps (on the project roadmap)
- lazy attribute access (e.g.
purchase.client); - related-manager reads/writes (
client.purchases.all(),client.purchases.create(...)); - a direct query on another aggregate's model, e.g.
Client.objects.get(...)written inside purchase code.
Status
Alpha - testing basic version.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_orm_boundaries-0.7.0.tar.gz.
File metadata
- Download URL: pytest_orm_boundaries-0.7.0.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1697b6ea289199044bb0e4428682a0d3cbbcbc1f3e37b56d2994242ee987530a
|
|
| MD5 |
04943741fe5a7f202c75d96aea239e4f
|
|
| BLAKE2b-256 |
f3bb06735aa2d2fc28336e97144571e541287ef45397381b05333879c46939e9
|
Provenance
The following attestation bundles were made for pytest_orm_boundaries-0.7.0.tar.gz:
Publisher:
publish.yml on evchibisova/pytest-orm-boundaries
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_orm_boundaries-0.7.0.tar.gz -
Subject digest:
1697b6ea289199044bb0e4428682a0d3cbbcbc1f3e37b56d2994242ee987530a - Sigstore transparency entry: 2160219732
- Sigstore integration time:
-
Permalink:
evchibisova/pytest-orm-boundaries@db29536911d60ccb9390afb4c522cb3d8353d7ec -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/evchibisova
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@db29536911d60ccb9390afb4c522cb3d8353d7ec -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_orm_boundaries-0.7.0-py3-none-any.whl.
File metadata
- Download URL: pytest_orm_boundaries-0.7.0-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79077819183b563dc72216faf9241156a242cef15c8095b756c98cbbf621ea30
|
|
| MD5 |
e9ea587b1030282027b04b6f2615330d
|
|
| BLAKE2b-256 |
ef6226ec791ee2966810e15ad2f5caf780d4aa4280178391bd44a4a5b37fda80
|
Provenance
The following attestation bundles were made for pytest_orm_boundaries-0.7.0-py3-none-any.whl:
Publisher:
publish.yml on evchibisova/pytest-orm-boundaries
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_orm_boundaries-0.7.0-py3-none-any.whl -
Subject digest:
79077819183b563dc72216faf9241156a242cef15c8095b756c98cbbf621ea30 - Sigstore transparency entry: 2160219877
- Sigstore integration time:
-
Permalink:
evchibisova/pytest-orm-boundaries@db29536911d60ccb9390afb4c522cb3d8353d7ec -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/evchibisova
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@db29536911d60ccb9390afb4c522cb3d8353d7ec -
Trigger Event:
release
-
Statement type: