Our app has a limit on the size of the file that can be uploaded(<15MB). Is there a way i can create a dummy file of this size to test this?
from django.core.files.uploadedfile import InMemoryUploadedFile
import io
f = io.StringIO("some initial text data")
f.seek(1024 * 1024 * 1024)
token_file=InMemoryUploadedFile(file=f , field_name=None, name="file.txt", content_type="text/plain", size=15729641, charset="utf8")
print(token_file.size)
response = self.client.post(HOME_URL, {'entity_level': 1, 'data_type': 1, 'team': GROUP_ID,
'upload_file': token_file}) # data=form_data, format='multipart')
Inside clean method of the Form Class :
def clean_upload_file(self):
uploaded_files = self.files.getlist("upload_file")
print(f"uploaded_files : {uploaded_files}")
total_upload_size = sum([file.size for file in uploaded_files]) / (1024 * 1024)
print(f"total_uploaded_size{total_upload_size}")
if total_upload_size > MAX_UPLOADED_SIZE:
raise forms.ValidationError(f"Total upload size is greater than {MAX_UPLOADED_SIZE} MB")
Output:
self.files.getlist('upload_file'): [<InMemoryUploadedFile: file.txt (text/plain)>]
uploaded_files : [<InMemoryUploadedFile: file.txt (text/plain)>]
total_uploaded_size0.0
FILE SIZE ALWAYS COMES AS ZERO WITH THIS APPROACH IS THERE A WAY TO MOCK THE FILE SIZE?