Skip to main content

A very simple Factory library. Generic, but compatible with Django. Let's build together!

Project description

Faketory

A simple implementation of the Factory pattern, build any number of elements with random values generated by Faker.

Requirements

IMPORTANT: This library depends on Faker. If by any chance you have any errors, make sure you have it installed. If you're using Python < 3.7, you can adapt this library by downgrading to a version of Faker that's compatible with it, you'll have to follow installation for devs/contributors and either build the library or just copy faketory folder to your Python project's root.

Installation

pip install faketory

Installation for devs/contributors

  1. Clone this repository.
  2. Run ./install.sh.

Note: You can build the library for distribution by simply running poetry build.

Quick Start

  1. Create my_file.py, then copy and paste this code:
from faketory.factory import Faketory
from faketory.gens import Fake


class MyModel:
    def __init__(self, age, name, email):
        self.age = age
        self.name = name
        self.email = email


class MyFactory(Faketory):
    age = Fake('pyint', min_value=18, max_value=120)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = MyModel

# () -> Builds an instance with autogenerated values
my_model = MyFactory()
print(f'My name is {my_model.name}\n')

# (age=20) -> Custom value
my_model_custom = MyFactory(age=20)
print(f'My age is {my_model_custom.age}\n')  # 20

# (_qty=9) -> Builds a list of 9 objects
my_models_list = MyFactory(_qty=9)
for index, my_mod in enumerate(my_models_list):
    print(f'{index + 1} {my_mod.email}')

  1. Run the file using python my_file.py.

Faketory

Builds 1 or more fake model instances of a Model class.

  • _qty: Number of fake model instances to return. Minimum value is 1.
  • _resolver: Function to resolve once the model instance is created. (Check Django ORM example below)
  • _type: Define the type of the model instance you want to return. Default: 'list', but you can have 'generator' or 'set' instead.
  • **custom_fields: These fields will override the Fake generated values of the Factory.

Tutorial

  1. Import Fake and Faketory:
from faketory.factory import Faketory
from faketory.gens import Fake
  1. Import your Model(s):
from my_poject.models import MyModel
  1. Create your Factory by passing Fake generators to each field you want to pass values in your Model constructor. Also add your Model to the model attribute of the Meta class:
class MyFactory(Faketory):
    my_field = Fake('name')
    my_other_field = Fake('text')

    class Meta:
        model = MyModel

Note: You can pass any value to any field to your Model constructor instead of a Fake generator.

  1. Create your single Model instance:
my_model_instance = MyFactory()

Now the attributes of my_model_instance will have randomly generated values by faker. For example, my_field could be 'Jon' and my_other_field could be 'Some text here'.

Inheritance

class SampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = SampleChild

This piece of code is equivalent to:

class BaseSampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')

    class Meta:
        model = SampleChild


class SampleFactory(BaseSampleFactory):
    name = Fake('name')

FaketoryGen

A Faketory can have other Faketories... But if you pass a Faketory instance, it'll be treated as a fixed value. If you want to have different values, you'll need to use a FaketoryGen.

from faketory.factory import Faketory
from faketory.gens import FaketoryGen


class SampleChild(Sample):
    def __init__(self, age, email, name):
        self.age = age
        self.email = email
        self.name = name


class SampleHasChild:
    def __init__(self, child, email):
        self.child = child
        self.email = email


class SampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = SampleChild


class SampleHasChildFactory(Faketory):
    child = FaketoryGen(SampleFactory)
    email = Fake('email')

    class Meta:
        model = SampleHasChild

Django ORM example

You can pass any Django Model to Faketory.Meta.model, but if you want to save it to your database, make sure you pass _resolver='save' to your Faketory instance.

class SampleDjangoFactory(Faketory):
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = MyDjangoModel

my_django_instance = Factory(_resolver='save')

Fake

This class simply calls the functions you get from Faker object, so you should check their docs. Here a quick example of how you can generate a fake name:

Faker

from faker import Faker


fake = Faker()

# Generate a name
first_name = fake.name()

Fake

from faketory.gens import Fake


first_name = Fake('name').generate()

Generate a random number

# Faker
fake.pyint(min_value=18, max_value=21)

# Fake
Fake('pyint', min_value=18, max_value=21).generate()

Generate unique random numbers

# Faker
numbers = set(
    fake.unique.random_int(min=1, max=10) for i in range(10)
)

# Fake
numbers = set(
    Fake('unique.random_int', min=1, max=10).generate() for i in range(10)
)

Creator

Wαя€vเℓ - jg@warevil.dev

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

faketory-0.1.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

faketory-0.1.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file faketory-0.1.2.tar.gz.

File metadata

  • Download URL: faketory-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0rc1 Darwin/21.6.0

File hashes

Hashes for faketory-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ff8a595f001d5fc55813dc4349fcb835dd4e3e54304f23b22340f770e861fe4c
MD5 eb319d019f82292589a0ba47ef81dbe7
BLAKE2b-256 d8a28c307012f03ac85362495b400dd0d5d56e1490b4a22ecc65e577178f3a24

See more details on using hashes here.

File details

Details for the file faketory-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: faketory-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0rc1 Darwin/21.6.0

File hashes

Hashes for faketory-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 22011ac646879dcc09f9cf3c6e3c9ab1c78ef69b9532273b2d2a551436ffb19e
MD5 3d4055ed318e91cb7882f42d3cfe3c9d
BLAKE2b-256 0ced5c152c006f0ecd8c9f894b15a09f9dbce8b238e642025dff0870184d00e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page