Package: django-override-storage
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.Installationpipinstalldjango-override-storageSimple UsageCallingoverride_storage()without any arguments will patch allFileFieldfields 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 todjango.test.utils.override_settings: as a class
decorator, a method decorator or a context manager.fromdjango.core.files.baseimportContentFilefromdjango.testimportTestCasefromoverride_storageimportoverride_storagefromoverride_storage.storageimportLocMemStoragefrom.modelsimportSimpleModelclassOverrideStorageTestCase(TestCase):deftest_context_manager(self):withoverride_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())deftest_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)deftest_method_decorator(self):# Passing in a class will create a new instance for every test.@override_storage()deftest_method_decorator(self):# Used as a method decorator....@override_storage()classOverrideStorageClassTestCase(TestCase):# You can also wrap classes....It can also be used globally through a custom test runner. This can be achieved
by setting theTEST_RUNNERsetting in your settings file or however else
you may choose to define the Django test runner.WarningTEST_RUNNERonly sets up the replacement storage once at the start of the
tests as there are no hooks into thesetUp/tearDownmethods of the
test class. Usingoverride_storage.LocMemStorageDiscoverRunnerwill 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 informationLikeoverride_storage,locmem_stats_override_storagepatches allFileFieldfields 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_openand_savemethods 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.fromdjango.core.files.baseimportContentFilefromdjango.testimportTestCasefromoverride_storageimportlocmem_stats_override_storagefrom.modelsimportSimpleModelclassOverrideStorageTestCase(TestCase):deftest_context_manager(self):withlocmem_stats_override_storage()asstorage_stats:# All files saved to in memory cache.obj=SimpleModel()obj.upload_file.save('test.txt',ContentFile('content'))# Check how many files have been savedstorage_stats.save_cnt# Check which fields were read or savedstorage_stats.fields_savedstorage_stats.fields_read# Get a list of names, by field, which have been saved or read.storage_stats.reads_by_fieldstorage_stats.saves_by_field# Get your file back!content=obj.upload_file.read()@locmem_stats_override_storage(name='storage_stats')deftest_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')classOverrideStorageClassTestCase(TestCase):storage_stats=None# access to storage stats by specifying attr_name...