测试上传Excel文件和JSONField Django REST

时间:2021-05-09 22:56:56

My models.py has 3 fields. One of them is JSONField()

我的models.py有3个字段。其中一个是JSONField()

attribute = JSONField(null=True, blank=True)  # Free to add any note to here
type = models.CharField(max_length=30, choices=FileType.choices, default=FileType.zipcode)
file = models.FileField(upload_to='import_files')

I normally set JSONField(null=True, blank=True) for the sake of easiness.

为了方便起见,我通常设置JSONField(null = True,blank = True)。

def test_upload_and_process_data_complete_case(self):
    from soken_web.apps.imported_files.models import ImportFile
    with open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx', 'rb') as uploaded_file:
        data = {
            'attribute': {'author': 'Singh'},
            'type': ImportFile.FileType.zipcode,
            'file': uploaded_file
        }
        response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

And my test runs fine without shooting with JSONField

我的测试运行良好,无需使用JSONField拍摄

Experiment:
When I shoot with JSONField like the given. It will failed with this error

实验:当我使用JSONField拍摄时就像给定的一样。它会因此错误而失败

AssertionError: Test data contained a dictionary value for key 'attribute', but multipart uploads do not support nested data. You may want to consider using format='json' in this test case.

However, from my understanding I have to post with multipart because of file.

但是,根据我的理解,我必须使用multipart发布文件。

Question:
Is it possible to do unittest shoot the endpoint that has JSONField and FileField in the same time?

问题:是否可以在同一时间进行unittest拍摄具有JSONField和FileField的端点?

Reference:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte

参考:UnicodeDecodeError:'utf-8'编解码器无法解码位置14中的字节0xb9:无效的起始字节

1 个解决方案

#1


0  

After played around with parser.
I found that nothing in the configurations are wrong. Only one thing I missed is. I have to put single quote cover the {"author": "Singh"}.
Because web browser does submit in the str not python object.

用解析器玩完之后。我发现配置中没有任何错误。我唯一遗漏的是。我必须把单引号覆盖{“作者”:“辛格”}。因为Web浏览器确实在str而不是python对象中提交。

def test_upload_and_process_data_complete_case(self):
    from soken_web.apps.imported_files.models import ImportFile
    with open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx', 'rb') as uploaded_file:
        data = {
            'attribute': '{"author": "Singh"}',
            'type': ImportFile.FileType.zipcode,
            'file': uploaded_file
        }
        response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

#1


0  

After played around with parser.
I found that nothing in the configurations are wrong. Only one thing I missed is. I have to put single quote cover the {"author": "Singh"}.
Because web browser does submit in the str not python object.

用解析器玩完之后。我发现配置中没有任何错误。我唯一遗漏的是。我必须把单引号覆盖{“作者”:“辛格”}。因为Web浏览器确实在str而不是python对象中提交。

def test_upload_and_process_data_complete_case(self):
    from soken_web.apps.imported_files.models import ImportFile
    with open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx', 'rb') as uploaded_file:
        data = {
            'attribute': '{"author": "Singh"}',
            'type': ImportFile.FileType.zipcode,
            'file': uploaded_file
        }
        response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)