A Python library to generate one or more Pydantic models with chainable state modifiers.
Project description
Factoria
Factoria is a lightweight Python library for generating one or more Pydantic model instances with chainable state modifiers.
It’s especially useful for testing, seeding, or creating predictable data scenarios.
Features
- Generate single or multiple Pydantic models easily
- Apply state transformations fluently
- Simple base class for building factories
- Perfect for unit testing and data seeding
Installation
Using Poetry:
poetry add factoria
Or with pip
pip install factoria
Examples
Create a Factory
from pydantic import BaseModel
from factoria import BaseFactory
class User(BaseModel):
id: int
name: str
email: str
class UserFactory(BaseFactory):
def definition(self):
return User(
id=1,
name="Alice",
email="alice@example.com",
)
Generate One or Many
# Create one instance
user = UserFactory().make()
print(user)
# → User(id=1, name='Alice', email='alice@example.com')
# Create multiple instances
users = UserFactory().count(3).make()
print(len(users))
# → 3
You can define chainable states using the @apply decorator
from datetime import datetime, timedelta
from pydantic import BaseModel
from factoria import BaseFactory, apply
class Order(BaseModel):
id: int
customer: str
total: float
status: str = "pending"
paid_at: datetime | None = None
shipped_at: datetime | None = None
cancelled: bool = False
class OrderFactory(BaseFactory):
def definition(self):
return Order(
id=1,
customer="John Doe",
total=199.90,
)
@apply
def paid(self, obj):
obj.status = "paid"
obj.paid_at = datetime.utcnow()
@apply
def shipped(self, obj):
obj.status = "shipped"
obj.shipped_at = datetime.utcnow() + timedelta(days=1)
@apply
def cancelled(self, obj):
obj.status = "cancelled"
obj.cancelled = True
Some modifiers examples
# One paid order
paid_order = OrderFactory().paid().make()
# Two shipped orders
shipped_orders = OrderFactory().count(2).shipped().make()
# Three cancelled orders
cancelled_orders = OrderFactory().count(3).cancelled().make()
# A complex example (paid and shipped)
paid_and_shipped = OrderFactory().count(2).paid().shipped().make()
Project details
Release history Release notifications | RSS feed
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 factoria-0.1.0.tar.gz.
File metadata
- Download URL: factoria-0.1.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff178f31939bd0044dc88261362286bb98c1f9f2df0394720c3bd604784d95df
|
|
| MD5 |
606856b95ee740524b9d1c0b4a75e7cb
|
|
| BLAKE2b-256 |
717e8176db6fae32bc9013fd817d19eac02b5f7b4f0d6e401cdef3955847da72
|
File details
Details for the file factoria-0.1.0-py3-none-any.whl.
File metadata
- Download URL: factoria-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e082c2f29754dfd0441285743088e7330277cd550fbf8371b3c2f03091ada4eb
|
|
| MD5 |
14f8191fe5673bc9896260d8137aa283
|
|
| BLAKE2b-256 |
9e531873d421209ae01cf1b74cfea125188563eca2d238cf50bdb614fd156fb0
|