Saltar a contenido

dorm.factories

Factories de modelo estilo factory_boy integradas en dorm. Defaults declarativos, secuencias, valores lazy, sub-factories.

API

dorm.factories.Factory

Base class for model factories.

Subclasses declare attribute defaults (plain values or :class:_Declaration instances) and a Meta inner class pointing at the target dorm Model. Meta is resolved dynamically via :func:getattr so subclass declarations (which are instance variables on the class object) don't collide with a strict ClassVar annotation on the base.

build(**overrides: Any) -> Any classmethod

Construct a model instance in memory — no DB write.

Useful for unit tests that don't want the schema to exist yet, or for threading an unsaved instance through a higher-level test as input data.

create(**overrides: Any) -> Any classmethod

Insert one row through the model's manager.

Equivalent to model.objects.create(**resolved_kwargs) — triggers post_save / temporal / audit signals so the side-effects of a real write apply.

create_batch(size: int, **overrides: Any) -> list[Any] classmethod

Convenience for [cls.create(**overrides) for _ in range(size)].

Each iteration advances the sequence counter so columns marked unique via :class:Sequence stay non-conflicting.

reset_sequence() -> None classmethod

Reset the per-factory sequence counter. Useful between tests when fixture isolation isn't strict about row ids.

dorm.factories.Sequence

Bases: _Declaration

Render an attribute from an auto-incrementing integer.

fn receives the sequence number and returns the value::

username = Sequence(lambda n: f"user{n}")

dorm.factories.LazyFunction

Bases: _Declaration

Render an attribute by calling fn (zero args) each time.

Useful when the column wants datetime.now() per row, a fresh UUID, etc.

dorm.factories.SubFactory

Bases: _Declaration

Build a related row via another :class:Factory subclass and use the resulting instance as the attribute value.

The default mode is :meth:Factory.create — the related row lands in the database, mirroring factory_boy's behaviour. Pass strategy="build" to skip the save (useful when the field will be threaded into a parent's build call).