0

I have a view that does this:

import pandas as pd

def ingest(request):
    for temp_file in request.FILES.values():
        # There only ever is one use next(gen) if you prefer
        path = temp_file.temporary_file_path()
        break
    
   df = pd.read_csv( path, encoding="UTF-16-le", header=None )

   ...

And I'd like to test this view. I can't actually change the view logic as it is designed to be the endpoint fpr an external service that I have no control over.

I've added FILE_UPLOAD_HANDLERS = ['django.core.files.uploadhandler.TemporaryFileUploadHandler',] to the settings to avoid nasty surprises, but I can't make the tests work.

My test.py :

from django.core.files.uploadedfile import TemporaryUploadedFile
from django.test import TestCase

class InputTest(TestCase):
    def test_extract_csv(self):
        form_data = {
            'upload_file': TemporaryUploadedFile("data/test.csv", "csv", 88614, "utf-16-le")
        }

        self.client.post("/ingest", form_data)

but that results in empty data in the view.

ic_fl2
  • 831
  • 9
  • 29
  • 1
    Does [how to unit test file upload in django](https://stackoverflow.com/q/11170425/1324033) help? – Sayse Oct 21 '21 at 11:51
  • 1
    I had tried the SimpleUpload answer as that is the highest rated one, and as SimpleUpload is a Subclass of MemoryUpload that had lead to the extra settings change and no success, but the accepted answer worked. https://stackoverflow.com/a/11170472/4895724 – ic_fl2 Oct 21 '21 at 15:16

0 Answers0