django 我怎么能Assert一个pdf文件在python中被返回?

lo8azlld  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(104)

我正在Assert从我的请求返回了一个PDF。
这是我的测试到目前为止的样子。

@mock.patch('path.to.class._get_file', return_value=MockResponse(status.HTTP_200_OK, 'application/pdf'))
def test_get_should_successfully_return_the_requested_pdf(self, get_file_mock):
    response = self.client.get(f'/api/v1/path/to/file/abc123.pdf, content_type='application/vnd.api+json')

    self.assertEqual(response.status_code, status.HTTP_200_OK)    # works great
    self.assertEqual(response['Content-Type'], 'application/pdf') # works great
    self.assertEqual(response, <abc123.pdf>) # not so great

字符串
如果我执行print来查看响应中的内容:

print(response)

<HttpResponse status_code=200, "application/pdf"


我很确定我没有正确设置@patch。具体而言:

status.HTTP_200_OK, 'application/pdf'


我看过很多关于阅读或打开文件的帖子,但我只需要确保它实际上是返回的pdf(文件)。
我如何设置我的模型,以便我可以Assert一个pdf(文件)被返回?

jxct1oxe

jxct1oxe1#

我看到你正在测试当一个有效的请求被发出时,你的Django视图是否在响应中返回一个PDF文件。使用@patch装饰器设置mock以进行测试是正确的。让我来指导您如何正确地设置mock来测试视图函数的行为。
要测试是否从视图返回PDF文件,需要模拟_get_file函数和requests.get函数。_get_file函数从给定的URL获取PDF内容,requests.get函数用于执行HTTP请求以获取PDF内容。
下面是更新后的测试用例,它正确地模拟了这两个函数来测试您的视图:

from django.test import TestCase
from unittest.mock import patch

class YourViewTestCase(TestCase):

    @patch('path.to.class._get_file')
    def test_get_should_successfully_return_the_requested_pdf(self, _get_file_mock):
        # Mock the PDF content
        pdf_content = b'%PDF-1.3 ...'  # Replace this with the actual PDF content as bytes

        # Set the return value of _get_file to the PDF content
        _get_file_mock.return_value = pdf_content

        # Perform the request to the view with a valid file_url
        file_url = 'http://example.com/abc123.pdf'
        response = self.client.get(f'/{file_url}', content_type='application/vnd.api+json')

        # Assert the response status code and content type
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/pdf')

        # Assert the content of the response to the expected PDF content
        self.assertEqual(response.content, pdf_content)

        # Assert that _get_file was called with the correct URL
        _get_file_mock.assert_called_once_with(file_url)

    @patch('path.to.class._get_file')
    def test_get_should_return_404_for_invalid_file_url(self, _get_file_mock):
        # Set the return value of _get_file to None, simulating a request failure
        _get_file_mock.return_value = None

        # Perform the request to the view with an invalid file_url
        file_url = 'http://example.com/non_existent.pdf'
        response = self.client.get(f'/{file_url}', content_type='application/vnd.api+json')

        # Assert the response status code is 404
        self.assertEqual(response.status_code, 404)

        # Assert that _get_file was called with the correct URL
        _get_file_mock.assert_called_once_with(file_url)

字符串

ohfgkhjo

ohfgkhjo2#

我还想分享我在与@godd0t建议合作后所想到的,这是非常有帮助的。您的millage可能会有所不同,具体取决于您的@patch需求。

@mock.patch('path.to.class._get_file', return_value=MockResponse(status.HTTP_200_OK, b'MockPDF'))
def test_get_should_successfully_return_the_requested_pdf(self, get_file_mock):
    response = self.client.get(f'/api/v1/path/to/file/abc123.pdf, content_type='application/vnd.api+json')

    self.assertEqual(response.status_code, status.HTTP_200_OK)
    self.assertEqual(response['Content-Type'], 'application/pdf')
    self.assertEqual(response.content.decode(), str(get_file_mock.return_value))

字符串

相关问题