python - 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 中的上传文件和保存文件之间进行断言?
解决方案
推荐阅读
- rollupjs - 如何使用一组配置在 Rollup 中查看新文件?
- pandas - Pandas 和 Tensorflow:想要将 Pandas Dataframe 保存到 .npz 文件
- javascript - 如何使用 getElementsByClassName 调用同一类的多个元素?
- java - QueryDSL 如何将 ParamExpression 添加到 JPAQuery 以使用 EntityManager.createQuery 执行
- google-cloud-platform - Suse 公共云按需虚拟机 (VM) 无法接收更新
- javascript - React js中的网页访问计数器
- node.js - Mongoose 将对象 ID 推送到现有 mongoDB 对象的数组中
- html - 单击 Bootstrap 导航栏切换按钮不显示折叠的内容
- .net - 从现有模板创建 .NET Framework 项目命令行
- javascript - 有没有办法让用户使用 Reast.js 将图像上传到服务器?