django-data-shape
A realistically shaped test database from Django models.
Declare the shape of your data -- cardinality, value skew, foreign-key fan-out as
a distribution with a long tail, and where related rows physically sit -- then
load it by COPY and ANALYZE it, so the query planner makes the same choices
it will make in production.
It exists because a plan over ten rows is a lie, and because the loop it replaces is not merely smaller: uniform fan-out makes the planner always right, and generating children parent-by-parent clusters them perfectly, which flatters every index scan. A test database can be wrong in the flattering direction, and usually is.
Install
pip install 'django-data-shape[postgres]'
The quotes are not decoration: zsh globs the brackets and reports
no matches found without them.
Use
import datetime
from django_data_shape import Sequential, Shape, Skew, Table, Uniform, build
from myapp.models import Order
shape = Shape(
Table(
Order,
rows=1_000_000,
status=Skew({"complete": 0.98, "pending": 0.015, "cancelled": 0.005}),
total=Uniform(0, 500, places=2),
created_at=Sequential(
datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc),
datetime.timedelta(seconds=3),
),
),
seed=1234,
)
build(shape)
build() generates the rows, loads them with COPY, moves the identity sequence
past the keys it assigned, and runs ANALYZE so the planner can see the shape.
It raises on any backend that is not PostgreSQL rather than degrading quietly --
unless you say require_statistics=False, which asks for rows and cardinality
instead of a database the planner can reason about, and is what the growth
harness below is built on.
Relations
from django_data_shape import Constant, FanOut, Shape, Table, Zipf, build
build(
Shape(
Table(Company, rows=50, name=Constant("acme")),
Table(
Order,
rows=2_000_000,
# A distribution, not a number: giving every parent ten children is
# the one shape in which the planner is never wrong, because its
# n_distinct average is then the truth.
company=FanOut(Zipf(1.2), childless=0.35),
status=Constant("complete"),
),
)
)
The parents can be rows this package built or rows your own code did -- their real keys are read, not assumed, so the ORM can own the small tables while this owns the large ones.
From pytest
# conftest.py
from django_data_shape import Constant, Shape, Table
from django_data_shape.fixtures import scale_fixture, shape_fixture
orders = shape_fixture(Shape(Table(Order, rows=100_000, status=Constant("complete"))))
world = scale_fixture(Shape(Table(Order, rows=100, status=Constant("complete"))))
orders is one world built once for the whole session, composed with
pytest-django rather than replacing it. world is the scale protocol: make
the world be at factor F, then let the caller run its block, which is what a
query count asserted to be O(1) rather than O(N) needs.
def test_the_dashboard_does_not_grow(world, django_assert_num_queries):
for factor in (1, 10):
with world(factor):
with django_assert_num_queries(3):
dashboard()
A factor varies the declaration rather than subsetting one larger build, and
pip install 'django-data-shape[pytest]' is what these two need. The growth
harness works on any backend Django supports, because a query count is an ORM
property and means the same everywhere; the session world skips with a stated
reason where a shaped database cannot exist, because a plan over it is the
thing it exists to make honest.
What it expects, and what it refuses
A declaration that cannot describe a database raises before a row is generated, naming the field. In particular:
- PostgreSQL and psycopg 3. Rows stream into
COPY FROM STDIN, which psycopg 2 cannot do without materialising them first. Both are refused by name rather than degraded around. PostgreSQL is required for the statistics half only:build(shape, require_statistics=False)loads rows on any backend and claims nothing about a plan. psycopg 2 is refused either way, because the vendor picks the route and not the caller. - A key type it can assign. Integer keys count from one and UUID keys are
derived from the seed; anything else is refused rather than guessed, and
keys=KeyFunction(...)declares one. - Empty tables. Keys start at 1 on every build, so
build()checks first and raises rather than colliding partway through. - A callable model default such as
default=uuid4must be declared as a distribution:uuid4varies per row anddictdoes not, and nothing on the field distinguishes them.
Status
Early. Single tables, the model graph and the pytest surface. Derived fields, collections copied along a join, per-group invariants and template-database reuse come next.
Full documentation: https://artui.github.io/django-data-shape/
License
MIT
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 django_data_shape-0.4.0.tar.gz.
File metadata
- Download URL: django_data_shape-0.4.0.tar.gz
- Upload date:
- Size: 180.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56401664960c55f90d3191fbb0860222168a86a1173eff967367a757ae8ad3d9
|
|
| MD5 |
908a8484a90a6d2cc451d50f240c78b1
|
|
| BLAKE2b-256 |
408a25a7d77466f21b5bed8ed775781b2b5b0db6d71db041e867ab561771a818
|
Provenance
The following attestation bundles were made for django_data_shape-0.4.0.tar.gz:
Publisher:
release.yml on Artui/django-data-shape
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_data_shape-0.4.0.tar.gz -
Subject digest:
56401664960c55f90d3191fbb0860222168a86a1173eff967367a757ae8ad3d9 - Sigstore transparency entry: 2685018765
- Sigstore integration time:
-
Permalink:
Artui/django-data-shape@84d5bcf5f96e8d498b56c27c736c794537a519cd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Artui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@84d5bcf5f96e8d498b56c27c736c794537a519cd -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_data_shape-0.4.0-py3-none-any.whl.
File metadata
- Download URL: django_data_shape-0.4.0-py3-none-any.whl
- Upload date:
- Size: 54.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c98126b5d19d8867e07ea8bd7d661e7888e7f2b578f17a72df7658584526317
|
|
| MD5 |
14c27c1a1480aa4d843c279b3e63ef2e
|
|
| BLAKE2b-256 |
562f4f11d6f27fdb9e0f96f5dfa31456dd96eaecb11190ac07d22f1566b0dc0c
|
Provenance
The following attestation bundles were made for django_data_shape-0.4.0-py3-none-any.whl:
Publisher:
release.yml on Artui/django-data-shape
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_data_shape-0.4.0-py3-none-any.whl -
Subject digest:
0c98126b5d19d8867e07ea8bd7d661e7888e7f2b578f17a72df7658584526317 - Sigstore transparency entry: 2685018776
- Sigstore integration time:
-
Permalink:
Artui/django-data-shape@84d5bcf5f96e8d498b56c27c736c794537a519cd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Artui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@84d5bcf5f96e8d498b56c27c736c794537a519cd -
Trigger Event:
push
-
Statement type: