Skip to main content

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
    ...

Release files for django-override-storage 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-override-storage 0.3.2
File Size Uploaded
django-override-storage-0.3.2.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-override-storage 0.3.2
File Interpreter ABI Platform
django_override_storage-0.3.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 21.4 kB

Release files / django-override-storage-0.3.2.tar.gz

Download URL django-override-storage-0.3.2.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
995e1a42f056c9f9bc114077c11d67520ec7d8a752a59be62729e641562b133e
BLAKE2b-256 checksum
How to use checksums
6aa2c484b3b0dbdf0bf67cf4ce8e661d92affc60a1027b5e2772d7bcc8b170d7
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / django_override_storage-0.3.2-py2.py3-none-any.whl

Download URL django_override_storage-0.3.2-py2.py3-none-any.whl
Size 11.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1f1a13274d66cc481b19d63c8bd43c94066824008bcdd26ec65d125b1ce8ec39
BLAKE2b-256 checksum
How to use checksums
2d919fdf0407cefa9d777fbfec4ad74f9fe2d36e0e39e6b1e58d171b9ce7cf50
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page