D
D
Dmitry Filyushin2018-11-28 18:45:20
Django
Dmitry Filyushin, 2018-11-28 18:45:20

How to write a test in Django to work with an uploaded file?

The site has a file upload button that is parsed and uploaded to the database.
In the code line:

file = request.FILES['impfile']
file_data = file.readlines()

    for line1 in file_data:
        line = line1.decode('cp1251')

Such code works through the post method when uploading a file.
When I try to write a file import test, I pass a reference to open(file) as f, I get an error about the lack of a decode method. I found out that with the POST method, the file has the InMemoryUploadedFile type.
Tried to convert:
def test_read_one(self):
        with open(self.test_file, 'r+') as file:
            mem_file = InMemoryUploadedFile(file, field_name='kfile', name='201810.txt', content_type='text/plain', charset=None, size=os.path.getsize(self.test_file))
            print(mem_file)
            table = load_file(mem_file)
        self.assertTrue(table[0] == self.one_record)

Did not help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question