un1t/django-cleanup

`storage` option for `FileField` doesn't change for tests

OttoAndrey opened this issue · 4 comments

Hi, thank for useful package!

I have a one problem. For local development I use minio to imitate S3. But for tests I want use django.core.files.storage.FileSystemStorage because I dont't want write files in S3 storage while testing and it is slow and etc. So, for testing I use pytest and improve pytest_configure function like this:

# conftest.py
def pytest_configure():
    settings.DEFAULT_FILE_STORAGE = (
        "django.core.files.storage.FileSystemStorage"
    )

And it works fine. But if I add django_cleanup.apps.CleanupConfig in INSTALLED_APPS in that case storage for FileField always be storages.backends.s3boto3.S3Boto3Storage :(

Example:
Django==4.1.9
django-cleanup==7.0.0
pytest==7.3.1

# settings.py
INSTALLED_APPS = [
    ...
    "django_cleanup.apps.CleanupConfig"
]

DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"  # S3 as default file storage for local development
# models.py
class Document(models.Model):
    file = models.FileField()  # simple model with simple FileField
# conftest.py
def pytest_configure():
    settings.DEFAULT_FILE_STORAGE = (
        "django.core.files.storage.FileSystemStorage"  # set FileSystemStorage for tests as default file storage
    )
# tests.py
from django.core.files.storage import FileSystemStorage
from models import Document
def test_default_storage():
    document = Document()
    assert document.file.storage.__class__ == FileSystemStorage  # assert will fail because django-cleanup now allow change default file storage for testing :(

If I disable django-cleanup in INSTALLED_APPS test will pass.

Django-cleanup is setup on django app setup. So if you want to change settings you would have to do it before django is setup. You didn't specify if you use pytest-django or how you setup django but you may want to explore the options that modify or provide the settings before django is setup such as through a tests specific settings file or a pytest plugin: https://pytest-django.readthedocs.io/en/latest/configuring_django.html

Sorry for late answer. Yes, I'm using pytest-django. And yes, I already use special file for testing testing_settings.py with one row DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage" and it works for tests.

But I hoped that here is way setup settings without special separate file :(
For example use only def pytest_configure(): - all settings for pytest in one place.

I think this is likely just an issue with your test setup. I don't think you can avoid a separate settings file, after all a settings file is a great place to have all your settings.

For example, how testing is done in this repo, the tests are all in a separate folder from the source of the package, so a separate settings file is used: https://github.com/un1t/django-cleanup/tree/master/test

You will notice though that there is one test where we do change a setting on the fly:

settings.DEFAULT_FILE_STORAGE = 'test.storage.DeleteErrorStorage'

Going to close as not an issue with django-cleanup.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.