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.