A factory library for pydantic models.
Project description
pydactory
pydactory is a factory library for pydantic models with an API inspired by factory_boy.
Installation
PyPI: https://pypi.org/project/pydactory/
pip install pydactory
Features
pydactory is...
low boilerplate: provides default values for many common types. You don't need to tell pydactory how to build your name: str fields
familiar: define your factories like you define your pydantic models: in a simple, declarative syntax
Getting started
Declare your pydantic models
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field
class Address(BaseModel):
street1: str
street2: str
city: str
state: str
zip_code: str = Field(max_length=5)
class Author(BaseModel):
name: str
address: Address
date_of_birth: datetime
class Book(BaseModel):
title: str = Field(alias="Title")
author: Author = Field(alias="Author")
pages: int = Field(alias="PageCount")
publish_date: datetime = Field(alias="PublishDate")
isbn_13: str = Field(alias="ISBN-13")
isbn_10: Optional[str] = Field(alias="ISBN-10")
Declare your factories
from pydactory import Factory
class AuthorFactory(Factory[Author]):
name = "Leo Tolstoy"
class BookFactory(Factory[Book]):
title = "War and Peace"
author = AuthorFactory
publish_date = datetime.today
Use the factories to build your models
def test_book_factory():
book: Book = BookFactory.build(title="Anna Karenina")
assert Book(
title="Anna Karenina",
author=Author(
name="Leo Tolstoy",
address=Address(
street1="fake", street2="fake", city="fake", state="fake", zip_code="fake"
),
date_of_birth=datetime.datetime(2000, 1, 1, 0, 0),
),
pages=1,
publish_date=datetime.datetime(2021, 3, 26, 14, 15, 22, 613309),
isbn_13="fake",
isbn_10=None,
) == book
Roadmap
pydactory is still very much in progress.
Project details
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 pydactory-0.2.0.tar.gz.
File metadata
- Download URL: pydactory-0.2.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/20.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c60001e938749b2dfdead015f884ee1fa99cb6b708d9f885f3eb1b1bcc0797de
|
|
| MD5 |
681ed85a74c492bb47108c3879b30323
|
|
| BLAKE2b-256 |
0377f6357186ec1496c722fadf252529d30782f767ea193f6fd6167df77d9345
|
File details
Details for the file pydactory-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pydactory-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/20.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52bf677602c5cf2c6732cca6ebd95bedbd805ecd6bf821c47a097827c4185ece
|
|
| MD5 |
14f9cd33c2932232c6cbd0f4f97b8335
|
|
| BLAKE2b-256 |
bdf4d4fc7f55ee23252b4220db81da190b004d2b951b796e50b48449f08ab85f
|