如何断言 python 返回了 pdf 文件?

问题描述 投票:0回答:2

我正在努力断言我的请求返回了 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(文件)。

如何设置我的模型以便我可以断言已返回 pdf(文件)?

python django django-rest-framework python-unittest
2个回答
1
投票

我看到您正在尝试测试当发出有效请求时您的 Django 视图是否在响应中返回 PDF 文件。通过使用

@patch
装饰器来设置模拟以进行测试,您走在正确的轨道上。让我指导您如何正确设置模拟来测试视图函数的行为。

要测试是否从您的视图返回 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)

0
投票

我还想分享我在使用@godd0t建议后的想法,这非常有帮助。您的里程可能会根据您的

@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))

© www.soinside.com 2019 - 2024. All rights reserved.