Skip to main content

A simple factory for Django Applications

Project description

Django Simple Factory

A simple factory implementation built upon Faker.

Installation

pip install django-simple-factory

Usage

To use factories, create a factories.py inside of a Django app. Then subclass the django_simple_factory.factories.Factory class. To define the model generation, override the definition method.

from django_simple_factory.factories import Factory
from posts import models

class PostFactory(Factory):
    model = models.Post

    def definition(self):
        return {
            "title": self.faker.word(),
            "content": self.faker.text(),
        }

class CommentFactory(Factory):
    model = "comments.Comment" # You can provide a model string

    def definition(self):
        return {
            "content": self.faker.text(),
            "post": PostFactory() # You can provide either a factory instance or a factory string (e.g)
        }

To use these models, instaniate the factories, then call either make or create.

post_factory = PostFactory()
post_factory.make() # Does not persist
post_factory.create() # Does Persist

Factories that have related factories, say a Comment requires a Post, can pass a new dictionary for the post values to override.

comment_factory = CommentFactory()
comment = comment_factory.make(content="Hello", post={"title": "My Post"})

comment.content # Hello
comment.post.title # My Post

Alternatively, you may use the "django-style" of post__title="My Title".

comment_factory = CommentFactory()
comment = comment_factory.make(content="Hello", post__title="My Post")

comment.content # Hello
comment.post.title # My Post

To enable easier testing, a FactoryTestMixin has been included that enables rich definition of factories.

from django_simple_factory.mixins import FactoryTestMixin
from django.test import TestCase
from posts import models

class PostTest(FactoryTestMixin, TestCase):
    factories = ["posts.PostFactory"]

    def test_post_creation(self):
        post = self.factories[models.Post].create()
        ...

API

django_simple_factory.factories.Factory

The primary class of the package, factories allow for simple and easy testing of Django models.

Method Description
configure_faker A hook that enables the configuration of the factory's faker instance. Each factory contains its own separate faker instance.
get_factory A class method that enables the discovery of a Factory (e.g. Factory.get_factory("posts.PostFactory")). The string passed in mimics the models access. To access a factory via string it should be of the form <app_name>.<factory_name>.
make Make is one of the primary methods for creating an model. make does not persist the model, rather it just gives an instance. To override the generated values, provide keyword arguments to the method. If you want to override related object creation, provide a keyword argument with a dictionary (e.g. post_factory.make(user={"email": "test@example.com"}))
create Create is very much like make, except it does persist the model.

Contact

If there are any issues, please feel free to make an issue. If you have suggested improvements, please make an issue where we can discuss.

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

django_simple_factory-0.2.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

django_simple_factory-0.2.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file django_simple_factory-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django_simple_factory-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c1f44c38450fd708edecbb026f2d26fa1e09531035d49942dd88f1d9cf4cfb60
MD5 e29a7b6b73ebe3a757f542fd26c9f06f
BLAKE2b-256 af3e9ac56202696e792d4c749874ba617467ea17eb68cc037bab2b029eee8698

See more details on using hashes here.

File details

Details for the file django_simple_factory-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_simple_factory-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40ea79e7ab4b122cec6c3c271b0deb113e91bb82fa84a27fdab55f4906e259b2
MD5 62372f8ae9ab11b09ddc9af5e190e92e
BLAKE2b-256 3f60b6f327420bbcf07d83ca47546363fb9e1297361981e1b36e999f9a471e91

See more details on using hashes here.

Supported by

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