Skip to main content

Django test helpers to manage file storage side effects.

Project description

Stop filling up your disk with test files or your code with file system mocks!

This project provides tools to help you reduce the side effects of using FileFields during tests.

Installation

pip install django-override-storage

Simple Usage

Calling override_storage() without any arguments will patch all FileField fields to store the contents of the file in an in-memory cache and returns the fields to their previous storages when leaving its context. The storage cache is deleted at the end of each test or when exiting the context manager depending on how it is called.

It can be used similarly to django.test.utils.override_settings: as a class decorator, a method decorator or a context manager.

from django.core.files.base import ContentFile
from django.test import TestCase

from override_storage import override_storage
from override_storage.storage import LocMemStorage

from .models import SimpleModel

class OverrideStorageTestCase(TestCase):

    def test_context_manager(self):
        with override_storage():
            # By default, all files saved to in memory cache.
            obj = SimpleModel()
            obj.upload_file.save('test.txt', ContentFile('content'))

            # Get your file back!
            content = obj.upload_file.read()

    @override_storage(storage=LocMemStorage())
    def test_method_decorator(self):
        # You can also specify to replace all storage backends with a
        # storage instance of your choosing. Depending on the storage type,
        # this could mean all writes will persist for the life of the
        # instance. This does not really matter if you wanted to pass in a
        # FileSystemStorage instance as those writes will be persisted
        # regardless.
        ...

    @override_storage(storage=LocMemStorage)
    def test_method_decorator(self):
        # Passing in a class will create a new instance for every test.

    @override_storage()
    def test_method_decorator(self):
        # Used as a method decorator.
        ...


@override_storage()
class OverrideStorageClassTestCase(TestCase):
    # You can also wrap classes.
    ...

It can also be used globally through a custom test runner. This can be achieved by setting the TEST_RUNNER setting in your settings file or however else you may choose to define the Django test runner.

Warning

TEST_RUNNER only sets up the replacement storage once at the start of the tests as there are no hooks into the setUp / tearDown methods of the test class. Using override_storage.LocMemStorageDiscoverRunner will share a single in memory cache across all tests. While this shouldn’t affect your tests, if you write a lot of big files, you may run out of memory.

TEST_RUNNER = 'override_storage.LocMemStorageDiscoverRunner'

Storage information

Like override_storage, locmem_stats_override_storage patches all FileField fields to store the contents of the file in an in-memory cache and returns the fields to their previous storages when leaving its context.

In addition to the normal functionality, it returns an object with information about the calls to the _open and _save methods of the test storage. In general it records which fields have had files read from or written to them and the names of the files are recorded.

from django.core.files.base import ContentFile
from django.test import TestCase

from override_storage import locmem_stats_override_storage

from .models import SimpleModel

class OverrideStorageTestCase(TestCase):

    def test_context_manager(self):
        with locmem_stats_override_storage() as storage_stats:
            # All files saved to in memory cache.
            obj = SimpleModel()
            obj.upload_file.save('test.txt', ContentFile('content'))

            # Check how many files have been saved
            storage_stats.save_cnt

            # Check which fields were read or saved
            storage_stats.fields_saved
            storage_stats.fields_read

            # Get a list of names, by field, which have been saved or read.
            storage_stats.reads_by_field
            storage_stats.saves_by_field

            # Get your file back!
            content = obj.upload_file.read()

    @locmem_stats_override_storage(name='storage_stats')
    def test_method_decorator(self, storage_stats):
        # access to storage stats by specifying `name` which is the name of
        # the kwarg to be used in the function signature.
        ...


@locmem_stats_override_storage(name='storage_stats')
class OverrideStorageClassTestCase(TestCase):
    storage_stats = None

    # access to storage stats by specifying attr_name
    ...

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-override-storage-0.3.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

django_override_storage-0.3.2-py2.py3-none-any.whl (11.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-override-storage-0.3.2.tar.gz.

File metadata

  • Download URL: django-override-storage-0.3.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for django-override-storage-0.3.2.tar.gz
Algorithm Hash digest
SHA256 995e1a42f056c9f9bc114077c11d67520ec7d8a752a59be62729e641562b133e
MD5 7a9e4532668f4414b29b3086a09c0ac9
BLAKE2b-256 6aa2c484b3b0dbdf0bf67cf4ce8e661d92affc60a1027b5e2772d7bcc8b170d7

See more details on using hashes here.

File details

Details for the file django_override_storage-0.3.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_override_storage-0.3.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for django_override_storage-0.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1f1a13274d66cc481b19d63c8bd43c94066824008bcdd26ec65d125b1ce8ec39
MD5 ed5c03e13ccc49f48e4ddb7696a11181
BLAKE2b-256 2d919fdf0407cefa9d777fbfec4ad74f9fe2d36e0e39e6b1e58d171b9ce7cf50

See more details on using hashes here.

Supported by

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