Skip to main content

Shared database connections for multi-threaded Django test setups.

Project description

This proxy database backend is designed to speed up multi-threaded testing setups.

It enables those setups (for instance using LiveServerTestCase) to use Django’s standard TestCase - where each test runs in its own transaction - instead of the much slower TransactionTestCase that needs to flush the whole database between each tests.

It is NOT intended for production use, only for faster testing setups.

This project support Django versions 1.6 and later, and Python 2.7, 3.3 and later.

Setup

First, install django-shareddb:

$ pip install django-shareddb

Then, simply update your settings to use its wrapping engine:

DATABASES = {
    'default': {
        'ENGINE': 'shareddb.backends.shareddb',
        'INNER_ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'test-dbsharing',
    }
}

Since altering those settings is very frequent, django-shareddb also provides a simple patch_databases function:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'test-dbsharing',
    }
}

if os.environ['FAST_TESTS']:
    import shareddb
    DATABASES = shareddb.patch_databases(DATABASES)

Once the settings are ready, simply replace your calls to Django’s LiveServerTestCase with the django-shareddb variant:

from shareddb import testcases

class MyTests(testcases.LiveServerTestCase):
    def test_something(self):
        # Your test here

The django-shareddb LiveServerTestCase is simply a clone of Django’s version, but uses django.test.TestCase instead of django.test.TransactionTestCase.

Testing

This libraries has been succesfully tested with sqlite and postgresql.

If you want to test it with other databases, please clone it and alter dev/settings.py for your setup, then run ./manage.py test.

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-shareddb-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

File details

Details for the file django-shareddb-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-shareddb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73c1eb27a4b8f9a02c70db047d2e33de977429fc4de818959be3f3dd302b4ba9
MD5 15d9472ef5f51d00cd23c86f18c90dcc
BLAKE2b-256 39c267acd9bc22ee58246c8cba39c64a9bd22c1f4ccf39bcde4c1f25b7a99233

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