[PYTHON] [Django] Test to send a file by POST and check the returned context [TDD]

I want to test a page with a form!

Previous article As I said, I would like to make a test for a page with a Form. But for some reason I got an error and couldn't test. I thought I should test the Form for the time being, but when I thought about it, I couldn't test the processing after that, so it didn't help. After all, I have to POST and check the context.

What was the solution and what was it?

The solution is as follows. All I had to do was open it with open, insert the file handler, and POST it.

tests.py


from django.test import TestCase, Client
from django.contrib.auth.models import User

class Test_Model_Create(TestCase):
    def setUp(self):
        #Create test user
        self.user = User.objects.create_user(username='tdd', email='[email protected]', password='test_pass') 
        self.client=Client()
        #Login
        self.client.login(username='tdd', password="test_pass")  

    def test_step1(self):      
        with open('./path/test.csv') as f:  
            data = {
                "nametest" : "test",
                "file_data" : f
            }
            response = self.client.post('/model_create/form/', data)
        self.assertIn('file_data', response.context, "file in context_data not included")

It's okay like this.

The point that was stuck is

--I did POST after closing the file. --I passed a file instead of a file handler (File (open ('./ path / excel_test.csv')))

That was the reason. I also tried using RequestFactory instead of Client, but that was the reason ...

Multiple file upload test

By the way, I have to give two files this time, but in such a case it seems that I can write as below.

tests.py


        with open('./path/testA.csv') as fa,open('./path/testB.csv') as fb:    
            data = {
                "name" : "test",
                "file_dataA" : fa,
                "file_dataB" : fb
            }
            response = self.client.post('/model_create/form/', data)

The with sentence is really easy. It's amazing.

Summary

I'm really wondering why I was so worried. I want to reduce Pokamis.

Recommended Posts

[Django] Test to send a file by POST and check the returned context [TDD]
[Django] I wanted to test when POSTing a large file [TDD]
I want to receive the configuration file and check if the JSON file generated by jinja2 is a valid JSON
Create a Python image in Django without a dummy image file and test the image upload
[Django] How to test Form [TDD]
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
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
How to check the version of Django
The road to fighting the connection between Nginx, Django and uwsgi and winning a little
Attempt to launch another .exe and save the console output to a text file
How to count the number of elements in Django and output to a template
I want to make a music player and file music at the same time
I want to write an element to a file with numpy and check it.
Zip-compress any file with the [shell] command to create a file and delete the original file.
The first thing to check when a No Reverse Match occurs in Django
I tried to pass the G test and E qualification by training from 50
Save the object to a file with pickle
Put the file uploaded by django into MinIO
How to execute a schedule by specifying the Python time zone and execution frequency
[Python] You can save an object to a file by using the pickle module.
Create an audio file with the text-to-speech function with Google Text To Speak and check the text as a guide for the speech for 3 minutes.
Created a module to monitor file and URL updates
Read the xml file by referring to the Python tutorial
How to check the Java version used by Maven
Transit to the update screen with the Django a tag
How to post a ticket from the Shogun API
Output a binary dump in binary and revert to a binary file
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
How to create a record by pasting a relation to the inheriting source Model in the Model inherited by Django
How to send a file in one shot by connecting to a host on the other side of the platform with SCP in multiple stages