Skip to main content

Django object manager for tests

Project description

django-object-manager

Simple, declarative, repeatable object management for Django tests

PyPI version Build Status Coverage Status

Examples:

# Model registration - usuallu in app/tests.py
# from .models import ...
from .models import User, Film, FilmCategory
from django_object_manager import ObjManagerMixin, ObjectManager


ObjectManager.register(
    User,
    {
        'bob': {
            'name': 'Bob',
            'email': 'bob@domain.com',
        },
        'alice': {
            'name': 'Alice',
            'email': 'alice@example.com',
        },
    })
ObjectManager.register(
    FilmCategory,
    {
        'drama': {
            'name': 'Drama',
        },
        'crime': {
            'name': 'Crime',
        },
        'serious': {
            'name': 'Adult films',
        },
        'anime': {
            'name': 'Anime',
            'parent': 'serious',
        },
    })
ObjectManager.register(
    Film,
    {
        'memento': {
            'name': 'Memento',
            'year': 2000,
            'uploaded_by': 'bob',
            'categories': ['crime', 'drama']
        },
})

# Further usage in tests:
object_manager = ObjectManager() # or self.object_manager if inherited from 
# Single object creation - 'bob' is previously registered idenitifier
bob = object_manager.get_user('bob')

# Object creation with attribute owerwriting
user3 = object_manager.get_user('bob', email='other@domain.com')

# Fully custom object creation
user = object_manager.get_user(name='Jack',
                               email='test@test.org')

# All predefined objects of given model - returns id:object dictionary
user = object_manager.get_users()


# Object with dependencies - dependencies are referenced by
# - their registered indentifiers
# - by passing already created object

memento = object_manager.get_film('memento') # Will create two categories and one user

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

django_object_manager-0.0.9-py2.py3-none-any.whl (9.9 kB view hashes)

Uploaded Python 2 Python 3

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