[PYTHON] [Django] I wanted to test when POSTing a large file [TDD]

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.

You can make something like dummy data!

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.)

Summary

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

[Django] I wanted to test when POSTing a large file [TDD]
Django test
[Django] How to test Form [TDD]
Test standard output with Pytest
[Django] I wanted to test when POSTing a large file [TDD]
Test Driven Development with Django Part 4
Test Driven Development with Django Part 6
Test Driven Development with Django Part 2
[Test Driven Development (TDD)] Chapter 21 Summary
Test Driven Development with Django Part 1
Test Driven Development with Django Part 5
[Django] Test to send a file by POST and check the returned context [TDD]
[Django] How to test Form [TDD]
I don't want to take a coding test
I want to write to a file with Python
I want to upload a Django app to heroku
I tried to create a table only with Django
I want to write in Python! (2) Let's write a test
What to do when gdal_merge creates a huge file
What I did when I stumbled on a Django tutorial
I want to randomly sample a file in Python
I wanted to modify Django's admin site a bit
When I got a list of study sessions in Python, I found something I wanted to make
[Django] A memorandum when you want to communicate asynchronously [Python3]
I wanted to skip certain extensions when building Sphinx documentation
I made a command to generate a table comment in Django
I stumbled when I tried to install Basemap, so a memorandum
[Django] Test doesn't work if I change the file location
Upload a file to Dropbox
I tried to convert a Python file to EXE (Recursion error supported)
I wanted to do it like running an AtCoder test case.
A memorandum when I tried to get it automatically with selenium
Python> I made a test code for my own external file
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
I wanted to convert my face photo into a Yuyushiki style.
A note I was addicted to when making a beep on Linux
I wanted to solve the ABC164 A ~ D problem with Python
How to create a large amount of test data in MySQL? ??
What I did when I wanted to make Python faster -Numba edition-
I made a library that adds docstring to a Python stub file.
[Python] When I tried to make a decompression tool with a zip file I just knew, I was addicted to sys.exit ()
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
Steps to create a Django project
Hash chain I wanted to avoid (2)
Write standard output to a file
I wanted to evolve cGAN to ACGAN
Hash chain I wanted to avoid (1)
How to create a config file
Create a file uploader with Django
I wanted to generate a sentence like "Fucking Deca Rashomon" (past form)
A story I was addicted to when inserting from Python to a PostgreSQL table
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I made a program to notify you by LINE when switches arrive
I made a program to check the size of a file in Python
I tried to verify the result of A / B test by chi-square test
When I try to go back using chainer, it fits a little
When writing a test using DB with django, you may be able to do it faster by using `setUpTestData ()`