﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36269	Document overriding STORAGES for tests when using callable storage in a FileField	emillumine	Ahmed Nassar	"It seems that the default storage and other user defined storages dont have the same behaviour in tests.
When overriding the settings.STORAGES in tests we can easily replace the default storage whereas it doesn't work for another user defined storage.
I tried to reproduce my problem with a minimal code below.

It may seems successful at first because the first assertion `assert storages[""other_storage""]._location == ""other_location""` is green. But the last one `assert test.other_file.storage._location == ""other_location""` is not. Its value is the one defined in settings.STORAGES.

I would expect a similar behaviour in both cases.

{{{
# settings.py
STORAGES = {
    ""default"": {
        ""BACKEND"": ""django.core.files.storage.FileSystemStorage"",
    },
    ""staticfiles"": {
        ""BACKEND"": ""django.contrib.staticfiles.storage.StaticFilesStorage"",
    },
    ""other_storage"": {
        ""BACKEND"": ""django.core.files.storage.FileSystemStorage"",
    }
}
}}}


{{{
# test.py

from django.test import override_settings
from django.core.files.storage import default_storage
from django.core.files.storage import storages
from django.core.files.storage import InMemoryStorage
from django.db import models
from django.core.files.base import ContentFile


def select_other_storage():
    return storages[""other_storage""]

def select_default_storage():
    return default_storage


class TestModel(models.Model):
    other_file = models.FileField(storage=select_other_storage)
    default_file = models.FileField(storage=select_default_storage)

    class Meta:
        app_label = ""test""

@override_settings(
    STORAGES={
        ""default"": {
            ""BACKEND"": ""django.core.files.storage.InMemoryStorage"",
            ""OPTIONS"": {
                ""location"": ""default_location"",
                },
            },
        ""other_storage"": {
            ""BACKEND"": ""django.core.files.storage.InMemoryStorage"",
            ""OPTIONS"": {
                ""location"": ""other_location"",
                },
        }
    }
)
def test_override_settings():
    assert default_storage._location == ""default_location""
    assert storages[""other_storage""]._location == ""other_location""

    test = TestModel(
        other_file=ContentFile(""test data"", name=""test.pdf""),
        default_file=ContentFile(""test data"", name=""test.pdf""),
    )
    assert isinstance(test.default_file.storage, InMemoryStorage)
    assert test.default_file.storage._location == ""default_location""
    # following assertions fail
    assert isinstance(test.other_file.storage, InMemoryStorage)
    assert test.other_file.storage._location == ""other_location""
}}}"	Cleanup/optimization	closed	File uploads/storage	5.1	Normal	fixed	override_settings, storages	emillumine	Ready for checkin	1	0	0	0	0	0
