5

Im testing one of my webpages POST functions. if request.method == 'POST' it generates a pdf and attaches it to the user. Every time I ran tests I was generating pdf files which were building up in my MEDIA folder and I had to manually delete them. I decided to look for ways to prevent this from happening and what seems to me to be the best way is to override my MEDIA_ROOT with a tempfile.

I am getting the following error message

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Acer\\AppData\\Local\\Temp\\profile_pics\\default.jpg'

I am sure this is because I must create and log-in a user prior to running the test (I dont want to do this in setup as I must generate different types of users for different tests). Every time a user is created their profile picture is set to profile_pics\\default.jpg'.

My understanding is that the mocked MEDIA_ROOT is empty and doesnt contain the default profile pic, so the error is thrown.

My question is how can I circumvent this (ideally mocking the default.jpg image)? I found other solutions besides mocking the MEDIA_ROOT folder, but they seem quite hacky and id prefer to do it properly and hopefully learn from this.

tests.py

from django.test import override_settings
import tempfile

@override_settings(MEDIA_ROOT=tempfile.gettempdir())
def test_redirects_after_POST(self):
    user = User.objects.create_superuser('username')
    self.client.force_login(user)

    response = self.client.post(
        reverse('final_question'), data={
        'answer': 'this has me totally stumped'}
    )

    self.assertRedirects(response, reverse('page_2'))

Thank you.

horse
  • 479
  • 7
  • 25

1 Answers1

3

The problem:

PDF files are building up in your MEDIA_ROOT folder because there is nothing automatically deleting them.

Recommended solution:

I actually ran into this problem a few months ago myself, and the solution I found was the django-cleanup tool. It's very easy to install and integrate into your app (simply add it to your INSTALLED_APPS in settings.py).

Note: This is assuming that your PDF files are in a FileField of one of your models. If that's not the case, then this solution won't work for you.

ketcham
  • 922
  • 4
  • 15
  • Thank you. I will have a look at that when i can and let you know how i go – horse Jun 11 '20 at 05:05
  • I installed it and added it to my installed apps, yet it didnt clean up my files. The docs says if it doesnt work to check that your models are loaded using ```apps.get_models()```. Im not sure where to put this, though I put it at the top of one of my models.py and got the following error ```raise AppRegistryNotReady("Models aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.```. Ive searched this error but the fixes havent helped me. Do you have any suggestions? Thank you very much – horse Jun 11 '20 at 07:54
  • It should delete files when the model that corresponds to the file to-be-deleted is saved. Have you tried calling .save() after attaching a new PDF file to that model? – ketcham Jun 11 '20 at 14:36
  • Hi. I cant seem to figure out why its not working, though im sure your recommendation is fine and Ive just got something set incorrectly somewhere. Thank you – horse Jun 13 '20 at 12:49