Я пытаюсь проверить конкретное представление набора, которое занимает formData
, которое содержит файл csv
. Мой тест выглядит так:
@staticmethod
def _create_file(rows: List[List[Any]], content_type: str = 'text/csv') -> UploadedFile:
f = StringIO()
csv.writer(f).writerows(rows)
return UploadedFile(file=f.read(), name='test.csv', content_type=content_type)
def test_upload_valid(self):
""" Asserts that the upload view works correctly with valid information. """
response = self.client.post(reverse('core_api:upload-upload'), {
'file_type': self.file_type,
'file': self._create_file([['Test', 'Test', 'Test', 'Test'], [1, 'Test', 'tokyo', 2]])
})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.content['process_status'], DemoConsumerDataParser.SUCCESS_STATUS)
self.assertEqual(len(response.content['errors']), 0)
Однако здесь это не получается:
'file': self._create_file([self.header, self.acceptable_row])
Со следующей ошибкой:
AttributeError: 'str' object has no attribute 'read'
Как изменить сообщение, чтобы оно правильно отправляло этот файл?