Define a single factory to generate the same data in multiple formats
Project description
multi_factory
Define a single factory to generate the same data in multiple formats:
- Base (original data as defined on the factory)
- JSON (original data converted into a Python dict that is JSON serialisable)
- Domain (JSON data that is passed through a
marshmallowschema that validates it and converts it into a domain object like a@dataclass)
Installation
multi_factory can be installed using pip (requires Python >=3.10):
pip install multi-factory
Quick start
With the following setup:
from enum import Enum
from uuid import uuid4, UUID
from datetime import datetime
from dataclasses import dataclass
from marshmallow import Schema, fields
class Gender(Enum):
MALE = 1
FEMALE = 2
OTHER = 3
class UserSchema(Schema):
id = fields.UUID()
first_name = fields.String()
last_name = fields.String()
age = fields.Integer()
birthday = fields.DateTime()
gender = fields.Enum(Gender)
@dataclass
class User:
id: UUID
first_name: str
last_name: str
age: int
birthday: datetime
gender: Gender
Look at the following example:
import factory
class UserDictFactory(factory.Factory):
class Meta:
model = dict
id = uuid4()
first_name = "Bob"
last_name = "Dylan"
age = 21
birthday = datetime(year=2000, month=1, day=1, hour=0)
gender = Gender.MALE
class UserJSONFactory(factory.Factory):
class Meta:
model = dict
id = str(uuid4())
first_name = "Bob"
last_name = "Dylan"
age = 21
birthday = datetime(year=2000, month=1, day=1, hour=0).isoformat()
gender = Gender.MALE.name
class UserDomainFactory(factory.Factory):
class Meta:
model = User
id = uuid4()
first_name = "Bob"
last_name = "Dylan"
age = 21
birthday = datetime(year=2000, month=1, day=1, hour=0)
gender = Gender.MALE
We have to define multiple independent factories to represent the same data in different forms.
With multi-factory, we can do the following instead:
import multi_factory import JSONToDomainFactory
class UserFactory(JSONToDomainFactory[User, UserSchema]):
id = uuid4()
first_name = "Bob"
last_name = "Dylan"
age = 21
birthday = datetime(year=2000, month=1, day=1, hour=0)
gender = Gender.MALE
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 multi_factory-0.1.0.tar.gz.
File metadata
- Download URL: multi_factory-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd6768ca1cc1167c7b3412d8ffbb1e9465ce62fcb8d4fca8a4a0a558dcfe2a4b
|
|
| MD5 |
522b25d88a1b9f659945f48749751ea7
|
|
| BLAKE2b-256 |
c736da8c93c953b570b4d2f554e087134906cef14a1eb9d3f05cf11a03318a60
|
File details
Details for the file multi_factory-0.1.0-py3-none-any.whl.
File metadata
- Download URL: multi_factory-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8e550f8c0d290dcf1c292e68869c3820215a59dff979a2a3b9fb0199f2e52a7
|
|
| MD5 |
5129aa48703bac97e31b7a423952b795
|
|
| BLAKE2b-256 |
578a2d97ebfa71dad3fe327ef16408d3da9006a3cb39a21a5a73355b4abd262a
|