I wanted to test if a large file (about 200MB this time) was correctly repelled by validation when POSTed.
I wanted to, but when I actually tried to test using a file of about 200MB, I got angry with Memory Error
.
When I was wondering what happened, there seems to be a Temporary Uploaded File.
test.py
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.test import TestCase
#Omission
class TestClass(TestCase):
def test_testfunction(self):
form_data = {
'upload_file': TemporaryUploadedFile("test.csv", "csv", 214958080, "utf8")
}
response = self.client.post('/test/path/', form_data)
self.assertEqual(302, response.status_code, "I didn't redirect properly")
It's broken a lot, but it looks like it can be used like this. The capacity can be specified in b units with the third argument. This time, I put a value that is a little over 200MB. (The digit changes at 1024. Note that it's not 1000.)
It's very short, but I wonder if it's good in a memorial sense. By the way, if it is a small amount of data, there seems to be ʻInMemoryUploadedFile`.
reference
https://docs.djangoproject.com/ja/3.0/_modules/django/core/files/uploadedfile/
Recommended Posts