DEFAULT_FILE_STORAGE and tests

137 views
Skip to first unread message

sandro dentella

unread,
Sep 20, 2018, 9:25:23 AM9/20/18
to Django users
Hi,

I have a storage that create some thumbnail and is declared in some  models' fields. It's created inheriting from DEFAULT_FILE_STORAGE and it just works fine both in FileSystemStorage and in Aws.

For this project need the aws Storage (from Django_storages) but when testing I prefer the local FileSystemStorage, so my TEST_RUNNER sets the desired one as follows:



from django.conf import settings
from django.test.runner import DiscoverRunner


class GeneraliDiscoverRunner(DiscoverRunner):
   
def __init__(self, *args, **kwargs):
        settings
.DEFAULT_FILE_STORAGE = 'web.storage.TestStorage'
       
super().__init__(*args, **kwargs)

It seems that this changes the Storage in all situations apart from the fields declared in the models via the storage=ThumbnailStorage. I guess when model are read the settings from the runner has not yet been set. Is there a way to set the Storage in due time?


Thanks in advance
sandro
*:-)

Raffaele Salmaso

unread,
Sep 20, 2018, 9:37:25 AM9/20/18
to django...@googlegroups.com
On Thu, Sep 20, 2018 at 3:25 PM sandro dentella <sandro....@gmail.com> wrote:


from django.conf import settings
from django.test.runner import DiscoverRunner


class GeneraliDiscoverRunner(DiscoverRunner):
   
def __init__(self, *args, **kwargs):
        settings
.DEFAULT_FILE_STORAGE = 'web.storage.TestStorage'
       
super().__init__(*args, **kwargs)

It seems that this changes the Storage in all situations apart from the fields declared in the models via the storage=ThumbnailStorage. I guess when model are read the settings from the runner has not yet been set. Is there a way to set the Storage in due time?

Not really sure, but you can use override_settings from django.test.utils as

from django.test.utils import override_settings
@override_settings(DEFAULT_FILE_STORAGE='web.storage.TestStorage')
class GeneraliDiscoverRunner(DiscoverRunner):
    pass

(override_settings does a lot of other things)

or use a custom DJANGO_SETTINGS_MODULE

-- 

Alessandro Dentella

unread,
Sep 20, 2018, 9:59:11 AM9/20/18
to django...@googlegroups.com

Hi Raffaele,
really I've already tried it as well, but it happens the same: it
doesn't work for the Storage declared in the models.

I'd need to set the settings *before* the models are read. It seems
that the runner is instantiated too late!


sandro
Message has been deleted

luca bocchi

unread,
Sep 25, 2018, 6:25:50 PM9/25/18
to Django users
already tried:

class GeneraliDiscoverRunner(DiscoverRunner):
    
def setup_test_environment(self, **kwargs):
        settings
.DEFAULT_FILE_STORAGE = 'web.storage.TestStorage'
        
super().setup_test_environment(**kwargs)

?

although I'm not sure that settings can be changed at runtime by that.

other options:
- use a custom settings module (already suggested)
- try to make the change in the setUp method of your TestCase subclass.

additional info about how you're trying to do that would be helpful.

L
Message has been deleted

Alessandro Dentella

unread,
Sep 26, 2018, 8:22:16 AM9/26/18
to django...@googlegroups.com
Ciao Luca!

On Tue, Sep 25, 2018 at 12:29:45PM -0700, Luca Bocchi wrote:
> Already tried with:
> class GeneraliDiscoverRunner(DiscoverRunner):
> def setup_test_environment(self, **kwargs):
> settings.DEFAULT_FILE_STORAGE = 'web.storage.TestStorage'
> super().setup_test_environment(**kwargs)
> ?
> otherwise, I'd say to run the test with a custom settings module like
> ./manage.py test --settings=test_settings

This seems to be the only viable solution, thanks


> or
> try to override that value in the setUp method of your TestCase class.

I did try this as well and to my surprise it didn't work eather, even
thought I should spend more time debugging why...

sandro
Reply all
Reply to author
Forward
0 new messages