首页 > 解决方案 > Django 文件断言

问题描述

我成功地PATCH使用了我的有效载荷postman app。为了节省我的时间,我想在代码库而不是图形库中进行。

    def test_owner_patch_avatar_and_background(self):
        client = APIClient()
        client.force_authenticate(user=self.jc)
        url = reverse('mobile_me')
        with open('media/background.jpg', 'rb') as background:
            with open('media/default.png', 'rb') as avatar:
                data = {
                    'avatar': avatar,
                    'background': background,
                }
                res = client.patch(url, data=data, format='multipart')
        user_profile = UserProfile.objects.first()
        assert status.HTTP_202_ACCEPTED == res.status_code
        assert background == user_profile.background
        assert avatar == user_profile.avatar

我坚持内存文件断言。我想断言保存的文件与修补的文件相同。目前我做name他们的断言。这是我的测试用例中的调试行。

In[4]: background
Out[4]: <_io.BufferedReader name='media/background.jpg'>
In[5]: user_profile.background
Out[5]: <ImageFieldFile: backgrounds/background_b4gQ4BS.jpg>

问题:
如何在 Python/Django REST 中的上传文件和保存文件之间进行断言?

标签: pythondjangofileunit-testingdjango-rest-framework

解决方案


推荐阅读